-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclone-log-and-extract-src.py
91 lines (74 loc) · 3.79 KB
/
clone-log-and-extract-src.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
doc = """
This programm will
(i) clone a git repo (which is an OOMMF extension) with name REPONAME
(ii) copy from a particular folder in that direcotry (typically 'src')
all files into the oommf/app/oxs/local/REPONAME folder
(iii) create a provenance-REPONAME-HEAD.zip folder that contains a zipped
version of the git repo HEAD at the time of the copy
(iv) store some additional information about that extension repository
in provenance-REPONAME.log
See helpstring and source code for details.
"""
import os
import subprocess
import sys
import time
def main(argv):
repo_url = argv[1]
copy_from = sys.argv[2] + "/"
assert repo_url.endswith('.git')
repo_name = os.path.split(repo_url)[1].split('.git')[0]
print(f"Processing {repo_name}, copy from {copy_from}")
subprocess.check_call(f"git clone {repo_url} tmp_git_clone", shell=True)
# log what repository and version these files are from
filename = f'provenance-{repo_name}.log'
# create a zip file of the HEAD of the repository (the version we use).
# This way, additional documentation and examples
# are available if desired:
zipfilename = f'provenance-{repo_name}-HEAD.zip'
subprocess.check_call(f"cd tmp_git_clone && git archive --format=zip --output ../{zipfilename} HEAD",
shell=True)
with open(filename, 'tw') as f:
f.write(f"== git-repo-url: {repo_url}\n")
f.write(f"== repo-name: {repo_name}\n")
f.write(f"== snap-shot-of-repo: {zipfilename}\n")
f.write(f"== cloned-on-date: {time.asctime()}\n")
f.write(f"== copied-files-from-path-in-repo: {copy_from}\n")
f.write("last commit:\n")
output_bytes = subprocess.check_output(f"cd tmp_git_clone && git log HEAD^..HEAD", shell=True)
f.write(f"{output_bytes.decode('utf8')}\n")
f.write("== src file-list:\n")
output_bytes = subprocess.check_output(f"cd tmp_git_clone && ls -l {copy_from}", shell=True)
f.write(f"{output_bytes.decode('utf8')}\n")
f.write(f"== see contents of {zipfilename} for additional info, examples, etc.\n")
# if directory exists already, delete it so we start from a clean state
if os.path.exists(f"oommf/app/oxs/local/{repo_name}"):
subprocess.check_call(f"rm -rf -v oommf/app/oxs/local/{repo_name}", shell=True)
# create target directory
subprocess.check_call(f"mkdir -p -v oommf/app/oxs/local/{repo_name}", shell=True)
src_path = os.path.relpath(os.path.join('tmp_git_clone', copy_from))
copy_command = f"rsync -v {src_path}/* oommf/app/oxs/local/{repo_name}"
print(f"Copy src files from {repo_name}:")
print(f" {copy_command}")
subprocess.check_call(copy_command, shell=True)
# copy additional information to target directory
print(f"Provenance information recorded in {filename}")
subprocess.check_call(f"mv -v {filename} oommf/app/oxs/local/{repo_name}", shell=True)
subprocess.check_call(f"mv -v {zipfilename} oommf/app/oxs/local/{repo_name}", shell=True)
# remove temporary checkout directory
subprocess.check_call("rm -rf tmp_git_clone", shell=True)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("python3 clone-log-and-extract-src.py URL FROMPATH\n")
print("- Provide URL of repo from which we should git-clone.")
print("- Provide the path from which to copy (FROMPATH).\n")
print("Examples:\n")
print(" python3 clone-log-and-extract-src.py https://github.com/joommf/oommf-extension-dmi-cnv.git src")
print(" python3 clone-log-and-extract-src.py https://github.com/yuyahagi/oommf-mel.git .\n")
print("More context:\n")
print(doc)
print()
print(f"I received {len(sys.argv)} arguments ({sys.argv}), but need 3.")
print()
else:
main(sys.argv)