Skip to content

Commit bbb0328

Browse files
committed
Added typesystem import API example, improved task tracker in httpops.p, added oslc_auto prefix
1 parent e5c0a5c commit bbb0328

File tree

4 files changed

+148
-4
lines changed

4 files changed

+148
-4
lines changed

elmclient/_rm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ def load_components_and_configurations(self,force=False):
317317
thisconfu = confmember.get("{%s}resource" % rdfxml.RDF_DEFAULT_PREFIX["rdf"])
318318
confs_to_load.append(thisconfu)
319319
# maybe it's got configuration(s)
320-
confmemberx = rdfxml.xml_find_elements(configs_xml, './/oslc_config:Configuration') or rdfxml.xml_find_elements(configs_xml, './/oslc_config:Stream') or rdfxml.xml_find_elements(configs_xml, './/oslc_config:Baseline')
320+
confmemberx = rdfxml.xml_find_elements(configs_xml, './/oslc_config:Configuration') + rdfxml.xml_find_elements(configs_xml, './/oslc_config:Stream') + rdfxml.xml_find_elements(configs_xml, './/oslc_config:Baseline') + rdfxml.xml_find_elements(configs_xml, './/oslc_config:ChangeSet')
321+
321322
for confmember in confmemberx:
322323
thisconfu = rdfxml.xmlrdf_get_resource_uri( confmember )
323324
logger.debug( f"{thisconfu=}" )
@@ -349,7 +350,6 @@ def load_components_and_configurations(self,force=False):
349350
confs_to_load.append( rdfxml.xmlrdf_get_resource_uri(confmember, './oslc_config:streams') )
350351
confs_to_load.append( rdfxml.xmlrdf_get_resource_uri(confmember, './oslc_config:baselines') )
351352
confs_to_load.append( rdfxml.xmlrdf_get_resource_uri(confmember, './rm_config:changesets') )
352-
353353
nconfs += 1
354354

355355
# now create the "components"
@@ -364,6 +364,7 @@ def load_components_and_configurations(self,force=False):
364364
return (ncomps, nconfs)
365365

366366
def get_local_config(self, name_or_uri):
367+
print( f"GLC {self=} {name_or_uri=}" )
367368
result = None
368369
filter = None
369370
if name_or_uri.startswith("S:"):
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
##
2+
## © Copyright 2021- IBM Inc. All rights reserved
3+
# SPDX-License-Identifier: MIT
4+
##
5+
6+
# example of using the type system import API
7+
# also see https://jazz.net/wiki/bin/view/Main/DNGTypeImport
8+
9+
# the names for projects/configs involved are hard-coded for simplicity
10+
11+
# IMPORTANT NOTE this hardcodes the url for creating a type import session.
12+
# This should really be done by discovery looking in the component's config-specific services.xml for a CreationFactory for resourceType http://www.ibm.com/xmlns/rdm/types/TypeImportSession
13+
14+
import csv
15+
import logging
16+
import os.path
17+
import sys
18+
import time
19+
20+
import lxml.etree as ET
21+
import tqdm
22+
23+
import elmclient.server as elmserver
24+
import elmclient.utils as utils
25+
import elmclient.rdfxml as rdfxml
26+
27+
# setup logging - see levels in utils.py
28+
#loglevel = "INFO,INFO"
29+
loglevel = "TRACE,OFF"
30+
levels = [utils.loglevels.get(l,-1) for l in loglevel.split(",",1)]
31+
if len(levels)<2:
32+
# assert console logging level OFF if not provided
33+
levels.append(None)
34+
if -1 in levels:
35+
raise Exception( f'Logging level {loglevel} not valid - should be comma-separated one or two values from DEBUG, INFO, WARNING, ERROR, CRITICAL, OFF' )
36+
utils.setup_logging( filelevel=levels[0], consolelevel=levels[1] )
37+
38+
logger = logging.getLogger(__name__)
39+
40+
utils.log_commandline( os.path.basename(sys.argv[0]) )
41+
42+
jazzhost = 'https://jazz.ibm.com:9443'
43+
44+
username = 'ibm'
45+
password = 'ibm'
46+
47+
jtscontext = 'jts'
48+
rmcontext = 'rm'
49+
50+
src_proj = "rm_optin_p1"
51+
src_comp = "rm_optin_p1"
52+
src_config = "rm_optin_p1 Initial Stream"
53+
tgt_proj = "rm_optin_p2"
54+
tgt_comp = "rm_optin_p2"
55+
tgt_config = "changeset for typesystem import"
56+
57+
# caching control
58+
# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache
59+
# 1=clear cache initially then continue with cache enabled
60+
# 2=clear cache and disable caching
61+
caching = 2
62+
63+
64+
##################################################################################
65+
if __name__=="__main__":
66+
67+
# create our "server" which is how we connect to DOORS Next
68+
# first enable the proxy so if a proxy is running it can monitor the communication with server (this is ignored if proxy isn't running)
69+
elmserver.setupproxy(jazzhost,proxyport=8888)
70+
theserver = elmserver.JazzTeamServer(jazzhost, username, password, verifysslcerts=False, jtsappstring=f"jts:{jtscontext}", appstring='rm', cachingcontrol=caching)
71+
72+
# create the RM application interface
73+
dnapp = theserver.find_app( f"rm:{rmcontext}", ok_to_create=True )
74+
75+
# open the source project
76+
src_p = dnapp.find_project(src_proj)
77+
78+
# find the component
79+
src_c = src_p.find_local_component(src_comp)
80+
src_comp_u = src_c.project_uri
81+
print( f"{src_comp_u=}" )
82+
83+
# select the configuration
84+
src_config_u = src_c.get_local_config(src_config)
85+
print( f"{src_config_u=}" )
86+
src_c.set_local_config(src_config_u)
87+
88+
89+
# open the target project
90+
tgt_p = dnapp.find_project(tgt_proj)
91+
92+
# find the component
93+
tgt_c = tgt_p.find_local_component(tgt_comp)
94+
tgt_comp_u = tgt_c.project_uri
95+
print( f"{tgt_comp_u=}" )
96+
97+
# select the configuration
98+
tgt_config_u = tgt_c.get_local_config(tgt_config)
99+
print( f"{tgt_config_u=}" )
100+
tgt_c.set_local_config(tgt_config_u)
101+
102+
if tgt_config_u is None or src_config_u is None:
103+
raise Exception( "Source or target config not found!" )
104+
105+
# create the RDF body with the source and target configurations
106+
content = f"""<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:types="http://www.ibm.com/xmlns/rdm/types/">
107+
<types:TypeSystemCopySession>
108+
<types:target rdf:resource="{tgt_config_u}"/>
109+
<types:source rdf:resource="{src_config_u}"/>
110+
</types:TypeSystemCopySession>
111+
</rdf:RDF>"""
112+
113+
# initiate the delivery session - if successful will return 202 with a task tracker location
114+
response = tgt_c.execute_post_rdf_xml( reluri="type-import-sessions", data=content, cacheable=False, intent="Initiate typesystem import" )
115+
logger.debug( f" {response.status_code=} {response=}" )
116+
117+
# get the location
118+
location = response.headers.get('Location')
119+
120+
# check for 202 and location
121+
if response.status_code == 202 and location is not None:
122+
# wait for the tracker to finished
123+
result = tgt_c.wait_for_tracker( location, interval=1.0, progressbar=True, msg=f"Importing typesystem")
124+
125+
# result None is a success!
126+
if result is not None:
127+
print( f"Result={result}" )
128+
else:
129+
print( f"Result is None" )
130+
else:
131+
raise Exception( "Typesystem import failed!" )
132+

elmclient/httpops.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def execute_get_raw(self, reluri, *, params=None, headers=None, **kwargs):
233233
return response
234234

235235
def wait_for_tracker( self, location, *, interval=1.0, progressbar=False, msg='Waiting for tracker' ):
236+
verdict = None
236237
if progressbar:
237238
pbar = tqdm.tqdm(initial=0, total=100,smoothing=1,unit=" results",desc=msg)
238239
donelasttime=0
@@ -246,14 +247,23 @@ def wait_for_tracker( self, location, *, interval=1.0, progressbar=False, msg='W
246247
else:
247248
pbar.update(100-donelasttime)
248249

249-
if percent is None or percent=="100":
250+
if percent is None:
251+
# check for complete status
252+
if rdfxml.xmlrdf_get_resource_uri( response_x, ".//oslc_auto:state[@rdf:resource='http://open-services.net/ns/auto#complete']" ) is not None:
253+
if rdfxml.xmlrdf_get_resource_uri( response_x, ".//oslc_auto:verdict[@rdf:resource='http://open-services.net/ns/auto#error']" ) is not None:
254+
status = rdfxml.xmlrdf_get_resource_text( response_x, ".//oslc:statusCode" ) or "NO STATUS CODE"
255+
message = rdfxml.xmlrdf_get_resource_text( response_x, ".//oslc:message" ) or "NO MESSAGE"
256+
verdict = f"{status} {message}"
257+
break
258+
elif percent=="100":
250259
break
251260
time.sleep( interval )
252261
if progressbar:
253262
pbar.close()
254263
if percent=="100":
255264
return response_x
256-
return None
265+
print( f"Returning {verdict=}" )
266+
return verdict
257267

258268
###########################################################################
259269
# below here is internal implementation

elmclient/rdfxml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'ldp': 'http://www.w3.org/ns/ldp#',
2929
'ns': 'http://com.ibm.rdm/navigation#',
3030
'oslc': 'http://open-services.net/ns/core#',
31+
'oslc_auto': 'http://open-services.net/ns/auto#',
3132
'oslc_cm': 'http://open-services.net/xmlns/cm/1.0/',
3233
'oslc_cm1': 'http://open-services.net/ns/cm#',
3334
'oslc_cm_10': 'http://open-services.net/xmlns/cm/1.0/cm#',

0 commit comments

Comments
 (0)