-
Notifications
You must be signed in to change notification settings - Fork 22
/
vendor.py
executable file
·168 lines (145 loc) · 6.45 KB
/
vendor.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
#!/usr/bin/env python3
import argparse
import configparser
import os
import re
import shutil
import subprocess
import tempfile
import time
import requests
from git import Repo
def vendor_check():
if not os.path.isfile('options.conf'):
return False
config = configparser.ConfigParser(interpolation=None)
config.read('options.conf')
if 'autospec' not in config.sections():
return False
if vendor := config['autospec'].get('cargo_vendor'):
if vendor == "true":
return 'cargo'
return False
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('archives')
parser.add_argument('url')
parser.add_argument('name')
parser.add_argument('git')
return parser.parse_args()
def setup_content(url):
tdir = tempfile.mkdtemp()
outfile = os.path.join(tdir, os.path.basename(url))
response = requests.get(url, timeout=30)
response.raise_for_status()
with open(outfile, 'wb') as cfile:
cfile.write(response.content)
subprocess.run(f"tar xf {outfile}", shell=True, cwd=tdir, check=True, stdout=subprocess.DEVNULL)
os.remove(outfile)
return tdir
def setup_cargo_vendor(path):
cargo_paths = []
for dirpath, _, files in os.walk(path):
for fname in files:
if fname == "Cargo.toml":
cargo_paths.append(os.path.join(dirpath, fname))
return cargo_paths
def update_cargo_vendor(tmpdir, cargo_paths, name, git):
git_uri = os.path.join(git, name)
vendor_path = os.path.join(tmpdir, 'vendor')
subprocess.run(f"git clone {git_uri} {vendor_path}", shell=True, check=True,
stdout=subprocess.DEVNULL)
vendor_git = os.path.join(vendor_path, '.git')
if not os.path.isdir(vendor_git):
# initialize a git repo
subprocess.run('git init .', cwd=vendor_path, shell=True, check=True,
stdout=subprocess.DEVNULL)
subprocess.run(f"git remote add origin {git_uri}", cwd=vendor_path,
shell=True, check=True, stdout=subprocess.DEVNULL)
backup_vendor_git = os.path.join(tmpdir, 'clear-linux-vendor-git')
subprocess.run(f"cp -a {vendor_git} {backup_vendor_git}", cwd=tmpdir,
shell=True, check=True, stdout=subprocess.DEVNULL)
shutil.rmtree(vendor_path)
vendor_cmd = 'cargo vendor ' + ' '.join([f"-s {x}" for x in cargo_paths[:-1]])
vendor_cmd += f" --manifest-path {cargo_paths[-1]}"
cargo_vendors = subprocess.run(vendor_cmd, cwd=tmpdir, shell=True,
check=True, stdout=subprocess.PIPE,
universal_newlines=True).stdout
with open(os.path.join(vendor_path, ".gitattributes"), "w", encoding='utf8') as gafile:
gafile.write("* text=false\n")
subprocess.run(f"cp -a {backup_vendor_git} {vendor_git}", cwd=tmpdir,
shell=True, check=True, stdout=subprocess.DEVNULL)
repo = Repo(vendor_path)
if not (len(repo.untracked_files) > 0 or repo.is_dirty()):
# Always use the newest tag as sometimes a new tag will
# be created but the package won't be updated to use it
# for a different failure reason.
tag = sorted(repo.tags, key=lambda x: x.name, reverse=True)[0]
return tag, cargo_vendors
subprocess.run('git add .', cwd=vendor_path, shell=True, check=True,
stdout=subprocess.DEVNULL)
subprocess.run('git commit -m "vendor update"', cwd=vendor_path,
shell=True, check=True, stdout=subprocess.DEVNULL)
gmt = time.gmtime()
tag = f"{gmt.tm_year}-{gmt.tm_mon:02d}-{gmt.tm_mday:02d}-{gmt.tm_hour:02d}-{gmt.tm_min:02d}-{gmt.tm_sec:02d}"
subprocess.run(f"git tag {tag}", cwd=vendor_path, shell=True,
check=True, stdout=subprocess.DEVNULL)
subprocess.run(f"git push origin main:main {tag}", cwd=vendor_path,
shell=True, check=True, stdout=subprocess.DEVNULL)
time.sleep(30)
return tag, cargo_vendors
def update_cargo_sources(name, tag, cargo_vendors):
makefile = []
options = []
archive_match = os.path.join(r'\$\(CGIT_BASE_URL\)', 'vendor', name,
'snapshot', name)
archive_replace = os.path.join('$(CGIT_BASE_URL)', 'vendor', name,
'snapshot', name)
with open('Makefile', encoding='utf8') as mfile:
for line in mfile.readlines():
if line.startswith('ARCHIVES'):
if re.search(archive_match + r'[a-zA-Z0-9_\-.]+\.tar\.gz', line):
new_archives = re.sub(archive_match + r'[a-zA-Z0-9_\-.]+\.tar\.gz',
f"{archive_replace}-{tag}.tar.gz", line)
else:
new_archives = f"{line[:-1]} {archive_replace}-{tag}.tar.gz ./vendor\n"
print(new_archives.replace('ARCHIVES = ', '', 1))
makefile.append(new_archives)
else:
makefile.append(line)
with open('Makefile', 'w', encoding='utf8') as mfile:
mfile.writelines(makefile)
archive_match = os.path.join('http://localhost', 'cgit', 'vendor', name,
'snapshot', name)
with open('options.conf', encoding='utf8') as ofile:
for line in ofile.readlines():
if line.startswith('archives'):
if re.search(archive_match + r'[a-zA-Z0-9_\-.]+\.tar\.gz', line):
new_archives = re.sub(archive_match + r'[a-zA-Z0-9_\-.]+\.tar\.gz',
f"{archive_match}-{tag}.tar.gz", line)
else:
new_archives = f"{line[:-1]} {archive_match}-{tag}.tar.gz ./vendor\n"
options.append(new_archives)
else:
options.append(line)
with open('options.conf', 'w', encoding='utf8') as ofile:
ofile.writelines(options)
with open('cargo_vendors', 'w', encoding='utf8') as cfile:
cfile.write(cargo_vendors)
def main():
args = get_args()
vtype = vendor_check()
if not vtype:
print(args.archives)
return
tdir = setup_content(args.url)
if vtype == 'cargo':
cargo_paths = setup_cargo_vendor(tdir)
if len(cargo_paths) == 0:
print(args.archives)
else:
tag, cargo_vendors = update_cargo_vendor(tdir, cargo_paths, args.name, args.git)
update_cargo_sources(args.name, tag, cargo_vendors)
shutil.rmtree(tdir)
if __name__ == '__main__':
main()