ack-sync: Convert to PEP8.

This commit is contained in:
Benjamin Drung 2010-12-22 21:40:23 +01:00
parent 30df1c0ec8
commit f6335cf993

618
ack-sync
View File

@ -37,267 +37,267 @@ PRIVATE_USER_EMAIL = 3
UPLOAD_NOT_PERMITTED = 4 UPLOAD_NOT_PERMITTED = 4
def get_version(title): def get_version(title):
m = re.search("[() ][0-9][0-9a-zA-Z.:+-~]*", title) m = re.search("[() ][0-9][0-9a-zA-Z.:+-~]*", title)
if m is None: if m is None:
print >> sys.stderr, "Version could not be detected. Please specify it with -V." print >> sys.stderr, "Version could not be detected. Please specify it with -V."
sys.exit(VERSION_DETECTION_FAILED) sys.exit(VERSION_DETECTION_FAILED)
return m.group(0).strip("() ") return m.group(0).strip("() ")
def strip_epoch(version): def strip_epoch(version):
parts = version.split(':') parts = version.split(':')
if len(parts) > 1: if len(parts) > 1:
del parts[0] del parts[0]
version = ':'.join(parts) version = ':'.join(parts)
return version return version
def LogCall(command): def LogCall(command):
command = map(str, command) command = map(str, command)
logging.info("Running %s", " ".join(command)) logging.info("Running %s", " ".join(command))
return command return command
def get_source(package, version, section, dist, uploader_name, uploader_email, def get_source(package, version, section, dist, uploader_name, uploader_email,
bug, key, upload): bug, key, upload):
if os.path.isdir("/tmpfs"): if os.path.isdir("/tmpfs"):
workdir = "/tmpfs/ack-sync" workdir = "/tmpfs/ack-sync"
else: else:
workdir = "/tmp/ack-sync" workdir = "/tmp/ack-sync"
if not os.path.isdir(workdir): if not os.path.isdir(workdir):
os.makedirs(workdir) os.makedirs(workdir)
os.chdir(workdir) os.chdir(workdir)
cmd = ["syncpackage", package, "-r", dist, "-V", version, "-b", str(bug)] cmd = ["syncpackage", package, "-r", dist, "-V", version, "-b", str(bug)]
if section is not None: if section is not None:
cmd += ["-c", section] cmd += ["-c", section]
if uploader_email is not None: if uploader_email is not None:
cmd += ["-e", uploader_email] cmd += ["-e", uploader_email]
if uploader_name is not None: if uploader_name is not None:
cmd += ["-n", uploader_name] cmd += ["-n", uploader_name]
if not upload: if not upload:
cmd += ['--dont-sign',] cmd += ['--dont-sign',]
if upload and key is not None: if upload and key is not None:
cmd += ["-k", key] cmd += ["-k", key]
subprocess.check_call(cmd) subprocess.check_call(cmd)
dsc_file = package + "_" + strip_epoch(version) + "fakesync1.dsc" dsc_file = package + "_" + strip_epoch(version) + "fakesync1.dsc"
if not os.path.exists(os.path.join(workdir, dsc_file)): if not os.path.exists(os.path.join(workdir, dsc_file)):
dsc_file = package + "_" + strip_epoch(version) + ".dsc" dsc_file = package + "_" + strip_epoch(version) + ".dsc"
if not os.path.exists(os.path.join(workdir, dsc_file)): if not os.path.exists(os.path.join(workdir, dsc_file)):
print >> sys.stderr, "E: Failed to find .dsc file created by syncpackage." print >> sys.stderr, "E: Failed to find .dsc file created by syncpackage."
sys.exit(1) sys.exit(1)
return dsc_file return dsc_file
def build_source(dist, dsc_file): def build_source(dist, dsc_file):
try: try:
if sbuild: if sbuild:
subprocess.check_call(LogCall(["sbuild", "-d", dist,"-A", dsc_file])) subprocess.check_call(LogCall(["sbuild", "-d", dist,"-A", dsc_file]))
else: else:
if not os.path.isdir("buildresult"): if not os.path.isdir("buildresult"):
os.makedirs("buildresult") os.makedirs("buildresult")
cmd = ["sudo", "-E", "DIST=" + dist, pbuilder, "--build", cmd = ["sudo", "-E", "DIST=" + dist, pbuilder, "--build",
"--buildresult", "buildresult", dsc_file] "--buildresult", "buildresult", dsc_file]
subprocess.check_call(LogCall(cmd)) subprocess.check_call(LogCall(cmd))
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print >> sys.stderr, "E: %s failed to build." % (dsc_file) print >> sys.stderr, "E: %s failed to build." % (dsc_file)
sys.exit(1) sys.exit(1)
def test_install(dist, dsc_file): def test_install(dist, dsc_file):
changes_files=glob.glob(os.path.splitext(dsc_file)[0]+"_*.changes") changes_files=glob.glob(os.path.splitext(dsc_file)[0]+"_*.changes")
changes_file = "" changes_file = ""
for temp_file in changes_files: for temp_file in changes_files:
if not fnmatch.fnmatch(temp_file, '*_source.changes'): if not fnmatch.fnmatch(temp_file, '*_source.changes'):
changes_file = temp_file changes_file = temp_file
if not (os.path.isfile(changes_file)): # if no file exists at all => exit if not (os.path.isfile(changes_file)): # if no file exists at all => exit
print >> sys.stderr, "E: No .changes file has been generated." print >> sys.stderr, "E: No .changes file has been generated."
sys.exit(1) sys.exit(1)
try: try:
cmd = ["sudo", "piuparts", "-N", "-W", "--single-changes-list", cmd = ["sudo", "piuparts", "-N", "-W", "--single-changes-list",
"--log-level=info", "--ignore=/var/log/apt/history.log", "--log-level=info", "--ignore=/var/log/apt/history.log",
"--mirror=http://archive.ubuntu.com/ubuntu main universe restricted multiverse", "--mirror=http://archive.ubuntu.com/ubuntu main universe restricted multiverse",
changes_file] changes_file]
if sbuild: if sbuild:
subprocess.check_call(LogCall(cmd + ["--lvm-volume="+lvm+"/"+dist+"_chroot"])) subprocess.check_call(LogCall(cmd + ["--lvm-volume="+lvm+"/"+dist+"_chroot"]))
else: else:
subprocess.check_call(LogCall(cmd + ["--pbuilder"])) subprocess.check_call(LogCall(cmd + ["--pbuilder"]))
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print >> sys.stderr, "E: %s failed to install. Please check log" % (changes_file) print >> sys.stderr, "E: %s failed to install. Please check log" % (changes_file)
def get_email_from_file(name): def get_email_from_file(name):
filename = os.path.expanduser("~/.ack-sync-email.list") filename = os.path.expanduser("~/.ack-sync-email.list")
if os.path.isfile(filename): if os.path.isfile(filename):
csvfile = open(filename) csvfile = open(filename)
csv_reader = csv.reader(csvfile) csv_reader = csv.reader(csvfile)
for row in csv_reader: for row in csv_reader:
if row and row[0] == name: if row and row[0] == name:
return row[1] return row[1]
return None return None
def unsubscribe_sponsors(launchpad, bug): def unsubscribe_sponsors(launchpad, bug):
ums = launchpad.people['ubuntu-main-sponsors'] ums = launchpad.people['ubuntu-main-sponsors']
try: try:
bug.unsubscribe(person=ums) bug.unsubscribe(person=ums)
print "ubuntu-main-sponsors unsubscribed (for backward compatibility)" print "ubuntu-main-sponsors unsubscribed (for backward compatibility)"
except lazr.restfulclient.errors.HTTPError, http_error: except lazr.restfulclient.errors.HTTPError, http_error:
print "failed to unsubscribe ubuntu-main-sponsors: " + http_error.content print "failed to unsubscribe ubuntu-main-sponsors: " + http_error.content
us = launchpad.people['ubuntu-sponsors'] us = launchpad.people['ubuntu-sponsors']
bug.unsubscribe(person=us) bug.unsubscribe(person=us)
print "ubuntu-sponsors unsubscribed" print "ubuntu-sponsors unsubscribed"
def main(bug_numbers, all_package, all_version, all_section, update, def main(bug_numbers, all_package, all_version, all_section, update,
all_uploader_email, key, upload, lpinstance, verbose=False, all_uploader_email, key, upload, lpinstance, verbose=False,
silent=False): silent=False):
launchpad = get_launchpad("ubuntu-dev-tools", server=lpinstance) launchpad = get_launchpad("ubuntu-dev-tools", server=lpinstance)
# TODO: use release-info (once available) # TODO: use release-info (once available)
series = launchpad.distributions["ubuntu"].current_series series = launchpad.distributions["ubuntu"].current_series
dist = series.name dist = series.name
# update pbuilder # update pbuilder
if update: if update:
if sbuild: if sbuild:
subprocess.call(LogCall(["sbuild-update", dist])) subprocess.call(LogCall(["sbuild-update", dist]))
else: else:
cmd = ["sudo", "-E", "DIST=" + dist, pbuilder, "--update"] cmd = ["sudo", "-E", "DIST=" + dist, pbuilder, "--update"]
subprocess.call(LogCall(cmd)) subprocess.call(LogCall(cmd))
for bug_number in bug_numbers: for bug_number in bug_numbers:
bug = launchpad.bugs[bug_number] bug = launchpad.bugs[bug_number]
uploader = bug.owner uploader = bug.owner
uploader_name = uploader.display_name uploader_name = uploader.display_name
if all_uploader_email is not None: if all_uploader_email is not None:
uploader_email = all_uploader_email uploader_email = all_uploader_email
elif launchpad.people['ubuntumembers'] in uploader.super_teams: elif launchpad.people['ubuntumembers'] in uploader.super_teams:
uploader_email = uploader.name + '@ubuntu.com' uploader_email = uploader.name + '@ubuntu.com'
else: else:
try: try:
uploader_email = uploader.preferred_email_address.email uploader_email = uploader.preferred_email_address.email
except ValueError: except ValueError:
uploader_email = get_email_from_file(uploader.name) uploader_email = get_email_from_file(uploader.name)
if uploader_email is None: if uploader_email is None:
if not silent: if not silent:
print >> sys.stderr, "E: Bug owner '%s' does not have a public email address. Specify uploader with '-e'." % (uploader_name) print >> sys.stderr, "E: Bug owner '%s' does not have a public email address. Specify uploader with '-e'." % (uploader_name)
sys.exit(PRIVATE_USER_EMAIL) sys.exit(PRIVATE_USER_EMAIL)
elif not silent: elif not silent:
print "Taking email address from local file: " + uploader_email print "Taking email address from local file: " + uploader_email
task = list(bug.bug_tasks)[0] task = list(bug.bug_tasks)[0]
if all_package is not None: if all_package is not None:
package = all_package package = all_package
else: else:
package = task.bug_target_name.split(" ")[0] package = task.bug_target_name.split(" ")[0]
if package == "ubuntu": if package == "ubuntu":
words = bug.title.split(" ") words = bug.title.split(" ")
# no source package was defined. Guessing that the second or # no source package was defined. Guessing that the second or
# third word in the title is the package name, because most # third word in the title is the package name, because most
# titles start with "Please sync <package>" or "Sync <package>" # titles start with "Please sync <package>" or "Sync <package>"
if words[0].lower() == "please": if words[0].lower() == "please":
package = words[2] package = words[2]
else: else:
package = words[1] package = words[1]
if all_version is not None: if all_version is not None:
version = all_version version = all_version
else: else:
version = get_version(bug.title) version = get_version(bug.title)
src_pkg = series.getSourcePackage(name=package) src_pkg = series.getSourcePackage(name=package)
if src_pkg is None: if src_pkg is None:
print "%s is NEW in %s." % (package, dist) print "%s is NEW in %s." % (package, dist)
if src_pkg is not None: if src_pkg is not None:
# TODO: Port ack-sync to use lpapicache and reduce code-duplication. # TODO: Port ack-sync to use lpapicache and reduce code-duplication.
can_upload = None can_upload = None
try: try:
series.main_archive.checkUpload( series.main_archive.checkUpload(
component=src_pkg.latest_published_component_name, component=src_pkg.latest_published_component_name,
distroseries=series, person=launchpad.me, pocket='Release', distroseries=series, person=launchpad.me, pocket='Release',
sourcepackagename=package) sourcepackagename=package)
can_upload = True can_upload = True
except lazr.restfulclient.errors.HTTPError, e: except lazr.restfulclient.errors.HTTPError, e:
if e.response.status == 403: if e.response.status == 403:
can_upload = False can_upload = False
elif e.response.status == 400: elif e.response.status == 400:
print "W: Package is probably not in Ubuntu. Can't check upload rights." print "W: Package is probably not in Ubuntu. Can't check upload rights."
can_upload = True can_upload = True
else: else:
raise e raise e
if not can_upload: if not can_upload:
print >> sys.stderr, ("E: Sorry, you are not allowed to " print >> sys.stderr, ("E: Sorry, you are not allowed to upload "
'upload package "%s" to %s.' % (package, dist)) 'package "%s" to %s.' % (package, dist))
sys.exit(UPLOAD_NOT_PERMITTED) sys.exit(UPLOAD_NOT_PERMITTED)
if task.assignee == None: if task.assignee == None:
task.assignee = launchpad.me task.assignee = launchpad.me
print "assigned me" print "assigned me"
task.lp_save() task.lp_save()
if task.assignee != launchpad.me: if task.assignee != launchpad.me:
print >> sys.stderr, "E: %s is already assigned to https://launchpad.net/bugs/%i" % \ print >> sys.stderr, "E: %s is already assigned to https://launchpad.net/bugs/%i" % \
(task.assignee.display_name, bug.id) (task.assignee.display_name, bug.id)
sys.exit(1) sys.exit(1)
old_status = task.status old_status = task.status
task.status = 'In Progress' task.status = 'In Progress'
unsubscribe_sponsors(launchpad, bug) unsubscribe_sponsors(launchpad, bug)
if task.importance == "Undecided": if task.importance == "Undecided":
task.importance = "Wishlist" task.importance = "Wishlist"
print "importance set to Wishlist" print "importance set to Wishlist"
task.lp_save() task.lp_save()
print "package:", package print "package:", package
print "version:", version print "version:", version
dsc_file = get_source(package, version, all_section, dist, dsc_file = get_source(package, version, all_section, dist,
uploader_name, uploader_email, bug_number, key, uploader_name, uploader_email, bug_number, key,
upload) upload)
if dsc_file.endswith('fakesync1.dsc'): if dsc_file.endswith('fakesync1.dsc'):
upload = True upload = True
# extract source # extract source
env = os.environ env = os.environ
env['DEB_VENDOR'] = 'Ubuntu' env['DEB_VENDOR'] = 'Ubuntu'
subprocess.check_call(["dpkg-source", "-x", dsc_file], env=env) subprocess.check_call(["dpkg-source", "-x", dsc_file], env=env)
build_source(dist, dsc_file) build_source(dist, dsc_file)
if piuparts: if piuparts:
test_install(dist, dsc_file) test_install(dist, dsc_file)
print bug.title print bug.title
print '%s (was %s)' % (task.status, old_status) print '%s (was %s)' % (task.status, old_status)
print "Uploader:", uploader_name + " <" + uploader_email + ">" print "Uploader:", uploader_name + " <" + uploader_email + ">"
if upload: if upload:
print "Will upload sync directly, rather than subscribing ubuntu-archive" print "Will upload sync directly, rather than subscribing ubuntu-archive"
try: try:
raw_input('Press [Enter] to continue or [Ctrl-C] to abort.') raw_input('Press [Enter] to continue or [Ctrl-C] to abort.')
except KeyboardInterrupt: except KeyboardInterrupt:
continue continue
bug.subscribe(person=launchpad.me) bug.subscribe(person=launchpad.me)
print "subscribed me" print "subscribed me"
if upload: if upload:
task.status = 'Fix Committed' task.status = 'Fix Committed'
task.assignee = None task.assignee = None
print "unassigned me" print "unassigned me"
task.lp_save() task.lp_save()
changes_file = dsc_file[:-4] + "_source.changes" changes_file = dsc_file[:-4] + "_source.changes"
subprocess.check_call(["dput", "ubuntu", changes_file]) subprocess.check_call(["dput", "ubuntu", changes_file])
else: else:
task.status = 'Confirmed' task.status = 'Confirmed'
task.assignee = None task.assignee = None
print "unassigned me" print "unassigned me"
task.lp_save() task.lp_save()
cmd = ["dpkg-architecture", "-qDEB_BUILD_ARCH"] cmd = ["dpkg-architecture", "-qDEB_BUILD_ARCH"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE) process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
architecture = process.communicate()[0].strip() architecture = process.communicate()[0].strip()
content = "%s %s builds on %s. Sync request ACK'd." % (package, version, architecture) content = "%s %s builds on %s. Sync request ACK'd." % (package, version, architecture)
bug.newMessage(content=content, subject="ack-sync") bug.newMessage(content=content, subject="ack-sync")
bug.subscribe(person=launchpad.people['ubuntu-archive']) bug.subscribe(person=launchpad.people['ubuntu-archive'])
print "subscribed ubuntu-archive" print "subscribed ubuntu-archive"
def usage(): def usage():
print """ack-sync <bug numbers> print """ack-sync <bug numbers>
-e, specify uploader email address -e, specify uploader email address
-h, --help displays this help -h, --help displays this help
@ -320,99 +320,99 @@ def usage():
-V, --version=<version> set the version""" -V, --version=<version> set the version"""
if __name__ == '__main__': if __name__ == '__main__':
try: try:
long_opts = ["help", "key=", "lvm=", "package=", "section=", "silent", long_opts = ["help", "key=", "lvm=", "package=", "section=", "silent",
"update", "upload", "verbose", "version=", "with-sbuild", "update", "upload", "verbose", "version=", "with-sbuild",
"pbuilder=", "with-piuparts", "lpinstance=", "no-conf"] "pbuilder=", "with-piuparts", "lpinstance=", "no-conf"]
opts, args = getopt.gnu_getopt(sys.argv[1:], "e:hk:p:PsSC:uUvV:", long_opts) opts, args = getopt.gnu_getopt(sys.argv[1:], "e:hk:p:PsSC:uUvV:", long_opts)
except getopt.GetoptError, e: except getopt.GetoptError, e:
# will print something like "option -a not recognized" # will print something like "option -a not recognized"
print >> sys.stderr, str(e) print >> sys.stderr, str(e)
sys.exit(COMMAND_LINE_SYNTAX_ERROR) sys.exit(COMMAND_LINE_SYNTAX_ERROR)
upload = False upload = False
package = None package = None
sbuild = False sbuild = False
section = None section = None
silent = False silent = False
update = False update = False
uploader_email = None uploader_email = None
verbose = False verbose = False
version = None version = None
piuparts = False piuparts = False
pbuilder = None pbuilder = None
lvm = "/dev/vg" lvm = "/dev/vg"
key = None key = None
lpinstance = None lpinstance = None
no_conf = False no_conf = False
for o, a in opts: for o, a in opts:
if o in ("-h", "--help"): if o in ("-h", "--help"):
usage() usage()
sys.exit() sys.exit()
elif o in ("-e"): elif o in ("-e"):
uploader_email = a uploader_email = a
elif o in ("-k", "--key"): elif o in ("-k", "--key"):
key = a key = a
elif o in ("--lpinstance"): elif o in ("--lpinstance"):
lpinstance = a lpinstance = a
elif o in ("-l", "--lvm"): elif o in ("-l", "--lvm"):
lvm = a lvm = a
elif o in ("--no-conf"): elif o in ("--no-conf"):
no_conf = True no_conf = True
elif o in ("-p", "--package"): elif o in ("-p", "--package"):
package = a package = a
elif o in ("-P", "--with-piuparts"): elif o in ("-P", "--with-piuparts"):
piuparts = True piuparts = True
elif o in ("--section"): elif o in ("--section"):
section = a section = a
elif o in ("-s", "--silent"): elif o in ("-s", "--silent"):
silent = True silent = True
elif o in ("-S", "--with-sbuild"): elif o in ("-S", "--with-sbuild"):
sbuild = True sbuild = True
elif o in ("-C", "--pbuilder"): elif o in ("-C", "--pbuilder"):
pbuilder=a pbuilder=a
elif o in ("-u", "--update"): elif o in ("-u", "--update"):
update = True update = True
elif o in ("-U", "--upload"): elif o in ("-U", "--upload"):
upload = True upload = True
elif o in ("-v", "--verbose"): elif o in ("-v", "--verbose"):
verbose = True verbose = True
elif o in ("-V", "--version"): elif o in ("-V", "--version"):
version = a version = a
else: else:
assert False, "unhandled option" assert False, "unhandled option"
if len(args) == 0: if len(args) == 0:
if not silent: if not silent:
print >> sys.stderr, "E: You must specify at least one bug number." print >> sys.stderr, "E: You must specify at least one bug number."
sys.exit(COMMAND_LINE_SYNTAX_ERROR) sys.exit(COMMAND_LINE_SYNTAX_ERROR)
bug_numbers = [] bug_numbers = []
for arg in args: for arg in args:
try: try:
number = int(arg) number = int(arg)
except: except:
if not silent: if not silent:
print >> sys.stderr, "E: '%s' is not a valid bug number." % arg print >> sys.stderr, "E: '%s' is not a valid bug number." % arg
sys.exit(COMMAND_LINE_SYNTAX_ERROR) sys.exit(COMMAND_LINE_SYNTAX_ERROR)
bug_numbers.append(number) bug_numbers.append(number)
config = UDTConfig(no_conf) config = UDTConfig(no_conf)
if lpinstance is None: if lpinstance is None:
lpinstance = config.get_value('LPINSTANCE') lpinstance = config.get_value('LPINSTANCE')
if pbuilder is None and not sbuild: if pbuilder is None and not sbuild:
builder = config.get_value('BUILDER') builder = config.get_value('BUILDER')
if builder == 'pbuilder': if builder == 'pbuilder':
pbuilder = 'pbuilder' pbuilder = 'pbuilder'
elif builder == 'sbuild': elif builder == 'sbuild':
sbuild = True sbuild = True
else: else:
print >> sys.stderr, "E: Unsupported build-system: %s" % builder print >> sys.stderr, "E: Unsupported build-system: %s" % builder
sys.exit(COMMAND_LINE_SYNTAX_ERROR) sys.exit(COMMAND_LINE_SYNTAX_ERROR)
if not update: if not update:
update = config.get_value('UPDATE_BUILDER', boolean=True) update = config.get_value('UPDATE_BUILDER', boolean=True)
#TODO: Support WORKDIR #TODO: Support WORKDIR
main(bug_numbers, package, version, section, update, uploader_email, key, main(bug_numbers, package, version, section, update, uploader_email, key,
upload, lpinstance, verbose, silent) upload, lpinstance, verbose, silent)