Disable locking during deployment
[sdc.git] / catalog-be / src / main / resources / scripts / sdcBePy / tosca / imports / run.py
1 #!/usr/bin/env python3
2
3 import os
4 from pathlib import Path
5
6 import sdcBePy.common.logger as logger
7 from sdcBePy.common.normative.main import process_element_list, process_type_list
8 from sdcBePy.tosca.main import parse_and_create_proxy
9 from sdcBePy.tosca.models import normativeElementsList
10 from sdcBePy.tosca.models import normativeTypesList
11 from sdcBePy.tosca.models.model_client import ModelClient
12 from sdcBePy.tosca.models.model_import_manager import ModelImportManager
13
14
15 def main(sdc_be_proxy, update_version):
16     # use to run script form this dir (not like the command)
17     # base_file_location = os.getcwd() + "/../../../import/tosca/"
18     base_file_location = os.getcwd() + os.path.sep
19     logger.debug("working directory =" + base_file_location)
20     if sdc_be_proxy.disable_locking("true") != 200:
21         raise RuntimeError("Failed to disable locking")
22     try:
23         model_import_manager = ModelImportManager(Path(base_file_location) / 'models', ModelClient(sdc_be_proxy))
24         try:
25             model_import_manager.create_models()
26         except Exception as ex:
27             logger.log("An error has occurred while uploading the models: ", str(ex))
28             raise ex
29         process_element_list(normativeElementsList.get_normative_element_candidate_list(base_file_location), sdc_be_proxy)
30         process_type_list(normativeTypesList.get_normative_type_candidate_list(base_file_location), sdc_be_proxy, update_version)
31         process_element_list(normativeElementsList.get_normative_element_with_metadata_list(base_file_location), sdc_be_proxy)
32     except Exception as ex:
33         logger.log("An error has occurred while uploading elements and types: ", str(ex))
34         raise ex
35     finally:
36         if sdc_be_proxy.disable_locking("false") != 200:
37             raise RuntimeError("Failed to enable locking")
38     logger.log("Script end ->", "All normatives imported successfully!")
39     logger.print_and_exit(0, None)
40
41 def run():
42     sdc_be_proxy, update_version = parse_and_create_proxy()
43     main(sdc_be_proxy, update_version)
44
45
46 if __name__ == "__main__":
47     run()