summaryrefslogtreecommitdiff
path: root/scripts/pbuilder-create
blob: fb702d6fdf79f68d195ece49e5d1c9839a731348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# Copyright 2011  Lars Wirzenius
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cliapp
import logging
import os
import subprocess
import time


class PbuilderCreate(cliapp.Application):

    def add_settings(self):
        self.settings.string(['mirror', 'm'], 'use URL for mirror (%default)',
                             metavar='URL', default='http://mirror/debian')
        self.settings.string_list(['release', 'r'], 
                                  'build tgz for RELEASE (%default)',
                                  metavar='RELEASE', 
                                  default=['squeeze', 'sid'])
        self.settings.string_list(['arch', 'a'], 
                                  'build tgz for ARCH (%default)',
                                  metavar='ARCH', default=['amd64', 'i386'])
        self.settings.string(['othermirror', 'o'], 
                             'also build tgz with pbuilder --othermirror, '
                                'set to empty to avoid this '
                                '(%default)',
                             default='deb http://code.liw.fi/debian squeeze main')
        self.settings.integer(['max-age'], 
                              'trigger tgz update if older than N days '
                                '(%default)',
                              metavar='N', default=1)
        self.settings.boolean(['no-act', 'dry-run'],
                              'just pretend')
        self.settings.boolean(['verbose'], 'print commands before executing')

    def process_args(self, args):
        for release in self.settings['release']:
            for arch in self.settings['arch']:
                self.pbuilder(release, arch, 'debian', None)
                if self.settings['othermirror']:
                    self.pbuilder(release, arch, 'custom', 
                                  self.settings['othermirror'])
            
    def pbuilder(self, release, arch, suffix, othermirror):
        tgz = self.tgz(release, arch, suffix)
        if os.path.exists(tgz):
            if not self.need_to_update_tgz(tgz):
                return
            argv = ['pbuilder',
                    '--update',
                    '--basetgz', tgz,
                    '--logfile', tgz + '.log']
        else:
            argv = ['pbuilder',
                    '--create',
                    '--mirror', self.settings['mirror'],
                    '--architecture', arch,
                    '--distribution', release,
                    '--basetgz', tgz,
                    '--logfile', tgz + '.log']
            if othermirror:
                argv += ['--othermirror', othermirror]
        self.runcmd(argv)

    def need_to_update_tgz(self, tgz):
        mtime = os.path.getmtime(tgz)
        now = time.time()
        max_age_secs = self.settings['max-age'] * 24 * 60 * 40
        return mtime <= now - max_age_secs

    def tgz(self, release, arch, suffix):
        basename = '%s-%s-%s.tgz' % (release, arch, suffix)
        return os.path.join('/', 'var', 'cache', 'pbuilder', basename)

    def runcmd(self, argv, *args, **kwargs):
        logging.info('runcmd: %s' % argv)
        if self.settings['verbose']:
            print ' '. join(argv)
        if self.settings['no-act']:
            return
        devnull = os.open('/dev/null', os.O_WRONLY)
        p = subprocess.Popen(argv, stdout=subprocess.PIPE, 
                             stderr=subprocess.STDOUT, *args, **kwargs)
        out, err = p.communicate()
        os.close(devnull)
        if p.returncode:
            raise cliapp.AppException('command failed: %s\n%s' % 
                                       (' '.join(argv), out))


if __name__ == '__main__':
    PbuilderCreate().run()