| [f9321aa] | 1 | #!/usr/bin/python
|
|---|
| 2 |
|
|---|
| 3 | import os
|
|---|
| 4 | from pathlib import Path
|
|---|
| 5 | import subprocess
|
|---|
| 6 | import re
|
|---|
| 7 | import shutil
|
|---|
| 8 | import yaml
|
|---|
| 9 |
|
|---|
| 10 | CURRENT_DIR = Path(os.path.dirname(__file__))
|
|---|
| 11 | SRC = CURRENT_DIR.joinpath('../src/gnuboat')
|
|---|
| 12 | SRC_GEN = CURRENT_DIR.joinpath('../src/gnuboat/')
|
|---|
| 13 | version_file = open(CURRENT_DIR.joinpath('../VERSION').resolve(), 'r')
|
|---|
| 14 | VERSION = version_file.read().strip()
|
|---|
| 15 | version_file.close()
|
|---|
| 16 |
|
|---|
| 17 | PACKAGES=["mapservice", "deviceservice"]
|
|---|
| 18 |
|
|---|
| 19 | print('Generating the {} services:'.format(len(PACKAGES)))
|
|---|
| 20 | for package in PACKAGES:
|
|---|
| 21 | print(' {}'.format(package))
|
|---|
| 22 |
|
|---|
| 23 | for package in PACKAGES:
|
|---|
| 24 | package_name_gen = 'gnuboat.{}'.format(package)
|
|---|
| 25 | target = SRC_GEN.joinpath(package)
|
|---|
| 26 | target_tmp = SRC_GEN.joinpath('{}_tmp'.format(package))
|
|---|
| 27 | service_specification_file = SRC.joinpath('{}.yaml'.format(package)).resolve()
|
|---|
| 28 | server_port = 8080
|
|---|
| 29 |
|
|---|
| 30 | openapi_specification = yaml.safe_load(open(service_specification_file))
|
|---|
| 31 | if 'servers' in openapi_specification:
|
|---|
| 32 | for entry in openapi_specification['servers']:
|
|---|
| 33 | if 'description' in entry and 'development' in entry['description'].lower():
|
|---|
| 34 | result = re.search(r':([0-9]+)', entry['url'])
|
|---|
| 35 | if result is not None:
|
|---|
| 36 | server_port = result.group(1)
|
|---|
| 37 |
|
|---|
| 38 | print('Preparing package {} version {} using port {}'.format(package, VERSION, server_port))
|
|---|
| 39 | print(' Cleaning directory')
|
|---|
| 40 | if os.path.isdir(target):
|
|---|
| 41 | shutil.rmtree(target.resolve())
|
|---|
| 42 | if os.path.isdir(target_tmp):
|
|---|
| 43 | shutil.rmtree(target_tmp.resolve())
|
|---|
| 44 | os.mkdir(target_tmp)
|
|---|
| 45 |
|
|---|
| 46 | print(' Generating backend API code')
|
|---|
| 47 | try:
|
|---|
| 48 | subprocess.run([
|
|---|
| 49 | CURRENT_DIR.joinpath('codegen/openapi-generator-cli').resolve(),
|
|---|
| 50 | 'generate', '-i', service_specification_file,
|
|---|
| 51 | '-g', 'python-flask', '-t',
|
|---|
| 52 | CURRENT_DIR.joinpath('codegen/templates').resolve(),
|
|---|
| 53 | ('--additional-properties=packageVersion={},packageName={},'
|
|---|
| 54 | 'serverPort={}').format(VERSION, package_name_gen, server_port),
|
|---|
| 55 | '-o', target_tmp.resolve()
|
|---|
| 56 | ],
|
|---|
| 57 | check=True,
|
|---|
| 58 | stdout=subprocess.PIPE,
|
|---|
| 59 | stderr=subprocess.PIPE)
|
|---|
| 60 | except subprocess.CalledProcessError as e:
|
|---|
| 61 | print(' openapi-generator-cli failed:')
|
|---|
| 62 | print(' STDOUT: {}'.format(e.stdout))
|
|---|
| 63 | print(' STDERR: {}'.format(e.stderr))
|
|---|
| 64 |
|
|---|
| 65 | shutil.move(target_tmp.joinpath('gnuboat/{}'.format(package)).resolve(), target.resolve())
|
|---|
| 66 | shutil.rmtree(target_tmp.resolve())
|
|---|
| 67 |
|
|---|
| 68 | # running yapf
|
|---|
| 69 | print(' Formatting the generated code accoring to style file')
|
|---|
| 70 | subprocess.run(['yapf', '--style', 'python-style.yapf', '-r', '-i',
|
|---|
| 71 | target.resolve()])
|
|---|