-
Notifications
You must be signed in to change notification settings - Fork 3
/
snapshot.py
117 lines (91 loc) · 4.16 KB
/
snapshot.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
import requests
import json
import argparse
import sys
def shortShaToLongSha(username, repository, shortHash, token=""):
resource_url = "https://api.github.com/search/commits?q=repo:{}/{}+hash:{}".format(username, repository, shortHash)
headers = {"Accept": "application/vnd.github.cloak-preview+json"}
if token != "":
headers["Authorization"] = "token {}".format(token)
r = requests.get(resource_url, headers=headers)
if r.status_code != requests.codes.ok:
raise ValueError("status code not ok: {}, body: {}".format(r.status_code, r.text ))
data = r.json()
if data["total_count"] != 1:
raise ValueError("total_count not 1: {}".format(data["total_count"]))
return data["items"][0]["sha"]
def versionRefToLongSha(username, repository, versionRef, token=""):
resource_url = "https://api.github.com/repos/{}/{}/git/ref/tags/{}".format(username, repository, versionRef)
headers = {"Accept": "application/vnd.github.cloak-preview+json"}
if token != "":
headers["Authorization"] = "token {}".format(token)
r = requests.get(resource_url, headers=headers)
if r.status_code != requests.codes.ok:
raise ValueError("status code not ok: {}, body: {}".format(r.status_code, r.text ))
data = r.json()
return data["object"]["sha"]
def getOptions(args=sys.argv[1:]):
parser = argparse.ArgumentParser(description="Parses command.")
parser.add_argument("-t", "--token", type=str, help="Github personal token.")
parser.add_argument("-m", "--modules", type=str, help="Path to vendor/modules.txt file.")
parser.add_argument("-i", "--ip2ppmapping", type=str, help="Path to ip2pp mapping file.")
parser.add_argument("-o", "--output", type=str, help="Target Godeps.json file.")
parser.add_argument("-p", "--project", type=str, help="Project vendor/modules.txt file belongs to.")
options = parser.parse_args(args)
return options
if __name__ == "__main__":
options = getOptions(sys.argv[1:])
print(options)
if options.modules == None:
raise ValueError("--modules option not set")
if options.ip2ppmapping == None:
raise ValueError("--ip2ppmapping option not set")
if options.project == None:
raise ValueError("--project option not set")
token = ""
if options.token != None:
token = options.token
with open(options.ip2ppmapping) as data:
ip2pp = json.loads(data.read())
with open(options.modules) as data:
lines = data.readlines()
deps = []
for line in lines:
if not line.startswith("# "):
continue
line = line.strip()
parts = line.split(" ")
if len(parts) != 3:
raise ValueError("line {} does not have 3 pieces, it has {}".format(line, len(parts)))
userRepo = parts[1]
newUserRepo = userRepo
for ip in ip2pp:
if userRepo.startswith(ip["ipprefix"]):
newUserRepo = ip["provider_prefix"] + userRepo[len(ip["ipprefix"]):]
break
if newUserRepo.startswith("github.com/"):
bits = newUserRepo.split("/")
username = bits[1]
repository = bits[2]
else:
raise ValueError("Repo {} not github".format(userRepo))
verParts = parts[2].split("-")
if len(verParts) == 1:
sha = versionRefToLongSha(username, repository, verParts[0], token)
deps.append({"ImportPath": userRepo, "Rev": sha})
print("{}/{}, version: {}, Lhash: {}".format(username, repository, verParts[0], sha))
elif len(verParts) == 3:
sha = shortShaToLongSha(username, repository, verParts[2], token)
deps.append({"ImportPath": userRepo, "Rev": sha})
print("{}/{}, Shash: {}, Lhash: {}".format(username, repository, verParts[2], sha))
godeps = {
"ImportPath": options.project,
"GoVersion": "go1.7",
"GodepVersion": "v74",
"Deps": deps
}
if options.output != None:
with open(options.output, "w") as f:
f.write(json.dumps(godeps))
else:
print(json.dumps(godeps))