-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbunchPicker.py
More file actions
170 lines (135 loc) · 5.05 KB
/
bunchPicker.py
File metadata and controls
170 lines (135 loc) · 5.05 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
import requests
import argparse
import logging
from pathlib import Path
from datetime import datetime
import json
API_BASE = "https://gitlab.cern.ch/api/v4"
PROJECT = "lhc-injection-scheme%2Finjection-schemes"
BRANCH = "master"
def list_all_files():
json_files = []
page = 1
per_page = 100
while True:
url = f"{API_BASE}/projects/{PROJECT}/repository/tree"
params = {"ref": BRANCH, "recursive": True, "per_page": per_page, "page": page}
response = requests.get(url, params=params)
response.raise_for_status()
items = response.json()
if not items:
break
json_files.extend(
f["path"]
for f in items
if f["type"] == "blob" and f["path"].endswith(".json")
)
page += 1
return json_files
def get_injection_scheme(
injection_scheme_name,
): # filenames with extensions (e.g. file.json)
url = f"https://gitlab.cern.ch/lhc-injection-scheme/injection-schemes/raw/master/{injection_scheme_name}"
response = requests.get(url)
data = response.json()
return data
def generate_leftmost_bunches(injection_scheme_name):
data = get_injection_scheme(injection_scheme_name=injection_scheme_name)
if "collsIP1/5" not in data.keys() or not data["collsIP1/5"]:
return []
bx = list(
map(lambda x: (x - 1) // 10 + 1, data["collsIP1/5"])
) # convert from rf bucket to CMS count
leftmost_bx = [bx[0]] + [bx[i] for i in range(1, len(bx)) if bx[i - 1] + 1 < bx[i]]
return leftmost_bx
def get_newest_update_timestamp(directory):
newest = None
for file in Path(directory).iterdir():
try:
with file.open("r", encoding="utf-8") as f:
data = json.load(f)
update_date = data.get("update_date")
if update_date:
dt = datetime.fromisoformat(update_date.rstrip("Z"))
if newest is None or dt > newest:
newest = dt
except (json.JSONDecodeError, ValueError):
continue
return newest.isoformat() + "Z" if newest else None
def which_files_recently_updated(since_update_iso_time):
changed_files = set()
page = 1
while True:
url = f"{API_BASE}/projects/{PROJECT}/repository/commits"
params = {
"ref_name": BRANCH,
"since": since_update_iso_time,
"per_page": 100,
"page": page,
}
response = requests.get(url, params=params)
response.raise_for_status()
commits = response.json()
if not commits:
break
for commit in commits:
sha = commit["id"]
diff_url = f"{API_BASE}/projects/{PROJECT}/repository/commits/{sha}/diff"
diff_response = requests.get(diff_url)
diff_response.raise_for_status()
diffs = diff_response.json()
for d in diffs:
path = d.get("new_path")
if path and path.endswith(".json"):
changed_files.add(path)
page += 1
return changed_files
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--injection_scheme_dir",
dest="injection_scheme_directory",
type=str,
help="Path to output directory for parsed injection schemes.",
required=True,
)
args = parser.parse_args()
injection_scheme_directory = args.injection_scheme_directory
# create the basic streamer logger
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(logging.Formatter(f"[%(filename)s]: %(message)s"))
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
log.addHandler(stream_handler)
# remove default logger
log.handlers.pop(0)
injection_scheme_names = list_all_files()
newest_iso_timestamp = get_newest_update_timestamp(injection_scheme_directory)
recently_updated_injection_schemes = which_files_recently_updated(
newest_iso_timestamp
)
for injection_scheme_name in injection_scheme_names:
file_path = (
Path(injection_scheme_directory) / f"bunches_{injection_scheme_name}"
)
if not file_path.exists():
data = {}
else:
with open(file_path, "r", encoding="utf-8") as f:
try:
data = json.load(f)
except json.JSONDecodeError:
data = {}
missing_keys = any(k not in data for k in ("leftmost", "update_date"))
outdated = (
False
if missing_keys
else injection_scheme_name in recently_updated_injection_schemes
)
if missing_keys or outdated:
log.info(f"Updating {injection_scheme_name}...")
data["leftmost"] = generate_leftmost_bunches(injection_scheme_name)
data["update_date"] = datetime.utcnow().isoformat() + "Z"
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)