pbuilder-dist: Make pylint happier.

This commit is contained in:
Benjamin Drung 2010-12-27 20:23:08 +01:00
parent 4215c94b92
commit 7b7c84a9fe

View File

@ -31,11 +31,11 @@
import os import os
import subprocess import subprocess
from sys import exit, argv, stderr import sys
import ubuntutools.misc import ubuntutools.misc
debian_distros = ['etch', 'lenny', 'squeeze', 'sid', 'stable', 'testing', DEBIAN_DISTROS = ['etch', 'lenny', 'squeeze', 'sid', 'stable', 'testing',
'unstable', 'experimental'] 'unstable', 'experimental']
class pbuilder_dist: class pbuilder_dist:
@ -80,8 +80,8 @@ class pbuilder_dist:
paths = set(os.environ['PATH'].split(':')) paths = set(os.environ['PATH'].split(':'))
paths |= set(('/sbin', '/usr/sbin', '/usr/local/sbin')) paths |= set(('/sbin', '/usr/sbin', '/usr/local/sbin'))
if not any(os.path.exists(os.path.join(p, builder)) for p in paths): if not any(os.path.exists(os.path.join(p, builder)) for p in paths):
print >> stderr, 'Error: Could not find "%s".' % builder print >> sys.stderr, 'Error: Could not find "%s".' % builder
exit(1) sys.exit(1)
############################################################## ##############################################################
@ -89,21 +89,21 @@ class pbuilder_dist:
'~/pbuilder/')) '~/pbuilder/'))
if 'SUDO_USER' in os.environ: if 'SUDO_USER' in os.environ:
print >> stderr, ("Warning: pbuilder-dist running under sudo. " print >> sys.stderr, ("Warning: pbuilder-dist running under sudo. "
"This is probably not what you want. " "This is probably not what you want. "
"pbuilder-dist will use sudo itself, " "pbuilder-dist will use sudo itself, "
"when necessary.") "when necessary.")
if os.stat(os.environ['HOME']).st_uid != os.getuid(): if os.stat(os.environ['HOME']).st_uid != os.getuid():
print >> stderr, "Error: You don't own $HOME" print >> sys.stderr, "Error: You don't own $HOME"
exit(1) sys.exit(1)
if not os.path.isdir(self.base): if not os.path.isdir(self.base):
try: try:
os.makedirs(self.base) os.makedirs(self.base)
except OSError: except OSError:
print >> stderr, ('Error: Cannot create base directory "%s"' print >> sys.stderr, ('Error: Cannot create base directory "%s"'
% self.base) % self.base)
exit(1) sys.exit(1)
if 'PBUILDAUTH' in os.environ: if 'PBUILDAUTH' in os.environ:
self.auth = os.environ['PBUILDAUTH'] self.auth = os.environ['PBUILDAUTH']
@ -111,7 +111,7 @@ class pbuilder_dist:
self.system_architecture = ubuntutools.misc.host_architecture() self.system_architecture = ubuntutools.misc.host_architecture()
self.system_distro = ubuntutools.misc.system_distribution() self.system_distro = ubuntutools.misc.system_distribution()
if not self.system_architecture or not self.system_distro: if not self.system_architecture or not self.system_distro:
exit(1) sys.exit(1)
self.target_distro = self.system_distro self.target_distro = self.system_distro
@ -124,23 +124,23 @@ class pbuilder_dist:
variable or finalize pbuilder-dist's execution. variable or finalize pbuilder-dist's execution.
""" """
if not distro.isalpha(): if not distro.isalpha():
print >> stderr, ('Error: "%s" is an invalid distribution codename.' print >> sys.stderr, ('Error: "%s" is an invalid distribution '
% distro) 'codename.' % distro)
exit(1) sys.exit(1)
if not os.path.isfile(os.path.join('/usr/share/debootstrap/scripts/', if not os.path.isfile(os.path.join('/usr/share/debootstrap/scripts/',
distro)): distro)):
if os.path.isdir('/usr/share/debootstrap/scripts/'): if os.path.isdir('/usr/share/debootstrap/scripts/'):
# Debian experimental doesn't have a debootstrap file but # Debian experimental doesn't have a debootstrap file but
# should work nevertheless. # should work nevertheless.
if distro not in debian_distros: if distro not in DEBIAN_DISTROS:
answer = ask(('Warning: Unknown distribution "%s". Do you ' answer = ask(('Warning: Unknown distribution "%s". Do you '
'want to continue [y/N]? ') % distro) 'want to continue [y/N]? ') % distro)
if answer not in ('y', 'Y'): if answer not in ('y', 'Y'):
exit(0) sys.exit(0)
else: else:
print >> stderr, 'Please install package "debootstrap".' print >> sys.stderr, 'Please install package "debootstrap".'
exit(1) sys.exit(1)
self.target_distro = distro self.target_distro = distro
@ -159,15 +159,14 @@ class pbuilder_dist:
self.operation = 'build' self.operation = 'build'
return [operation] return [operation]
else: else:
print >> stderr, ('Error: Could not find file "%s".' print >> sys.stderr, ('Error: Could not find file "%s".'
% operation) % operation)
exit(1) sys.exit(1)
else: else:
print >> stderr, ( print >> sys.stderr, ('Error: "%s" is not a recognized '
'Error: "%s" is not a recognized argument.\n' 'argument.\nPlease use one of these: '
'Please use one of these: %s.' '%s.') % (operation, ', '.join(arguments))
) % (operation, ', '.join(arguments)) sys.exit(1)
exit(1)
else: else:
self.operation = operation self.operation = operation
return [] return []
@ -201,9 +200,9 @@ class pbuilder_dist:
try: try:
os.makedirs(result) os.makedirs(result)
except OSError: except OSError:
print >> stderr, ('Error: Cannot create results directory "%s"' print >> sys.stderr, ('Error: Cannot create results directory '
% result) '"%s"' % result)
exit(1) sys.exit(1)
arguments = [ arguments = [
'--%s' % self.operation, '--%s' % self.operation,
@ -218,8 +217,9 @@ class pbuilder_dist:
elif self.builder == 'cowbuilder': elif self.builder == 'cowbuilder':
arguments += ['--basepath', prefix + '-base.cow'] arguments += ['--basepath', prefix + '-base.cow']
else: else:
print >> stderr, 'Error: Unrecognized builder "%s".' % self.builder print >> sys.stderr, 'Error: Unrecognized builder "%s".' % \
exit(1) self.builder
sys.exit(1)
if self.logfile: if self.logfile:
@ -235,7 +235,7 @@ class pbuilder_dist:
'deb file:///var/cache/archive/ %s/' % self.target_distro, 'deb file:///var/cache/archive/ %s/' % self.target_distro,
] ]
if self.target_distro in debian_distros: if self.target_distro in DEBIAN_DISTROS:
arguments += ['--mirror', 'http://ftp.debian.org/debian'] arguments += ['--mirror', 'http://ftp.debian.org/debian']
components = 'main' components = 'main'
if self.extra_components: if self.extra_components:
@ -255,21 +255,22 @@ class pbuilder_dist:
# Work around LP:#599695 # Work around LP:#599695
if (ubuntutools.misc.system_distribution() == 'Debian' if (ubuntutools.misc.system_distribution() == 'Debian'
and self.target_distro not in debian_distros): and self.target_distro not in DEBIAN_DISTROS):
if not os.path.exists( if not os.path.exists(
'/usr/share/keyrings/ubuntu-archive-keyring.gpg'): '/usr/share/keyrings/ubuntu-archive-keyring.gpg'):
print >> stderr, 'Error: ubuntu-keyring not installed' print >> sys.stderr, 'Error: ubuntu-keyring not installed'
exit(1) sys.exit(1)
arguments += [ arguments += [
'--debootstrapopts', '--debootstrapopts',
'--keyring=/usr/share/keyrings/ubuntu-archive-keyring.gpg', '--keyring=/usr/share/keyrings/ubuntu-archive-keyring.gpg',
] ]
elif (ubuntutools.misc.system_distribution() == 'Ubuntu' elif (ubuntutools.misc.system_distribution() == 'Ubuntu'
and self.target_distro in debian_distros): and self.target_distro in DEBIAN_DISTROS):
if not os.path.exists( if not os.path.exists(
'/usr/share/keyrings/debian-archive-keyring.gpg'): '/usr/share/keyrings/debian-archive-keyring.gpg'):
print >> stderr, 'Error: debian-archive-keyring not installed' print >> sys.stderr, ('Error: debian-archive-keyring not '
exit(1) 'installed')
sys.exit(1)
arguments += [ arguments += [
'--debootstrapopts', '--debootstrapopts',
'--keyring=/usr/share/keyrings/debian-archive-keyring.gpg', '--keyring=/usr/share/keyrings/debian-archive-keyring.gpg',
@ -316,14 +317,14 @@ def ask(question):
return answer return answer
def help(exit_code = 0): def show_help(exit_code = 0):
""" help() -> None """ help() -> None
Print a help message for pbuilder-dist, and exit with the given code. Print a help message for pbuilder-dist, and exit with the given code.
""" """
print 'See man pbuilder-dist for more information.' print 'See man pbuilder-dist for more information.'
exit(exit_code) sys.exit(exit_code)
def main(): def main():
""" main() -> None """ main() -> None
@ -333,24 +334,24 @@ def main():
executable's name and command line options and finally either ends executable's name and command line options and finally either ends
the script and runs pbuilder itself or exists with an error message. the script and runs pbuilder itself or exists with an error message.
""" """
script_name = os.path.basename(argv[0]) script_name = os.path.basename(sys.argv[0])
parts = script_name.split('-') parts = script_name.split('-')
# Copy arguments into another list for save manipulation # Copy arguments into another list for save manipulation
args = argv[1:] args = sys.argv[1:]
if ('-' in script_name and parts[0] not in ('pbuilder', 'cowbuilder') if ('-' in script_name and parts[0] not in ('pbuilder', 'cowbuilder')
or len(parts) > 3): or len(parts) > 3):
print >> stderr, ('Error: "%s" is not a valid name for a ' print >> sys.stderr, ('Error: "%s" is not a valid name for a '
'"pbuilder-dist" executable.') % script_name '"pbuilder-dist" executable.') % script_name
exit(1) sys.exit(1)
if len(args) < 1: if len(args) < 1:
print >> stderr, 'Insufficient number of arguments.' print >> sys.stderr, 'Insufficient number of arguments.'
help(1) show_help(1)
if args[0] in ('-h', '--help', 'help'): if args[0] in ('-h', '--help', 'help'):
help(0) show_help(0)
app = pbuilder_dist(parts[0]) app = pbuilder_dist(parts[0])
@ -380,28 +381,28 @@ def main():
('sparc', 'sparc64'), ('sparc64', 'sparc')]): ('sparc', 'sparc64'), ('sparc64', 'sparc')]):
args += ['--debootstrap', 'qemu-debootstrap'] args += ['--debootstrap', 'qemu-debootstrap']
if 'mainonly' in argv or '--main-only' in argv: if 'mainonly' in sys.argv or '--main-only' in sys.argv:
app.extra_components = False app.extra_components = False
if 'mainonly' in argv: if 'mainonly' in sys.argv:
args.remove('mainonly') args.remove('mainonly')
else: else:
args.remove('--main-only') args.remove('--main-only')
if len(args) < 1: if len(args) < 1:
print >> stderr, 'Insufficient number of arguments.' print >> sys.stderr, 'Insufficient number of arguments.'
help(1) show_help(1)
# Parse the operation # Parse the operation
args = app.set_operation(args.pop(0)) + args args = app.set_operation(args.pop(0)) + args
if app.operation == 'build' and '.dsc' not in ' '.join(args): if app.operation == 'build' and '.dsc' not in ' '.join(args):
print >> stderr, ('Error: You have to specify a .dsc file if you want ' print >> sys.stderr, ('Error: You have to specify a .dsc file '
'to build.') 'if you want to build.')
exit(1) sys.exit(1)
# Execute the pbuilder command # Execute the pbuilder command
if not '--debug-echo' in args: if not '--debug-echo' in args:
exit(subprocess.call(app.get_command(args))) sys.exit(subprocess.call(app.get_command(args)))
else: else:
print app.get_command([arg for arg in args if arg != '--debug-echo']) print app.get_command([arg for arg in args if arg != '--debug-echo'])
@ -409,5 +410,5 @@ if __name__ == '__main__':
try: try:
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
print >> stderr, 'Manually aborted.' print >> sys.stderr, 'Manually aborted.'
exit(1) sys.exit(1)