commit:     c59ed6dad57d443a0fef14f9b120f5e6b026abc9
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Thu Jul  9 14:49:23 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Thu Jul  9 14:49:23 2015 +0000
URL:        https://gitweb.gentoo.org/proj/grss.git/commit/?id=c59ed6da

grs/Rotator.py: consolidate common rotation code.

 grs/Log.py         | 16 ++--------------
 grs/PivotChroot.py | 22 +++-------------------
 grs/Rotator.py     | 28 ++++++++++++++++++++++++++++
 grs/Seed.py        | 22 +++-------------------
 tests/test-log.py  |  4 +---
 5 files changed, 37 insertions(+), 55 deletions(-)

diff --git a/grs/Log.py b/grs/Log.py
index 2a7ef9d..81ac022 100644
--- a/grs/Log.py
+++ b/grs/Log.py
@@ -7,7 +7,7 @@ import re
 import shutil
 from grs.Constants import CONST
 
-class Log():
+class Log(Rotator):
 
     def __init__(self, logfile = CONST.LOGFILE):
         self.logfile = logfile
@@ -25,19 +25,7 @@ class Log():
 
 
     def rotate_logs(self):
-        logs = glob.glob('%s.*' % self.logfile)
-        indexed_log = {}
-        for l in logs:
-            m = re.search('^.+\.(\d+)$', l)
-            indexed_log[int(m.group(1))] = l
-        count = list(indexed_log.keys())
-        count.sort()
-        count.reverse()
-        for c in count:
-            current_log = indexed_log[c]
-            m = re.search('^(.+)\.\d+$', current_log)
-            next_log = '%s.%d' % (m.group(1), c+1)
-            shutil.move(current_log, next_log)
+        self.rotate(self.logfile)
         if os.path.isfile(self.logfile):
             shutil.move(self.logfile, '%s.0' % self.logfile)
         open('%s' % self.logfile, 'a').close()

diff --git a/grs/PivotChroot.py b/grs/PivotChroot.py
index 6eec3a0..8ec0fc6 100644
--- a/grs/PivotChroot.py
+++ b/grs/PivotChroot.py
@@ -9,7 +9,7 @@ from grs.Constants import CONST
 from grs.MountDirectories import MountDirectories
 
 
-class PivotChroot():
+class PivotChroot(Rotator):
     """ doc here
         more doc
     """
@@ -32,25 +32,9 @@ class PivotChroot():
         if some_mounted:
             md.umount_all()
 
-        # TODO: we need to generalize this code into its own class
-        # and inherit it here, in Log.py and Seed.py.
-        # Rotate any previous portage_configroots out of the way
-        dirs = glob.glob('%s.*' % self.portage_configroot)
-        indexed_dir = {}
-        for d in dirs:
-            m = re.search('^.+\.(\d+)$', d)
-            indexed_dir[int(m.group(1))] = d
-        count = list(indexed_dir.keys())
-        count.sort()
-        count.reverse()
-        for c in count:
-            current_dir = indexed_dir[c]
-            m = re.search('^(.+)\.\d+$', current_dir)
-            next_dir = '%s.%d' % (m.group(1), c+1)
-            shutil.move(current_dir, next_dir)
-
-        # Assume portage_configroot exists and out of the way to system.0,
+        # Move portage_configroot out of the way to system.0,
         # then pivot out the inner chroot to system.
+        self.rotate(self.portage_configroot)
         shutil.move(self.portage_configroot, '%s.0' % self.portage_configroot)
         inner_chroot = os.path.join('%s.0' % self.portage_configroot, 
subchroot)
         shutil.move(inner_chroot, os.path.join(self.tmpdir, 'system'))

diff --git a/grs/Rotator.py b/grs/Rotator.py
new file mode 100644
index 0000000..fb76274
--- /dev/null
+++ b/grs/Rotator.py
@@ -0,0 +1,28 @@
+i#!/usr/bin/env python
+
+import glob
+import re
+import shutil
+
+class Rotator():
+    """ doc here
+        more doc
+    """
+
+    def rotate(self, obj):
+        """ doc here
+            more doc
+        """
+        objs = glob.glob('%s.*' % obj)
+        indexed_obj = {}
+        for o in objs:
+            m = re.search('^.+\.(\d+)$', o)
+            indexed_obj[int(m.group(1))] = o
+        count = list(indexed_obj.keys())
+        count.sort()
+        count.reverse()
+        for c in count:
+            current_obj = indexed_obj[c]
+            m = re.search('^(.+)\.\d+$', current_obj)
+            next_obj = '%s.%d' % (m.group(1), c+1)
+            shutil.move(current_obj, next_obj)

diff --git a/grs/Seed.py b/grs/Seed.py
index 4ac95ed..493a32a 100644
--- a/grs/Seed.py
+++ b/grs/Seed.py
@@ -9,7 +9,7 @@ from grs.Constants import CONST
 from grs.Execute import Execute
 
 
-class Seed():
+class Seed(Rotator):
     """ doc here
         more doc
     """
@@ -31,27 +31,11 @@ class Seed():
         """ doc here
             more doc
         """
+        # Rotate the old portage_configroot and package out of the way
         for directory in [self.portage_configroot, self.package]:
-            # Rotate any previous directories out of the way
-            dirs = glob.glob('%s.*' % directory)
-            indexed_dir = {}
-            for d in dirs:
-                m = re.search('^.+\.(\d+)$', d)
-                indexed_dir[int(m.group(1))] = d
-            count = list(indexed_dir.keys())
-            count.sort()
-            count.reverse()
-            for c in count:
-                current_dir = indexed_dir[c]
-                m = re.search('^(.+)\.\d+$', current_dir)
-                next_dir = '%s.%d' % (m.group(1), c+1)
-                shutil.move(current_dir, next_dir)
-            # If there is a directory, then move it to %s.0
+            self.rotate(directory)
             if os.path.isdir(directory):
                 shutil.move(directory, '%s.0' % directory)
-            # Now that all prevous directory are out of the way,
-            # create a new empty directory.  Fail if the directory
-            # is still around.
             os.makedirs(directory, mode=0o755, exist_ok=False)
 
         # Download a stage tarball if we don't have one

diff --git a/tests/test-log.py b/tests/test-log.py
index 7f19b58..f132ca0 100755
--- a/tests/test-log.py
+++ b/tests/test-log.py
@@ -11,10 +11,8 @@ from grs import Log
 logdir = '/tmp/test-log'
 
 def doit(stamped = False):
-    try:
+    if os.path.isdir(logdir):
         shutil.rmtree(logdir)
-    except FileNotFoundError:
-        pass
     os.makedirs(logdir)
     logfile = os.path.join(logdir, 'test.log')
 

Reply via email to