Utilize pathlib over os.path for internal path handling

This commit is contained in:
Eric Torres
2019-03-17 18:21:04 -07:00
parent eab5bb108c
commit 30cb9e03a4
6 changed files with 123 additions and 100 deletions

View File

@@ -3,13 +3,21 @@
.. module:: rbackup.hierarchy.hierarchy
:synopsis: Classes for creating the backup hierarchy.
"""
from pathlib import Path
# ========== Classes ==========
class Hierarchy:
"""A class for organizing the backup root hierarchy.
Upon creation of a Hierarchy object, it is up to the caller
to call either shutil.mkdir() or a related method to create
the directory structure it emulates.
Attributes
----------
* path
* name
"""
def __init__(self, dest):
@@ -17,14 +25,14 @@ class Hierarchy:
Example
-------
>>> hier = Hierarchy('/tmp')
>>> hier = Hierarchy('backup')
>>> hier.path
'/tmp'
PosixPath('backup')
:param dest: the root directory of the backup hierarchy
:type dest: str, bytes
:type dest: str, path-like object
"""
self._dest = dest
self._path = Path(dest)
@property
def path(self):
@@ -32,13 +40,27 @@ class Hierarchy:
Example
-------
>>> hier = Hierarchy('/tmp')
>>> hier = Hierarchy('backup')
>>> hier.path
'/tmp'
PosixPath('backup')
:rtype: path-like object
"""
return self._path
@property
def name(self):
"""Return the name of this hierarchy.
Example
-------
>>> hier = Hierarchy('backup/data/snapshot-one')
>>> hier.name
'snapshot-one'
:rtype: str
"""
return self._dest
return self.path.name
# ========== Functions ==========

View File

@@ -4,9 +4,7 @@
:synopsis: Class for structuring a backup repository.
"""
import logging
import os.path
import datetime
import glob
from rbackup.hierarchy.hierarchy import Hierarchy
from rbackup.hierarchy.snapshot import Snapshot
@@ -32,6 +30,7 @@ class Repository(Hierarchy):
Attributes
----------
* path (inherited from Hierarchy)
* name (inherited from Hierarchy)
* curr_snapshot - return either the most recent snapshot
before running create_snapshot() or the new snapshot
created after running create_snapshot()
@@ -40,8 +39,6 @@ class Repository(Hierarchy):
Methods
-------
* create_snapshot() - create a new snapshot, then update curr_snapshot
* update_snapshots() - update the list of snapshots this repository
contains
Directory Structure
-------------------
@@ -57,14 +54,22 @@ class Repository(Hierarchy):
"""Default constructor for the Repository class."""
super().__init__(dest)
self._snapshot_dir = os.path.join(self.path, "data")
self.update_snapshots()
self._snapshot_dir = self.path / "data"
self._snapshots = [
Snapshot(s) for s in self._snapshot_dir.glob("*") if s.is_dir()
]
if self._snapshots == []:
self._curr_snapshot = None
else:
self._curr_snapshot = self._snapshots[-1]
def __len__(self):
return len(self._snapshots)
def __getitem__(self, position):
return self._snapshots[position]
@property
def snapshots(self):
"""Return a list of snapshots stored in this Repository.
@@ -96,7 +101,7 @@ class Repository(Hierarchy):
:rtype: bool
"""
return self._snapshots == []
return self.snapshots == []
@property
def curr_snapshot(self):
@@ -116,7 +121,10 @@ class Repository(Hierarchy):
:rtype: Snapshot object
"""
return self._curr_snapshot
try:
return self.snapshots[-1]
except IndexError:
return None
def create_snapshot(
self, name=datetime.datetime.utcnow().isoformat().replace(":", "-")
@@ -128,7 +136,6 @@ class Repository(Hierarchy):
Example
-------
directive ::= "#" "doctest":" ELLIPSIS
>>> repo = Repository('/tmp')
>>> repo.snapshots
[]
@@ -140,22 +147,14 @@ class Repository(Hierarchy):
:return: a new Snapshot object
"""
syslog.debug("Creating snapshot")
path = os.path.join(self._snapshot_dir, f"snapshot-{name}")
path = self._snapshot_dir / f"snapshot-{name}"
self._curr_snapshot = Snapshot(path)
self._snapshots.append(self._curr_snapshot)
self.snapshots.append(self._curr_snapshot)
syslog.debug("Snapshot created")
syslog.debug(f"Snapshot name: {self.curr_snapshot.name}")
def update_snapshots(self):
"""Update the list of snapshots in this repository."""
self._snapshots = [
Snapshot(s)
for s in glob.glob(f"{self._snapshot_dir}/*")
if os.path.isdir(s)
]
# ========== Functions ==========
if __name__ == "__main__":

View File

@@ -4,7 +4,6 @@
:synopsis: Classes for creating the /tmp hierarchy.
"""
import logging
import os.path
from rbackup.hierarchy.hierarchy import Hierarchy
@@ -19,7 +18,7 @@ class Snapshot(Hierarchy):
Attributes
----------
* path (inherited from Hierarchy)
* name
* name (inherited from Hierarchy)
* boot_dir
* etc_dir
* home_dir
@@ -30,24 +29,10 @@ class Snapshot(Hierarchy):
"""Default constructor for the Snapshot class."""
super().__init__(path)
self._boot_dir = os.path.join(self.path, "boot")
self._etc_dir = os.path.join(self.path, "etc")
self._home_dir = os.path.join(self.path, "home")
self._root_home_dir = os.path.join(self.path, "root")
@property
def name(self):
"""Return the name of this snapshot.
Example
-------
>>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.name
'snapshot-new'
:rtype: str
"""
return os.path.basename(self.path)
self._boot_dir = self.path / "boot"
self._etc_dir = self.path / "etc"
self._home_dir = self.path / "home"
self._root_home_dir = self.path / "root"
@property
def boot_dir(self):
@@ -57,9 +42,9 @@ class Snapshot(Hierarchy):
-------
>>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.boot_dir
'/tmp/data/snapshot-new/boot'
PosixPath('/tmp/data/snapshot-new/boot')
:rtype: str
:rtype: path-like object
"""
return self._boot_dir
@@ -71,9 +56,9 @@ class Snapshot(Hierarchy):
-------
>>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.etc_dir
'/tmp/data/snapshot-new/etc'
PosixPath('/tmp/data/snapshot-new/etc')
:rtype: str
:rtype: path-like object
"""
return self._etc_dir
@@ -85,9 +70,9 @@ class Snapshot(Hierarchy):
-------
>>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.home_dir
'/tmp/data/snapshot-new/home'
PosixPath('/tmp/data/snapshot-new/home')
:rtype: str
:rtype: path-like object
"""
return self._home_dir
@@ -99,9 +84,9 @@ class Snapshot(Hierarchy):
-------
>>> s = Snapshot('/tmp/data/snapshot-new')
>>> s.root_home_dir
'/tmp/data/snapshot-new/root'
PosixPath('/tmp/data/snapshot-new/root')
:rtype: str
:rtype: path-like object
"""
return self._root_home_dir