Make pylint a little bit happier.

This commit is contained in:
Benjamin Drung 2010-12-27 20:49:00 +01:00
parent f6cb127479
commit b05b4aa5ce
2 changed files with 59 additions and 58 deletions

87
dgetlp
View File

@ -36,7 +36,7 @@ import GnuPGInterface
from cStringIO import StringIO from cStringIO import StringIO
from email.feedparser import FeedParser from email.feedparser import FeedParser
Usage = u"""Usage: %prog [-d|(-v|-q)] <Launchpad URL> USAGE = u"""Usage: %prog [-d|(-v|-q)] <Launchpad URL>
This scripts simulates «dget»'s behaviour for files hosted at This scripts simulates «dget»'s behaviour for files hosted at
launchpadlibrarian.net. launchpadlibrarian.net.
@ -48,12 +48,11 @@ Example:
%prog http://launchpadlibrarian.net/10348157/coreutils_5.97-5.4ubuntu1.dsc %prog http://launchpadlibrarian.net/10348157/coreutils_5.97-5.4ubuntu1.dsc
""" """
unpack_cmd = "dpkg-source -x " BASE_URL = "http://launchpadlibrarian.net/"
base_url = "http://launchpadlibrarian.net/"
Debug = Verbose = Quiet = False Debug = Verbose = Quiet = False
def Unsign(data): def unsign(data):
if data.splitlines()[0] != "-----BEGIN PGP SIGNED MESSAGE-----": if data.splitlines()[0] != "-----BEGIN PGP SIGNED MESSAGE-----":
return data return data
oldstdout = sys.stdout oldstdout = sys.stdout
@ -74,7 +73,7 @@ def Unsign(data):
sys.stderr = oldstderr sys.stderr = oldstderr
return plain return plain
def getEntries(data): def get_entries(data):
parser = FeedParser() parser = FeedParser()
parser.feed(data) parser.feed(data)
return parser.close() return parser.close()
@ -86,7 +85,7 @@ class DscParse(object):
__init__(data) __init__(data)
Given the contents of a .dsc, parse it and extract it's content Given the contents of a .dsc, parse it and extract it's content
""" """
self.entries = getEntries(Unsign(data)) self.entries = get_entries(unsign(data))
self.files = [x.strip().split() for x in self.files = [x.strip().split() for x in
self.entries['Files'].splitlines()] self.entries['Files'].splitlines()]
@ -117,37 +116,37 @@ class DscParse(object):
f = i f = i
if not f: if not f:
raise ValueError, "%s is not in the .dsc" % name raise ValueError, "%s is not in the .dsc" % name
(sum, size, name) = tuple(f) (md5sum, size, name) = tuple(f)
stat = os.stat(name) stat = os.stat(name)
if str(stat.st_size) != size: if str(stat.st_size) != size:
return (False, name, "Expected a size of %s, got %s" % \ return (False, name, "Expected a size of %s, got %s" % \
(size, stat.st_size)) (size, stat.st_size))
return self.getsum(name, sum) return self.getsum(name, md5sum)
def getsum(self, name, sum=None): def getsum(self, name, md5sum=None):
""" """
getsum(name[, sum]) getsum(name[, md5sum])
Read the file 'name' (in 1MB chunks) and generate an md5 sum, Read the file 'name' (in 1MB chunks) and generate an md5 sum,
then compares that to the md5 sum in the .dsc file. then compares that to the md5 sum in the .dsc file.
""" """
chunk_size = 1073741824 chunk_size = 1073741824
fd = open(name, 'rb') fd = open(name, 'rb')
res = hashlib.md5() res = hashlib.md5()
if not sum: if not md5sum:
assert self.files, "I have no files" assert self.files, "I have no files"
sum = [x[0] for x in self.files if x[2] == name][0] md5sum = [x[0] for x in self.files if x[2] == name][0]
data = fd.read(chunk_size) data = fd.read(chunk_size)
while data: while data:
res.update(data) res.update(data)
data = fd.read(chunk_size) data = fd.read(chunk_size)
if res.hexdigest() != sum: if res.hexdigest() != md5sum:
return (False, name, "Expected md5sum of %r, got %r" % \ return (False, name, "Expected md5sum of %r, got %r" % \
(sum, res.hexdigest()) ) (md5sum, res.hexdigest()))
return (True, name, None) return (True, name, None)
def isNative(self): def is_native(self):
""" """
isNative() is_native()
Returns True if this .dsc describes a native debian package; Returns True if this .dsc describes a native debian package;
else false. else false.
""" """
@ -193,8 +192,8 @@ def status(msg, *args):
if not Quiet: if not Quiet:
print msg % tuple(args) print msg % tuple(args)
def Download(dscinfo, number, filename, verify=True): def download(dscinfo, number, filename, verify=True):
"""Download filename""" """download filename"""
ftype = filename.endswith(".diff.gz") and "diff.gz" or \ ftype = filename.endswith(".diff.gz") and "diff.gz" or \
filename.endswith(".orig.tar.gz") and "orig.tar.gz" or \ filename.endswith(".orig.tar.gz") and "orig.tar.gz" or \
filename.endswith(".dsc") and "dsc" or "tar.gz" filename.endswith(".dsc") and "dsc" or "tar.gz"
@ -204,44 +203,46 @@ def Download(dscinfo, number, filename, verify=True):
if not res[0]: if not res[0]:
error(104, "Verification of %s failed: %s", filename, res[2]) error(104, "Verification of %s failed: %s", filename, res[2])
status("Getting %s", filename) status("Getting %s", filename)
debug("%s%s/%s", base_url, number, filename) debug("%s%s/%s", BASE_URL, number, filename)
try: try:
fd = urllib2.urlopen("%s%s/%s" % (base_url, number, filename)) fd = urllib2.urlopen("%s%s/%s" % (BASE_URL, number, filename))
outfd = open(filename, 'wb') outfd = open(filename, 'wb')
outfd.write(fd.read()) outfd.write(fd.read())
fd.close() fd.close()
outfd.close() outfd.close()
except urllib2.HTTPError, e: except urllib2.HTTPError, err:
status("Failed to fetch «%s» file, aborting.", ftype) status("Failed to fetch «%s» file, aborting.", ftype)
error(106, "Error: (%d %s)", e.code, e.msg) error(106, "Error: (%d %s)", err.code, err.msg)
except urllib2.URLError, e: except urllib2.URLError, err:
status("Failed to fetch «%s» file, aborting.", ftype) status("Failed to fetch «%s» file, aborting.", ftype)
error(105, "Error: %s", e) error(105, "Error: %s", err)
except IOError, e: except IOError, err:
status('Could not create "%s"', filename) status('Could not create "%s"', filename)
error(107, "Error: %s", e) error(107, "Error: %s", err)
def unpack(): def unpack(filename):
out = open('/dev/null', 'w') out = open('/dev/null', 'w')
err = open('/dev/null', 'w') err = open('/dev/null', 'w')
ret = subprocess.call(unpack_cmd.split(), stdout=out, stderr=err) cmd = ["dpkg-source", "-x", filename]
ret = subprocess.call(cmd, stdout=out, stderr=err)
out.close() out.close()
err.close() err.close()
if ret: if ret:
status("Failed to unpack source, aborting.") status("Failed to unpack source, aborting.")
sys.exit(108) sys.exit(108)
def getHost(url): def get_host(url):
return urllib2.splithost(urllib2.splittype(url)[1])[0] return urllib2.splithost(urllib2.splittype(url)[1])[0]
def main(): def main():
parser = OptionParser(usage=Usage) global Debug, Verbose, Quiet
parser = OptionParser(usage=USAGE)
parser.add_option("-d", "--debug", action="store_true", dest="debug", parser.add_option("-d", "--debug", action="store_true", dest="debug",
default=False, help="Enable debugging") default=False, help="Enable debugging")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
default=False, help="Enable verbose output") default=False, help="Enable verbose output")
parser.add_option("-q", "--quiet", action="store_true", dest="quiet", parser.add_option("-q", "--quiet", action="store_true", dest="quiet",
default=False, help="Never print any output") default=False, help="Never print any output")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if len(args) != 1: if len(args) != 1:
@ -263,12 +264,12 @@ def main():
if not url.startswith("http://"): if not url.startswith("http://"):
url = "http://" + url url = "http://" + url
if getHost(url).startswith("www."): if get_host(url).startswith("www."):
url = url.replace("www.", "", 1) url = url.replace("www.", "", 1)
if getHost(url) != getHost(base_url): if get_host(url) != get_host(BASE_URL):
error(1, "Error: This utility only works for files on %s.\n" error(1, "Error: This utility only works for files on %s.\n"
"Maybe you want to try dget?", base_url) "Maybe you want to try dget?", BASE_URL)
(number, filename) = url.split('/')[3:] (number, filename) = url.split('/')[3:]
@ -280,17 +281,15 @@ def main():
except: except:
error(3, "Bad URL format") error(3, "Bad URL format")
unpack_cmd += filename
if os.path.exists(filename): if os.path.exists(filename):
os.remove(filename) os.remove(filename)
Download(None, number, filename, False) download(None, number, filename, False)
try: try:
fd = open(filename) fd = open(filename)
dsc_data = fd.read() dsc_data = fd.read()
fd.close() fd.close()
except Exception, e: except Exception:
status("Error: Please report this bug, providing the URL and attach" status("Error: Please report this bug, providing the URL and attach"
" the following backtrace") " the following backtrace")
raise raise
@ -313,13 +312,13 @@ def main():
# Only one file listed in the .dsc means it's native package # Only one file listed in the .dsc means it's native package
if len(dscinfo.files) == 1: if len(dscinfo.files) == 1:
Download(dscinfo, number-1, dscinfo.files[0][-1]) # .tar.gz download(dscinfo, number-1, dscinfo.files[0][-1]) # .tar.gz
else: else:
Download(dscinfo, number-1, dscinfo.files[1][-1]) # .diff.gz download(dscinfo, number-1, dscinfo.files[1][-1]) # .diff.gz
Download(dscinfo, number-2, dscinfo.files[0][-1]) # .orig.tar.gz download(dscinfo, number-2, dscinfo.files[0][-1]) # .orig.tar.gz
status("Unpacking") status("Unpacking")
unpack() unpack(filename)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -33,38 +33,40 @@ from ubuntutools.lp.lpapicache import PersonTeam
def main(): def main():
usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]" usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]"
usage += "\nUsage: %prog <team>" usage += "\nUsage: %prog <team>"
optParser = OptionParser(usage) opt_parser = OptionParser(usage)
# Our options. # Our options.
optParser.add_option("-d", "--directory", action = "store", type = "string", opt_parser.add_option("-d", "--directory", action="store", type="string",
dest = "directory", default = os.getcwd(), dest="directory", default=os.getcwd(),
help = "Directory to download branches to.") help="Directory to download branches to.")
optParser.add_option("-t", "--team", action = "store", type = "string", opt_parser.add_option("-t", "--team", action="store", type="string",
dest = "lpteam", help = "Launchpad team to download branches from.") dest="lpteam",
optParser.add_option("-o", "--operation", action = "store", type = "string", help="Launchpad team to download branches from.")
dest = "operation", default = "branch", opt_parser.add_option("-o", "--operation", action="store", type="string",
help = "Whether to branch or checkout the Bazaar branches. May be " \ dest="operation", default="branch",
"either 'branch' or 'checkout'.") help="Whether to branch or checkout the Bazaar "
"branches. May be either 'branch' or "
"'checkout'.")
(options, args) = optParser.parse_args() (options, args) = opt_parser.parse_args()
# Fetch our current directory to return to later. # Fetch our current directory to return to later.
pwd = os.getcwd() pwd = os.getcwd()
# Parse our options. # Parse our options.
if len(args) != 1 and options.lpteam == None: if len(args) != 1 and options.lpteam == None:
optParser.error("No team has been specified.") opt_parser.error("No team has been specified.")
# Dictionary settings. # Dictionary settings.
directory = options.directory directory = options.directory
if not os.path.isdir(directory): # Check that it is a directory. if not os.path.isdir(directory): # Check that it is a directory.
optParser.error("%s is not a valid directory." % directory) opt_parser.error("%s is not a valid directory." % directory)
os.chdir(directory) os.chdir(directory)
# Type of Bazaar operation to perform. # Type of Bazaar operation to perform.
operation_type = options.operation.lower() operation_type = options.operation.lower()
if operation_type not in ("branch", "checkout"): if operation_type not in ("branch", "checkout"):
optParser.error("Invalid operation '%s' for '-o' flag." % \ opt_parser.error("Invalid operation '%s' for '-o' flag." % \
operation_type) operation_type)
# Launchpad team setting. # Launchpad team setting.