Пример #1
0
    def testCreateAPIsMapFile(self):
        config = yaml.load(
            textwrap.dedent("""\
        orange:
          v1:
            discovery: organge_v1.json
            default: true
          v2:
            discovery: organge_v2.json
        banana:
          v2beta:
            discovery: banana_v2beta.json
          v2_staging:
            version: v2
            discovery: banana_v2_staging.json
            default: true
        pear:
          v7_test:
            discovery: pear_v7_test.json
    """))

        with files.TemporaryDirectory() as tmp_dir:
            dir_path = os.path.join(tmp_dir, 'fruits')
            os.makedirs(dir_path)
            generate.GenerateApiMap(tmp_dir, 'fruits', config)
            content = files.ReadFileContents(
                os.path.join(dir_path, 'apis_map.py'))

        self.maxDiff = None  # pylint: disable=invalid-name
        self.assertMultiLineEqual(
            files.ReadFileContents(
                os.path.join(os.path.dirname(__file__), 'testdata',
                             'api_map_sample.txt')), content)
Пример #2
0
    def Run(self, args):
        config = _LoadConfig(args.config)
        root_dir = config['root_dir']
        changed_config = False

        if args.api_discovery_doc:
            if not os.path.isfile(args.api_discovery_doc):
                raise regen_utils.DiscoveryDocError('File not found {}'.format(
                    args.api_discovery_doc))
            if len(args.api) != 1:
                raise parser_errors.ArgumentError(
                    'Can only specify one api when discovery doc is provided.')

            match = re.match(_API_REGEX, args.api[0])
            if not match:
                raise regen_utils.DiscoveryDocError(
                    'Api name must match {} pattern when discovery doc '
                    'is specified'.format(_API_REGEX))

            api_name, api_version = match.group(1), match.group(2)
            if api_name not in config['apis']:
                log.warning('No such api %s in config, adding...', api_name)
                config['apis'][api_name] = {api_version: {'discovery_doc': ''}}
                changed_config = True
            elif api_version not in config['apis'][api_name]:
                log.warning('No such api version %s in config, adding...',
                            api_version)
                config['apis'][api_name][api_version] = {'discovery_doc': ''}
                changed_config = True

            api_version_config = config['apis'].get(api_name).get(
                api_version, {})
            discovery_doc = api_name + '_' + api_version + '.json'
            new_discovery_doc = os.path.realpath(args.api_discovery_doc)
            old_discovery_doc = os.path.realpath(
                os.path.join(args.base_dir, root_dir, discovery_doc))

            if new_discovery_doc != old_discovery_doc:
                log.status.Print('Copying in {}'.format(new_discovery_doc))
                shutil.copyfile(new_discovery_doc, old_discovery_doc)

            if api_version_config['discovery_doc'] != discovery_doc:
                changed_config = True
                api_version_config['discovery_doc'] = discovery_doc

            regenerate_list = [(match.group(1), match.group(2),
                                api_version_config)]
        else:
            regex_patern = '|'.join(map(fnmatch.translate, args.api))
            regenerate_list = [
                (api_name, api_version, api_config)
                for api_name, api_version_config in six.iteritems(
                    config['apis'])
                for api_version, api_config in six.iteritems(
                    api_version_config)
                if re.match(regex_patern, api_name + '/' + api_version)
            ]

        if not regenerate_list:
            raise regen_utils.UnknownApi(
                'api [{api_name}] not found in "apis" section of '
                '{config_file}. Use [gcloud meta apis list] to see available apis.'
                .format(api_name=','.join(args.api), config_file=args.config))

        base_dir = args.base_dir or os.path.dirname(
            os.path.dirname(googlecloudsdk.__file__))
        for api_name, api_version, api_config in sorted(regenerate_list):
            log.status.Print('Generating {} {} from {}'.format(
                api_name, api_version,
                os.path.join(root_dir, api_config['discovery_doc'])))
            generate.GenerateApi(base_dir, root_dir, api_name, api_version,
                                 api_config)
            generate.GenerateResourceModule(base_dir, root_dir, api_name,
                                            api_version,
                                            api_config['discovery_doc'],
                                            api_config.get('resources', {}))

        generate.GenerateApiMap(base_dir, root_dir, config['apis'])

        # Now that everything passed, config can be updated if needed.
        if changed_config:
            log.warning('Updated %s', args.config)
            with files.FileWriter(args.config) as stream:
                ruamel.yaml.round_trip_dump(config, stream)
Пример #3
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = argparse.ArgumentParser(
        description='Regenerates apitools clients in given directory.')

    parser.add_argument('--config',
                        required=True,
                        help='Regeneration config filename.')

    parser.add_argument('--base-dir',
                        default=os.getcwd(),
                        help='Regeneration config filename.')

    parser.add_argument('--api',
                        help='api_name or api_name/api_version to regenerate. '
                        'If api_version is ommitted then all versions are '
                        'regenerated. If this argument is ommitted all apis '
                        'and their versions will be regenerated.')

    parser.add_argument(
        '-l',
        '--log-level',
        choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
        default='INFO',
        help='Set the logging level')

    args = parser.parse_args(argv[1:])

    if args.log_level:
        logging.basicConfig(
            format='%(asctime)s %(filename)s:%(lineno)d %(message)s',
            level=getattr(logging, args.log_level))

    config = yaml.load_path(args.config)

    logging.debug('Config %s', pprint.pformat(config))

    root_dir = config['root_dir']
    logging.debug('Based dir %s', args.base_dir)

    if args.api is not None:
        if '/' in args.api:
            api_name, api_version = args.api.split('/')
        else:
            api_name, api_version = args.api, None
        api_section = config['apis'].get(api_name)
        if api_section is None:
            raise UnknownApi('api [{api_name}] not found in "apis" section of '
                             '{config_file}'.format(api_name=api_name,
                                                    config_file=args.config))
        if api_version:
            api_config = api_section.get(api_version)
            if api_config is None:
                raise UnknownApi(
                    'api version [{api_version}] is not one of the '
                    'defined versions [{defined_versions}] of '
                    '[{api_name}] found in "apis" section of {config_file}'.
                    format(api_version=api_version,
                           api_name=api_name,
                           defined_versions=','.join(sorted(
                               api_section.keys())),
                           config_file=args.config))
            regenerate_list = [(api_name, api_version, api_config)]
        else:
            regenerate_list = [
                (api_name, api_version, api_config)
                for api_version, api_config in six.iteritems(api_section)
            ]
    else:
        regenerate_list = [
            (api_name, api_version, api_config)
            for api_name, api_version_config in six.iteritems(config['apis'])
            for api_version, api_config in six.iteritems(api_version_config)
        ]

    for api_name, api_version, api_config in sorted(regenerate_list):
        logging.info('Generating %s %s', api_name, api_version)
        generate.GenerateApi(args.base_dir, root_dir, api_name, api_version,
                             api_config)
        generate.GenerateResourceModule(args.base_dir, root_dir, api_name,
                                        api_version,
                                        api_config['discovery_doc'],
                                        api_config.get('resources', {}))

    generate.GenerateApiMap(args.base_dir, root_dir, config['apis'])