From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id B2AEE138D11 for ; Thu, 9 Jul 2015 14:47:00 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0E4F914013; Thu, 9 Jul 2015 14:46:55 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 9992914013 for ; Thu, 9 Jul 2015 14:46:54 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 4E35D3408BC for ; Thu, 9 Jul 2015 14:46:53 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 9409B738 for ; Thu, 9 Jul 2015 14:46:47 +0000 (UTC) From: "Anthony G. Basile" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Anthony G. Basile" Message-ID: <1436453363.c59ed6dad57d443a0fef14f9b120f5e6b026abc9.blueness@gentoo> Subject: [gentoo-commits] proj/grss:master commit in: tests/, grs/ X-VCS-Repository: proj/grss X-VCS-Files: grs/Log.py grs/PivotChroot.py grs/Rotator.py grs/Seed.py tests/test-log.py X-VCS-Directories: grs/ tests/ X-VCS-Committer: blueness X-VCS-Committer-Name: Anthony G. Basile X-VCS-Revision: c59ed6dad57d443a0fef14f9b120f5e6b026abc9 X-VCS-Branch: master Date: Thu, 9 Jul 2015 14:46:47 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: ea687bbd-2862-49e3-b0f3-152b1c845aab X-Archives-Hash: 440a2d7a4d56f6dd6fce14e4ffbcafc7 commit: c59ed6dad57d443a0fef14f9b120f5e6b026abc9 Author: Anthony G. Basile gentoo org> AuthorDate: Thu Jul 9 14:49:23 2015 +0000 Commit: Anthony G. Basile gentoo 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')