File: import-zips.py

package info (click to toggle)
git 1%3A1.7.10.4-1%2Bwheezy3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,468 kB
  • sloc: ansic: 131,677; sh: 101,927; perl: 25,746; tcl: 20,816; python: 4,441; makefile: 3,418; lisp: 1,785; asm: 98
file content (73 lines) | stat: -rwxr-xr-x 1,848 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python

## zip archive frontend for git-fast-import
##
## For example:
##
##  mkdir project; cd project; git init
##  python import-zips.py *.zip
##  git log --stat import-zips

from os import popen, path
from sys import argv, exit
from time import mktime
from zipfile import ZipFile

if len(argv) < 2:
	print 'Usage:', argv[0], '<zipfile>...'
	exit(1)

branch_ref = 'refs/heads/import-zips'
committer_name = 'Z Ip Creator'
committer_email = 'zip@example.com'

fast_import = popen('git fast-import --quiet', 'w')
def printlines(list):
	for str in list:
		fast_import.write(str + "\n")

for zipfile in argv[1:]:
	commit_time = 0
	next_mark = 1
	common_prefix = None
	mark = dict()

	zip = ZipFile(zipfile, 'r')
	for name in zip.namelist():
		if name.endswith('/'):
			continue
		info = zip.getinfo(name)

		if commit_time < info.date_time:
			commit_time = info.date_time
		if common_prefix == None:
			common_prefix = name[:name.rfind('/') + 1]
		else:
			while not name.startswith(common_prefix):
				last_slash = common_prefix[:-1].rfind('/') + 1
				common_prefix = common_prefix[:last_slash]

		mark[name] = ':' + str(next_mark)
		next_mark += 1

		printlines(('blob', 'mark ' + mark[name], \
					'data ' + str(info.file_size)))
		fast_import.write(zip.read(name) + "\n")

	committer = committer_name + ' <' + committer_email + '> %d +0000' % \
		mktime(commit_time + (0, 0, 0))

	printlines(('commit ' + branch_ref, 'committer ' + committer, \
		'data <<EOM', 'Imported from ' + zipfile + '.', 'EOM', \
		'', 'deleteall'))

	for name in mark.keys():
		fast_import.write('M 100644 ' + mark[name] + ' ' +
			name[len(common_prefix):] + "\n")

	printlines(('',  'tag ' + path.basename(zipfile), \
		'from ' + branch_ref, 'tagger ' + committer, \
		'data <<EOM', 'Package ' + zipfile, 'EOM', ''))

if fast_import.close():
	exit(1)