diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-06-03 17:44:33 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-06-03 17:44:33 +0100 |
commit | 8c0a0e8420d0c376dfd419d188c7e5b73c302f71 (patch) | |
tree | 4749fdb25050988e404ae3a94dc4eb6e8b65a34e | |
parent | c2e1fbf4b1503883c07a616f8eea470b0ffcd5de (diff) | |
parent | f217b7dd322d5700e7ec9174b01ff19f1ddc35c0 (diff) | |
download | liw-automation-8c0a0e8420d0c376dfd419d188c7e5b73c302f71.tar.gz |
Merge rewrite of pbuilder-create in Python, for flexibility.
-rwxr-xr-x | scripts/pbuilder-create | 144 |
1 files changed, 106 insertions, 38 deletions
diff --git a/scripts/pbuilder-create b/scripts/pbuilder-create index 86d5882..fb702d6 100755 --- a/scripts/pbuilder-create +++ b/scripts/pbuilder-create @@ -1,38 +1,106 @@ -#!/bin/sh - -set -e - -MIRROR="http://eskarina.lan/debian" -ARCHES="amd64 i386" -RELEASES="squeeze sid" -if [ -e "$HOME/.pbuilder-create.conf" ] -then - . "$HOME/.pbuilder-create.conf" -fi - -for arch in $ARCHES -do - for release in $RELEASES - do - tgz="/var/cache/pbuilder/$release-$arch.tgz" - if [ ! -e "$tgz" ] - then - echo "Building $tgz" - if ! sudo pbuilder --create \ - --mirror "$MIRROR" \ - --othermirror "deb http://code.liw.fi/debian squeeze main" \ - --architecture "$arch" \ - --distribution "$release" \ - --basetgz "$tgz" \ - --logfile "$tgz.log" > /dev/null - then - echo "FAILED! Try again later." - sudo rm "$tgz" - fi - fi - done -done - -find /var/cache/pbuilder -maxdepth 1 -name '*.tgz' -mtime +2 | -sudo xargs -t -i'{}' -n1 pbuilder --update --basetgz '{}' --logfile '{}.log' \ - > /dev/null +#!/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() |