-
Notifications
You must be signed in to change notification settings - Fork 1
/
archiver.py
257 lines (205 loc) · 7.64 KB
/
archiver.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#! /usr/bin/python3
from datetime import datetime
import sys
import time
import os
import subprocess
import tempfile
import shutil
sys.path.append(".")
import config
# repositories to be checked
repo_info = []
repo_dates = []
def removeRepo():
""" Removes the repository folder (repo) if it exists. """
if (os.path.isdir("repo")):
shutil.remove("repo")
def clone(address, into):
""" Clone the given repository (assuming passwordless ssh login) into local folder repo. """
removeRepo();
subprocess.call(["git", "clone", address, into])
def getRemotes(repo):
""" Returns set of remote addresses for given repository """
try:
raw = subprocess.check_output(["git", "-C", repo, "remote", "-v"], stderr=subprocess.PIPE).decode("utf-8")
result = set()
for l in raw.split("\n"):
l = l.strip()
if (len(l) == 0):
continue
result.add(l.split("\t")[1].split(" ")[0])
return result
except:
return set()
def branches():
""" Returns set of branches for the used repository. """
raw = subprocess.check_output(["git", "-C", "repo", "branch", "-a"]).decode("utf-8")
result = set()
for line in raw.split("\n"):
line = line.strip()
if (not line):
continue
if (line.find("->") >= 0):
continue
if (line[0] == "*"):
line = line[1:].strip()
if (line.startswith("remotes/origin/")):
line = line[15:].strip()
result.add(line)
return result
def checkout(branchName):
""" Checkout given branch """
subprocess.call(["git", "-C", "repo", "checkout", branchName])
def getRevisions(date, repo):
year = date[0]
month = date[1]
since = "\"{m}/1/{y}\"".format(m = month, y = year)
month += 1
if (month == 13):
month = 1
year += 1
until = "\"{m}/1/{y}\"".format(m = month, y = year)
raw = subprocess.check_output(["git", "-C", repo, "log", "--pretty=format:%H %ae", "--since", since, "--until", until, "--all"]).decode("utf-8")
result = []
for line in raw.split("\n"):
line = line.strip()
if (not line):
continue
line = line.split(" ")
if (line[1] in config.useremail):
result.append(line[0])
return result
def getPatch(rev, p, repo):
""" Creates a revision patch. """
subprocess.call(["git", "-C", repo, "format-patch", "-1", rev, "-o", p])
def compressDir(folder, name, outdir):
f = "{0}.tar.gz".format(name)
old = os.getcwd()
os.chdir(folder)
subprocess.call(["tar", "-zcf", os.path.join(outdir, f), "."])
os.chdir(old)
shutil.rmtree(folder)
def repoFilename(repoAddress):
""" Returns a safe filename created from the repository's address. Replaces all non-standard characters by underscore. """
return repoAddress.replace("/", "_").replace(":","_").replace("\\","_").replace(".","_")
def help():
""" A simple help print procedure. """
print("""
Archiver
""")
def loadConfig():
""" Loads the configuration from the commandline.
TODO: Actually implement, as of now, only takes information from the config.py file. """
if (config.till == ()):
y = datetime.now().year
m = datetime.now().month
if (m == 1):
m = 12
y = y - 1
config.till = (y, m)
pass
def loadRepositories():
""" Loads the repositories into the repository directory. """
global repo_info
to_clone = config.repository[:]
# make sure the repository directory exists
if (not os.path.isdir(config.repository_dir)):
print("- repository directory {0} does not exist, creating...".format(config.repository_dir))
os.makedirs(config.repository_dir)
# now that we are sure repository dir exists, walk its directories and try to find repositories
for d in os.listdir(config.repository_dir):
print("- checking {0} for git repository...".format(d))
repo_path = os.path.join(config.repository_dir, d)
remotes = getRemotes(repo_path)
if (len(remotes) == 0):
print(" not a git repository")
else:
# it is a git repository, try to find if it is in our list
for r in config.repository:
if (r in remotes):
print(" matched to {0}".format(r))
to_clone.remove(r)
repo_info.append((repo_path, False, r)) # false because we should not delete it at the end
break
# now fetch the missing repositories
if (len(to_clone) > 0):
print("- fetching missing repositories")
for r in to_clone:
print(" {0}...".format(r))
tf = tempfile.mkdtemp(prefix = "archiver", dir = config.repository_dir)
print(tf)
clone(r, tf)
repo_info.append((tf, True, r)) # true, because we must clean the directory afterwards
def initializeOutput():
""" Checks that the output directory exists, creates one, if it does not and sets the list of months to create. """
global repo_dates
# make sure the output directory exists
if (not os.path.isdir(config.output_dir)):
print("- output directory {0} does not exist, creating...".format(config.output_dir))
os.makedirs(config.output_dir)
# now check all months from since date to
y, m = config.since
end = config.till[0] * 12 + config.till[1]
while (y * 12 + m <= end):
if (not os.path.isdir(os.path.join(config.output_dir, "{0}_{1}".format(y,m)))):
repo_dates.append((y,m))
m = m + 1
if (m == 13):
m = 1
y = y + 1
def cleanup():
""" Deletes temporary files. """
global repo_info
print("- cleanup")
for x in repo_info:
if (x[1] == True):
print(" {0}...".format(x[0]))
shutil.rmtree(x[0])
if (__name__ == "__main__"):
# load the confifguration
loadConfig()
# populate repositories
loadRepositories()
# initialize the output directory and the months to archive
initializeOutput()
if (len(repo_info) == 0 or len(repo_dates) == 0):
print("! NOTHING TO DO")
else:
# for each repository, for each month output the tarball
for r in repo_info:
print("- {0}...".format(r[2]))
for m in repo_dates:
revs = getRevisions(m, r[0])
if (len(revs) > 0):
print(" {0}/{1}...{2} revisions".format(m[0], m[1], len(revs)))
outdir = os.path.join(config.output_dir, "{0}_{1}".format(m[0], m[1]))
os.makedirs(outdir, exist_ok = True)
# get all patches
outTemp = os.path.join(outdir, repoFilename(r[2]))
os.makedirs(outTemp, exist_ok = True)
print(" creating patches...")
for rev in revs:
getPatch(rev, outTemp, r[0])
# compress the directory
print(" archiving...")
compressDir(outTemp, repoFilename(r[2]), outdir)
# and now the cleanup
cleanup()
#clone("[email protected]:reactorlabs/rjit.git")
#for b in branches():
# print("Checking branch {0}".format(b))
#month = 2
#year = 2016
#
#tempDir = "{y}_{m}".format(y = year, m = month)
#
#os.mkdir(tempDir)
#
#revs = getRevisions(month, year, ["[email protected]"])
#print(" {0} commits found".format(len(revs)))
#for rev in revs:
# getPatch(rev, tempDir)
# now tar the dir
#compressDir(tempDir)
#removeRepo()