|
| 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 | + |
0 commit comments