-
Notifications
You must be signed in to change notification settings - Fork 1
/
exfor_trans.py
501 lines (374 loc) · 14.3 KB
/
exfor_trans.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
####################################################################
#
# This file is part of exfor-parser.
# Copyright (C) 2022 International Atomic Energy Agency (IAEA)
#
# Disclaimer: The code is still under developments and not ready
# to use. It has been made public to share the progress
# among collaborators.
# Contact: [email protected]
#
####################################################################
import requests
import os
import sys
import json
import shutil
import datetime
from git import (
Repo,
)
import logging
logging.basicConfig(filename="process.log", level=logging.DEBUG, filemode="w")
import pandas as pd
from config import (
EXFOR_TRANS_URL,
EXFOR_ALL_PATH,
TRANS_REPO_URL,
headers,
TEMP_TRANS,
GIT_REPO_PATH,
GIT_REPO_URL,
)
####################################################################
# For maintenance
####################################################################
def get_list_of_trans_files():
"""
pd.read_html returns the list of dataframes
groupby returns dictionary in { '2024-05-06': ['trans.1510', 'trans.1511', 'trans.c235', 'trans.c236', 'trans.l052']} format
"""
r = requests.get(EXFOR_TRANS_URL)
dfs = pd.read_html(r.text)
df = dfs[0]
df[["Date", "Time"]] = df["Last modified"].str.split(" ", expand=True)
df = df.drop(df[df["Name"].str.contains("trans.zip")].index)
df = df.drop(df[df["Name"].str.contains("trans.9")].index) # To skip EXFOR dictionary trans
df = df.dropna(subset=["Last modified"])
x = df.groupby("Date")["Name"].apply(list).to_dict()
return x
def get_latest_date(x: dict):
if x:
d = []
for a in x.keys():
d.append(convert_dtform(a))
return max(d)
else:
return None
####################################################################
# General functions
####################################################################
def convert_dtform(dtstring: str):
# dtstring: "YYYY-MM-DD" in text format
return datetime.date(
int(dtstring.split("-")[0]),
int(dtstring.split("-")[1]),
int(dtstring.split("-")[2]),
)
def del_file(filename: str):
if os.path.isfile(filename):
os.remove(filename)
logging.info(f"File has been deleted")
else:
logging.info(f"File does not exist")
def del_files(directory_path):
shutil.rmtree(directory_path)
####################################################################
# Git functions
####################################################################
repo = Repo(GIT_REPO_PATH)
assert not repo.bare
def git_new_branch(date_str):
repo.git.checkout("HEAD", b=date_str) # create a new branch
logging.info(f"repo.active_branch {repo.active_branch}")
def git_add_commit(date_str):
repo.git.add("exforall/")
repo.git.commit(m=date_str)
logging.info(f"branch commit {date_str}")
def git_push_branch(date_str):
origin = repo.remote(name="origin")
origin.push(date_str)
logging.info(f"origin.push {date_str}")
def git_merge_to_main(date_str):
repo.git.checkout("main")
repo.git.merge(date_str)
origin = repo.remote(name="origin")
origin.push()
logging.info(f"repo.git.merge and origin.push master")
def git_branches():
branches = []
remote_refs = repo.remote().refs
for refs in remote_refs:
branches.append(refs.name.replace("origin/", ""))
return branches
def git_tags():
tags = []
for tag in repo.tags:
tags.append(tag.name.replace("Backup-", ""))
return tags
def semantic_release_name(branch_name):
name_sp = branch_name.split("-")
if len(branch_name) == 10:
return "v" + str(name_sp[0]) + str(name_sp[1]) + str(name_sp[2]) + ".0"
elif branch_name.count("-") == 3:
return (
"v" + str(name_sp[0]) + str(name_sp[1]) + str(name_sp[2]) + "." + name_sp[3]
)
elif len(branch_name) == 11:
return (
"v"
+ str(name_sp[0])
+ str(name_sp[1])
+ str(name_sp[2])[0:2]
+ "."
+ name_sp[2][2:]
)
def git_log():
return repo.git.log("--name-status", "--no-merges", "-n1", "--pretty=oneline")
def git_tagging(branch_name):
repo.git.checkout(branch_name)
repo.git.pull("origin", branch_name)
repo.git.fetch("origin")
msg = git_log()
repo.create_tag("Backup-" + branch_name, ref=branch_name, message=msg)
origin = repo.remote(name="origin")
origin.push("Backup-" + branch_name)
logging.info(f"tagging branch_name: {branch_name}")
def git_release(branch_name):
data_body = {
"tag_name": "Backup-" + branch_name,
"name": semantic_release_name(branch_name),
"draft": False,
"prerelease": False,
"generate_release_notes": False,
}
r = requests.post(
GIT_REPO_URL,
data=json.dumps(data_body),
headers=headers,
verify=True
)
def git_delete_branch(date_str):
repo.git.checkout("main")
repo.git.branch("-d", date_str)
origin = repo.remote(name="origin")
origin.push()
logging.info(f"repo.git.branch -d")
####################################################################
# Update EXFOR ENTRY based on TRANS
####################################################################
def download_trans_files(trans_list: list):
for trans in trans_list:
try:
url = "".join([EXFOR_TRANS_URL, trans])
r = requests.get(url, allow_redirects=True)
except:
url = "".join([TRANS_REPO_URL, "-/tree/main/trans_files", trans])
r = requests.get(url, allow_redirects=True)
if r.status_code == 404:
logging.error(
f"Something wrong with retrieving trans list from both IAEA-NDS Web and Databank Gitlab."
)
sys.exit()
else:
if not os.path.exists(TEMP_TRANS):
os.mkdir(TEMP_TRANS)
open(f"{TEMP_TRANS}/{trans}", "wb").write(r.content)
logging.info(f"{trans} file downloaded")
return
def get_subent_index(lines):
content_indexes = {}
for i, line in enumerate(lines):
if "TRANS" in line[0:5] or "ENDTRANS" in line[0:8]:
continue
if "ENTRY" in line[0:5]:
entry_num = line[17:22]
ent_start_index = i
content_indexes[entry_num] = {"ENTRY": {}, "SUBENTRIES": {}}
elif "SUBENT" in line[0:6]:
subnet_num = line[14:22]
subent_start_index = i
elif "ENDSUBENT" in line[0:9]:
subent_end_index = i
content_indexes[entry_num]["SUBENTRIES"][subnet_num] = {
"start": subent_start_index,
"end": subent_end_index,
}
elif "NOSUBENT" in line[0:8]:
subnet_num = line[14:22]
content_indexes[entry_num]["SUBENTRIES"][subnet_num] = {
"start": i,
"end": i,
}
elif "ENDENTRY" in line[0:8]:
ent_end_index = i
content_indexes[entry_num]["ENTRY"] = {
"start": ent_start_index,
"end": ent_end_index,
}
"""
{'M0538': {
'ENTRY': {'start': 1, 'end': 454},
'SUBENTRIES':
{'M0538001': {'start': 2, 'end': 34}, 'M0538002': {'start': 35, 'end': 220}, 'M0538003': {'start': 221, 'end': 419}, 'M0538004': {'start': 420, 'end': 436}, 'M0538005': {'start': 437, 'end': 453}}},
'M0702': {
'ENTRY': {'start': 455, 'end': 1197},
'SUBENTRIES': {'M0702001': {'start': 456, 'end': 493}, 'M0702002': {'start': 494, 'end': 698}, 'M0702003': {'start': 699, 'end': 993}, 'M0702004': {'start': 994, 'end': 1196}}},
"""
return content_indexes
def ent_header(trans, entry_num, ent_date, trans_date):
if len(entry_num) == 5:
## ENTRY M0538 20230918 20231102 20231102 M126
return "{:11}{:>11}{:>11}{:>11}{:>11}{:>11}\n".format(
"ENTRY",
entry_num.upper(),
ent_date,
trans_date,
trans_date,
trans.split(".")[1].upper(),
)
if len(entry_num) == 8:
## SUBENT M1040001 20221223 20230307 20230307 M122
return "{:11}{:>11}{:>11}{:>11}{:>11}{:>11}\n".format(
"SUBENT",
entry_num.upper(),
ent_date,
trans_date,
trans_date,
trans.split(".")[1].upper(),
)
else:
return None
def ent_footer(num):
return "{:11}{:>11}\n".format("ENDENTRY", num)
def process_trans_file(trans: str):
logging.info(f"process: {trans} file started")
"""
trans_lines: all lines in the TRANS
trans_indexes: line numbers of start and end of ENTRY and SUBENT in the TRANS
ent_lines: all lines in the current ENTRY
ent_indexes: line numbers of start and end of SUBENT in the ENTRY
"""
## Read the source file
trans_file_path = os.path.join(TEMP_TRANS, trans)
trans_lines = []
with open(trans_file_path, "r") as trans_file:
for line in trans_file:
if line.startswith("END"):
trans_lines += [line[0:22].rstrip() + "\n"]
else:
trans_lines += [line[0:66].rstrip() + "\n"]
trans_indexes = get_subent_index(trans_lines)
for entry_num in trans_indexes.keys():
entry_dir = os.path.join(EXFOR_ALL_PATH, entry_num[0:3])
## Check if the entry dir exist
if not os.path.exists(entry_dir):
os.mkdir(entry_dir)
ent_file_name = os.path.join(entry_dir, entry_num[0:5] + ".x4")
## Check if the entry file exist, else create a new one
if os.path.exists(ent_file_name):
"""
Update existing entry file
"""
with open(ent_file_name, "r") as e:
ent_lines = e.readlines()
ent_indexes = get_subent_index(ent_lines)
subents = sorted(
set(
list(ent_indexes[entry_num]["SUBENTRIES"].keys())
+ list(trans_indexes[entry_num]["SUBENTRIES"].keys())
)
)
else:
"""
Create new file
"""
subents = sorted(list(trans_indexes[entry_num]["SUBENTRIES"].keys()))
trans_date = trans_lines[0][22:33]
ent_date = trans_lines[ trans_indexes[entry_num]["ENTRY"]["start"] ][22:33]
## create ENTRY in the list
new_subent_lines = []
new_subent_lines += [ ent_header(trans, entry_num, ent_date, trans_date) ]
for subent in subents:
"""
{'M0538': {'ENTRY': {'start': 0, 'end': 433},
'SUBENTRIES': {'M0538001': {'start': 1, 'end': 33},
'M0538002': {'start': 34, 'end': 212}...
"""
if trans_indexes[entry_num]["SUBENTRIES"].get(subent):
src_start = trans_indexes[entry_num]["SUBENTRIES"][subent]["start"]
src_end = trans_indexes[entry_num]["SUBENTRIES"][subent]["end"]
new_subent_lines += [ ent_header(trans, subent, ent_date, trans_date) ]
new_subent_lines += trans_lines[src_start + 1 : src_end + 1] # already a list
else:
dest_start = ent_indexes[entry_num]["SUBENTRIES"][subent]["start"]
dest_end = ent_indexes[entry_num]["SUBENTRIES"][subent]["end"]
if dest_start == dest_end:
new_subent_lines += ent_lines[dest_start : dest_end]
else:
new_subent_lines += ent_lines[dest_start : dest_end + 1] # already a list
## end ENTRY
new_subent_lines += [ ent_footer(len(subents)) ]
with open(ent_file_name, "w") as e:
for line in new_subent_lines:
e.write(line)
logging.info(f"end: {trans} {entry_num}")
logging.info(f"end: {trans}")
def process_release(date_str, trans_list):
## create new branch and extract master file and split into exntry
git_new_branch(date_str)
for trans in trans_list:
logging.info(f"start processing of {trans} in {date_str}")
process_trans_file(trans)
## git commit, push, merge
git_add_commit(date_str)
git_push_branch(date_str)
git_merge_to_main(date_str)
## create Release in Github
git_tagging(date_str)
## release does not work from the CLI from Github/Actions,
## it needs curl to post to REST API
## (see .github/workflows/manual.yaml)
git_release(date_str)
## delete branch
git_delete_branch(date_str)
## delete donwnloaded files
del_files(TEMP_TRANS)
####################################################################
# This is the process to update the repository.
####################################################################
def update():
x = get_list_of_trans_files()
tags = git_tags()
processed = []
not_processed = []
## check if there are any unprocessed trans file
for date_str in x.keys():
date_dt = convert_dtform(date_str)
if date_dt < datetime.date(2023, 11, 2):
"""
Since the production of EXFOR backup files are suspended by NRDC after the final backup file,
which corresponds to the 2023-10-27 database version and contains trans.o097,
https://github.com/IAEA-NDS/exfor_master/releases/tag/Backup-2023-10-27),
the master file will be updated based on TRANS files.
"""
continue
if date_str in tags:
processed.append(date_str)
else:
not_processed.append(date_str)
logging.info(f"process starts for {not_processed}")
if not_processed:
"""
After the TRANS files have been back public in May 2024, the name of the 'release'
will be the date of the TRANS file. If more than one TRANS files exist, all TRANS are
included in one 'release'.
"""
for date_str in not_processed:
download_trans_files(x[date_str])
process_release(date_str, x[date_str])
else:
logging.info(f"repository is up-to-date")
return " ".join(not_processed)
if __name__ == "__main__":
print(update())