#!/usr/bin/python import os from pathlib import Path import subprocess import re import shutil import yaml CURRENT_DIR = Path(os.path.dirname(__file__)) SRC = CURRENT_DIR.joinpath('../src/gnuboat') SRC_GEN = CURRENT_DIR.joinpath('../src/gnuboat/') version_file = open(CURRENT_DIR.joinpath('../VERSION').resolve(), 'r') VERSION = version_file.read().strip() version_file.close() PACKAGES=["mapservice", "deviceservice"] print('Generating the {} services:'.format(len(PACKAGES))) for package in PACKAGES: print(' {}'.format(package)) for package in PACKAGES: package_name_gen = 'gnuboat.{}'.format(package) target = SRC_GEN.joinpath(package) target_tmp = SRC_GEN.joinpath('{}_tmp'.format(package)) service_specification_file = SRC.joinpath('{}.yaml'.format(package)).resolve() server_port = 8080 openapi_specification = yaml.safe_load(open(service_specification_file)) if 'servers' in openapi_specification: for entry in openapi_specification['servers']: if 'description' in entry and 'development' in entry['description'].lower(): result = re.search(r':([0-9]+)', entry['url']) if result is not None: server_port = result.group(1) print('Preparing package {} version {} using port {}'.format(package, VERSION, server_port)) print(' Cleaning directory') if os.path.isdir(target): shutil.rmtree(target.resolve()) if os.path.isdir(target_tmp): shutil.rmtree(target_tmp.resolve()) os.mkdir(target_tmp) print(' Generating backend API code') try: subprocess.run([ CURRENT_DIR.joinpath('codegen/openapi-generator-cli').resolve(), 'generate', '-i', service_specification_file, '-g', 'python-flask', '-t', CURRENT_DIR.joinpath('codegen/templates').resolve(), ('--additional-properties=packageVersion={},packageName={},' 'serverPort={}').format(VERSION, package_name_gen, server_port), '-o', target_tmp.resolve() ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except subprocess.CalledProcessError as e: print(' openapi-generator-cli failed:') print(' STDOUT: {}'.format(e.stdout)) print(' STDERR: {}'.format(e.stderr)) shutil.move(target_tmp.joinpath('gnuboat/{}'.format(package)).resolve(), target.resolve()) shutil.rmtree(target_tmp.resolve()) # running yapf print(' Formatting the generated code accoring to style file') subprocess.run(['yapf', '--style', 'python-style.yapf', '-r', '-i', target.resolve()])