1#!/usr/bin/env python23## zip archive frontend for git-fast-import4##5## For example:6##7## mkdir project; cd project; git init8## python import-zips.py *.zip9## git log --stat import-zips1011from os import popen, path12from sys import argv, exit, hexversion, stderr13from time import mktime14from zipfile import ZipFile1516if hexversion < 0x01060000:17# The limiter is the zipfile module18stderr.write("import-zips.py: requires Python 1.6.0 or later.\n")19exit(1)2021if len(argv) < 2:22print 'usage:', argv[0], '<zipfile>...'23exit(1)2425branch_ref = 'refs/heads/import-zips'26committer_name = 'Z Ip Creator'27committer_email = 'zip@example.com'2829fast_import = popen('git fast-import --quiet', 'w')30def printlines(list):31for str in list:32fast_import.write(str + "\n")3334for zipfile in argv[1:]:35commit_time = 036next_mark = 137common_prefix = None38mark = dict()3940zip = ZipFile(zipfile, 'r')41for name in zip.namelist():42if name.endswith('/'):43continue44info = zip.getinfo(name)4546if commit_time < info.date_time:47commit_time = info.date_time48if common_prefix == None:49common_prefix = name[:name.rfind('/') + 1]50else:51while not name.startswith(common_prefix):52last_slash = common_prefix[:-1].rfind('/') + 153common_prefix = common_prefix[:last_slash]5455mark[name] = ':' + str(next_mark)56next_mark += 15758printlines(('blob', 'mark ' + mark[name], \59'data ' + str(info.file_size)))60fast_import.write(zip.read(name) + "\n")6162committer = committer_name + ' <' + committer_email + '> %d +0000' % \63mktime(commit_time + (0, 0, 0))6465printlines(('commit ' + branch_ref, 'committer ' + committer, \66'data <<EOM', 'Imported from ' + zipfile + '.', 'EOM', \67'', 'deleteall'))6869for name in mark.keys():70fast_import.write('M 100644 ' + mark[name] + ' ' +71name[len(common_prefix):] + "\n")7273printlines(('', 'tag ' + path.basename(zipfile), \74'from ' + branch_ref, 'tagger ' + committer, \75'data <<EOM', 'Package ' + zipfile, 'EOM', ''))7677if fast_import.close():78exit(1)