source: buildhelpers/code_generator@ 2c86c99

debian/bullseye
Last change on this file since 2c86c99 was f9321aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Adapted code_generator to
    • handle several packages
    • to configure the port from yaml specification
    • to use the VERSION used by setup.py
  • Added deviceservice.yaml with first version of webservice
  • Moved Config loader and HttpErrors to gnuboat.common
  • Moved rrd archive schema to gnuboat.common.device
  • Property mode set to 100755
File size: 2.6 KB
Line 
1#!/usr/bin/python
2
3import os
4from pathlib import Path
5import subprocess
6import re
7import shutil
8import yaml
9
10CURRENT_DIR = Path(os.path.dirname(__file__))
11SRC = CURRENT_DIR.joinpath('../src/gnuboat')
12SRC_GEN = CURRENT_DIR.joinpath('../src/gnuboat/')
13version_file = open(CURRENT_DIR.joinpath('../VERSION').resolve(), 'r')
14VERSION = version_file.read().strip()
15version_file.close()
16
17PACKAGES=["mapservice", "deviceservice"]
18
19print('Generating the {} services:'.format(len(PACKAGES)))
20for package in PACKAGES:
21 print(' {}'.format(package))
22
23for 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()])
Note: See TracBrowser for help on using the repository browser.