Ignore:
Timestamp:
Apr 26, 2020, 5:02:25 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
62569d9
Parents:
691c4ca
Message:
  • 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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • buildhelpers/code_generator

    r691c4ca rf9321aa  
    1 #!/bin/bash
     1#!/usr/bin/python
    22
    3 DIR=`dirname "$0"`
    4 SRC="${DIR}/../src/gnuboat"
    5 SRC_GEN="${DIR}/../src/gnuboat/"
    6 PACKAGE_NAME="mapservice"
    7 PACKAGE_NAME_GEN="gnuboat.${PACKAGE_NAME}"
    8 PACKAGE_VERSION="0.5.90"
    9 TARGET_TMP="${SRC_GEN}/${PACKAGE_NAME}_tmp/"
     3import os
     4from pathlib import Path
     5import subprocess
     6import re
     7import shutil
     8import yaml
    109
    11 echo "Cleaning directory"
    12 if [ -d "${SRC_GEN}/${PACKAGE_NAME}" ]; then
    13         rm -R "${SRC_GEN}/${PACKAGE_NAME}"
    14 fi
    15 if [ -d "${TARGET_TMP}" ]; then
    16         rm -R "${TARGET_TMP}"
    17 fi
     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()
    1816
    19 echo "Preparing symlinks"
    20 mkdir -p "${TARGET_TMP}"
     17PACKAGES=["mapservice", "deviceservice"]
    2118
    22 echo "Generating backend API code"
    23 ${DIR}/codegen/openapi-generator-cli generate -i "${SRC}/mapservice.yaml" -g python-flask -t "${DIR}/codegen/templates/" --additional-properties=packageVersion=${PACKAGE_VERSION},packageName=${PACKAGE_NAME_GEN} -o "${TARGET_TMP}"
    24 mv "${TARGET_TMP}/gnuboat/${PACKAGE_NAME}" "${SRC_GEN}/${PACKAGE_NAME}"
    25 rm -R "${TARGET_TMP}"
     19print('Generating the {} services:'.format(len(PACKAGES)))
     20for package in PACKAGES:
     21    print('  {}'.format(package))
    2622
    27 # running yapf
    28 echo "Formatting the generated code accoring to style file"
    29 yapf --style "python-style.yapf" -r -i "${SRC_GEN}/${PACKAGE_NAME}"
     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 TracChangeset for help on using the changeset viewer.