]> git.decadent.org.uk Git - dak.git/blob - dak/copy_installer.py
create new class InstallerCopier
[dak.git] / dak / copy_installer.py
1 #!/usr/bin/env python
2
3 from daklib.config import Config
4
5 import glob, os.path, re, shutil
6
7 root_dir = Config()['Dir::Root']
8
9 class InstallerCopier:
10     def __init__(self, source = 'unstable', dest = 'testing',
11             **keywords):
12         self.source = source
13         self.dest = dest
14         if 'version' not in keywords:
15             raise KeyError('no version specified')
16         self.version = keywords['version']
17
18         self.source_dir = os.path.join(root_dir, 'dists', source, 'main')
19         self.dest_dir = os.path.join(root_dir, 'dists', dest, 'main')
20         self.check_dir(self.source_dir, 'source does not exist')
21         self.check_dir(self.dest_dir, 'destination does not exist')
22
23         self.architectures = []
24         self.skip_architectures = []
25         self.trees_to_copy = []
26         self.symlinks_to_create = []
27         arch_pattern = os.path.join(self.source_dir, 'installer-*', self.version)
28         for arch_dir in glob.glob(arch_pattern):
29             self.check_architecture(arch_dir)
30
31     def check_dir(self, dir, message):
32         if not os.path.isdir(dir):
33             raise IOError(message)
34
35     def check_architecture(self, arch_dir):
36         architecture = re.sub('.*?/installer-(.*?)/.*', r'\1', arch_dir)
37         dest_basedir = os.path.join(self.dest_dir, \
38             'installer-%s' % architecture)
39         dest_dir = os.path.join(dest_basedir, self.version)
40         if os.path.isdir(dest_dir):
41             self.skip_architectures.append(architecture)
42         else:
43             self.architectures.append(architecture)
44             self.trees_to_copy.append((arch_dir, dest_dir))
45             symlink_target = os.path.join(dest_basedir, 'current')
46             self.symlinks_to_create.append((self.version, symlink_target))
47
48     def get_message(self):
49         return """
50 Will copy installer version %(version)s from suite %(source)s to
51 %(dest)s.
52 Architectures to copy: %(arch_list)s
53 Architectures to skip: %(skip_arch_list)s""" % {
54             'version':        self.version,
55             'source':         self.source,
56             'dest':           self.dest,
57             'arch_list':      ', '.join(self.architectures),
58             'skip_arch_list': ', '.join(self.skip_architectures)}
59
60     def do_copy(self):
61         for source, dest in self.trees_to_copy:
62             shutil.copytree(source, dest)
63         for source, dest in self.symlinks_to_create:
64             if os.path.lexists(dest):
65                 os.unlink(dest)
66             os.symlink(source, dest)