-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.py
More file actions
executable file
·369 lines (322 loc) · 11.8 KB
/
scan.py
File metadata and controls
executable file
·369 lines (322 loc) · 11.8 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/raven/bin/python3
"""
This script scans the ravensource directory and builds up a tree
compromised of previously deleted ports. If an argument in the
form of "YYYYMMDD" is provided, it stops when it reaches that date
(does not include that date).
If the file .last_commit exists in this directory (which contains
a 10-character commit hash), it starts from the next newest commit
otherwise it starts from the very first commit.
"""
import datetime
import glob
import hashlib
import os
import pathlib
import re
import shutil
import subprocess
import sys
import yaml
def read_configuration():
"""
Convert contents of config.yaml into a configuration
"""
config_file = pathlib.Path(__file__).parent / "config.yaml"
try:
with open(config_file, "r") as fin:
config = yaml.load(fin, Loader=yaml.FullLoader)
except OSError as err:
print("The config.yaml configuration file could not be opened for reading.")
sys.exit(1)
except yaml.YAMLError as err:
print("The contents of config.yaml could not be understood. Syntax error?")
sys.exit(2)
return config
def get_commit_order(repo_path):
"""
Retrieves the entire ravensource commit history from the first commit to the
latest one.
"""
command = [
"git", "-C", repo_path, "log", "--reverse", "--format=%h %aI", "--abbrev=10"
]
with subprocess.Popen(
command,
stdout=subprocess.PIPE,
text=True,
bufsize=1 # Line-buffered for efficient streaming
) as proc:
# proc.stdout is an iterable that yields one line (hash) at a time
for line in proc.stdout:
parts = line.strip().split(" ", 1)
if len(parts) == 2:
commit_hash, commit_date = parts
yield commit_hash, commit_date
def read_last_commit():
"""
If .last_commit exists, read it and verify a 10-character hexidecimal code
exists inside. If it does, pass it on, otherwise pass None
"""
commit_file = pathlib.Path(__file__).parent / ".last_commit"
if commit_file.is_file():
with commit_file.open("r") as fin:
line = fin.readline().strip()
if re.fullmatch(r'[0-9a-fA-F]{10}', line):
return line
return None
def save_last_commit(commit_hash):
"""
if commit_hash is a 10-character hexidecimal code, create or overwrite
the .last_commit file in this directory.
"""
commit_file = pathlib.Path(__file__).parent / ".last_commit"
if not isinstance(commit_hash, str) or not re.fullmatch(r'[0-9a-fA-F]{10}', commit_hash):
return
with commit_file.open("w") as fout:
fout.write(commit_hash)
def get_termination_date():
"""
If there's an argument, it should be in the YYYY-MM-DD format.
if it is, convert to unix epoch, otherwise return None
"""
if len(sys.argv) < 2:
return None
try:
dt = datetime.datetime.strptime(sys.argv[1], "%Y-%m-%d")
return int(dt.timestamp())
except ValueError:
return None
def get_unix_epoch(commit_time):
"""
Given a commit_time formated as an ISO 8601 date/time string, return
"""
try:
dt = datetime.datetime.fromisoformat(commit_time)
return int(dt.timestamp())
except (ValueError, TypeError):
return None
def return_to_head(repo_path):
"""
Moves the repository from an arbitrary commit back to the
remote's default branch head (main/master).
"""
try:
# 'origin/HEAD' is a symbolic ref that points to the default branch
subprocess.run(
["git", "-C", repo_path, "checkout", "origin/HEAD"],
check=True,
capture_output=True,
text=True
)
print("Successfully returned to HEAD.")
except subprocess.CalledProcessError as e:
print(f"Failed to return to HEAD: {e.stderr.strip()}")
def switch_to_commit(repo_path, commit_hash):
"""
Switches the repository to a specific commit hash.
Note: This puts the repo in 'detached HEAD' state.
"""
try:
subprocess.run(
["git", "-C", repo_path, "checkout", commit_hash],
check=True,
capture_output=True,
text=True
)
return True
except subprocess.CalledProcessError as e:
print(f"Error: Could not switch to {commit_hash}. {e.stderr.strip()}")
return False
def get_bucket_subdirs(base_path):
"""
Equivalent to: find bucket_?? -maxdepth 1 -mindepth 1 -type d
Returns: ['bucket_00/libwebsockets', 'bucket_00/aspell-fo', ...]
"""
base = pathlib.Path(base_path)
return [
str(p.relative_to(base))
for p in base.glob("bucket_??/*")
if p.is_dir()
]
def build_filter_from_conspiracy(conspiracy_path):
"""
Read conspiracy_variants file and construct an array of ports from it.
"""
results = set()
variants = pathlib.Path(conspiracy_path) / "Mk" / "Misc" / "conspiracy_variants"
if not variants.is_file():
print("The conspiracy variants map cannot be found")
sys.exit(1)
with variants.open("r") as fin:
for line in fin:
parts = line.split()
if len(parts) >= 2:
bucket_id, portname, *_ = parts
directory = f"bucket_{bucket_id}/{portname}"
results.add(directory)
return results
def reset_deleted_ports_tree():
"""
Completely remove any existing tree and create an empty directory.
This is done on the first run, or when the previous commit is not
available.
"""
tree_directory = pathlib.Path(__file__).parent / "deleted_ports"
if tree_directory.exists():
if tree_directory.is_dir():
shutil.rmtree(tree_directory)
else:
tree_directory.unlink()
tree_directory.mkdir(parents=True, exist_ok=True)
history = pathlib.Path(__file__).parent / "history.md"
if history.is_file():
history.unlink()
def remaining_ports(filter, rsource):
"""
The bucket directories of the current state of the ravensource
repository is determined. Any directories that are in the filter set
are removed. The port directories remaining are returned.
"""
results = set()
allports = get_bucket_subdirs(rsource)
for port in allports:
if port not in filter:
portname = port.split("/")[-1]
prefix = bucket(portname)
calced = f"{prefix}/{portname}"
if calced == port:
results.add(port)
else:
print(f"SKIP MISPLACED {port}, should be located at {calced}")
return results
def write_out_index(deleted_ports):
"""
deleted ports is a dictionary with "portname" as the key and a three
element array as the value. ELement 0 of the array is the "bucket" value,
element 1 is the 10-character commit hash, and element 2 is the
ISO 8601 timestamp that it was last seen.
The file is written to <cwd>/history.md file.
"""
history = pathlib.Path(__file__).parent / "history.md"
with history.open("w") as fout:
fout.write("# Last time deleted Ravenport was available\n\n")
fout.write("```\n")
fout.write("Directory Commit Date Portname\n")
fout.write("```\n\n---\n\n```\n")
# "bucket_00 0123456789 2017-04-22T11:42:17-05:00 ravensys-uname
for name, data in sorted(deleted_ports.items()):
fout.write(f"{data[0]} {data[1]} {data[2]} {name}\n")
fout.write("```\n")
def read_existing_index():
"""
If history.md exists, read it and return the deleted ports data.
The first 9 lines are the header and can be ignore.
The final line ("```") is also ignored.
"""
results = {}
history = pathlib.Path(__file__).parent / "history.md"
if history.is_file():
for counter, line in enumerate(history.read_text().splitlines()):
if counter < 9 or line == "```":
continue
parts = line.split()
if len(parts) >= 4:
results[parts[3]] = parts[:3]
return results
def update_deleted_ports(deleted_ports, purged, commit_hash, iso_date):
"""
Iterates through purged ports and upserts them into deleted_ports
"""
for port in purged:
portname = port.split("/")[-1]
prefix = bucket(portname)
deleted_ports[portname] = [prefix, commit_hash, iso_date]
def sync_purged_ports(filter, rsource):
"""
1. Obtain a list of remaining ports (ports not in the filter).
2. If there is at least one, create an inclusion file for rsync
3. run rsync
"""
destination = pathlib.Path(__file__).parent / "deleted_ports"
purged = remaining_ports(filter, rsource)
if purged:
with open("/tmp/folders.txt", "w") as fout:
for path in purged:
fout.write(f"{path}\n")
slash_rsource = os.path.join(rsource, "")
slash_destination = os.path.join(str(destination), "")
# -r: Recursive (Must be explicit with --files-from)
# -l: Preserve symlinks
# -p: Preserve permissions
# -t: Preserve modification times
# -g: Preserve group
# -o: Preserve owner
# -D: Preserve devices/specials
# -R: Relative (Preserves the bucket_XX/subdir/ structure)
# -v: Verbose
cmd = [
"rsync", "-aR", "--delete",
"--info=NAME",
"--files-from=/tmp/folders.txt",
"-r", # <--- MANUALLY RE-ENABLE RECURSION
slash_rsource, slash_destination
]
# print(f"Executing: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
transferred = result.stdout.strip()
if transferred:
print(transferred)
except subprocess.CalledProcessError as e:
print("Error during sync:\n", e.stderr)
return purged
def bucket(portname):
"""
Given the portname, return "bucket_" + first 2 characters of the sha1 hash of portname
"""
portname_hash = hashlib.sha1(portname.encode())
digest = portname_hash.hexdigest().upper()
first2 = digest[:2]
return f"bucket_{first2}"
def main():
"""
This is the entry point of the script
"""
config = read_configuration()
rsource = config["location"]["ravensource"]
csource = config["location"]["conspiracy"]
termination_epoch = get_termination_date()
previous_run = read_last_commit()
previous_found = False
filter = build_filter_from_conspiracy(csource)
if not previous_run:
reset_deleted_ports_tree()
deleted_ports = read_existing_index()
try:
for counter, (commit_hash, iso_date) in enumerate(get_commit_order(rsource), 1):
if previous_run:
if not previous_found:
if previous_run == commit_hash:
previous_found = True
continue
if termination_epoch:
current_epoch = get_unix_epoch(iso_date)
if current_epoch > termination_epoch:
print(f"Reached termination date. Stopping at {commit_hash}.")
break
print(f"Switched to commit: {commit_hash} {iso_date} ({counter})")
switch_to_commit(rsource, commit_hash)
purged = sync_purged_ports(filter, rsource)
update_deleted_ports(deleted_ports, purged, commit_hash, iso_date)
save_last_commit(commit_hash)
except KeyboardInterrupt:
print("\nInterrupted by user (Ctrl+C). Cleaning up...")
except BrokenPipeError:
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
finally:
write_out_index(deleted_ports)
return_to_head(rsource)
if __name__ == "__main__":
main()