-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy path_app.py
More file actions
371 lines (329 loc) · 16.8 KB
/
Copy path_app.py
File metadata and controls
371 lines (329 loc) · 16.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
##
## © Copyright 2021- IBM Inc. All rights reserved
# SPDX-License-Identifier: MIT
##
import logging
import urllib
import requests.exceptions
from . import rdfxml
from . import oslcqueryapi
from . import utils
from . import httpops
from . import _validate
from . import _customScenarios
logger = logging.getLogger(__name__)
#################################################################################################
# a generic jazz application
class _App( httpops.HttpOperations_Mixin, _validate.Validate_Mixin, _customScenarios.CustomScenarios_Mixin ):
'A generic Jazz application'
domain = 'UNSPECIFIED APP DOMAIN'
project_class = None
artifact_formats = [] # For RR
reportablerest_baseurl = "publish"
supports_reportable_rest = False
reportable_rest_status = "Not supported by application"
majorVersion = None
version = None
def __init__(self, server, contextroot, *, jts=None):
super().__init__()
logger.info( f'Creating app {contextroot} {server=}' )
self.contextroot = contextroot
self.baseurl = urllib.parse.urljoin(server.baseurl, contextroot) + "/"
self.jts = jts
self.server = server
self.project_areas_xml = None
self._projects = None
self.headers = {}
self.cmServiceProviders = 'oslc_config:cmServiceProviders'
self.iid=None # app has a dummy (empty) iid
self.hooks = []
self.default_query_resource = None
def retrieve_cm_service_provider_xml(self):
cm_service_provider_uri = rdfxml.xmlrdf_get_resource_uri(self.rootservices_xml,
self.cmServiceProviders)
rdf = self.execute_get_rdf_xml(cm_service_provider_uri, intent="Retrieve application CM Service Provider" )
return rdf
def retrieve_oslc_catalog_xml(self):
oslccataloguri = rdfxml.xmlrdf_get_resource_uri(self.rootservices_xml, self.serviceproviders)
if oslccataloguri is None:
return None
return self.execute_get_rdf_xml(oslccataloguri, intent="Retrieve application OSLC Catalog (list of projects)")
# get local headers
def _get_headers(self, headers=None):
logger.info( f"app_gh" )
result = {'X-Requested-With': 'XMLHttpRequest', 'Referer': self.reluri('web'),'OSLC-Core-Version':'2.0'}
if self.headers:
result.update(self.headers)
result.update(self._get_oslc_headers())
if headers:
result.update(headers)
logger.info( f"app_gh {result}" )
return result
# get a request with local headers
def _get_request(self, verb, reluri='', *, params=None, headers=None, data=None):
fullheaders = self._get_headers()
if headers is not None:
fullheaders.update(headers)
sortedparams = None if params is None else {k:params[k] for k in sorted(params.keys())}
request = httpops.HttpRequest( self.server._session, verb, self.reluri(reluri), params=sortedparams, headers=fullheaders, data=data)
return request
def _get_oslc_headers(self, headers=None):
result = {
'Accept': 'application/rdf+xml'
, 'Referer': self.reluri('web')
, 'OSLC-Core-Version': '2.0'
}
if headers:
result.update(headers)
return result
def find_projectname_from_uri(self,name_or_uri):
self._load_projects()
if self.is_project_uri(name_or_uri):
# find the project
if name_or_uri in self._projects:
return self._projects[name_or_uri]['name']
else:
return name_or_uri
def is_project_uri(self, uri):
if uri.startswith(self.baseurl) and '/process/project-areas/' in uri:
return True
return False
def is_server_uri( self, uri ):
if uri and uri.startswith(self.baseurl):
return True
return False
# return an absolute URL for a url relative to this app
# NOTE if reluri has a leading / this will be relative to the serverhostname:port
# i.e. the app context root will be removed.
# So if you want an app-relative URL don't use a leading /
def reluri(self, reluri=''):
return urllib.parse.urljoin(self.baseurl,reluri)
# load the projects from the project areas XML - doesn't create any project classes, this is done later when finding a project to open
def _load_projects(self,include_archived=False,force=False):
if self.project_class is None:
raise Exception(f"projectClass has not been set on {self}!")
if self._projects is not None and not force:
return
logger.info( "Loading projects")
self._projects = {}
uri = rdfxml.xmlrdf_get_resource_uri(self.rootservices_xml, 'jp06:projectAreas')
params = {}
if include_archived:
params['includeArchived'] = 'true'
self.project_areas_xml = self.execute_get_xml(uri, params=params, intent="Retrieve all project area definitions" )
logger.debug( f"{self.project_areas_xml=}" )
for projectel in rdfxml.xml_find_elements(self.project_areas_xml,".//jp06:project-area" ):
logger.debug( f"{projectel=}" )
projectu = rdfxml.xmlrdf_get_resource_text(projectel,".//jp06:url")
projectname = rdfxml.xmlrdf_get_resource_uri(projectel,attrib='jp06:name')
logger.debug( f"{projectname=}" )
is_optin = False
singlemode = False
if self.supports_configs:
en = rdfxml.xmlrdf_get_resource_text(projectel,'.//jp:configuration-management-enabled')
is_optin = ( rdfxml.xmlrdf_get_resource_text(projectel,'.//jp:configuration-management-enabled') == "true" )
singlemode = ( rdfxml.xmlrdf_get_resource_text(projectel,'.//jp:configuration-management-mode') == "SINGLE" )
logger.info( f"{projectname=} {projectu=} {is_optin=} {singlemode=}" )
self._projects[projectu] = {'name':projectname, 'project': None, 'projectu': projectu, 'is_optin': is_optin, 'singlemode': singlemode }
self._projects[projectname] = projectu
# get an instance for a specific project
def find_project(self, projectname_or_uri, include_archived=False):
logger.info( f"Find project {projectname_or_uri}")
self._load_projects(include_archived=include_archived)
if self.is_project_uri(projectname_or_uri):
if projectname_or_uri in self._projects:
res = self._projects[projectname_or_uri]['project']
if res is None:
# create the project instance
res = self.project_class(self._projects[projectname_or_uri]['name'], self._projects[projectname_or_uri]['projectu'], self, is_optin=self._projects[projectname_or_uri]['is_optin'],singlemode=self._projects[projectname_or_uri]['singlemode'])
else:
res = None
else:
# must be a name
projectu = self._projects.get(projectname_or_uri)
if projectu is None:
res = None
else:
res = self._projects[projectu]['project']
if res is None:
# create the project instance
res = self.project_class(self._projects[projectu]['name'], self._projects[projectu]['projectu'], self, is_optin=self._projects[projectu]['is_optin'],singlemode=self._projects[projectu]['singlemode'])
logger.info( f'Project {projectname_or_uri} found {res}' )
return res
def is_uri( self, name_or_uri ):
if name_or_uri.startswith('http://') or name_or_uri.startswith('https://'):
return True
return False
def list_projects( self ):
self._load_projects()
projects = [p for p in self._projects if not self.is_uri(p)]
return projects
def report_type_system( self ):
qcdetails = self.get_query_capability_uris()
report = "<HTML><BODY>\n"
report += f"<H1>Type system report for application {self.domain}</H1>\n"
report += "<H2>Application Queryable Resource Types, short name and URI</H2>\n"
rows = []
for k in sorted(qcdetails.keys()):
shortname = k.split('#')[-1]
shortname += " (default)" if self.default_query_resource is not None and k==rdfxml.tag_to_uri(self.default_query_resource) else ""
rows.append( [shortname,k,qcdetails[k]])
# print in a nice table with equal length columns
report += utils.print_in_html(rows,['Short Name', 'URI', 'Query Capability URI'])
report += self.textreport()
rows = []
for prefix in sorted(rdfxml.RDF_DEFAULT_PREFIX.keys()):
rows.append([prefix,rdfxml.RDF_DEFAULT_PREFIX[prefix]] )
report += "<H2>Prefixes</H2>\n"
report += utils.print_in_html(rows,['Prefix', 'URI'])
report += "</BODY></HTML>\n"
return report
def get_query_capability_uri(self,resource_type=None,context=None):
context = context or self
resource_type = resource_type or context.default_query_resource
return self.get_query_capability_uri_from_xml(capabilitiesxml=context.retrieve_cm_service_provider_xml(), resource_type=resource_type,context=context)
def get_query_capability_uris(self,resource_type=None,context=None):
context = context or self
resource_type = resource_type or context.default_query_resource
return self.get_query_capability_uris_from_xml(capabilitiesxml=context.retrieve_cm_service_provider_xml(),context=context)
def get_query_capability_uri_from_xml(self,capabilitiesxml,resource_type,context):
logger.info( f"get_query_capability_uri_from_xml {self=} {resource_type=} {capabilitiesxml=}" )
if resource_type is None:
raise Exception( "You must provide a resource type" )
# ensure we have a URI for the resource type
resource_type_u = rdfxml.tag_to_uri(resource_type)
# get list of [resourcetype,uri]
qcs = self.get_query_capability_uris_from_xml(capabilitiesxml=capabilitiesxml,context=context)
if resource_type_u.startswith( 'http' ):
# looking for a complete precise URI
if resource_type_u in qcs:
return qcs[resource_type_u]
raise Exception( f"Resource type {resource_type} not found" )
# didn't specify a URI - find the first match at the end of the resouce type
for k,v in qcs.items():
if k.endswith(resource_type):
return v
raise Exception( f"Query capability {resource_type} {resource_type_u} not found!" )
# returns a dictionary of resource type to query capability URI
# this is used when the XML doesn't have references off to other URLs (like GCM does)
def get_query_capability_uris_from_xml(self,capabilitiesxml,context):
logger.info( f"get_query_capability_uris_from_xml {self=} {capabilitiesxml=}" )
qcs = {}
#<oslc:QueryCapability>
# <oslc:resourceType rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
# <oslc:queryBase rdf:resource="https://jazz.ibm.com:9443/ccm/oslc/contexts/_2H-_4OpoEemSicvc8AFfxQ/workitems"/>
# find a queryBase and it's the containing tag that has the info
for qcx in rdfxml.xml_find_elements(capabilitiesxml,'.//oslc:queryBase/..'):
for qcrtx in rdfxml.xml_find_elements( qcx, 'oslc:resourceType'):
qcs[rdfxml.xmlrdf_get_resource_uri(qcrtx)] = rdfxml.xmlrdf_get_resource_uri(qcx, "oslc:queryBase")
logger.debug( f"{rdfxml.xmlrdf_get_resource_uri(qcrtx)=}" )
return qcs
def get_factory_uri_from_xml(self,factoriesxml,resource_type,context, return_shapes=False ):
logger.info( f"get_factory_uri_from_xml {self=} {resource_type=} {factoriesxml=} {return_shapes=}" )
if resource_type is None:
raise Exception( "You must provide a resource type" )
# ensure we have a URI for the resource type
resource_type_u = rdfxml.tag_to_uri(resource_type)
# get list of [resourcetype,uri]
qcs = self.get_factory_uris_from_xml(factoriesxml=factoriesxml,context=context)
result = None
if resource_type_u.startswith( 'http' ):
# looking for a complete precise URI
if resource_type_u in qcs:
result = qcs[resource_type_u]
else:
raise Exception( f"Factory for resource type {resource_type} not found" )
else:
# didn't specify a URI - find the first match at the end of the resouce type
for k,v in qcs.items():
if k.endswith(resource_type):
result = v
if result is None:
raise Exception( f"QFactory {resource_type} {resource_type_u} not found!" )
if return_shapes:
shapeuris = []
# get the shapes from this factory capability
# find the factory capability xml
# print( f"{result=}" )
fc_rs = rdfxml.xml_find_elements( factoriesxml, f".//oslc:CreationFactory/oslc:creation[@rdf:resource='{result}']/../oslc:resourceShape" )
# print( f"{fc_rs=}" )
for rs in fc_rs:
# collect the <oslc:resourceShape entries
shapeuris.append(rdfxml.xmlrdf_get_resource_uri( rs) )
# print( f"{shapeuris=}" )
return result, shapeuris
else:
return result
# returns a dictionary of resource type to factory URI
# this is used when the XML doesn't have references off to other URLs (like GCM does)
def get_factory_uris_from_xml(self,factoriesxml,context):
logger.info( f"get_factory_uris_from_xml {self=} {factoriesxml=}" )
qcs = {}
#<oslc:QueryCapability>
# <oslc:resourceType rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
# <oslc:queryBase rdf:resource="https://jazz.ibm.com:9443/ccm/oslc/contexts/_2H-_4OpoEemSicvc8AFfxQ/workitems"/>
# find a queryBase and it's the containing tag that has the info
for qcx in rdfxml.xml_find_elements(factoriesxml,'.//oslc:CreationFactory'):
for qcrtx in rdfxml.xml_find_elements( qcx, 'oslc:resourceType'):
qcs[rdfxml.xmlrdf_get_resource_uri(qcrtx)] = rdfxml.xmlrdf_get_resource_uri(qcx, "oslc:creation")
logger.debug( f"{rdfxml.xmlrdf_get_resource_uri(qcrtx)=}" )
return qcs
def is_user_uri(self, uri):
logger.info( f"{self=} {self.jts=}" )
if uri and uri.startswith(self.jts.baseurl) and '/users/' in uri:
return True
return False
def user_uritoname_resolver(self, uri):
if self.is_user_uri(uri):
res = uri[uri.rfind("/") + 1:]
return res
raise Exception(f"Bad user uri {uri}")
def is_user_name(self, name):
logger.info( f"Checking name {name}" )
if not name or name.startswith( "http:") or name.startswith( "https:"):
return False
res = self.user_nametouri_resolver( name,raiseifinvalid=False)
if res is not None:
return True
return False
def user_nametouri_resolver(self, name, raiseifinvalid=True):
logger.info( f"Converting name {name}" )
if not raiseifinvalid or self.is_user_name(name):
user_uri = self.jts.baseurl+f"users/{name}"
# check it using whoami
try:
res = self.execute_get(user_uri, intent="Try to retrieve User" )
except requests.exceptions.HTTPError as e:
res = None
if res:
return user_uri
else:
if raiseifinvalid:
raise Exception( f"User {name} is not known on this server" )
return None
raise Exception(f"Bad user name {name}")
def resolve_project_nametouri(self, name, raiseifinvalid=True):
# find project for name
self._load_projects()
result = self._projects.get(name)
logger.debug( f"resolve_project_nametouri {name} {result}" )
return result
# return True if the uri can be accessed (used e.g. to detect archived components/configs)
def is_accessible( self, uri ):
try:
res = self.execute_get( uri )
return True
except requests.exceptions.HTTPError as e:
return False
#################################################################################################
class JTSApp(_App):
'The JTS application'
domain = 'jts'
project_class = None
supports_configs = False
supports_components = False
supports_reportable_rest = False
def __init__(self, server, contextroot, jts=None):
super().__init__(server, contextroot, jts=self)
def find_project(self, projectname):
raise Exception("JTS does not have projects!")