Reimplement pbuilder-dist in python.

This commit is contained in:
Siegfried-Angel Gevatter Pujals 2008-03-12 00:40:45 +01:00
parent 670db8f99d
commit eb67a7bbac

View File

@ -1,8 +1,8 @@
#!/bin/sh #! /usr/bin/env python
# -*- coding: utf-8 -*-
# #
# Copyright (C) Jamin W. Collins <jcollins@asgardsrealm.net>
# Copyright (C) Jordan Mantha <mantha@ubuntu.com>
# Copyright (C) 2007-2008 Siegfried-A. Gevatter <rainct@ubuntu.com> # Copyright (C) 2007-2008 Siegfried-A. Gevatter <rainct@ubuntu.com>
# Based upon pbuilder-dist-simple by Jamin Collins and Jordan Mantha.
# #
# License: GPLv2 or later # License: GPLv2 or later
# #
@ -11,300 +11,297 @@
# #
# You can create symlinks to a pbuilder-dist executable to get different # You can create symlinks to a pbuilder-dist executable to get different
# configurations. For example, a symlink called pbuilder-hardy will assume # configurations. For example, a symlink called pbuilder-hardy will assume
# that the target distribution is always Ubuntu Hardy. # that the target distribution is always meant to be Ubuntu Hardy.
###################################################################### import sys
import os
class pbuilder_dist:
def __init__(self):
# Base directory where pbuilder will put all the files it creates. # Base directory where pbuilder will put all the files it creates.
# This is overriden by the global variable $PBUILDFOLDER self.base = None
BASE_DIR="$HOME/pbuilder"
# Change this to 0 if you don't want additional components to be used. # Name of the operation which pbuilder should perform.
# That is, 'universe' and 'multiverse' for Ubuntu chroots and 'contrib' self.operation = None
# and 'non-free' for Debian. (This option can be overwriten at runtime).
EXTRACOMP=1
# Change this to 1 if you want the log for the last operation to be saved # Wheter additional components should be used or not. That is,
# in the base directory by default (it will be named '.lastlog'). # 'universe' and 'multiverse' for Ubuntu chroots and 'contrib'
SAVELOG=0 # and 'non-free' for Debian.
self.extra_components = True
# Allow this script to use /var/cache/apt/archives/ when possible. # Wheter the log for the last operation should be saved in the
if [ -z $SYSCACHE ] # base directory by default. If False, it will be saved in /tmp.
then self.save_log = False
SYSCACHE=1
fi
###################################################################### # File where the log of the last operation will be saved.
self.logfile = None
# Detect system architecture # System architecture
REALARCH=$(dpkg-architecture -qDEB_HOST_ARCH) self.system_architecture = None
# Detect Ubuntu distribution (wheter it is gutsy, hardy, etc.) # Build architecture
SYSDIST=$(lsb_release -cs 2>/dev/null) self.build_architecture = None
# Overwrite hardcoded base directory by that one in the global variable # System's distribution
if [ $PBUILDFOLDER ] && [ $PBUILDFOLDER != "" ] self.system_distro = None
then
BASE_DIR=$PBUILDFOLDER
fi
###################################################################### # Target distribution
self.target_distro = None
# Abort if the name of the executable has hypens but it doesn't # This is an identificative string which will either take the form
# start with "pbuilder-". # 'distribution' or 'distribution-architecture'.
if [ -n $(basename $0 | grep '-') ] && [ $(basename $0 | cut -d'-' -f1) != 'pbuilder' ] self.chroot_string = None
then
echo "Error: " $(basename $0) " is not a valid name for a pbuilder-dist executable."
exit 1
fi
# Detect if the script has it's original name or if a symlink is being used, # Proxy
# and if it's a symlink extract the information that it contains. self.proxy = None
if [ -n $(basename $0 | grep '-') ] && [ `basename $0` != 'pbuilder-dist' ]
then
ORIGINAL_NAME=0
DISTRIBUTION=$(basename $0 | cut -d'-' -f2)
ARCHITECTURE=$(basename $0 | cut -d'-' -f3)
else
ORIGINAL_NAME=1
DISTRIBUTION=$1
shift 1
fi
# Check if the choosen architecture is supported on the user's system. # Authentication method
if [ "$1" = 'i386' ] || [ "$1" = 'amd64' ] self.auth = 'sudo'
then
if [ $REALARCH = 'amd64' ]; then
ARCHITECTURE=$1
else
echo "Warning: Architecture switching is not supported on your system; ignoring argument '$1'."
fi
shift 1 ##############################################################
fi
# If architecture hasn't been set yet, use the system's one. if 'PBUILDFOLDER' in os.environ:
if [ -z "$ARCHITECTURE" ] self.base = os.environ['PBUILDFOLDER']
then else:
ARCHITECTURE=$REALARCH self.base = os.path.expanduser('~/pbuilder/')
fi
# Check if there's a component modifier if 'PBUILDAUTH' in os.environ:
if [ "$1" = 'mainonly' ]; then self.auth = os.environ['PBUILDAUTH']
EXTRACOMP=0
shift 1
elif [ "$1" = 'allcomp' ]; then
EXTRACOMP=1
shift 1
fi
# Check if the default logging preferences should be overwriten self.system_architecture = host_architecture()
if [ "$1" = 'withlog' ]; then
SAVELOG=1
shift 1
elif [ "$1" = 'nolog' ]; then
SAVELOG=0
shift 1
fi
# Check if some proxy should be used. if not self.system_architecture or 'not found' in self.system_architecture:
if [ -n "$http_proxy" ] print 'Error: Not running on a Debian based system; could not detect its architecture.'
then
PROXY=$http_proxy
fi
if [ -z "$PROXY" ] && [ -n "$HTTP_PROXY" ] if not os.path.isfile('/etc/lsb-release'):
then print 'Error: Not running on a Debian based system; could not find /etc/lsb-release.'
PROXY=$HTTP_PROXY exit(1)
fi
###################################################################### for line in open('/etc/lsb-release'):
line = line.strip()
if line.startswith('DISTRIB_CODENAME'):
self.system_distro = line[17:]
break
usage() if not self.system_distro:
{ print 'Error: Could not determine what distribution you are running.'
echo "Usage: $0 "$( [ $ORIGINAL_NAME = 0 ] || echo "<distribution> " )$( [ $ARCHITECTURE != "amd64" ] || echo "[i386|amd64] " )"[mainonly|allcomp] [withlog|nolog] <operation>" exit(1)
}
distdata() self.target_distro = self.system_distro
{
# Populate variables with Debian / Ubuntu specific data
if [ "$1" = "debian" ]
then
# Set Debian specific data
ISDEBIAN=True if 'http_proxy' in os.environ:
self.base = os.environ['http_proxy']
elif 'HTTP_PROXY' in os.environ:
self.base = os.environ['HTTP_PROXY']
if [ -z $ARCHIVE ] ##############################################################
then
ARCHIVE="http://ftp.debian.org"
fi
COMPONENTS="main"$( [ $EXTRACOMP = 0 ] || echo " contrib non-free" ) def __getitem__(self, name):
else
# Set Ubuntu specific data
ISDEBIAN=False return getattr(self, name)
if [ -z $ARCHIVE ] def _calculate(self):
then """ pbuilder_dist.calculate(distro) -> None
ARCHIVE="http://archive.ubuntu.com/ubuntu"
fi
COMPONENTS="main restricted"$( [ $EXTRACOMP = 0 ] || echo " universe multiverse" ) Do all necessary variable changes (and therefore required checks)
fi before the string that will be executed is generated. At this
} point it's expected that no more variables will be modified
outside this class.
###################################################################### """
# Check if there is at least one argument remaining. if not self.build_architecture:
if [ $# -lt 1 ] self.chroot_string = self.target_distro
then self.build_architecture = self.system_architecture
echo "You provided an insufficent number of arguments." else:
usage self.chroot_string = '%(target_distro)s-%(build_architecture)s' % self
exit 1
fi
###################################################################### if not self.logfile:
self.logfile = '/etc/pbuilder-%(chroot_string)s.log' % self
else:
self.logfile = '%(base)s/.%(chroot_string)s.log' % self
# Check if the distribution exists, and fill the variables that change def set_target_distro(self, distro):
# depending on wheter the target distribution is Ubuntu or Debian. """ pbuilder_dist.set_target_distro(distro) -> None
case $DISTRIBUTION in
dapper|edgy|feisty|gutsy|hardy)
distdata ubuntu
;;
oldstable|sarge|stable|etch|testing|lenny|unstable|sid|experimental) Check if the given target distribution name is correct, if it
distdata debian isn't know to the system ask the user for confirmation before
;; proceeding, and finally either save the value into the appropiate
variable or finalize pbuilder-dist's execution.
*) """
if [ ! -d $BASE_DIR/${DISTRIBUTION}-* ]
then
echo -n "Warning: Unknown distribution «$DISTRIBUTION». Do you want to continue [y/N]? "
read continue
if [ "$continue" != 'y' ] && [ "$continue" != 'Y' ] if not distro.isalpha():
then print 'Error: «%s» is an invalid distribution codename.'
echo "Aborting..." sys.exit(1)
exit 1
fi
fi
distdata ubuntu if not os.path.isfile(os.path.join('/usr/share/debootstrap/scripts/', distro)):
;; answer = ask('Warning: Unknown distribution «%s». Do you want to continue [y/N]? ' % distro)
esac if answer not in ('y', 'Y'):
sys.exit(0)
# Save the selected operation in a variable. self.target_distro = distro
OPERATION=$1
shift 1
# Check if the selected operation is an alias for another one. def set_operation(self, operation):
case "$OPERATION" in """ pbuilder_dist.set_operation -> None
upgrade)
OPERATION=update
;;
esac
# Check if the selected operation is correct, or if it is an alias for Check if the given string is a valid pbuilder operation and
# another one. depending on this either save it into the appropiate variable
case "$OPERATION" in or finalize pbuilder-dist's execution.
create|update|build|clean|login|execute)
# Allright.
;;
upgrade) """
OPERATION=update
;;
*) arguments = ('create', 'update', 'build', 'clean', 'login', 'execute')
if [ ${OPERATION##*.} = 'dsc' ]
then
OPERATION=build
else
echo "Unrecognized argument '$OPERATION'. Please use one of those:"
echo " create"
echo " update"
echo " build"
echo " clean"
echo " login"
echo " execute"
exit 1
fi
;;
esac
# Determine the base name for the chroot tarball and the folder where the if operation not in arguments:
# resulting files will be stored. print 'Error: «%s» is not a recognized argument.' % operation
FOLDERBASE="${DISTRIBUTION}-$ARCHITECTURE" print 'Please use one of those: ' + ', '.join(arguments) + '.'
sys.exit(1)
# Create the folder where the resulting files will be placed (if the self.operation = operation
# option is build), if it doesn't exist yet.
if [ ! -d $BASE_DIR/${FOLDERBASE}_result ]
then
mkdir -p $BASE_DIR/${FOLDERBASE}_result
fi
# Determine wheter system cache should be used or not. def get_command(self, remaining_arguments = None):
if [ $SYSCACHE = 1 ] && [ "$SYSDIST" = "$DISTRIBUTION" ] && [ "$REALARCH" = "$ARCHITECTURE"] """ pbuilder_dist.get_command -> string
then
DEBCACHE='/var/cache/apt/archives/'
fi
# If it's an Ubuntu system, create an editable configuration file, Generate the pbuilder command which matches the given configuration
# and if it's a stable release add the -security and -updates repositories. and return it as a string.
if [ $ISDEBIAN = "False" ]
then
if [ ! -d $BASE_DIR/etc/$DISTRIBUTION/apt.conf/ ]
then
mkdir -p $BASE_DIR/etc/$DISTRIBUTION/apt.conf
fi
if [ ! -e $BASE_DIR/etc/$DISTRIBUTION/apt.conf/sources.list ]
then
echo "deb $ARCHIVE $DISTRIBUTION $COMPONENTS" > $BASE_DIR/etc/$DISTRIBUTION/apt.conf/sources.list
case $DISTRIBUTION in
dapper|edgy|feisty|gutsy )
cat >> $BASE_DIR/etc/$DISTRIBUTION/apt.conf/sources.list <<EOF
deb $ARCHIVE $DISTRIBUTION-security $COMPONENTS
deb $ARCHIVE $DISTRIBUTION-updates $COMPONENTS
EOF
;;
* )
;;
esac
fi
fi
#if [ -z "$PBUILDAUTH" ] """
#then
# if [ -n "$DESKTOP_SESSION" ]
# then
# case $DESKTOP_SESSION in
# gnome )
# SUDOREPLACE="gksudo -D \"Pbuilder\""
# ;;
# kde|kde4 )
# SUDOREPLACE="kdesudo -d --comment \"Pbuilder\""
# ;;
# * )
# SUDOREPLACE="sudo"
# ;;
# esac
# else
# SUDOREPLACE=sudo
# fi
#else
# SUDOREPLACE=$PBUILDAUTH
#fi
sudo pbuilder $OPERATION \ # Calculate variables which depend on arguments given at runtime.
--basetgz "$BASE_DIR/${FOLDERBASE}-base.tgz" \ self._calculate()
--distribution "$DISTRIBUTION" \
--debootstrapopts --arch \ arguments = [
--debootstrapopts "$ARCHITECTURE" \ self.operation,
$( [ "$SAVELOG" = 0 ] || echo "--logfile ${BASE_DIR}/.lastlog" ) \ '--basetgz "%(base)s/%(chroot_string)s-base.tgz"' % self,
$( [ -z "$PROXY" ] || echo "--http-proxy ${PROXY}" ) \ '--distribution "%(target_distro)s"' % self,
$( [ -z "$DEBCACHE" ] || echo "--aptcache ${DEBCACHE}" ) \ '--buildresult "%(base)s/%(chroot_string)s_result/"' % self,
--buildresult "${BASE_DIR}/${FOLDERBASE}_result" \ '--logfile "%(logfile)s"' % self,
'--aptcache "/var/cache/apt/archives/"',
]
if self.build_architecture != self.system_architecture:
arguments.append('--debootstrapopts --arch')
arguments.append('--debootstrapopts "%(build_architecture)s"' % self)
if self.proxy:
arguments.append('--http-proxy "%(proxy)s"' % self)
return self.auth + ' /usr/sbin/pbuilder ' + ' '.join(arguments)
"""
--mirror "${ARCHIVE}" \ --mirror "${ARCHIVE}" \
$( [ $ISDEBIAN != "False" ] || echo "--aptconfdir \"${BASE_DIR}/etc/${DISTRIBUTION}/apt.conf/\"" ) \ $( [ $ISDEBIAN != "False" ] || echo "--aptconfdir \"${BASE_DIR}/etc/${DISTRIBUTION}/apt.conf/\"" ) \
$@ $@"""
def host_architecture():
""" host_architecture -> string
Detect the host's architecture and return it as a string
(i386/amd64/other values).
"""
return os.uname()[4].replace('x86_64', 'amd64').replace('i586', 'i386').replace('i686', 'i386')
def ask(question):
""" ask(question) -> string
Ask the given question and return the answer. Also catch
KeyboardInterrupt (Ctrl+C) and EOFError (Ctrl+D) exceptions and
immediately return None if one of those is found.
"""
try:
answer = raw_input(question)
except (KeyboardInterrupt, EOFError):
print
answer = None
return answer
def help(exit_code = 0):
""" help() -> None
Print a help message for pbuilder-dist, and exit with the given code.
"""
print 'Bad...'
sys.exit(exit_code)
def main():
""" main() -> None
This is pbuilder-dist's main function. It creates a pbuilder_dist
object, modifies all necessary settings taking data from the
executable's name and command line options and finally either ends
the script and runs pbuilder itself or exists with an error message.
"""
script_name = os.path.basename(__name__ or sys.argv[0])
parts = script_name.split('-')
# Copy arguments into another list for save manipulation
args = sys.argv[1:]
if '-' in script_name and parts[0] != 'pbuilder' or len(parts) > 3:
print 'Error: «%s» is not a valid name for a «pbuilder-dist» executable.' % script_name
sys.exit(1)
if len(args) < 1:
print 'Insufficient number of arguments.'
help(1)
if args[0] in ('-h', '--help', 'help'):
help(0)
app = pbuilder_dist()
if len(parts) > 1:
app.set_target_distro(parts[1])
else:
app.set_target_distro(args.pop(0))
if len(parts) > 2:
requested_arch = parts[2]
elif args[0] in ('i386', 'amd64'):
requested_arch = args.pop(0)
else:
requested_arch = None
if requested_arch:
if requested_arch in ('i386', 'amd64') and app.system_architecture == 'amd64':
app.build_architecture = requested_arch
else:
print 'Error: Architecture switching is not supported on your system; wrong filename.'
sys.exit(1)
if 'mainonly' in sys.argv:
app.extra_components = False
args.remove('mainonly')
if 'withlog' in sys.argv:
app.save_log = True
args.remove('withlog')
if len(args) < 1:
print 'Insufficient number of arguments.'
help(1)
app.set_operation(args.pop(0))
sys.exit(os.system(app.get_command(args)))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print 'Manually aborted.'
sys.exit(1)