def TestGenClient(self): api_version_in_targets = api_config.get('version', api_version) prefix = api_name + '_' + api_version_in_targets with files.TemporaryDirectory() as tmp_dir_path: # Place discovery doc into tmp folder. discovery_dir = os.path.join(tmp_dir_path, apis_dir) files.MakeDir(discovery_dir) shutil.copy( _GetClientPath(base_dir, apis_dir, api_config['discovery_doc']), discovery_dir) # Create parent folder __init__ files, as they do not exist in tmp dir, # this is to avoid unnecessary warnings which generally does not happen. api_dir = os.path.join(discovery_dir, api_name) files.MakeDir(api_dir) with open(os.path.join(discovery_dir, '__init__.py'), 'w'): pass with open(os.path.join(api_dir, '__init__.py'), 'w'): pass generate.GenerateApi(tmp_dir_path, apis_dir, api_name, api_version, api_config) generate.GenerateResourceModule(tmp_dir_path, apis_dir, api_name, api_version, api_config['discovery_doc'], api_config.get('resources', {})) expected_files = set([ prefix + '_client.py', prefix + '_messages.py', 'resources.py', '__init__.py' ]) output_dir = os.path.join(tmp_dir_path, apis_dir, api_name, api_version) actual_files = set(os.listdir(output_dir)) self.assertTrue( actual_files <= expected_files, 'At most expected {0} but got {1}'.format( expected_files, actual_files)) for file_name in actual_files: AssertDiffEqual( files.ReadFileContents( _GetClientPath(base_dir, apis_dir, api_name, api_version, file_name)), files.ReadFileContents(os.path.join(output_dir, file_name)))
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)
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'])