diff --git a/rbackup/hierarchy.py b/rbackup/hierarchy.py index 8144b2f..a96894d 100644 --- a/rbackup/hierarchy.py +++ b/rbackup/hierarchy.py @@ -1,10 +1,14 @@ """This module contains a class for creating a backup hierarchy.""" + import os.path +import datetime -class Hierarchy(): - dest = None - def Hierarchy(self, dest): +class Hierarchy: + """A class for generating the backup root hierarchy. + """ + + def __init__(self, dest): """Default constructor for the hierarchy class. :param dest: the root directory of the backup hierarchy @@ -16,41 +20,81 @@ class Hierarchy(): self.dest = dest @property - def etc_dir: - """Retrieve the /etc backup directory of this hierarchy. - - :rtype: str - """ - return os.path.join(self.dest, backup, etc) - - @property - def home_dir: - """Retrieve the /home backup directory of this hierarchy. - - :rtype: str - """ - return os.path.join(self.dest, backup, home) - - @property - def snapshot_dir: - """Retrieve the snapshot directory of this hierarchy. - - :rtype: str - """ - return os.path.join(self.dest, backup, snapshots) - - @property - def root_home_dir: - """Retrieve root's home directory of this hierarchy. - - :rtype: str - """ - return os.path.join(self.dest, backup, root) - - @property - def base_dir: + def base_dir(self): """Return the base directory of this hierarchy. :rtype: str """ - return dest + return self.dest + + @property + def prev_snapshot(self): + """Return the canonical path of the previous snapshot stored in + this hierarchy. + + This method checks whether or not '/dest/prev' is a symlink to + a snapshot. If it is not, then a FileExistsError is raised. + + :rtype: str + :raises: FileExistsError if 'dest/prev' is not a symlink + to a snapshot + """ + prevpath = os.path.join(self.base_dir, "prev") + + if os.path.exists(prevpath) and not os.path.islink(prevpath): + raise FileExistsError(f"{prevpath} exists and is not a symlink") + elif not os.path.exists(prevpath): + raise FileNotFoundError(f"prevpath does not exist") + + return os.path.realpath(prevpath) + + @property + def prev_snapshot_link(self): + """Return the symlink path of the previous snapshot stored in + this hierarchy. + + :rtype: str + """ + return os.path.join(self.base_dir, "prev") + + @property + def curr_snapshot(self): + """Retrieve the current date and time and use that in the path + for the current snapshot-to-be. + + :rtype: str + """ + date = datetime.datetime.utcnow().isoformat() + return os.path.join(self.base_dir, date.replace(":", "-")) + + @property + def etc_dir(self): + """Retrieve the /etc backup directory of this hierarchy. + + :rtype: str + """ + return os.path.join(self.curr_snapshot, "etc") + + @property + def home_dir(self): + """Retrieve the /home backup directory of this hierarchy. + + :rtype: str + """ + return os.path.join(self.curr_snapshot, "home") + + @property + def snapshot_dir(self): + """Retrieve the snapshot directory of this hierarchy. + + :rtype: str + """ + return os.path.join(self.curr_snapshot, "snapshots") + + @property + def root_home_dir(self): + """Retrieve root's home directory of this hierarchy. + + :rtype: str + """ + return os.path.join(self.curr_snapshot, "root")