-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_all_campus_restricted.py
executable file
·289 lines (251 loc) · 10.2 KB
/
fix_all_campus_restricted.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
#!/usr/bin/env python3
#
"""Update RDM records based on eprinte metadata"""
import sys
import os
import json, csv
import requests
from caltechdata_api import caltechdata_edit
from datetime import datetime
from urllib.parse import urlparse, unquote_plus
from subprocess import Popen, PIPE
from irdm import RdmUtil, eprint2rdm, fixup_record
from eprints_to_rdm import get_file_list
# Global list of DOIs aded during run, which are used to manage slow indexing times when records
# are updated
DOI_LIST = []
class WorkObject:
"""create a working object from dict for managing state in complete function."""
def __init__(self, working_object):
self.eprintid = working_object.get("eprintid", None)
self.community_id = working_object.get("community_id", None)
self.root_rdm_id = working_object.get("root_rdm_id", None)
self.rdm_id = working_object.get("rdm_id", None)
self.version_record = working_object.get("version_record", None)
self.rec = working_object.get("record", None)
self.restriction = working_object.get("restriction", None)
self.version = working_object.get("version", "")
self.publication_date = working_object.get("publication_date", None)
def display(self):
"""return a JSON version of object contents."""
return json.dumps(
{
"eprintid": self.eprintid,
"community_id": self.community_id,
"root_rdm_id": self.root_rdm_id,
"rdm_id": self.rdm_id,
"version": self.version,
"restriction": self.restriction,
"version_record": self.version_record,
"publication_date": self.publication_date,
}
)
def as_dict(self):
"""return object as a dict"""
return {
"eprintid": self.eprintid,
"community_id": self.community_id,
"root_rdm_id": self.root_rdm_id,
"rdm_id": self.rdm_id,
"version_record": self.version_record,
"rec": self.rec,
"restrictions": self.restriction,
}
def check_environment():
"""Check to make sure all the environment variables have values and are avialable"""
varnames = [
"EPRINT_HOST",
"EPRINT_USER",
"EPRINT_PASSWORD",
"EPRINT_DOC_PATH",
"RDMTOK",
]
config = {}
is_ok = True
for varname in varnames:
val = os.getenv(varname, None)
if val is None:
print(f"missing enviroment {varname}", file=sys.stderr)
is_ok = False
else:
config[varname] = val
return config, is_ok
def get_restrictions(obj):
"""return any restrictins indicated in .access attribute"""
restrict_record = False
restrict_files = False
if "access" in obj and "record" in obj["access"]:
restrict_record = obj["access"]["record"] == "restricted"
if "access" in obj and "files" in obj["access"]:
restrict_files = obj["access"]["files"] == "restricted"
return restrict_record, restrict_files
def set_restrictions(rdmutil, rdm_id, rec):
"""set the restrictions for a draft using rec"""
restrict_record, restrict_files = get_restrictions(rec)
if restrict_files:
_, err = rdmutil.set_access(rdm_id, "files", "restricted")
if err is not None:
return err
if restrict_record:
_, err = rdmutil.set_access(rdm_id, "record", "restricted")
if err is not None:
return err
return None
def pairtree(txt):
"""take a text string and generate a pairtree path from it."""
return "/".join([txt[i : i + 2] for i in range(0, len(txt), 2)])
def update_record(config, rec, rdmutil, rdm_id):
caltechdata_edit(
rdm_id,
metadata=rec,
token=config["RDMTOK"],
production=True,
publish=True,
authors=True,
)
def fix_record(config, eprintid, rdm_id, restriction, reload=False):
"""Migrate a single record from EPrints to RDM using the document security model
to guide versioning."""
rdmutil = RdmUtil(config)
eprint_host = config.get("EPRINT_HOST", None)
token = config["RDMTOK"]
headers = {"Authorization": f"Bearer {token}"}
existing = requests.get(
"https://authors.library.caltech.edu/api/records/" + rdm_id, headers=headers
).json()
rec, err = eprint2rdm(eprintid)
if err is not None:
print(f"{eprintid}, None, failed ({eprintid}): eprint2rdm {eprintid}")
sys.stdout.flush()
return err # sys.exit(1)
file_list = get_file_list(config, eprintid, rec, restriction)
file_description = ""
file_types = set()
campusonly_description = """<p><strong>Files attached to this record are
restricted to users connected to the Caltech campus
network:</strong></p><ul>"""
campusonly_files = False
if len(file_list) > 0:
for file in file_list:
filename = file.get("filename", None)
description = file["description"]
if restriction == "validuser":
campusonly_description += f' <li>{description} - <a href="https://campus-restricted.library.caltech.edu/{rdm_id}/{filename}">{filename}</a></li>'
campusonly_files = True
file_types.add("campus only")
campusonly_description += "</ul>"
existing["metadata"]["version"] = "Campus-Access Only"
if file_description != "" or campusonly_files:
additional_descriptions = existing["metadata"].get(
"additional_descriptions", []
)
for desc in additional_descriptions:
if desc["type"]["id"] == "attached-files" or desc["type"]["id"] == "files":
additional_descriptions.remove(desc)
additional_descriptions.append(
{"type": {"id": "files"}, "description": campusonly_description}
)
existing["metadata"]["additional_descriptions"] = additional_descriptions
update_record(config, existing, rdmutil, rdm_id)
return None
def process_status(app_name, tot, cnt, started):
if (cnt % 10) == 0:
# calculate the duration in minutes.
now = datetime.now()
duration = (now - started).total_seconds()
x = cnt / duration
minutes_remaining = round((tot - cnt) * x)
percent_completed = round((cnt / tot) * 100)
if cnt == 0 or duration == 0:
print(
f'# {now.isoformat(" ", "seconds")} {app_name}: {cnt}/{tot} {percent_completed}% eta: unknown',
file=sys.stderr,
)
else:
print(
f'# {now.isoformat(" ", "seconds")} {app_name}: {cnt}/{tot} {percent_completed}% eta: {minutes_remaining} minutes',
file=sys.stderr,
)
def display_status(app_name, cnt, started, completed):
# calculate the duration in minutes.
duration = round((completed - started).total_seconds() / 60) + 1
x = round(cnt / duration)
print(f"# records processed: {cnt}", file=sys.stderr)
print(f"# duration: {duration} minutes", file=sys.stderr)
print(f"# records per minute: {x}")
print(
f'# {app_name} started: {started.isoformat(" ", "seconds")}, completed: {completed.isoformat(" ", "seconds")}',
file=sys.stderr,
)
def process_document_and_eprintids(config, app_name, eprintids):
"""Process and array of EPrint Ids and migrate those records."""
started = datetime.now()
tot = len(eprintids)
print(
f'# Processing {tot} eprintids, started {started.isoformat(" ", "seconds")}',
file=sys.stderr,
)
for i, _id in enumerate(eprintids):
err = migrate_record(config, _id)
if err is not None:
print(f"error processing {_id}, row {i}, {err}", file=sys.stderr)
process_status(app_name, tot, i, started)
completed = datetime.now()
display_status(app_name, len(eprintids), started, completed)
return None
def get_eprint_ids():
"""review the command line parameters and get a list of eprint ids"""
eprint_ids = []
if len(sys.argv) > 1:
arg = sys.argv[1]
if os.path.exists(arg):
with open(arg, encoding="utf-8") as _f:
for line in _f:
eprint_ids.append(line.strip())
elif arg.isdigit():
args = sys.argv[:]
for eprint_id in args[1:]:
eprint_ids.append(eprint_id.strip())
return eprint_ids
#
# Migrate a records using eprint2rdm, ./migrate_record.py and rdmutil.
#
def main():
"""main program entry point. I'm avoiding global scope on variables."""
app_name = os.path.basename(sys.argv[0])
config, is_ok = check_environment()
if is_ok:
with open("migrated_records.csv", "r") as f:
token = config["RDMTOK"]
headers = {"Authorization": f"Bearer {token}"}
migrated = csv.DictReader(f)
records = {}
to_update = {}
for row in migrated:
if row['record_status'] == 'public':
if row['eprintid'] not in records:
records[row['eprintid']] = row['rdmid']
else:
api_url = f"https://authors.library.caltech.edu/api/records/"
response = requests.get(api_url + row['rdmid'],headers=headers)
if 'versions' in response.json():
latest = response.json()['versions']['is_latest']
else:
print(row['rdmid'])
print(response.json())
if latest==True:
to_update[records[row['eprintid']]]=row['eprintid']
else:
to_update[row['rdmid']]=row['eprintid']
for rdm_id, eprintid in to_update.items():
restriction = "validuser"
err = fix_record(config, eprintid, rdm_id, restriction)
if err is not None:
print(f"Aborting {app_name}, {err}", file=sys.stderr)
sys.exit(1)
print(f"Updated {rdm_id}")
else:
print(f"Aborting {app_name}, environment not setup", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()