From 0e7070c5cb231b7c1d9a4494c905935b55d10608 Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Thu, 16 Nov 2023 16:40:22 +0100 Subject: [PATCH 1/7] options parsing reorganized --- pycrest/api/crest_api.py | 27 +- pycrest/cli/pycrest10.py | 877 +++++++++++++++++++++++++++++++++++++ pycrest/cli/pycrest_cli.py | 40 +- setup.py | 1 + 4 files changed, 935 insertions(+), 10 deletions(-) create mode 100644 pycrest/cli/pycrest10.py diff --git a/pycrest/api/crest_api.py b/pycrest/api/crest_api.py index 14322ab..3541046 100644 --- a/pycrest/api/crest_api.py +++ b/pycrest/api/crest_api.py @@ -157,11 +157,25 @@ class CrestApi(): } api_instance = globaltagmaps_api.GlobaltagmapsApi(self._api_client) try: - api_response = api_instance.list_global_tag_maps(name=name, headers=headers) + # api_response = api_instance.list_global_tag_maps(name=name, headers=headers) + api_response = api_instance.find_global_tag_map(name=name) return api_response except ApiException as e: print("Exception when calling GlobaltagmapsApi->list_global_tag_maps: %s\n" % e) + + def find_global_tag_map2(self, name=None): + """ + Find global tag map + name - global tag name + """ + api_instance = globaltagmaps_api.GlobaltagmapsApi(self._api_client) + try: + api_response = api_instance.find_global_tag_map(name=name) + return api_response + except ApiException as e: + print("Exception when calling GlobaltagmapsApi->find_global_tag_map: %s\n" % e) + def create_tag(self, name, timeType, **kwargs): """ Create a new tag @@ -304,14 +318,15 @@ class CrestApi(): print("Exception when calling GlobaltagmapsApi->create_global_tag_map: %s\n" % e) return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) - def delete_globaltagmap(self, globaltagname, tagname, label): + def delete_global_tag_map(self, globaltagname, tagname, label): """ Remove a tag from a global tag Required parameters: globaltagname, tagname, label """ api_instance = globaltagmaps_api.GlobaltagmapsApi(self._api_client) try: - api_response = api_instance.remove_global_tag_map(global_tag_name=globaltagname, tag_name=tagname, label=label) + #api_response = api_instance.remove_global_tag_map(global_tag_name=globaltagname, tag_name=tagname, label=label) + api_response = api_instance.delete_global_tag_map(name=globaltagname, tagname=tagname, label=label) return api_response except ApiException as e: print("Exception when calling GlobaltagmapsApi->remove_global_tag_map: %s\n" % e) @@ -336,7 +351,7 @@ class CrestApi(): api_instance = iovs_api.IovsApi(self._api_client) try: - api_response = api_instance.get_size_by_tag(tagname) + api_response = api_instance.get_size_by_tag(tagname,_check_return_type = False) res = api_response["resources"][0]["niovs"] return res except ApiException as e: @@ -467,10 +482,6 @@ class CrestApi(): for store in json_array: storeset["resources"].append(store) - # Redefine the size - niovs = len(storeset["resources"]) - storeset["size"] = niovs - temp_file = f".{tagname}.json" with open(temp_file, "w") as outfile: json.dump(storeset.to_dict(), outfile) diff --git a/pycrest/cli/pycrest10.py b/pycrest/cli/pycrest10.py new file mode 100644 index 0000000..a267a91 --- /dev/null +++ b/pycrest/cli/pycrest10.py @@ -0,0 +1,877 @@ +import argparse +import sys +import time +import json + + +from pprint import pprint +from datetime import datetime +from pycrest.api.crest_api import CrestApi + +import requests + + +crest_path_param = 'CREST_SERVER_PATH' +crest_host = "" +crest_proxy = "" + +def get_crest_path(): + import os + try: + path = os.environ['CREST_SERVER_PATH'] + return path + except KeyError: + sys.exit("Error: CREST_SERVER_PATH environment variable is not set.") + +def print_commands(): + cli_commands = """ + + Python CREST client commands: + + pycrest get tag [OPTIONS] + pycrest create tag [OPTIONS] + pycrest get iovList [OPTIONS] + pycrest get tagSize [OPTIONS] + + pycrest create globalTag [OPTIONS] + pycrest create globalTagMap [OPTIONS] + pycrest get globalTag [OPTIONS] + pycrest get globalTagMap [OPTIONS] + + pycrest remove tag [OPTIONS] + pycrest remove globalTag [OPTIONS] + pycrest remove globalTagMap [OPTIONS] + + pycrest create tagMetaInfo [OPTIONS] + pycrest get tagMetaInfo [OPTIONS] + + pycrest get payload [OPTIONS] + pycrest store payload [OPTIONS] + + pycrest get commands + pycrest get version + """ + print (cli_commands) + +#======================================== + + +# Custom serializer for datetime objects +def datetime_serializer(obj): + if isinstance(obj, datetime): + return obj.isoformat() + +def print_json(obj): + json_res = json.dumps(obj.to_dict(), default=datetime_serializer) + print(f'###CREST: {json_res}') + + +#---------------------------- + +def print_single_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) + + if 'resources' not in data: + # print(jsonData) + print_json(data) + else: + res = json_res['resources'] + print(json.dumps(res[0], indent=4)) + + +def print_multiple_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) + + if 'resources' not in data: + # print(jsonData) + print_json(data) + else: + res = json_res['resources'] + print(json.dumps(res, indent=4)) + + +# ========================================== + +def get_tag_func(args): + # print("GET TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.list_tags(name=name) + # print_json(resp) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + +def get_iov_list_func(args): + # print("GET IOV LIST function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.find_all_iovs(tagname=name) + # print_json(resp) # OLD MvG + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def create_tag_func(args): + # print("CREATE TAG function") + + if args.time_type is None: + sys.exit("Error: time_type parameter is not set") + if args.payload_spec is None: + sys.exit("Error: payload_spec parameter is not set") + if args.description is None: + sys.exit("Error: description parameter is not set") + if args.synchronization is None: + sys.exit("Error: synchronization parameter is not set") + if args.last_validated_time is None: + sys.exit("Error: last_validated_time parameter is not set") + if args.end_of_validity is None: + sys.exit("Error: end_of_validity parameter is not set") + + params = { + 'payload_spec': args.payload_spec, + 'description': args.description, + 'synchronization': args.synchronization, + 'last_validated_time': args.last_validated_time, + 'end_of_validity': args.end_of_validity, + } + + try: + global crest_host + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_tag(name=args.name, timeType=args.time_type, **params) + # print(f"tag {args.name} created") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_version(): + + from urllib.parse import urlparse + # from urlparse import urlparse # Python 2 + + url = crest_host + parsed_uri = urlparse(url) + result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) + + host = result + "actuator/info" + + resp = requests.get(host) + + # If the HTTP GET request can be served + if resp.status_code == 200: + data = json.loads(resp.content) + + if 'build' not in data: + raise ValueError(f"No target \"build\" in the string {data}") + else: + build = data["build"] + if 'version' not in build: + raise ValueError(f"No target \"version\" in the string {build}") + else: + vers = build["version"] + print(f"CREST server API version = {vers}") + + +def remove_tag_func(args): + # print("GET TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {args.crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.remove_tag(name=name) + # print(f"tag {name} deleted") + except Exception as e: + print("Error: "+ repr(e)) + +def create_tag_meta_info_func(args): + if args.description is None: + sys.exit("Error: description is not set") + if args.chansize is None: + sys.exit("Error: chansize is not set") + if args.colsize is None: + sys.exit("Error: colsize is not set") + if args.tag_info is None: + sys.exit("Error: tag_info is not set") + + params = { + 'description': args.description, + 'chansize': args.chansize, + 'colsize': args.colsize, + 'tag_info': args.tag_info, + } + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_tag_info(name=args.name, **params) + print(f"tag meta info {args.name} created") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_tag_meta_info_func(args): + # print("GET TAG META INFO function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_tag_info(name=name) + print_single_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + + +def get_payload_func(args): + command = args.command + hash = args.hash + file = args.file + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_payload(hash=hash) + with open(file, 'wb') as f: + f.write(resp) + + print(f"payload {hash} stored in {file}") + except Exception as e: + print("Error: "+ repr(e)) + + + +def store_payload_func(args): + name = args.name + file = args.file + since = args.since + + resources = [] + with open(file, 'r') as f: + data = f.read() + dto = { + 'since': since, + 'data': data, + 'streamerInfo': '{\"filename\": \"' + file + '\"}', + } + resources.append(dto) + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.store_json_data(tagname=name, json_array=resources) + print(f"payload for tag {name} stored in CREST from file {file}") + except Exception as e: + print("Error: "+ repr(e)) + + + +def create_global_tag_func(args): + params = { + 'validity': args.validity, + 'description': args.description, + 'release': args.release, + 'scenario': args.scenario, + 'workflow': args.workflow, + 'type': args.type, + } + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_global_tag(name=args.name, **params) + # print(f"global tag {args.name} created") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_global_tag_list_func(args): + # print("GET GLOBAL TAG LIST function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.list_global_tags(name=name) + # print_json(resp) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def remove_global_tag_func(args): + # print("REMOVE GLOBAL TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {args.crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.remove_global_tag(name=name) + # print(f"tag {name} deleted") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_tag_size_func(args): + # print("GET TAG SIZE function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_tag_size(tagname=name) + # print_json(resp) + print(f"tag size ({name}) = {resp} IOVs") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_global_tag_map_func(args): + # print("GET GLOBAL TAG MAP function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.find_global_tag_map2(name=name) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def create_global_tag_map_func(args): + + params = { + 'record': args.record, + 'label': args.label, + } + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_global_tag_map(globaltagname=args.name, tagname=args.tag_name, **params) + # print(f"global tag map {args.name} - {args.tag_name} created") + except Exception as e: + print("Error: "+ repr(e)) + +def remove_global_tag_map_func(args): + try: + crest_api = CrestApi(host=crest_host) + # resp = crestremovemap(globaltagname=args.name, tagname=args.tag_name, label=args.label) + resp = crest_api.delete_global_tag_map(globaltagname=args.name, tagname=args.tag_name, label=args.label) + print_json(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +#======================================================= + +def get_tag_command(): + # print("GET TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + +def get_iov_list_command(): + # print("GET IOV LIST command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["iovList"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_iov_list_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + +def create_tag_commamnd(): + # print("CREATE TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + + parser.add_argument('-time_type', dest = 'time_type', required = False, default='time', help = 'time type') + parser.add_argument('-description', dest = 'description', required = False, default='test', help = 'description') + parser.add_argument('-payload_spec', dest = 'payload_spec', required = False, default='JSON', help = 'payload specification') + parser.add_argument('-synchronization', dest = 'synchronization', required = False, default='all', help = 'synchronization') + parser.add_argument('-last_validated_time', dest = 'last_validated_time',required = False, default=0.0, help = 'last validated time', type = float) + parser.add_argument('-end_of_validity', dest = 'end_of_validity', required = False, default=-1.0, help = 'end of validity', type = float) + + parser.set_defaults(func=create_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def remove_tag_command(): + # print("REMOVE TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=remove_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def create_tag_meta_info_command(): + # print("CREATE TAG META INFO command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + + parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') + parser.add_argument('-chansize', dest = 'chansize', required = False, default=1, help = 'number of channels', type = int) + parser.add_argument('-colsize', dest = 'colsize', required = False, default=1, help = 'number of columns', type = int) + parser.add_argument('-tag_info', dest = 'tag_info', required = False, default='tag info', help = 'tag info') + + parser.set_defaults(func=create_tag_meta_info_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def get_tag_meta_info_command(): + # print("GET TAG META INFO command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_meta_info_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def get_payload_command(): + # print("GET PAYLOAD command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["payload"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'hash',required = True, help = 'paylaod hash') + parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') + parser.set_defaults(func=get_payload_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def store_payload_command(): + # print("STORE PAYLOD command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["store"], help='commands') + parser.add_argument(dest='type', choices=["payload"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') + parser.add_argument('-since', dest = 'since',required = True, help = 'since time', type = float) + parser.set_defaults(func=store_payload_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + + +def create_global_tag_command(): + # print("CREATE GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.add_argument('-validity', dest = 'validity', required = False, default=0.0, help = 'validity', type = float) + parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') + parser.add_argument('-release', dest = 'release', required = False, default='1.0', help = 'release') + parser.add_argument('-scenario', dest = 'scenario', required = False, default='none', help = 'scenario') + parser.add_argument('-workflow', dest = 'workflow', required = False, default='none', help = 'workflow') + parser.add_argument('-type', dest = 'type', required = False, default='A', help = 'type') + + parser.set_defaults(func=create_global_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + + +def get_global_tag_command(): + # print("GET GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_global_tag_list_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def remove_global_tag_command(): + # print("REMOVE GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.set_defaults(func=remove_global_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def get_tag_size_command(): + # print("GET TAG SIZE command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tagSize"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_size_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + + +def get_global_tag_map_command(): + # print("GET GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.set_defaults(func=get_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def create_global_tag_map_command(): + # print("CREATE GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + + parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') + parser.add_argument('-record', dest = 'record', required = False, help = 'record', default = 'none') + parser.add_argument('-label', dest = 'label', required = True, help = 'label') + + parser.set_defaults(func=create_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def remove_global_tag_map_command(): + # print("REMOVE GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + + parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') + parser.add_argument('-label', dest = 'label', required = True, help = 'label') + + parser.set_defaults(func=remove_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +#=================================================================== + +def main(): + + global crest_host + global crest_proxy + global crest_config + + global crest_api + + crest_host = get_crest_path() + # print(f'CREST server path = {crest_host}') + + ''' + print(type(sys.argv)) + print('The command line arguments are:') + for i in sys.argv: + print(i) + ''' + + size = len(sys.argv) + # print(f"Number of arguments = {size}") + + if size < 3: + print("Pyton CREST Client. Chose one of the commands:") + print_commands() + elif size == 3: + crest_command = sys.argv[1] + crest_type = sys.argv[2] + if crest_command == "get": + if crest_type == "commands": + print_commands() + elif crest_type == "version": + get_version() + else: + print("Pyton CREST Client. Chose one of the commands:") + print_commands() + else: + # print("lets go...") + crest_command = sys.argv[1] + crest_type = sys.argv[2] + # print(f"Command = {crest_command}") + # print(f"Type = {crest_type}") + + if crest_command == "get" : + # print("GET block:") + if crest_type == "tag": + # print("GET TAG command") + get_tag_command() + elif crest_type == "iovList": + # print ("GET IOV LIST command") + get_iov_list_command() + elif crest_type == "tagMetaInfo": + # print ("GET TAG META INFO command") + get_tag_meta_info_command() + elif crest_type == "payload": + # print ("GET PAYLOAD command") + get_payload_command() + elif crest_type == "globalTag": + # print ("GET GLOBAL TAG command") + get_global_tag_command() + elif crest_type == "tagSize": + # print("GET TAG SIZE command") + get_tag_size_command() + elif crest_type == "globalTagMap": + # print("GET GLOBAL TAG MAP command") + get_global_tag_map_command() + else: + sys.exit(f"Data type {crest_type} with this command does not work.") + elif crest_command == "remove": + # print("REMOVE block:") + if crest_type == "tag": + # print("REMOVE TAG command") + remove_tag_command() + elif crest_type == "globalTag": + # print("REMOVE GLOBAL TAG command") + remove_global_tag_command() + elif crest_type == "globalTagMap": + # print("REMOVE GLOBAL TAG MAP command") + remove_global_tag_map_command() + else: + sys.exit(f"Data {crest_type} cannot be removed.") + + elif crest_command == "create": + # print("CREATE block") + if crest_type == "tag": + # print("CREATE TAG command:") + create_tag_commamnd() + elif crest_type == "tagMetaInfo": + # print("CREATE TAG META INFO command:") + create_tag_meta_info_command() + elif crest_type == "globalTag": + # print("CREATE GLOBAL TAG command:") + create_global_tag_command() + elif crest_type == "globalTagMap": + create_global_tag_map_command() + else: + sys.exit(f"Cannot create {crest_type}.") + + elif crest_command == "store": + if crest_type == "payload": + # print("STORE PAYLOAD command:") + store_payload_command() + else: + sys.exit(f"Cannot store {crest_type}.") + else: + sys.exit(f"Command {crest_command} not supported.") + +#----------------------------------- + + +if __name__ == '__main__': + main() diff --git a/pycrest/cli/pycrest_cli.py b/pycrest/cli/pycrest_cli.py index 5b8f322..1361c1f 100644 --- a/pycrest/cli/pycrest_cli.py +++ b/pycrest/cli/pycrest_cli.py @@ -134,7 +134,19 @@ def print_json(obj): def find_all_iovs(tagname): resp = crest_api.find_all_iovs(tagname=tagname) - print_json(resp) + print_json(resp) # OLD MvG + + # print("TEST ") + # print_res(resp) + + # print("TEST C:") + # json_res = json.dumps(resp.to_dict(), default=datetime_serializer) + # print(type(json_res)) + # print_res(json.loads(json_res)) + + # last working wariant + # print_res(resp) + #----------------------------- def list_tags(name): @@ -313,7 +325,8 @@ def store_json_payload(name,since,path,file): def get_tag_size(tagname): resp = crest_api.get_tag_size(tagname=tagname) - print_json(resp) + # print_json(resp) + print(resp) def get_version(): @@ -344,6 +357,28 @@ def get_version(): #----------------------------- +#NEW (MvG): + +def print_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) + + # print(type(data)) + if 'resources' not in data: + # print(jsonData) + print ("TEST C") + else: + res = json_res['resources'] + size = len(res) + if size == 1: + # print(res[0]) + print(json.dumps(res[0], indent=4)) + else: + # print(res) + print(json.dumps(res, indent=4)) + + +#----------------------------- #================================= @@ -371,6 +406,7 @@ def iovList(args): command = args.command name = args.name if command == "get": + # print("TEST 02") find_all_iovs(name) else: print(f"Error: {command} command not supported") diff --git a/setup.py b/setup.py index 2ab1978..76c62b8 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,7 @@ setup( entry_points={ 'console_scripts': [ 'pycrest = pycrest.cli.pycrest_cli:main', + 'pyctest = pycrest.cli.pycrest10:main', ], }, scripts=['scripts/crest-python-setup.sh', 'scripts/crest-server-path-setup.sh'], -- GitLab From 308facba964953930a9f1c2fb08bf70cb846347b Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Fri, 17 Nov 2023 15:59:50 +0100 Subject: [PATCH 2/7] correction in create_global_tag and printing --- pycrest/api/crest_api.py | 5 +++-- pycrest/cli/pycrest10.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pycrest/api/crest_api.py b/pycrest/api/crest_api.py index 3541046..ce70e78 100644 --- a/pycrest/api/crest_api.py +++ b/pycrest/api/crest_api.py @@ -250,6 +250,7 @@ class CrestApi(): print("Exception when calling Tags remove_tag: %s\n" % e) return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) + def create_global_tag(self, name, force="false", **kwargs): """ Create a new global tag @@ -258,7 +259,7 @@ class CrestApi(): criteria = CrestApi.build_params(kwargs) params = { 'description': 'none', - 'synchronization': 'none', + 'validity': 0.0, 'release': 'none', 'scenario': 'none', 'workflow': 'all', @@ -269,7 +270,7 @@ class CrestApi(): description=params['description'], release=params['release'], scenario=params['scenario'], - synchronization=params['synchronization'], + validity=params['validity'], workflow=params['workflow'], type=params['type']) diff --git a/pycrest/cli/pycrest10.py b/pycrest/cli/pycrest10.py index a267a91..181902e 100644 --- a/pycrest/cli/pycrest10.py +++ b/pycrest/cli/pycrest10.py @@ -91,6 +91,12 @@ def print_multiple_res(data): res = json_res['resources'] print(json.dumps(res, indent=4)) +def print_full_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) + + print(json.dumps(json_res, indent=4)) + # ========================================== @@ -156,6 +162,7 @@ def create_tag_func(args): crest_api = CrestApi(host=crest_host) resp = crest_api.create_tag(name=args.name, timeType=args.time_type, **params) # print(f"tag {args.name} created") + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -223,7 +230,8 @@ def create_tag_meta_info_func(args): try: crest_api = CrestApi(host=crest_host) resp = crest_api.create_tag_info(name=args.name, **params) - print(f"tag meta info {args.name} created") + # print(f"tag meta info {args.name} created") + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -299,6 +307,7 @@ def create_global_tag_func(args): crest_api = CrestApi(host=crest_host) resp = crest_api.create_global_tag(name=args.name, **params) # print(f"global tag {args.name} created") + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -332,6 +341,7 @@ def remove_global_tag_func(args): crest_api = CrestApi(host=crest_host) resp = crest_api.remove_global_tag(name=name) # print(f"tag {name} deleted") + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -379,6 +389,7 @@ def create_global_tag_map_func(args): crest_api = CrestApi(host=crest_host) resp = crest_api.create_global_tag_map(globaltagname=args.name, tagname=args.tag_name, **params) # print(f"global tag map {args.name} - {args.tag_name} created") + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -387,7 +398,8 @@ def remove_global_tag_map_func(args): crest_api = CrestApi(host=crest_host) # resp = crestremovemap(globaltagname=args.name, tagname=args.tag_name, label=args.label) resp = crest_api.delete_global_tag_map(globaltagname=args.name, tagname=args.tag_name, label=args.label) - print_json(resp) + # print_json(resp) + print_full_res(resp) except Exception as e: print("Error: "+ repr(e)) -- GitLab From 1bb9e370126526408720fb0278fedbcbc0bdedd2 Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Fri, 17 Nov 2023 16:27:16 +0100 Subject: [PATCH 3/7] version in setup.py corrected --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 76c62b8..74010e7 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages NAME = "pycrest-client" -VERSION = "1.0.0" +VERSION = "1.0.1" REQUIRES = [ "urllib3 >= 1.25.3", "python-dateutil", -- GitLab From de0fab5974adae0198b22610120058e1a768bc6d Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Tue, 21 Nov 2023 16:14:23 +0100 Subject: [PATCH 4/7] find_global_tag_map method corrected --- pycrest/api/crest_api.py | 16 ++-------------- pycrest/cli/pycrest10.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pycrest/api/crest_api.py b/pycrest/api/crest_api.py index ce70e78..17fb039 100644 --- a/pycrest/api/crest_api.py +++ b/pycrest/api/crest_api.py @@ -153,28 +153,16 @@ class CrestApi(): Depending on mode, the name is either the global tag name or the tag name """ headers = { - 'X-Crest-MapMode': mode + 'x_crest_map_mode': mode } api_instance = globaltagmaps_api.GlobaltagmapsApi(self._api_client) try: - # api_response = api_instance.list_global_tag_maps(name=name, headers=headers) - api_response = api_instance.find_global_tag_map(name=name) + api_response = api_instance.find_global_tag_map(name=name, **headers) return api_response except ApiException as e: print("Exception when calling GlobaltagmapsApi->list_global_tag_maps: %s\n" % e) - def find_global_tag_map2(self, name=None): - """ - Find global tag map - name - global tag name - """ - api_instance = globaltagmaps_api.GlobaltagmapsApi(self._api_client) - try: - api_response = api_instance.find_global_tag_map(name=name) - return api_response - except ApiException as e: - print("Exception when calling GlobaltagmapsApi->find_global_tag_map: %s\n" % e) def create_tag(self, name, timeType, **kwargs): """ diff --git a/pycrest/cli/pycrest10.py b/pycrest/cli/pycrest10.py index 181902e..47129da 100644 --- a/pycrest/cli/pycrest10.py +++ b/pycrest/cli/pycrest10.py @@ -370,10 +370,15 @@ def get_global_tag_map_func(args): # print(f"host = {crest_host}") command = args.command name = args.name + tag_type = args.tag_type + # print(f"tag_type = {tag_type}") try: crest_api = CrestApi(host=crest_host) - resp = crest_api.find_global_tag_map2(name=name) + if tag_type == "globaltag": + resp = crest_api.find_global_tag_map(name=name) + else: + resp = crest_api.find_global_tag_map(mode="BackTrace",name=name) print_multiple_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -707,6 +712,7 @@ def get_global_tag_map_command(): parser.add_argument(dest='type', choices=["globalTagMap"], help='type') parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.add_argument('-for', dest = 'tag_type',required = False, choices=["tag","globaltag"], default = "globaltag", help = 'name tag or global tag') parser.set_defaults(func=get_global_tag_map_func) args = parser.parse_args() @@ -798,8 +804,10 @@ def main(): # print(f"Number of arguments = {size}") if size < 3: - print("Pyton CREST Client. Chose one of the commands:") - print_commands() + print("Pyton CREST Client.") + print("command list:") + print("> pycrest get commands") + # print_commands() elif size == 3: crest_command = sys.argv[1] crest_type = sys.argv[2] -- GitLab From 4e8b4781f0df28b36c203000babb127ee0a62a28 Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Thu, 23 Nov 2023 15:29:44 +0100 Subject: [PATCH 5/7] method names corrected --- README.md | 6 +++--- pycrest/api/crest_api.py | 8 +++++--- pycrest/cli/pycrest10.py | 6 +++--- pycrest/cli/pycrest_cli.py | 6 +++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index eb49dc2..bbd9662 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python client for CREST Server -CREST python command line client (package `hep.crest.crestcli`). +CREST python command line client (package `pycrest_client`). It is based on the generate _OpenApi_ client. ## Table of Contents @@ -32,7 +32,7 @@ pip install . ``` To uninstall this package: ``` -python -m pip uninstall pycrest-client +python -m pip uninstall pycrest_client ``` ### Lxplus @@ -46,7 +46,7 @@ crest-python-setup.sh ``` The correct installation paths for pycrest and scropts can be found with command: ``` -pip show -f hep.crest.crestcli +pip show -f pycrest_client ``` The CREST python client requires CREST server URL. You need to setup the environment variable: ``` diff --git a/pycrest/api/crest_api.py b/pycrest/api/crest_api.py index 17fb039..99c5300 100644 --- a/pycrest/api/crest_api.py +++ b/pycrest/api/crest_api.py @@ -87,6 +87,7 @@ class CrestApi(): except ApiException as e: print("Exception when calling IovsApi->find_all_iovs: %s\n" % e) + def list_tags(self, **kwargs): """ List all tags @@ -194,7 +195,7 @@ class CrestApi(): print("Exception when calling TagsApi->create_tag: %s\n" % e) return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) - def create_tag_info(self, name, **kwargs): + def create_tag_meta_info(self, name, **kwargs): """ Create the meta information for a tag Required parameters: name, @@ -321,7 +322,7 @@ class CrestApi(): print("Exception when calling GlobaltagmapsApi->remove_global_tag_map: %s\n" % e) return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) - def get_tag_info(self, name='none'): + def get_tag_meta_info(self, name='none'): """ Get tag meta info """ @@ -333,7 +334,8 @@ class CrestApi(): print("Exception when calling TagsApi->find_tag_meta: %s\n" % e) return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) - def get_tag_size(self, tagname): + + def get_size(self, tagname): """ Get tag size """ diff --git a/pycrest/cli/pycrest10.py b/pycrest/cli/pycrest10.py index 47129da..f549dbb 100644 --- a/pycrest/cli/pycrest10.py +++ b/pycrest/cli/pycrest10.py @@ -229,7 +229,7 @@ def create_tag_meta_info_func(args): try: crest_api = CrestApi(host=crest_host) - resp = crest_api.create_tag_info(name=args.name, **params) + resp = crest_api.create_tag_meta_info(name=args.name, **params) # print(f"tag meta info {args.name} created") print_full_res(resp) except Exception as e: @@ -246,7 +246,7 @@ def get_tag_meta_info_func(args): try: crest_api = CrestApi(host=crest_host) - resp = crest_api.get_tag_info(name=name) + resp = crest_api.get_tag_meta_info(name=name) print_single_res(resp) except Exception as e: print("Error: "+ repr(e)) @@ -356,7 +356,7 @@ def get_tag_size_func(args): try: crest_api = CrestApi(host=crest_host) - resp = crest_api.get_tag_size(tagname=name) + resp = crest_api.get_size(tagname=name) # print_json(resp) print(f"tag size ({name}) = {resp} IOVs") except Exception as e: diff --git a/pycrest/cli/pycrest_cli.py b/pycrest/cli/pycrest_cli.py index 1361c1f..d4c0571 100644 --- a/pycrest/cli/pycrest_cli.py +++ b/pycrest/cli/pycrest_cli.py @@ -204,7 +204,7 @@ def create_tag_info(args): 'colsize': args.colsize, 'tag_info': args.tag_info, } - resp = crest_api.create_tag_info(name=args.name, **params) + resp = crest_api.create_tag_meta_info(name=args.name, **params) print(f"tag info {args.name} created") #----------------------------- def find_global_tag(name): @@ -273,7 +273,7 @@ def create_global_tag_map(args): #----------------------------- def get_tagmetainfo(name): - resp = crest_api.get_tag_info(name=name) + resp = crest_api.get_tag_meta_info(name=name) print_json(resp) #----------------------------- @@ -324,7 +324,7 @@ def store_json_payload(name,since,path,file): print(f"payload {name} stored in CREST using {file}") def get_tag_size(tagname): - resp = crest_api.get_tag_size(tagname=tagname) + resp = crest_api.get_size(tagname=tagname) # print_json(resp) print(resp) -- GitLab From bdd59c7c44f37faddcedcc0a5932028d461dc627 Mon Sep 17 00:00:00 2001 From: formica <andrea.formica@cern.ch> Date: Mon, 27 Nov 2023 16:40:58 +0100 Subject: [PATCH 6/7] add example for run lumi --- examples/run_lumi_info.py | 24 ++++++++++++++++++ pycrest/api/crest_api.py | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 examples/run_lumi_info.py diff --git a/examples/run_lumi_info.py b/examples/run_lumi_info.py new file mode 100644 index 0000000..703b4dc --- /dev/null +++ b/examples/run_lumi_info.py @@ -0,0 +1,24 @@ +from pycrest.api.crest_api import CrestApi +from datetime import datetime + +# create an instance of the API class +api_instance = CrestApi(host='http://crest-undertow-api.web.cern.ch/api-v4.0') +#api_instance = CrestApi(host='http://localhost:8090/api') +run = 150 +lumi = 200 +st = datetime(2020, 2, 10, 0, 0, 0) +et = datetime(2020, 2, 11, 0, 0, 0) + +print(f'Create a run-lumi entry for run {run} lumi {lumi}') +try: + # Create a new run lumi entry + api_response = api_instance.create_run_lumi(run=run, lumiblock=lumi, startdate=st.timestamp(), enddate=et.timestamp()) + print(api_response) +except Exception as e: + print("Exception when calling CrestApi->create_run_lumi: %s\n" % e) + +since = 10 +until = 150 +print(f'Get data for run range since {since} until {until}') +runlbset = api_instance.list_run_lumi(since=since, until=until, mode='runrange') +print(f'Select set {runlbset} for since {since}') diff --git a/pycrest/api/crest_api.py b/pycrest/api/crest_api.py index 99c5300..ad02a49 100644 --- a/pycrest/api/crest_api.py +++ b/pycrest/api/crest_api.py @@ -10,6 +10,7 @@ from hep.crest.client.api import globaltags_api from hep.crest.client.api import globaltagmaps_api from hep.crest.client.api import payloads_api from hep.crest.client.api import tags_api +from hep.crest.client.api import runinfo_api from hep.crest.client.model.tag_summary_set_dto import TagSummarySetDto from hep.crest.client.model.iov_set_dto import IovSetDto from hep.crest.client.model.http_response import HTTPResponse @@ -21,6 +22,8 @@ from hep.crest.client.model.global_tag_set_dto import GlobalTagSetDto from hep.crest.client.model.global_tag_map_dto import GlobalTagMapDto from hep.crest.client.model.store_set_dto import StoreSetDto from hep.crest.client.model.store_dto import StoreDto +from hep.crest.client.model.run_lumi_info_dto import RunLumiInfoDto +from hep.crest.client.model.run_lumi_set_dto import RunLumiSetDto class CrestApi(): _config = None @@ -486,3 +489,51 @@ class CrestApi(): except ApiException as e: print("Exception when calling PayloadsApi->upload_json: %s\n" % e) + + def create_run_lumi(self, run, lumiblock, startdate, enddate): + """ + Create a new entry for run and lumi block + Required parameters: run, lumiblock, startdate, enddate + """ + + rlb = RunLumiInfoDto(run_number=float(run), + lb=float(lumiblock), + starttime=float(startdate), + endtime=float(enddate)) + rlbset = RunLumiSetDto( + size = 1, + datatype = "runs", + format = "RunLumiSetDto", + resources = [rlb] + ) + api_instance = runinfo_api.RuninfoApi(self._api_client) + try: + api_response = api_instance.create_run_info(run_lumi_set_dto=rlbset) + return api_response + except ApiException as e: + print("Exception when calling RuninfoApi->create_run_info: %s\n" % e) + return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) + + def list_run_lumi(self, since, until, **kwargs): + """ + List run and lumi block + Required parameters: since, until + Optional parameters: format, mode, page, size + Format describes the type used for since and until. It can be NUMBER, ISO. + Mode describes the range used: runrange or daterange + """ + criteria = CrestApi.build_params(kwargs) + params = { + 'format': 'NUMBER', + 'mode': 'runrange', + 'page': 0, + 'size': 1000 + } + params.update(criteria) + api_instance = runinfo_api.RuninfoApi(self._api_client) + try: + api_response = api_instance.list_run_info(since=str(since), until=str(until), **params) + return api_response + except ApiException as e: + print("Exception when calling RuninfoApi->list_run_info: %s\n" % e) + return HTTPResponse(status_code=e.status, reason=e.reason, code=e.status, message=e.body) -- GitLab From e041eca56b134b68dadbf8731ca7133d9c7735ee Mon Sep 17 00:00:00 2001 From: Mikhail Mineev <Mikhail.Mineev@cern.ch> Date: Thu, 30 Nov 2023 15:01:04 +0100 Subject: [PATCH 7/7] old codes removed --- pycrest/cli/pycrest10.py | 897 ----------------------------- pycrest/cli/pycrest_cli.py | 1084 ++++++++++++++++++++++++------------ setup.py | 1 - 3 files changed, 733 insertions(+), 1249 deletions(-) delete mode 100644 pycrest/cli/pycrest10.py diff --git a/pycrest/cli/pycrest10.py b/pycrest/cli/pycrest10.py deleted file mode 100644 index f549dbb..0000000 --- a/pycrest/cli/pycrest10.py +++ /dev/null @@ -1,897 +0,0 @@ -import argparse -import sys -import time -import json - - -from pprint import pprint -from datetime import datetime -from pycrest.api.crest_api import CrestApi - -import requests - - -crest_path_param = 'CREST_SERVER_PATH' -crest_host = "" -crest_proxy = "" - -def get_crest_path(): - import os - try: - path = os.environ['CREST_SERVER_PATH'] - return path - except KeyError: - sys.exit("Error: CREST_SERVER_PATH environment variable is not set.") - -def print_commands(): - cli_commands = """ - - Python CREST client commands: - - pycrest get tag [OPTIONS] - pycrest create tag [OPTIONS] - pycrest get iovList [OPTIONS] - pycrest get tagSize [OPTIONS] - - pycrest create globalTag [OPTIONS] - pycrest create globalTagMap [OPTIONS] - pycrest get globalTag [OPTIONS] - pycrest get globalTagMap [OPTIONS] - - pycrest remove tag [OPTIONS] - pycrest remove globalTag [OPTIONS] - pycrest remove globalTagMap [OPTIONS] - - pycrest create tagMetaInfo [OPTIONS] - pycrest get tagMetaInfo [OPTIONS] - - pycrest get payload [OPTIONS] - pycrest store payload [OPTIONS] - - pycrest get commands - pycrest get version - """ - print (cli_commands) - -#======================================== - - -# Custom serializer for datetime objects -def datetime_serializer(obj): - if isinstance(obj, datetime): - return obj.isoformat() - -def print_json(obj): - json_res = json.dumps(obj.to_dict(), default=datetime_serializer) - print(f'###CREST: {json_res}') - - -#---------------------------- - -def print_single_res(data): - str_res = json.dumps(data.to_dict(), default=datetime_serializer) - json_res = json.loads(str_res) - - if 'resources' not in data: - # print(jsonData) - print_json(data) - else: - res = json_res['resources'] - print(json.dumps(res[0], indent=4)) - - -def print_multiple_res(data): - str_res = json.dumps(data.to_dict(), default=datetime_serializer) - json_res = json.loads(str_res) - - if 'resources' not in data: - # print(jsonData) - print_json(data) - else: - res = json_res['resources'] - print(json.dumps(res, indent=4)) - -def print_full_res(data): - str_res = json.dumps(data.to_dict(), default=datetime_serializer) - json_res = json.loads(str_res) - - print(json.dumps(json_res, indent=4)) - - -# ========================================== - -def get_tag_func(args): - # print("GET TAG function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.list_tags(name=name) - # print_json(resp) - print_multiple_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - -def get_iov_list_func(args): - # print("GET IOV LIST function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.find_all_iovs(tagname=name) - # print_json(resp) # OLD MvG - print_multiple_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def create_tag_func(args): - # print("CREATE TAG function") - - if args.time_type is None: - sys.exit("Error: time_type parameter is not set") - if args.payload_spec is None: - sys.exit("Error: payload_spec parameter is not set") - if args.description is None: - sys.exit("Error: description parameter is not set") - if args.synchronization is None: - sys.exit("Error: synchronization parameter is not set") - if args.last_validated_time is None: - sys.exit("Error: last_validated_time parameter is not set") - if args.end_of_validity is None: - sys.exit("Error: end_of_validity parameter is not set") - - params = { - 'payload_spec': args.payload_spec, - 'description': args.description, - 'synchronization': args.synchronization, - 'last_validated_time': args.last_validated_time, - 'end_of_validity': args.end_of_validity, - } - - try: - global crest_host - crest_api = CrestApi(host=crest_host) - resp = crest_api.create_tag(name=args.name, timeType=args.time_type, **params) - # print(f"tag {args.name} created") - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def get_version(): - - from urllib.parse import urlparse - # from urlparse import urlparse # Python 2 - - url = crest_host - parsed_uri = urlparse(url) - result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) - - host = result + "actuator/info" - - resp = requests.get(host) - - # If the HTTP GET request can be served - if resp.status_code == 200: - data = json.loads(resp.content) - - if 'build' not in data: - raise ValueError(f"No target \"build\" in the string {data}") - else: - build = data["build"] - if 'version' not in build: - raise ValueError(f"No target \"version\" in the string {build}") - else: - vers = build["version"] - print(f"CREST server API version = {vers}") - - -def remove_tag_func(args): - # print("GET TAG function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {args.crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.remove_tag(name=name) - # print(f"tag {name} deleted") - except Exception as e: - print("Error: "+ repr(e)) - -def create_tag_meta_info_func(args): - if args.description is None: - sys.exit("Error: description is not set") - if args.chansize is None: - sys.exit("Error: chansize is not set") - if args.colsize is None: - sys.exit("Error: colsize is not set") - if args.tag_info is None: - sys.exit("Error: tag_info is not set") - - params = { - 'description': args.description, - 'chansize': args.chansize, - 'colsize': args.colsize, - 'tag_info': args.tag_info, - } - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.create_tag_meta_info(name=args.name, **params) - # print(f"tag meta info {args.name} created") - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def get_tag_meta_info_func(args): - # print("GET TAG META INFO function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.get_tag_meta_info(name=name) - print_single_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - - -def get_payload_func(args): - command = args.command - hash = args.hash - file = args.file - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.get_payload(hash=hash) - with open(file, 'wb') as f: - f.write(resp) - - print(f"payload {hash} stored in {file}") - except Exception as e: - print("Error: "+ repr(e)) - - - -def store_payload_func(args): - name = args.name - file = args.file - since = args.since - - resources = [] - with open(file, 'r') as f: - data = f.read() - dto = { - 'since': since, - 'data': data, - 'streamerInfo': '{\"filename\": \"' + file + '\"}', - } - resources.append(dto) - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.store_json_data(tagname=name, json_array=resources) - print(f"payload for tag {name} stored in CREST from file {file}") - except Exception as e: - print("Error: "+ repr(e)) - - - -def create_global_tag_func(args): - params = { - 'validity': args.validity, - 'description': args.description, - 'release': args.release, - 'scenario': args.scenario, - 'workflow': args.workflow, - 'type': args.type, - } - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.create_global_tag(name=args.name, **params) - # print(f"global tag {args.name} created") - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def get_global_tag_list_func(args): - # print("GET GLOBAL TAG LIST function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.list_global_tags(name=name) - # print_json(resp) - print_multiple_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def remove_global_tag_func(args): - # print("REMOVE GLOBAL TAG function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {args.crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.remove_global_tag(name=name) - # print(f"tag {name} deleted") - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def get_tag_size_func(args): - # print("GET TAG SIZE function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.get_size(tagname=name) - # print_json(resp) - print(f"tag size ({name}) = {resp} IOVs") - except Exception as e: - print("Error: "+ repr(e)) - - -def get_global_tag_map_func(args): - # print("GET GLOBAL TAG MAP function") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - tag_type = args.tag_type - # print(f"tag_type = {tag_type}") - - try: - crest_api = CrestApi(host=crest_host) - if tag_type == "globaltag": - resp = crest_api.find_global_tag_map(name=name) - else: - resp = crest_api.find_global_tag_map(mode="BackTrace",name=name) - print_multiple_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -def create_global_tag_map_func(args): - - params = { - 'record': args.record, - 'label': args.label, - } - try: - crest_api = CrestApi(host=crest_host) - resp = crest_api.create_global_tag_map(globaltagname=args.name, tagname=args.tag_name, **params) - # print(f"global tag map {args.name} - {args.tag_name} created") - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - -def remove_global_tag_map_func(args): - try: - crest_api = CrestApi(host=crest_host) - # resp = crestremovemap(globaltagname=args.name, tagname=args.tag_name, label=args.label) - resp = crest_api.delete_global_tag_map(globaltagname=args.name, tagname=args.tag_name, label=args.label) - # print_json(resp) - print_full_res(resp) - except Exception as e: - print("Error: "+ repr(e)) - - -#======================================================= - -def get_tag_command(): - # print("GET TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["tag"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=get_tag_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - -def get_iov_list_command(): - # print("GET IOV LIST command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["iovList"], help='type') - # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=get_iov_list_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - -def create_tag_commamnd(): - # print("CREATE TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["create"], help='commands') - parser.add_argument(dest='type', choices=["tag"], help='type') - # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - - parser.add_argument('-time_type', dest = 'time_type', required = False, default='time', help = 'time type') - parser.add_argument('-description', dest = 'description', required = False, default='test', help = 'description') - parser.add_argument('-payload_spec', dest = 'payload_spec', required = False, default='JSON', help = 'payload specification') - parser.add_argument('-synchronization', dest = 'synchronization', required = False, default='all', help = 'synchronization') - parser.add_argument('-last_validated_time', dest = 'last_validated_time',required = False, default=0.0, help = 'last validated time', type = float) - parser.add_argument('-end_of_validity', dest = 'end_of_validity', required = False, default=-1.0, help = 'end of validity', type = float) - - parser.set_defaults(func=create_tag_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def remove_tag_command(): - # print("REMOVE TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["remove"], help='commands') - parser.add_argument(dest='type', choices=["tag"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=remove_tag_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def create_tag_meta_info_command(): - # print("CREATE TAG META INFO command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["create"], help='commands') - parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') - # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - - parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') - parser.add_argument('-chansize', dest = 'chansize', required = False, default=1, help = 'number of channels', type = int) - parser.add_argument('-colsize', dest = 'colsize', required = False, default=1, help = 'number of columns', type = int) - parser.add_argument('-tag_info', dest = 'tag_info', required = False, default='tag info', help = 'tag info') - - parser.set_defaults(func=create_tag_meta_info_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def get_tag_meta_info_command(): - # print("GET TAG META INFO command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=get_tag_meta_info_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def get_payload_command(): - # print("GET PAYLOAD command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["payload"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'hash',required = True, help = 'paylaod hash') - parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') - parser.set_defaults(func=get_payload_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def store_payload_command(): - # print("STORE PAYLOD command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["store"], help='commands') - parser.add_argument(dest='type', choices=["payload"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') - parser.add_argument('-since', dest = 'since',required = True, help = 'since time', type = float) - parser.set_defaults(func=store_payload_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - - -def create_global_tag_command(): - # print("CREATE GLOBAL TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["create"], help='commands') - parser.add_argument(dest='type', choices=["globalTag"], help='type') - - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - parser.add_argument('-validity', dest = 'validity', required = False, default=0.0, help = 'validity', type = float) - parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') - parser.add_argument('-release', dest = 'release', required = False, default='1.0', help = 'release') - parser.add_argument('-scenario', dest = 'scenario', required = False, default='none', help = 'scenario') - parser.add_argument('-workflow', dest = 'workflow', required = False, default='none', help = 'workflow') - parser.add_argument('-type', dest = 'type', required = False, default='A', help = 'type') - - parser.set_defaults(func=create_global_tag_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - - -def get_global_tag_command(): - # print("GET GLOBAL TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["globalTag"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=get_global_tag_list_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def remove_global_tag_command(): - # print("REMOVE GLOBAL TAG command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["remove"], help='commands') - parser.add_argument(dest='type', choices=["globalTag"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - parser.set_defaults(func=remove_global_tag_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def get_tag_size_command(): - # print("GET TAG SIZE command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["tagSize"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - parser.set_defaults(func=get_tag_size_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - - -def get_global_tag_map_command(): - # print("GET GLOBAL TAG MAP command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get"], help='commands') - parser.add_argument(dest='type', choices=["globalTagMap"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - parser.add_argument('-for', dest = 'tag_type',required = False, choices=["tag","globaltag"], default = "globaltag", help = 'name tag or global tag') - parser.set_defaults(func=get_global_tag_map_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def create_global_tag_map_command(): - # print("CREATE GLOBAL TAG MAP command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["create"], help='commands') - parser.add_argument(dest='type', choices=["globalTagMap"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - - parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') - parser.add_argument('-record', dest = 'record', required = False, help = 'record', default = 'none') - parser.add_argument('-label', dest = 'label', required = True, help = 'label') - - parser.set_defaults(func=create_global_tag_map_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -def remove_global_tag_map_command(): - # print("REMOVE GLOBAL TAG MAP command") - global crest_host - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["remove"], help='commands') - parser.add_argument(dest='type', choices=["globalTagMap"], help='type') - parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) - parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - - parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') - parser.add_argument('-label', dest = 'label', required = True, help = 'label') - - parser.set_defaults(func=remove_global_tag_map_func) - - args = parser.parse_args() - #crest_config.proxy = args.proxy - - # print(type(args)) - if not vars(args): - parser.print_usage() - else: - # print(f"new crest host = {args.crest_host}") - crest_host = args.crest_host - args.func(args) - - -#=================================================================== - -def main(): - - global crest_host - global crest_proxy - global crest_config - - global crest_api - - crest_host = get_crest_path() - # print(f'CREST server path = {crest_host}') - - ''' - print(type(sys.argv)) - print('The command line arguments are:') - for i in sys.argv: - print(i) - ''' - - size = len(sys.argv) - # print(f"Number of arguments = {size}") - - if size < 3: - print("Pyton CREST Client.") - print("command list:") - print("> pycrest get commands") - # print_commands() - elif size == 3: - crest_command = sys.argv[1] - crest_type = sys.argv[2] - if crest_command == "get": - if crest_type == "commands": - print_commands() - elif crest_type == "version": - get_version() - else: - print("Pyton CREST Client. Chose one of the commands:") - print_commands() - else: - # print("lets go...") - crest_command = sys.argv[1] - crest_type = sys.argv[2] - # print(f"Command = {crest_command}") - # print(f"Type = {crest_type}") - - if crest_command == "get" : - # print("GET block:") - if crest_type == "tag": - # print("GET TAG command") - get_tag_command() - elif crest_type == "iovList": - # print ("GET IOV LIST command") - get_iov_list_command() - elif crest_type == "tagMetaInfo": - # print ("GET TAG META INFO command") - get_tag_meta_info_command() - elif crest_type == "payload": - # print ("GET PAYLOAD command") - get_payload_command() - elif crest_type == "globalTag": - # print ("GET GLOBAL TAG command") - get_global_tag_command() - elif crest_type == "tagSize": - # print("GET TAG SIZE command") - get_tag_size_command() - elif crest_type == "globalTagMap": - # print("GET GLOBAL TAG MAP command") - get_global_tag_map_command() - else: - sys.exit(f"Data type {crest_type} with this command does not work.") - elif crest_command == "remove": - # print("REMOVE block:") - if crest_type == "tag": - # print("REMOVE TAG command") - remove_tag_command() - elif crest_type == "globalTag": - # print("REMOVE GLOBAL TAG command") - remove_global_tag_command() - elif crest_type == "globalTagMap": - # print("REMOVE GLOBAL TAG MAP command") - remove_global_tag_map_command() - else: - sys.exit(f"Data {crest_type} cannot be removed.") - - elif crest_command == "create": - # print("CREATE block") - if crest_type == "tag": - # print("CREATE TAG command:") - create_tag_commamnd() - elif crest_type == "tagMetaInfo": - # print("CREATE TAG META INFO command:") - create_tag_meta_info_command() - elif crest_type == "globalTag": - # print("CREATE GLOBAL TAG command:") - create_global_tag_command() - elif crest_type == "globalTagMap": - create_global_tag_map_command() - else: - sys.exit(f"Cannot create {crest_type}.") - - elif crest_command == "store": - if crest_type == "payload": - # print("STORE PAYLOAD command:") - store_payload_command() - else: - sys.exit(f"Cannot store {crest_type}.") - else: - sys.exit(f"Command {crest_command} not supported.") - -#----------------------------------- - - -if __name__ == '__main__': - main() diff --git a/pycrest/cli/pycrest_cli.py b/pycrest/cli/pycrest_cli.py index d4c0571..f549dbb 100644 --- a/pycrest/cli/pycrest_cli.py +++ b/pycrest/cli/pycrest_cli.py @@ -2,7 +2,7 @@ import argparse import sys import time import json -import os + from pprint import pprint from datetime import datetime @@ -10,6 +10,7 @@ from pycrest.api.crest_api import CrestApi import requests + crest_path_param = 'CREST_SERVER_PATH' crest_host = "" crest_proxy = "" @@ -22,140 +23,118 @@ def get_crest_path(): except KeyError: sys.exit("Error: CREST_SERVER_PATH environment variable is not set.") -#-------------------------------- +def print_commands(): + cli_commands = """ -def main(): - #import sys - - ''' - print(type(sys.argv)) - print('The command line arguments are:') - for i in sys.argv: - print(i) - ''' + Python CREST client commands: - global crest_host - global crest_proxy - global crest_api + pycrest get tag [OPTIONS] + pycrest create tag [OPTIONS] + pycrest get iovList [OPTIONS] + pycrest get tagSize [OPTIONS] - crest_host = get_crest_path() - print(f'CREST server path = {crest_host}') - - parser = argparse.ArgumentParser() - parser.add_argument(dest='command', choices=["get","remove","create","store"], help='commands') - # Define the --host argument - parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") - # Define the --proxy argument - #parser.add_argument("--proxy", help="Proxy URL for making requests: use socks5://<host>:<port> for SOCKS5 proxy") - - subparsers1 = parser.add_subparsers(title='CREST data types', - description='Options to set data type', - help='description') - - tag_parser = subparsers1.add_parser('tag', help='tag type') - tag_parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - - tag_parser.add_argument('-time_type', dest = 'time_type', required = False, default='time', help = 'time type') - tag_parser.add_argument('-description', dest = 'description', required = False, default='test', help = 'description') - tag_parser.add_argument('-payload_spec', dest = 'payload_spec', required = False, default='JSON', help = 'payload specification') - tag_parser.add_argument('-synchronization', dest = 'synchronization', required = False, default='all', help = 'synchronization') - tag_parser.add_argument('-last_validated_time', dest = 'last_validated_time',required = False, default=0.0, help = 'last validated time', type = float) - tag_parser.add_argument('-end_of_validity', dest = 'end_of_validity', required = False, default=-1.0, help = 'end of validity', type = float) + pycrest create globalTag [OPTIONS] + pycrest create globalTagMap [OPTIONS] + pycrest get globalTag [OPTIONS] + pycrest get globalTagMap [OPTIONS] - tag_parser.set_defaults(func=tag) + pycrest remove tag [OPTIONS] + pycrest remove globalTag [OPTIONS] + pycrest remove globalTagMap [OPTIONS] - iovlist_parser = subparsers1.add_parser('iovList', help='IOV list type') - iovlist_parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - iovlist_parser.set_defaults(func=iovList) + pycrest create tagMetaInfo [OPTIONS] + pycrest get tagMetaInfo [OPTIONS] - payload_parser = subparsers1.add_parser('payload', help='payload type') - payload_parser.add_argument('-n', dest = 'name',required = True, help = 'payload hash') - payload_parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') - payload_parser.add_argument('-since', dest = 'since',required = False, help = 'since time', type = float) - payload_parser.set_defaults(func=payload) + pycrest get payload [OPTIONS] + pycrest store payload [OPTIONS] - globaltag_parser = subparsers1.add_parser('globalTag', help='global tag type') - globaltag_parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + pycrest get commands + pycrest get version + """ + print (cli_commands) - globaltag_parser.add_argument('-validity', dest = 'validity', required = False, default=0.0, help = 'validity', type = float) - globaltag_parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') - globaltag_parser.add_argument('-release', dest = 'release', required = False, default='1.0', help = 'release') - globaltag_parser.add_argument('-scenario', dest = 'scenario', required = False, default='none', help = 'scenario') - globaltag_parser.add_argument('-workflow', dest = 'workflow', required = False, default='none', help = 'workflow') - globaltag_parser.add_argument('-type', dest = 'type', required = False, default='A', help = 'type') +#======================================== - globaltag_parser.set_defaults(func=globaltag) - tagmeta_parser = subparsers1.add_parser('tagMetaInfo', help='tag meta info type') - tagmeta_parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - - tagmeta_parser.add_argument('-description', dest = 'description',required = False, help = 'description') - tagmeta_parser.add_argument('-chansize', dest = 'chansize', required = False, help = 'number of channels', type = int) - tagmeta_parser.add_argument('-colsize', dest = 'colsize', required = False, help = 'number of columns', type = int) - tagmeta_parser.add_argument('-tag_info', dest = 'tag_info', required = False, help = 'tag info') +# Custom serializer for datetime objects +def datetime_serializer(obj): + if isinstance(obj, datetime): + return obj.isoformat() - tagmeta_parser.set_defaults(func=tagMetaInfo) +def print_json(obj): + json_res = json.dumps(obj.to_dict(), default=datetime_serializer) + print(f'###CREST: {json_res}') - globaltagmap_parser = subparsers1.add_parser('globalTagMap', help='gloabal tag map type') - globaltagmap_parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') - globaltagmap_parser.add_argument('-tag_name', dest = 'tag_name',required = False, help = 'tag name') - globaltagmap_parser.add_argument('-record', dest = 'record', required = False, help = 'record') - globaltagmap_parser.add_argument('-label', dest = 'label', required = False, help = 'label') +#---------------------------- - globaltagmap_parser.set_defaults(func=globalTagMap) +def print_single_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) - version_parser = subparsers1.add_parser('version', help='CREST server version') - version_parser.set_defaults(func=version) + if 'resources' not in data: + # print(jsonData) + print_json(data) + else: + res = json_res['resources'] + print(json.dumps(res[0], indent=4)) - tagsize_parser = subparsers1.add_parser('tagSize', help='number of IOVs in the tag') - tagsize_parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') - tagsize_parser.set_defaults(func=tagSize) - args = parser.parse_args() +def print_multiple_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) - crest_api = CrestApi(host=crest_host) - - if not vars(args): - parser.print_usage() + if 'resources' not in data: + # print(jsonData) + print_json(data) else: - args.func(args) + res = json_res['resources'] + print(json.dumps(res, indent=4)) -# Custom serializer for datetime objects -def datetime_serializer(obj): - if isinstance(obj, datetime): - return obj.isoformat() +def print_full_res(data): + str_res = json.dumps(data.to_dict(), default=datetime_serializer) + json_res = json.loads(str_res) -def print_json(obj): - json_res = json.dumps(obj.to_dict(), default=datetime_serializer) - print(f'###CREST: {json_res}') + print(json.dumps(json_res, indent=4)) -#-------------------------------- -def find_all_iovs(tagname): - resp = crest_api.find_all_iovs(tagname=tagname) - print_json(resp) # OLD MvG +# ========================================== - # print("TEST ") - # print_res(resp) +def get_tag_func(args): + # print("GET TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name - # print("TEST C:") - # json_res = json.dumps(resp.to_dict(), default=datetime_serializer) - # print(type(json_res)) - # print_res(json.loads(json_res)) + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.list_tags(name=name) + # print_json(resp) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + +def get_iov_list_func(args): + # print("GET IOV LIST function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name - # last working wariant - # print_res(resp) - - -#----------------------------- -def list_tags(name): + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.find_all_iovs(tagname=name) + # print_json(resp) # OLD MvG + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) - resp = crest_api.list_tags(name=name) - print_json(resp) -#----------------------------- -def create_tag(args): +def create_tag_func(args): + # print("CREATE TAG function") if args.time_type is None: sys.exit("Error: time_type parameter is not set") @@ -178,17 +157,60 @@ def create_tag(args): 'end_of_validity': args.end_of_validity, } - resp = crest_api.create_tag(name=args.name, timeType=args.time_type, **params) - print(f"tag {args.name} created") + try: + global crest_host + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_tag(name=args.name, timeType=args.time_type, **params) + # print(f"tag {args.name} created") + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def get_version(): + + from urllib.parse import urlparse + # from urlparse import urlparse # Python 2 + + url = crest_host + parsed_uri = urlparse(url) + result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) + + host = result + "actuator/info" + + resp = requests.get(host) + + # If the HTTP GET request can be served + if resp.status_code == 200: + data = json.loads(resp.content) + + if 'build' not in data: + raise ValueError(f"No target \"build\" in the string {data}") + else: + build = data["build"] + if 'version' not in build: + raise ValueError(f"No target \"version\" in the string {build}") + else: + vers = build["version"] + print(f"CREST server API version = {vers}") + -#----------------------------- -def remove_tag(name): +def remove_tag_func(args): + # print("GET TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {args.crest_host}") + command = args.command + name = args.name - resp = crest_api.remove_tag(name=name) - print(f"tag {name} deleted") + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.remove_tag(name=name) + # print(f"tag {name} deleted") + except Exception as e: + print("Error: "+ repr(e)) -#----------------------------- -def create_tag_info(args): +def create_tag_meta_info_func(args): if args.description is None: sys.exit("Error: description is not set") if args.chansize is None: @@ -204,36 +226,75 @@ def create_tag_info(args): 'colsize': args.colsize, 'tag_info': args.tag_info, } - resp = crest_api.create_tag_meta_info(name=args.name, **params) - print(f"tag info {args.name} created") -#----------------------------- -def find_global_tag(name): - resp = crest_api.find_global_tag(name=name) - print_json(resp) -#----------------------------- -def list_global_tags(name): + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_tag_meta_info(name=args.name, **params) + # print(f"tag meta info {args.name} created") + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) - resp = crest_api.list_global_tags(name=name) - print_json(resp) -#----------------------------- +def get_tag_meta_info_func(args): + # print("GET TAG META INFO function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name -def create_global_tag(args): + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_tag_meta_info(name=name) + print_single_res(resp) + except Exception as e: + print("Error: "+ repr(e)) - if args.validity is None: - sys.exit("Error: validity is not set") - if args.description is None: - sys.exit("Error: description is not set") - if args.release is None: - sys.exit("Error: release is not set") - if args.scenario is None: - sys.exit("Error: scenario is not set") - if args.workflow is None: - sys.exit("Error: workflow is not set") - if args.type is None: - sys.exit("Error: type parameter is not set") + +def get_payload_func(args): + command = args.command + hash = args.hash + file = args.file + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_payload(hash=hash) + with open(file, 'wb') as f: + f.write(resp) + + print(f"payload {hash} stored in {file}") + except Exception as e: + print("Error: "+ repr(e)) + + + +def store_payload_func(args): + name = args.name + file = args.file + since = args.since + + resources = [] + with open(file, 'r') as f: + data = f.read() + dto = { + 'since': since, + 'data': data, + 'streamerInfo': '{\"filename\": \"' + file + '\"}', + } + resources.append(dto) + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.store_json_data(tagname=name, json_array=resources) + print(f"payload for tag {name} stored in CREST from file {file}") + except Exception as e: + print("Error: "+ repr(e)) + + + +def create_global_tag_func(args): params = { 'validity': args.validity, 'description': args.description, @@ -242,274 +303,595 @@ def create_global_tag(args): 'workflow': args.workflow, 'type': args.type, } + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_global_tag(name=args.name, **params) + # print(f"global tag {args.name} created") + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) - resp = crest_api.create_global_tag(name=args.name, **params) - print(f"global tag {args.name} created") -#----------------------------- +def get_global_tag_list_func(args): + # print("GET GLOBAL TAG LIST function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name -def remove_global_tag(name): - - resp = crest_api.remove_global_tag(name=name) - print(f"global tag {name} deleted") + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.list_global_tags(name=name) + # print_json(resp) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) -#----------------------------- -def create_global_tag_map(args): - if args.tag_name is None: - sys.exit("Error: tag_name parameter is not set") - if args.record is None: - sys.exit("Error: record parameter is not set") - if args.label is None: - sys.exit("Error: label parameter is not set") +def remove_global_tag_func(args): + # print("REMOVE GLOBAL TAG function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {args.crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.remove_global_tag(name=name) + # print(f"tag {name} deleted") + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def get_tag_size_func(args): + # print("GET TAG SIZE function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.get_size(tagname=name) + # print_json(resp) + print(f"tag size ({name}) = {resp} IOVs") + except Exception as e: + print("Error: "+ repr(e)) + + +def get_global_tag_map_func(args): + # print("GET GLOBAL TAG MAP function") + # print(f"command = {args.command}") + # print(f"name = {args.name}") + # print(f"host = {crest_host}") + command = args.command + name = args.name + tag_type = args.tag_type + # print(f"tag_type = {tag_type}") + + try: + crest_api = CrestApi(host=crest_host) + if tag_type == "globaltag": + resp = crest_api.find_global_tag_map(name=name) + else: + resp = crest_api.find_global_tag_map(mode="BackTrace",name=name) + print_multiple_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + + +def create_global_tag_map_func(args): params = { 'record': args.record, 'label': args.label, } - resp = crest_api.create_global_tag_map(globaltagname=args.name, tagname=args.tag_name, **params) - print(f"global tag map {args.name} - {args.tag_name} created") + try: + crest_api = CrestApi(host=crest_host) + resp = crest_api.create_global_tag_map(globaltagname=args.name, tagname=args.tag_name, **params) + # print(f"global tag map {args.name} - {args.tag_name} created") + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) + +def remove_global_tag_map_func(args): + try: + crest_api = CrestApi(host=crest_host) + # resp = crestremovemap(globaltagname=args.name, tagname=args.tag_name, label=args.label) + resp = crest_api.delete_global_tag_map(globaltagname=args.name, tagname=args.tag_name, label=args.label) + # print_json(resp) + print_full_res(resp) + except Exception as e: + print("Error: "+ repr(e)) -#----------------------------- -def get_tagmetainfo(name): - resp = crest_api.get_tag_meta_info(name=name) - print_json(resp) +#======================================================= -#----------------------------- -def find_global_tag_map(name, mode='Trace'): +def get_tag_command(): + # print("GET TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_func) - resp = crest_api.find_global_tag_map(name=name, mode=mode) - print_json(resp) + args = parser.parse_args() + #crest_config.proxy = args.proxy -#----------------------------- -def remove_global_tag_map(args): - if args.tag_name is None: - sys.exit("Error: tag_name parameter is not set") - if args.label is None: - sys.exit("Error: label parameter is not set") + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) - resp = crestremovemap(globaltagname=args.name, tagname=args.tag_name, label=args.label) - print_json(resp) +def get_iov_list_command(): + # print("GET IOV LIST command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["iovList"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_iov_list_func) -#----------------------------- -def get_payload(name,file): - -# resp = crest_api.get_payload(name=name, file=file) -# print_json(resp) - resp = crest_api.get_payload(hash=name) - with open(file, 'wb') as f: - f.write(resp) + args = parser.parse_args() + #crest_config.proxy = args.proxy - print(f"payload {name} stored in {file}") + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def store_payload(name,since,path,file): - - resp = crest_api.store_data(tagname=name, since=since, filepath=path, filename=file) - print(f"payload {name} stored in CREST using {file}") +def create_tag_commamnd(): + # print("CREATE TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') -def store_json_payload(name,since,path,file): - # we create the StoreDto objects in a separate function - resources = [] - filename = os.path.join(path, file) - with open(filename, 'r') as f: - data = f.read() - dto = { - 'since': since, - 'data': data, - 'streamerInfo': '{\"filename\": \"' + file + '\"}', - } - resources.append(dto) - resp = crest_api.store_json_data(tagname=name, json_array=resources) - print(f"payload {name} stored in CREST using {file}") + parser.add_argument('-time_type', dest = 'time_type', required = False, default='time', help = 'time type') + parser.add_argument('-description', dest = 'description', required = False, default='test', help = 'description') + parser.add_argument('-payload_spec', dest = 'payload_spec', required = False, default='JSON', help = 'payload specification') + parser.add_argument('-synchronization', dest = 'synchronization', required = False, default='all', help = 'synchronization') + parser.add_argument('-last_validated_time', dest = 'last_validated_time',required = False, default=0.0, help = 'last validated time', type = float) + parser.add_argument('-end_of_validity', dest = 'end_of_validity', required = False, default=-1.0, help = 'end of validity', type = float) -def get_tag_size(tagname): - resp = crest_api.get_size(tagname=tagname) - # print_json(resp) - print(resp) + parser.set_defaults(func=create_tag_func) -def get_version(): + args = parser.parse_args() + #crest_config.proxy = args.proxy - from urllib.parse import urlparse - # from urlparse import urlparse # Python 2 + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) - url = crest_host - parsed_uri = urlparse(url) - result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) - host = result + "actuator/info" +def remove_tag_command(): + # print("REMOVE TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["tag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=remove_tag_func) - resp = requests.get(host) + args = parser.parse_args() + #crest_config.proxy = args.proxy - # If the HTTP GET request can be served - if resp.status_code == 200: - data = json.loads(resp.content) + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) - if 'build' not in data: - raise ValueError(f"No target \"build\" in the string {data}") - else: - build = data["build"] - if 'version' not in build: - raise ValueError(f"No target \"version\" in the string {build}") - else: - vers = build["version"] - print(f"CREST server API version = {vers}") -#----------------------------- +def create_tag_meta_info_command(): + # print("CREATE TAG META INFO command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') + # parser.add_argument("--host", default=crest_host, help="Base URL of the REST API") + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') -#NEW (MvG): + parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') + parser.add_argument('-chansize', dest = 'chansize', required = False, default=1, help = 'number of channels', type = int) + parser.add_argument('-colsize', dest = 'colsize', required = False, default=1, help = 'number of columns', type = int) + parser.add_argument('-tag_info', dest = 'tag_info', required = False, default='tag info', help = 'tag info') -def print_res(data): - str_res = json.dumps(data.to_dict(), default=datetime_serializer) - json_res = json.loads(str_res) + parser.set_defaults(func=create_tag_meta_info_func) - # print(type(data)) - if 'resources' not in data: - # print(jsonData) - print ("TEST C") + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - res = json_res['resources'] - size = len(res) - if size == 1: - # print(res[0]) - print(json.dumps(res[0], indent=4)) - else: - # print(res) - print(json.dumps(res, indent=4)) + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -#----------------------------- +def get_tag_meta_info_command(): + # print("GET TAG META INFO command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tagMetaInfo"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_meta_info_func) -#================================= + args = parser.parse_args() + #crest_config.proxy = args.proxy -def tag(args): - # print(f"tag method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - list_tags(name) - elif command == "remove": - remove_tag(name) - elif command == "create": - create_tag(args) + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def iovList(args): - # print(f"iovList method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - # print("TEST 02") - find_all_iovs(name) + +def get_payload_command(): + # print("GET PAYLOAD command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["payload"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'hash',required = True, help = 'paylaod hash') + parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') + parser.set_defaults(func=get_payload_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def payload(args): - # print(f"payload method:") - # print(f"command = {args.command}") - # print(f"hash = {args.name}") - # print(f"hash = {args.file}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - file = args.file - since = args.since - if command == "get": - get_payload(name,file) - elif command == "store": - if args.since is None: - sys.exit("Error: since parameter is not set") - if args.file is None: - sys.exit("Error: file parameter is not set") - # Extract the filename (including the extension) - filename = os.path.basename(file) - - # Extract the path (directory) - path = os.path.dirname(file) - #store_payload(name,since,path,filename) - store_json_payload(name,since,path,filename) + +def store_payload_command(): + # print("STORE PAYLOD command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["store"], help='commands') + parser.add_argument(dest='type', choices=["payload"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.add_argument('-file', dest = 'file',required = True, help = 'file to store payload') + parser.add_argument('-since', dest = 'since',required = True, help = 'since time', type = float) + parser.set_defaults(func=store_payload_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def globaltag(args): - # print(f"globaltag method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - list_global_tags(name) - elif command == "remove": - remove_global_tag(name) - elif command == "create": - create_global_tag(args) + + +def create_global_tag_command(): + # print("CREATE GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.add_argument('-validity', dest = 'validity', required = False, default=0.0, help = 'validity', type = float) + parser.add_argument('-description', dest = 'description',required = False, default='test', help = 'description') + parser.add_argument('-release', dest = 'release', required = False, default='1.0', help = 'release') + parser.add_argument('-scenario', dest = 'scenario', required = False, default='none', help = 'scenario') + parser.add_argument('-workflow', dest = 'workflow', required = False, default='none', help = 'workflow') + parser.add_argument('-type', dest = 'type', required = False, default='A', help = 'type') + + parser.set_defaults(func=create_global_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def tagMetaInfo(args): - # print(f"tagMetaInfo method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - get_tagmetainfo(name) - elif command == "create": - create_tag_info(args) + + +def get_global_tag_command(): + # print("GET GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_global_tag_list_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def globalTagMap(args): - # print(f"globalTagMap method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - find_global_tag_map(name) - elif command == "create": - create_global_tag_map(args) - elif command == "remove": - remove_global_tag_map(args) + +def remove_global_tag_command(): + # print("REMOVE GLOBAL TAG command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["globalTag"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.set_defaults(func=remove_global_tag_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def version(args): - # print(f"version method:") - # print(f"command = {args.command}") - # print(f"host = {crest_host}") - command = args.command - if command == "get": - get_version() + +def get_tag_size_command(): + # print("GET TAG SIZE command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["tagSize"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'tag name') + parser.set_defaults(func=get_tag_size_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) -def tagSize(args): - # print(f"tagSize method:") - # print(f"command = {args.command}") - # print(f"name = {args.name}") - # print(f"host = {crest_host}") - command = args.command - name = args.name - if command == "get": - get_tag_size(name) + + +def get_global_tag_map_command(): + # print("GET GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["get"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + parser.add_argument('-for', dest = 'tag_type',required = False, choices=["tag","globaltag"], default = "globaltag", help = 'name tag or global tag') + parser.set_defaults(func=get_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def create_global_tag_map_command(): + # print("CREATE GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["create"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + + parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') + parser.add_argument('-record', dest = 'record', required = False, help = 'record', default = 'none') + parser.add_argument('-label', dest = 'label', required = True, help = 'label') + + parser.set_defaults(func=create_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() + else: + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +def remove_global_tag_map_command(): + # print("REMOVE GLOBAL TAG MAP command") + global crest_host + parser = argparse.ArgumentParser() + parser.add_argument(dest='command', choices=["remove"], help='commands') + parser.add_argument(dest='type', choices=["globalTagMap"], help='type') + parser.add_argument("--host", dest = 'crest_host', default = crest_host, help="Base URL of the REST API", required = False) + parser.add_argument('-n', dest = 'name',required = True, help = 'global tag name') + + parser.add_argument('-tag_name', dest = 'tag_name',required = True, help = 'tag name') + parser.add_argument('-label', dest = 'label', required = True, help = 'label') + + parser.set_defaults(func=remove_global_tag_map_func) + + args = parser.parse_args() + #crest_config.proxy = args.proxy + + # print(type(args)) + if not vars(args): + parser.print_usage() else: - print(f"Error: {command} command not supported") + # print(f"new crest host = {args.crest_host}") + crest_host = args.crest_host + args.func(args) + + +#=================================================================== + +def main(): + + global crest_host + global crest_proxy + global crest_config + + global crest_api + + crest_host = get_crest_path() + # print(f'CREST server path = {crest_host}') + ''' + print(type(sys.argv)) + print('The command line arguments are:') + for i in sys.argv: + print(i) + ''' + + size = len(sys.argv) + # print(f"Number of arguments = {size}") + + if size < 3: + print("Pyton CREST Client.") + print("command list:") + print("> pycrest get commands") + # print_commands() + elif size == 3: + crest_command = sys.argv[1] + crest_type = sys.argv[2] + if crest_command == "get": + if crest_type == "commands": + print_commands() + elif crest_type == "version": + get_version() + else: + print("Pyton CREST Client. Chose one of the commands:") + print_commands() + else: + # print("lets go...") + crest_command = sys.argv[1] + crest_type = sys.argv[2] + # print(f"Command = {crest_command}") + # print(f"Type = {crest_type}") + + if crest_command == "get" : + # print("GET block:") + if crest_type == "tag": + # print("GET TAG command") + get_tag_command() + elif crest_type == "iovList": + # print ("GET IOV LIST command") + get_iov_list_command() + elif crest_type == "tagMetaInfo": + # print ("GET TAG META INFO command") + get_tag_meta_info_command() + elif crest_type == "payload": + # print ("GET PAYLOAD command") + get_payload_command() + elif crest_type == "globalTag": + # print ("GET GLOBAL TAG command") + get_global_tag_command() + elif crest_type == "tagSize": + # print("GET TAG SIZE command") + get_tag_size_command() + elif crest_type == "globalTagMap": + # print("GET GLOBAL TAG MAP command") + get_global_tag_map_command() + else: + sys.exit(f"Data type {crest_type} with this command does not work.") + elif crest_command == "remove": + # print("REMOVE block:") + if crest_type == "tag": + # print("REMOVE TAG command") + remove_tag_command() + elif crest_type == "globalTag": + # print("REMOVE GLOBAL TAG command") + remove_global_tag_command() + elif crest_type == "globalTagMap": + # print("REMOVE GLOBAL TAG MAP command") + remove_global_tag_map_command() + else: + sys.exit(f"Data {crest_type} cannot be removed.") + + elif crest_command == "create": + # print("CREATE block") + if crest_type == "tag": + # print("CREATE TAG command:") + create_tag_commamnd() + elif crest_type == "tagMetaInfo": + # print("CREATE TAG META INFO command:") + create_tag_meta_info_command() + elif crest_type == "globalTag": + # print("CREATE GLOBAL TAG command:") + create_global_tag_command() + elif crest_type == "globalTagMap": + create_global_tag_map_command() + else: + sys.exit(f"Cannot create {crest_type}.") + + elif crest_command == "store": + if crest_type == "payload": + # print("STORE PAYLOAD command:") + store_payload_command() + else: + sys.exit(f"Cannot store {crest_type}.") + else: + sys.exit(f"Command {crest_command} not supported.") #----------------------------------- if __name__ == '__main__': main() - diff --git a/setup.py b/setup.py index 74010e7..b31cb29 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,6 @@ setup( entry_points={ 'console_scripts': [ 'pycrest = pycrest.cli.pycrest_cli:main', - 'pyctest = pycrest.cli.pycrest10:main', ], }, scripts=['scripts/crest-python-setup.sh', 'scripts/crest-server-path-setup.sh'], -- GitLab