rda-python-common 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rda-python-common might be problematic. Click here for more details.

@@ -0,0 +1,198 @@
1
+ #
2
+ ###############################################################################
3
+ #
4
+ # Title : PgGLBS.py
5
+ # Author : Thomas Cram, tcram@ucar.edu
6
+ # Date : 12/10/2014
7
+ # Purpose : python library module for Globus functions and utilities
8
+ # Changes : 10/10/2020, Zaihua Ji, zji@ucar.edu:
9
+ # converted from perl package to python module
10
+ #
11
+ # Work File : $DSSHOME/lib/python/PgGLBS.py
12
+ # Github : https://github.com/NCAR/rda-shared-libraries.git
13
+ #
14
+ ###############################################################################
15
+ #
16
+ import os
17
+ import re
18
+ import PgLOG
19
+ import PgUtil
20
+ from MyGlobus import MyGlobus, MyEndpoints, GLOBUS_REQUEST_DOMAIN
21
+ import PgDBI
22
+
23
+ try:
24
+ from urllib.parse import urlencode
25
+ except:
26
+ from urllib import urlencode
27
+
28
+ BFILES = {} # cache backup file names and dates for each bid
29
+
30
+ #
31
+ # Remove the Globus share rule ID for a dsrqst share
32
+ #
33
+ def remove_globus_rid(ridx, dsid):
34
+
35
+ if not ridx: return PgLOG.pglog("[remove_globus_rid] Request index is not defined", PgLOG.LOGWRN)
36
+ if not dsid: return PgLOG.pglog("[remove_globus_rid] Dataset ID is not defined", PgLOG.LOGWRN)
37
+
38
+ cmd = "dsglobus"
39
+ args = "'-rp -ri {}'".format(ridx)
40
+ action = "RP"
41
+ host = "PBS"
42
+ workdir = "/glade/u/home/tcram"
43
+ opts = "'-l walltime=15:00'"
44
+ spec = "tcram"
45
+ check_cmd = "dscheck ac -cm {} -av {} -ds {} -an {} -hn {} -wd {} -sn {} -qs {} -md".format(cmd, args, dsid, action, host, workdir, spec, opts)
46
+
47
+ PgLOG.pgsystem(check_cmd)
48
+
49
+ #
50
+ # Submit a Globus transfer of the request output on behalf of the user
51
+ #
52
+ def submit_globus_transfer(ridx):
53
+
54
+ # call dsglobus to submit transfer
55
+ cmd = "dsglobus -st -ri {}".format(ridx)
56
+ return PgLOG.pgsystem(cmd, PgLOG.LOGWRN, 16)
57
+
58
+ #
59
+ # check a RDA file is backed up or not for given file record;
60
+ # clear the cached bfile records if frec is None.
61
+ # return 0 if not yet, 1 if backed up, or -1 if backed up but modified
62
+ #
63
+ def file_backup_status(frec, chgdays = 1, logact = 0):
64
+
65
+ if frec is None:
66
+ BFILES.clear()
67
+ return 0
68
+
69
+ bid = frec['bid']
70
+ if not bid: return 0
71
+
72
+ fields = 'bfile, dsid, date_modified'
73
+ if chgdays > 0: fields += ', note'
74
+ if bid not in BFILES: BFILES[bid] = PgDBI.pgget('bfile', fields, 'bid = {}'.format(bid), logact)
75
+ brec = BFILES[bid]
76
+ if not brec: return 0
77
+
78
+ if 'sfile' in frec:
79
+ fname = frec['sfile']
80
+ ftype = 'Saved'
81
+ else:
82
+ fname = frec['wfile']
83
+ ftype = 'Web'
84
+ ret = 1
85
+ fdate = frec['date_modified']
86
+ bdate = brec['date_modified']
87
+ if chgdays > 0 and PgUtil.diffdate(fdate, bdate) >= chgdays:
88
+ ret = -1
89
+ if brec['note']:
90
+ mp = r'{}<:>{}<:>(\d+)<:>(\w+)<:>'.format(fname, frec['type'])
91
+ ms = re.search(mp, brec['note'])
92
+ if ms:
93
+ fsize = int(ms.group(1))
94
+ cksum = ms.group(2)
95
+ if cksum and cksum == frec['checksum'] or not cksum and fsize == frec['data_size']:
96
+ ret = 1
97
+
98
+ if logact:
99
+ if ret == 1:
100
+ msg = "{}-{}: {} file backed up to /{}/{} by {}".format(frec['dsid'], fname, ftype, brec['dsid'], brec['bfile'], bdate)
101
+ else:
102
+ msg = "{}-{}: {} file changed on {}".format(frec['dsid'], fname, ftype, fdate)
103
+ PgLOG.pglog(msg, logact)
104
+
105
+ return ret
106
+
107
+ #=========================================================================================
108
+ def get_request_file_url(rfile, rpath=None, logact=0):
109
+ """ Returns the URL for a request file
110
+ Input arguments:
111
+ rfile = request file
112
+ dsid = dataset ID (dsnnn.n)
113
+ rpath = path to request file, relatvie to RDA data base path (e.g. '/dsrqst/<rqstid>/'
114
+ """
115
+ domain = GLOBUS_REQUEST_DOMAIN
116
+
117
+ if not rpath:
118
+ try:
119
+ cond = "wfile='{}'".format(rfile)
120
+ wfrqst = PgDBI.pgget('wfrqst', 'rindex', cond, logact)
121
+ except:
122
+ msg = "[get_request_file_url] Problem getting rindex for request file {}".format(rfile)
123
+ PgLOG.pglog(msg)
124
+ if not wfrqst:
125
+ raise TypeError("Request file {} not found in table 'wfrqst'".format(rfile))
126
+ rpath = get_request_path(wfrqst['rindex'], logact=0)
127
+
128
+ if (rpath.find('/',0,1) != -1):
129
+ rpath = rpath.replace('/','',1)
130
+
131
+ url = os.path.join(domain, rpath, rfile)
132
+ return url
133
+
134
+ #=========================================================================================
135
+ def get_request_path(rindex, logact=0):
136
+ """ Returns relative path to request file
137
+ Example: '/dsrqst/<rqstid>/'
138
+ """
139
+ try:
140
+ fields = 'rqstid, location'
141
+ cond = 'rindex={}'.format(rindex)
142
+ rqst_info = PgDBI.pgget('dsrqst', fields, cond, logact)
143
+ except:
144
+ msg = "[get_request_path] Problem getting info for request index {}".format(rindex)
145
+ PgLOG.pglog(msg)
146
+ if not rqst_info:
147
+ raise TypeError("Request index {} not found in RDADB".format(rindex))
148
+
149
+ if rqst_info['location']:
150
+ base_path = MyGlobus['data_request_endpoint_base']
151
+ loc = rqst_info['location']
152
+ loc = loc.rstrip("/")
153
+ if (loc.find(base_path) != -1):
154
+ path_len = len(base_path)
155
+ path = "/{0}/".format(loc[path_len:])
156
+ else:
157
+ path = "/"
158
+ else:
159
+ path = "/dsrqst/{0}/".format(rqst_info['rqstid'])
160
+
161
+ return path
162
+
163
+ #=========================================================================================
164
+ def get_guest_collection_url(dsid=None, locflag=None, rindex=None, logact=0):
165
+ """ Returns the URL for the guest collection endpoint in the Globus File Manager.
166
+ Either dataset ID (dsid) or request index (rindex) is required. If neither
167
+ dsid or rindex are provided, the default URL returned is the top level URL for
168
+ the 'NCAR RDA Dataset Archive' guest collection.
169
+
170
+ Optional argument locflag = location flag of dataset ('G' = glade, 'O' = stratus,
171
+ 'B' = both glade and stratus, 'C' = CGD data under /glade/campaign/cgd/cesm)
172
+ """
173
+
174
+ if rindex:
175
+ origin_id = MyEndpoints['rda#data_request']
176
+ origin_path = get_request_path(rindex, logact=logact)
177
+ elif dsid:
178
+ if not locflag:
179
+ cond = "dsid='{}'".format(dsid)
180
+ pgloc = PgDBI.pgget('dataset', 'locflag', cond, logact)
181
+ locflag = pgloc['locflag']
182
+ if locflag == 'C':
183
+ origin_id = MyEndpoints['rda-cgd']
184
+ origin_path = "/"
185
+ elif locflag == 'O' or locflag == 'B':
186
+ origin_id = MyEndpoints['rda-stratus']
187
+ origin_path = "/{}/".format(dsid)
188
+ else:
189
+ origin_id = MyEndpoints['rda#datashare']
190
+ origin_path = "/{}/".format(dsid)
191
+ else:
192
+ origin_id = MyEndpoints['rda#datashare']
193
+ origin_path = "/"
194
+
195
+ params = {'origin_id': origin_id, 'origin_path': origin_path}
196
+ url = '{0}?{1}'.format(MyGlobus['globus_share_url'], urlencode(params))
197
+
198
+ return url