-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_projects.py
More file actions
269 lines (163 loc) · 7.67 KB
/
import_projects.py
File metadata and controls
269 lines (163 loc) · 7.67 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
import os, sys, argparse, pprint, json, ssl, getpass
from irods.session import iRODSSession
from irods.collection import iRODSCollection
from irods.models import Collection, DataObject, DataAccess, User
from irods.meta import iRODSMeta
from irods.exception import CollectionDoesNotExist
######################################
def ParseProject (project, irods_session, verbosity, selected_project_uuids):
irods_path = project ["irods_path"]
if (irods_path != None):
if (verbosity > 0):
print ("Parsing project at", irods_path)
normalized_path = iRODSCollection.normalize_path (irods_path)
print ("normalized_path", normalized_path)
print ("using session ", irods_session)
irods_obj = None
try:
irods_obj = irods_session.collections.get (irods_path)
except CollectionDoesNotExist as cdne:
print ("ERROR: irods_path:", irods_path, " does not exist")
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print (message)
if (irods_obj):
AddMetadataForProject (irods_obj, project, verbosity)
AddMetadataForAllChildren (irods_obj, project ["uuid"], verbosity)
else:
print ("ERROR: irods_path not set for", project)
#####################################
def AddMetadataForProject (irods_obj, project, verbosity):
if (project ["uuid"]):
if (project ["projectName"]):
AddMetadataKeyAndValue (irods_obj, "license", "Toronto", verbosity)
AddMetadataKeyAndValue (irods_obj, "license_url", "https://www.nature.com/articles/461168a#Sec2", verbosity)
AddMetadataKeyAndValue (irods_obj, "uuid", project ["uuid"], verbosity)
if (project ["authors"]):
authors = ", ".join(project ["authors"])
AddMetadataKeyAndValue (irods_obj, "authors", authors, verbosity)
AddMetadataKeyAndValue (irods_obj, "projectName", project ["projectName"], verbosity)
if (project ["description"]):
AddMetadataKeyAndValue (irods_obj, "description", project ["description"], verbosity)
else:
print ("ERROR: No projectName", project);
else:
print ("ERROR: No uuid", project);
#####################################
def AddMetadataKeyAndValue (irods_obj, key, value, verbosity):
#irods_obj.metadata.add (key, value)
coll = 1
if (verbosity > 1):
print ("key:", key, "\nvalue:")
pprint.pprint (value)
print ("\n")
existing_values = irods_obj.metadata.get_all (key)
for item in existing_values:
irods_obj.metadata.remove (item)
irods_obj.metadata.add (key, value)
######################################
def AddMetadataForAllChildren (irods_obj, project_uuid, verbosity):
uuid_key = "uuid"
for collection, subcollections, data_objects in irods_obj.walk (topdown = True):
if (len (data_objects) > 0):
for irods_obj in data_objects:
if (verbosity > 0):
print ("adding ", uuid_key, " = ", project_uuid, " for ", irods_obj)
AddMetadataKeyAndValue (irods_obj, uuid_key, project_uuid, verbosity)
if (len (subcollections) > 0):
for subc in subcollections:
if (verbosity > 0):
print ("adding ", uuid_key, " = ", project_uuid, " for ", subc)
AddMetadataKeyAndValue (subc, uuid_key, project_uuid, verbosity)
###################################
def GetCommandLineArgs (parser):
parser.add_argument ("-i", "--input_file", help = "The input Projects JSON file to load")
parser.add_argument ("-e", "--env_file", help = "Connect using this iRODS environment file")
parser.add_argument ("-H", "--host", default = "localhost", help = "The iRODS server hostname")
parser.add_argument ("-P", "--port", default = 1247, type = int, help = "The port that the iRODS server is running on")
parser.add_argument ("-u", "--user", help = "The username to use to log in to the iRODS server")
parser.add_argument ("-p", "--password", help = "The password to use to log in to the iRODS server")
parser.add_argument ("-z", "--zone", help = "The iRODS zone to connect to")
parser.add_argument ("--uuids", nargs="*", help = "The Project UUIDS to parse. If this is not set, all projects in the file will be parsed.")
parser.add_argument ("-v", "--verbose", help = "Display progress messages", action = "store_true")
args = parser.parse_args ()
return args
########################################
def IsSelectedProject (project, project_uuids, verbose):
res = True
uuid = project ["uuid"]
if (uuid != None):
if (project_uuids != None):
if (uuid not in project_uuids):
res = False
else:
res = False;
return res
#######################################
# Taken from https://irods.wur.nl/userguide/irods_setup_client/
def ConnectToIRODS (env_file, password) -> iRODSSession:
irods_env_file_loc = os.path.expanduser (env_file)
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=None, capath=None, cadata=None)
ssl_settings = {"ssl_context": ssl_context}
print('Connecting to iRODS using file: ', irods_env_file_loc, '...')
#pw = getpass.getpass().encode()
with open (irods_env_file_loc) as f:
ienv = json.load (f)
if (password == None):
password = getpass.getpass ().encode ()
password = pw.decode ()
session = iRODSSession (**ienv, password = password, **ssl_settings)
return session
#######################################
# Taken from https://irods.wur.nl/course/introduction_to_irods/course_content_step_03/
def List (irods_session, path, recurse_flag):
print ("Listing", path)
try:
coll = irods_session.collections.get (path)
print(' C - ', coll.path)
for data_obj in coll.data_objects:
print(' D - ', data_obj.name)
for sub_coll in coll.subcollections:
if (recurse_flag):
List (irods_session, sub_coll.path)
else:
print(' C - ', sub_coll.name)
except Exception as e:
print(f'Error: {e}')
###############################
parser = argparse.ArgumentParser ()
args = GetCommandLineArgs (parser)
if (args.input_file != None):
projects_file = None
if (args.verbose):
print ("loading", args.input_file)
try:
projects_file = open (args.input_file)
except FileNotFoundError:
print ("ERROR: File does not exist:", args.input_file)
except:
print ("ERROR: General error loading:", args.input_file)
if (projects_file != None):
# Get the contents as a dictionary
projects = json.load (projects_file);
if (args.verbose):
if (args.env_file):
print ("Opening session using environment file", args.env_file, "password", args.password)
irods_session = ConnectToIRODS (args.env_file, args.password)
else:
print ("Opening session to", args.host, "on port", args.port, "as user", args.user, "on zone", args.zone)
irods_session = iRODSSession (host = args.host, port = args.port, user = args.user, password = args.password, zone = args.zone)
if (irods_session):
if (args.verbose):
print ("Successfully opened session to", irods_session.host, "on port", irods_session.port, "as user", irods_session.username, "on zone", irods_session.zone)
i = 0
for project in projects:
if (IsSelectedProject (project, args.uuids, args.verbose)):
if (args.verbose):
print ("Working on Project", i, " with session ", irods_session)
ParseProject (project, irods_session, args.verbose, args.uuids)
i = i + 1
irods_session.cleanup ()
else:
parser.print_help ()