This repository has been archived by the owner on Sep 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch_gen.py
205 lines (171 loc) · 6.03 KB
/
patch_gen.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from subprocess import check_output
import re
import os
import io
import glob
import sys
import zipfile
import filelock
import json
import hashlib
import bz2
import lzma
import shutil
lock_filename = ".filelock"
###############################################
out = str(check_output(["git", "describe", "--long"]).decode("utf-8")).replace('-', '.', 1).replace("\r","").replace("\n","")
arr = out.split(".")
var = arr[0:-1]
if len(var) < 3:
var = var + ['0'] * (3 - len(arr))
vars = var + [arr[-1].split('-')[0]]
varl = var + [arr[-1]]
def tagOnly():
return ".".join(var[0:3])
def tagOnlyWithComma():
return ".".join(var[0:3])
def longVersion():
return ".".join(varl)
def longVersionWithComma():
return ",".join(varl)
def shortVersion():
return ".".join(vars)
def shortVersionWithComma():
return ",".join(vars)
###############################################
def genSHA1Hash(file):
BUF_SIZE = 65536 # lets read stuff in 64kb chunks!
sha1 = hashlib.sha1()
with open(file, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
def getHeadFileSet():
out = str(check_output(["git", "ls-files"]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return set([x for x in arr if x != '' and x != None])
def getRevisionList(branch="master", count=10):
out = str(check_output(["git", "rev-list", "--all", "--max-count", str(count), "--branches="+branch, "--", "."]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return [x for x in arr if x != '' and x != None]
def getDiffFileSetToHead(revision):
out = str(check_output(["git", "diff", "--name-only", revision, "HEAD"]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return set([x for x in arr if x != '' and x != None])
def getDiffFileStatusToHead(revision):
out = str(check_output(["git", "diff", "--name-status", revision, "HEAD"]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return set([x.split("\t") for x in arr if x != '' and x != None])
def getDiffModifiedFileSetToHead(revision):
out = str(check_output(["git", "diff", "--name-status", revision, "HEAD"]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return set([x.split("\t")[1] for x in arr if x != '' and x != None and x.split("\t")[0] !='D'])
def getDiffDeletedFileSetToHead(revision):
out = str(check_output(["git", "diff", "--name-status", revision, "HEAD"]).decode("utf-8")).replace("\r", "")
arr = out.split("\n")
return set([x.split("\t")[1] for x in arr if x != '' and x != None and x.split("\t")[0] =='D'])
def do(outdir, version, description):
print(getHeadFileSet())
revisions = getRevisionList("master", 10) #1
headRevision = revisions[0]
dir = outdir+'/'+headRevision
patchinfo_json = outdir+'/info.json'
patchinfo_json_lock = outdir+'/info.json.lock'
if not os.path.exists(dir):
os.makedirs(dir)
print(revisions)
revisionFilesetMap = {}
revisionFilesetList = []
fileSet = getHeadFileSet()
revisionFilesetMap[revisions[0]] = fileSet
revisionFilesetList.append(fileSet)
#2
for revision in revisions[1:]: # 0 == HEAD
fileSet = getDiffModifiedFileSetToHead(revision)
revisionFilesetMap[revision] = fileSet
#3
if fileSet not in revisionFilesetList:
revisionFilesetList.append(fileSet)
print(revisionFilesetMap)
print(revisionFilesetList)
#4
revisionFilesetHashList = []
revisionFilesetSizeList = []
for i in range(len(revisionFilesetList)):
zipname = dir + '/'+str(i)+'.zip'
#with zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) as myzip:
with zipfile.ZipFile(zipname, 'w', zipfile.ZIP_LZMA) as myzip:
# with lzma.open(zipname, 'w') as myzip:
for file in revisionFilesetList[i]:
myzip.write(file)
revisionFilesetHashList.append(genSHA1Hash(zipname))
revisionFilesetSizeList.append(os.path.getsize(zipname))
#5
patches = {}
hashes = {}
for revision in revisions:
idx = revisionFilesetList.index(revisionFilesetMap[revision])
patches[revision] = {'idx':idx, 'hash': revisionFilesetHashList[idx], 'removes':list(getDiffDeletedFileSetToHead(revision)), 'size': revisionFilesetSizeList[idx], 'path': revisions[0]+'/'+str(idx)+'.zip'}
info = {'revision':revisions[0], 'patches':patches, 'version':version, 'description':description}
print(info)
#6
#7
with io.open(patchinfo_json, 'w', encoding='utf-8') as f:
# while True:
# try:
# fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
# break
# except IOError as e:
# # raise on unrelated IOErrors
# if e.errno != errno.EAGAIN:
# raise
# else:
# time.sleep(0.1)
# except OSError as e:
# # raise on unrelated IOErrors
# if e.errno != errno.EAGAIN:
# raise
# else:
# time.sleep(0.1)
f.write(json.dumps(info))
# fcntl.flock(f, fcntl.LOCK_UN)
revisions20 = getRevisionList("master", 20)
revisions10 = getRevisionList("master", 10)
print(revisions20)
print(revisions10)
print((set(revisions20) - set(revisions10)))
for rev in (set(revisions20) - set(revisions10)):
print(rev)
del_dir = outdir+'/'+rev
if(os.path.exists(del_dir)):
shutil.rmtree(del_dir)
#print(getDiffFileListToHead())
#1. get revision List
#git rev-list --all --max-count 10 --branches=master
#2. get Diff File List
#git diff --name-only rev HEAD
#3. group file list
#4. zip grouped file list
#5. matching revision to file list
#6. publish zips
#7. publish revision with file lock
#8. del previos patchs n before
if __name__ == '__main__':
if len(sys.argv) < 3:
print("patch_gen.py [src] [patch]")
sys.exit(0)
#do('./patch')
lock = filelock.FileLock(lock_filename)
dir = os.path.abspath(sys.argv[1])
outdir = os.path.abspath(sys.argv[2])
version = longVersion()
description = None
if len(sys.argv) > 3:
description = sys.argv[3]
os.chdir(dir)
with lock:
do(outdir, version, description)
os.chdir(dir)