-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_projects.py
190 lines (117 loc) · 5.11 KB
/
import_projects.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
import os, sys, argparse, pprint, json
from irods.session import iRODSSession
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)
irods_obj = None
try:
irods_obj = irods_session.collections.get (irods_path)
except CollectionDoesNotExist:
print ("ERROR: irods_path:", irods_path, " does not exist")
# except:
# print (">>>>> irods_path:", irods_path, " general error")
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 ("-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
#######################################
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);
i = 0
if (args.verbose):
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)
for project in projects:
if (IsSelectedProject (project, args.uuids, args.verbose)):
if (args.verbose):
print ("Working on Project", i)
ParseProject (project, irods_session, args.verbose, args.uuids)
i = i + 1
else:
parser.print_help ()