gitscraper: support Python3.

Make gitscraper and tarball script work with both Python 2 and Python 3. Tested
with 2.7 and 3.2.

Change-Id: I31b2580660d764d013bca6fe59d5663ae9f7f5aa
This commit is contained in:
Dominik Riebeling 2012-04-22 21:32:35 +02:00
parent 0ca7b83595
commit 885db72b8b
2 changed files with 23 additions and 23 deletions

View file

@ -41,22 +41,22 @@ def get_refs(repo):
@param repo Path to repository root. @param repo Path to repository root.
@return Dict matching hashes to each ref. @return Dict matching hashes to each ref.
''' '''
print "Getting list of refs" print("Getting list of refs")
output = subprocess.Popen(["git", "show-ref"], stdout=subprocess.PIPE, output = subprocess.Popen(["git", "show-ref"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=repo) stderr=subprocess.PIPE, cwd=repo)
cmdout = output.communicate() cmdout = output.communicate()
refs = {} refs = {}
if len(cmdout[1]) > 0: if len(cmdout[1]) > 0:
print "An error occured!\n" print("An error occured!\n")
print cmdout[1] print(cmdout[1])
return refs return refs
for line in cmdout: for line in cmdout:
regex = re.findall(r'([a-f0-9]+)\s+(\S+)', line) regex = re.findall(b'([a-f0-9]+)\s+(\S+)', line)
for r in regex: for r in regex:
# ref is the key, hash its value. # ref is the key, hash its value.
refs[r[1]] = r[0] refs[r[1].decode()] = r[0].decode()
return refs return refs
@ -75,24 +75,25 @@ def get_lstree(repo, start, filterlist=[]):
objects = {} objects = {}
if len(cmdout[1]) > 0: if len(cmdout[1]) > 0:
print "An error occured!\n" print("An error occured!\n")
print cmdout[1] print(cmdout[1])
return objects return objects
for line in cmdout[0].split('\n'): for line in cmdout[0].decode().split('\n'):
regex = re.findall(r'([0-9]+)\s+([a-z]+)\s+([0-9a-f]+)\s+(\S+)', line) regex = re.findall(b'([0-9]+)\s+([a-z]+)\s+([0-9a-f]+)\s+(\S+)',
line.encode())
for rf in regex: for rf in regex:
# filter # filter
add = False add = False
for f in filterlist: for f in filterlist:
if rf[3].find(f) == 0: if rf[3].decode().find(f) == 0:
add = True add = True
# If two files have the same content they have the same hash, so # If two files have the same content they have the same hash, so
# the filename has to be used as key. # the filename has to be used as key.
if len(filterlist) == 0 or add == True: if len(filterlist) == 0 or add == True:
if rf[3] in objects: if rf[3] in objects:
print "FATAL: key already exists in dict!" print("FATAL: key already exists in dict!")
return {} return {}
objects[rf[3]] = rf[2] objects[rf[3]] = rf[2]
return objects return objects
@ -110,14 +111,13 @@ def get_object(repo, blob, destfile):
cmdout = output.communicate() cmdout = output.communicate()
# make sure output path exists # make sure output path exists
if len(cmdout[1]) > 0: if len(cmdout[1]) > 0:
print "An error occured!\n" print("An error occured!\n")
print cmdout[1] print(cmdout[1])
return False return False
if not os.path.exists(os.path.dirname(destfile)): if not os.path.exists(os.path.dirname(destfile)):
os.makedirs(os.path.dirname(destfile)) os.makedirs(os.path.dirname(destfile))
f = open(destfile, 'wb') f = open(destfile, 'wb')
for line in cmdout[0]: f.write(cmdout[0])
f.write(line)
f.close() f.close()
return True return True
@ -132,8 +132,8 @@ def describe_treehash(repo, treehash):
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=repo) stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=repo)
cmdout = output.communicate() cmdout = output.communicate()
if len(cmdout[1]) > 0: if len(cmdout[1]) > 0:
print "An error occured!\n" print("An error occured!\n")
print cmdout[1] print(cmdout[1])
return "" return ""
return cmdout[0].rstrip() return cmdout[0].rstrip()
@ -148,13 +148,13 @@ def scrape_files(repo, treehash, filelist, dest=""):
created below dest as necessary. created below dest as necessary.
@return Destination path. @return Destination path.
''' '''
print "Scraping files from repository" print("Scraping files from repository")
if dest == "": if dest == "":
dest = tempfile.mkdtemp() dest = tempfile.mkdtemp()
treeobjects = get_lstree(repo, treehash, filelist) treeobjects = get_lstree(repo, treehash, filelist)
for obj in treeobjects: for obj in treeobjects:
get_object(repo, treeobjects[obj], os.path.join(dest, obj)) get_object(repo, treeobjects[obj], os.path.join(dest.encode(), obj))
return dest return dest
@ -185,7 +185,7 @@ def archive_files(repo, treehash, filelist, basename, tmpfolder="",
os.path.join(tmpfolder, basename)) os.path.join(tmpfolder, basename))
if basename is "": if basename is "":
return "" return ""
print "Archiving files from repository" print("Archiving files from repository")
if archive == "7z": if archive == "7z":
outfile = basename + ".7z" outfile = basename + ".7z"
output = subprocess.Popen(["7z", "a", output = subprocess.Popen(["7z", "a",

View file

@ -5,7 +5,7 @@ import os
import sys import sys
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Usage: %s <version|hash>" % sys.argv[0] print("Usage: %s <version|hash>" % sys.argv[0])
sys.exit() sys.exit()
repository = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "/../..") repository = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "/../..")
@ -17,7 +17,7 @@ if '.' in sys.argv[1]:
if ref in refs: if ref in refs:
tree = refs[ref] tree = refs[ref]
else: else:
print "Could not find hash for version!" print("Could not find hash for version!")
sys.exit() sys.exit()
else: else:
tree = sys.argv[1] tree = sys.argv[1]
@ -25,6 +25,6 @@ else:
gitscraper.archive_files(repository, tree, [], basename, archive="7z") gitscraper.archive_files(repository, tree, [], basename, archive="7z")
print "done." print("done.")