예제 #1
0
def sdk_automation(input_file: str, output_file: str):
    base_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
    sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT))
    api_specs_file = os.path.join(base_dir, API_SPECS_FILE)
    with open(input_file, 'r') as fin:
        config = json.load(fin)

    packages = []
    for readme in config['relatedReadmeMdFiles']:
        match = re.search(
            'specification/([^/]+)/resource-manager/readme.md',
            readme,
            re.IGNORECASE,
        )
        if not match:
            logging.info(
                '[Skip] readme path does not format as specification/*/resource-manager/readme.md'
            )
        else:
            spec = match.group(1)
            service = get_and_update_service_from_api_specs(
                api_specs_file, spec)

            pre_suffix = SUFFIX
            suffix = get_suffix_from_api_specs(api_specs_file, spec)
            if suffix == None:
                suffix = SUFFIX
            update_parameters(suffix)

            # TODO: use specific function to detect tag in "resources"
            tag = None
            if service == 'resources':
                with open(os.path.join(config['specFolder'], readme)) as fin:
                    tag_match = re.search('tag: (package-resources-[\S]+)',
                                          fin.read())
                    if tag_match:
                        tag = tag_match.group(1)
                    else:
                        tag = 'package-resources-2020-10'

            module = ARTIFACT_FORMAT.format(service)
            output_folder = OUTPUT_FOLDER_FORMAT.format(service)
            namespace = NAMESPACE_FORMAT.format(service)
            stable_version, current_version = set_or_increase_version(
                sdk_root, GROUP_ID, module)
            succeeded = generate(
                sdk_root,
                service,
                spec_root=config['specFolder'],
                readme=readme,
                autorest=AUTOREST_CORE_VERSION,
                use=AUTOREST_JAVA,
                output_folder=output_folder,
                module=module,
                namespace=namespace,
                tag=tag,
            )
            if succeeded:
                compile_package(sdk_root, module)

            packages.append({
                'packageName':
                '{0}'.format(ARTIFACT_FORMAT.format(service)),
                'path': [
                    output_folder,
                    CI_FILE_FORMAT.format(service),
                    POM_FILE_FORMAT.format(service),
                    'eng/versioning',
                    'pom.xml',
                ],
                'readmeMd': [readme],
                'artifacts': ['{0}/pom.xml'.format(output_folder)] + [
                    jar for jar in glob.glob('{0}/target/*.jar'.format(
                        output_folder))
                ],
                'apiViewArtifact':
                next(
                    iter(
                        glob.glob(
                            '{0}/target/*-sources.jar'.format(output_folder))),
                    None),
                'language':
                'Java',
                'result':
                'succeeded' if succeeded else 'failed',
            })

            update_parameters(pre_suffix)

    if not packages:
        # try data-plane codegen
        packages = sdk_automation_data(config)

    with open(output_file, 'w') as fout:
        output = {
            'packages': packages,
        }
        json.dump(output, fout)
예제 #2
0
def main():
    args = vars(parse_args())

    if args.get('config'):
        return sdk_automation(args['config'][0], args['config'][1])

    base_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
    sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT))
    api_specs_file = os.path.join(base_dir, API_SPECS_FILE)

    readme = args['readme']
    match = re.match(
        'specification/([^/]+)/resource-manager/readme.md',
        readme,
        re.IGNORECASE,
    )
    if not match:
        spec = readme
        readme = 'specification/{0}/resource-manager/readme.md'.format(spec)
    else:
        spec = match.group(1)

    args['readme'] = readme
    args['spec'] = spec

    # update_parameters(
    #     args.get('suffix') or get_suffix_from_api_specs(api_specs_file, spec))
    update_parameters(args.get('suffix'))
    service = get_and_update_service_from_api_specs(api_specs_file, spec,
                                                    args['service'])
    args['service'] = service
    module = ARTIFACT_FORMAT.format(service)
    stable_version, current_version = set_or_increase_version(
        sdk_root, GROUP_ID, module, **args)
    args['version'] = current_version
    output_folder = OUTPUT_FOLDER_FORMAT.format(service),
    namespace = NAMESPACE_FORMAT.format(service)
    succeeded = generate(sdk_root,
                         module=module,
                         output_folder=output_folder,
                         namespace=namespace,
                         **args)

    if succeeded:
        succeeded = compile_package(sdk_root, module)
        if succeeded:
            compare_with_maven_package(sdk_root, service, stable_version,
                                       current_version, module)

            if args.get('auto_commit_external_change') and args.get(
                    'user_name') and args.get('user_email'):
                pwd = os.getcwd()
                try:
                    os.chdir(sdk_root)
                    os.system(
                        'git add eng/versioning eng/mgmt pom.xml {0} {1}'.
                        format(CI_FILE_FORMAT.format(service),
                               POM_FILE_FORMAT.format(service)))
                    os.system(
                        'git -c user.name={0} -c user.email={1} commit -m "[Automation] External Change"'
                        .format(args['user_name'], args['user_email']))
                finally:
                    os.chdir(pwd)

    if not succeeded:
        raise RuntimeError('Failed to generate code or compile the package')