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