From: Torsten Werner Date: Sat, 8 Jan 2011 16:28:17 +0000 (+0100) Subject: create new class InstallerCopier X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c435e4347a24df8740c2e052ce5794d29a53c7a7;p=dak create new class InstallerCopier - will support the now copy-installer command - includes test code Signed-off-by: Torsten Werner --- diff --git a/dak/copy_installer.py b/dak/copy_installer.py new file mode 100644 index 00000000..8e767d22 --- /dev/null +++ b/dak/copy_installer.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +from daklib.config import Config + +import glob, os.path, re, shutil + +root_dir = Config()['Dir::Root'] + +class InstallerCopier: + def __init__(self, source = 'unstable', dest = 'testing', + **keywords): + self.source = source + self.dest = dest + if 'version' not in keywords: + raise KeyError('no version specified') + self.version = keywords['version'] + + self.source_dir = os.path.join(root_dir, 'dists', source, 'main') + self.dest_dir = os.path.join(root_dir, 'dists', dest, 'main') + self.check_dir(self.source_dir, 'source does not exist') + self.check_dir(self.dest_dir, 'destination does not exist') + + self.architectures = [] + self.skip_architectures = [] + self.trees_to_copy = [] + self.symlinks_to_create = [] + arch_pattern = os.path.join(self.source_dir, 'installer-*', self.version) + for arch_dir in glob.glob(arch_pattern): + self.check_architecture(arch_dir) + + def check_dir(self, dir, message): + if not os.path.isdir(dir): + raise IOError(message) + + def check_architecture(self, arch_dir): + architecture = re.sub('.*?/installer-(.*?)/.*', r'\1', arch_dir) + dest_basedir = os.path.join(self.dest_dir, \ + 'installer-%s' % architecture) + dest_dir = os.path.join(dest_basedir, self.version) + if os.path.isdir(dest_dir): + self.skip_architectures.append(architecture) + else: + self.architectures.append(architecture) + self.trees_to_copy.append((arch_dir, dest_dir)) + symlink_target = os.path.join(dest_basedir, 'current') + self.symlinks_to_create.append((self.version, symlink_target)) + + def get_message(self): + return """ +Will copy installer version %(version)s from suite %(source)s to +%(dest)s. +Architectures to copy: %(arch_list)s +Architectures to skip: %(skip_arch_list)s""" % { + 'version': self.version, + 'source': self.source, + 'dest': self.dest, + 'arch_list': ', '.join(self.architectures), + 'skip_arch_list': ', '.join(self.skip_architectures)} + + def do_copy(self): + for source, dest in self.trees_to_copy: + shutil.copytree(source, dest) + for source, dest in self.symlinks_to_create: + if os.path.lexists(dest): + os.unlink(dest) + os.symlink(source, dest) diff --git a/tests/fixtures/dak.conf b/tests/fixtures/dak.conf index a7388408..1c50af07 100644 --- a/tests/fixtures/dak.conf +++ b/tests/fixtures/dak.conf @@ -17,3 +17,8 @@ Component Description "Software that fails to meet the DFSG"; }; }; + +Dir +{ + Root "fixtures/ftp/"; +}; diff --git a/tests/fixtures/ftp/dists/proposed-updates/main/.keepme b/tests/fixtures/ftp/dists/proposed-updates/main/.keepme new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/stable/main/.keepme b/tests/fixtures/ftp/dists/stable/main/.keepme new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/testing/main/installer-i386/20110106/something b/tests/fixtures/ftp/dists/testing/main/installer-i386/20110106/something new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/unstable/main/installer-amd64/20110106/somedir/file b/tests/fixtures/ftp/dists/unstable/main/installer-amd64/20110106/somedir/file new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/unstable/main/installer-amd64/20110106/something b/tests/fixtures/ftp/dists/unstable/main/installer-amd64/20110106/something new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/unstable/main/installer-i386/20110106/somedir/file b/tests/fixtures/ftp/dists/unstable/main/installer-i386/20110106/somedir/file new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/ftp/dists/unstable/main/installer-i386/20110106/something b/tests/fixtures/ftp/dists/unstable/main/installer-i386/20110106/something new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_copy_installer.py b/tests/test_copy_installer.py new file mode 100755 index 00000000..440e4c12 --- /dev/null +++ b/tests/test_copy_installer.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +from base_test import DakTestCase + +from dak.copy_installer import InstallerCopier + +import unittest + +class ImportTestCase(DakTestCase): + def test_arguments(self): + '''test constructor arguments''' + # version argument is required + self.assertRaises(KeyError, InstallerCopier) + + copier = InstallerCopier(version = '20110106') + self.assertEqual('20110106', copier.version) + self.assertEqual('unstable', copier.source) + self.assertEqual('testing', copier.dest) + + copier = InstallerCopier(version = '20110106', source = \ + 'proposed-updates') + self.assertEqual('proposed-updates', copier.source) + + copier = InstallerCopier(version = '20110106', dest = 'stable') + self.assertEqual('stable', copier.dest) + + def test_dir_names(self): + copier = InstallerCopier(version = '20110106') + self.assertEqual('fixtures/ftp/dists/unstable/main', + copier.source_dir) + self.assertEqual('fixtures/ftp/dists/testing/main', + copier.dest_dir) + + def missing_source(self): + copier = InstallerCopier(version = '20110106', source = 'foo') + + def missing_dest(self): + copier = InstallerCopier(version = '20110106', dest = 'bar') + + def test_suites(self): + self.assertRaises(IOError, self.missing_source) + self.assertRaises(IOError, self.missing_dest) + + def test_copy(self): + copier = InstallerCopier(version = '20110106') + self.assertEqual(['amd64'], copier.architectures) + self.assertEqual(['i386'], copier.skip_architectures) + self.assertEqual( \ + [('fixtures/ftp/dists/unstable/main/installer-amd64/20110106', \ + 'fixtures/ftp/dists/testing/main/installer-amd64/20110106'),], \ + copier.trees_to_copy) + self.assertEqual([('20110106', \ + 'fixtures/ftp/dists/testing/main/installer-amd64/current')], \ + copier.symlinks_to_create) + self.assertEqual(''' +Will copy installer version 20110106 from suite unstable to +testing. +Architectures to copy: amd64 +Architectures to skip: i386''', copier.get_message()) + +if __name__ == '__main__': + unittest.main()