Exemple #1
0
def GenerateApi(base_dir, root_dir, api_name, api_version, api_config):
  """Invokes apitools generator for given api."""
  discovery_doc = api_config['discovery_doc']

  args = [gen_client.__file__]

  unelidable_request_methods = api_config.get('unelidable_request_methods')
  if unelidable_request_methods:
    args.append('--unelidable_request_methods={0}'.format(
        ','.join(api_config['unelidable_request_methods'])))

  args.extend([
      '--init-file=empty',
      '--nogenerate_cli',
      '--infile={0}'.format(os.path.join(base_dir, root_dir, discovery_doc)),
      '--outdir={0}'.format(os.path.join(base_dir, root_dir, api_name,
                                         api_version)),
      '--overwrite',
      '--apitools_version=CloudSDK',
      '--root_package',
      '{0}.{1}.{2}'.format(
          root_dir.replace('/', '.'), api_name, api_version),
      'client',
  ])
  logging.debug('Apitools gen %s', args)
  gen_client.main(args)

  package_dir = base_dir
  for subdir in [root_dir, api_name, api_version]:
    package_dir = os.path.join(package_dir, subdir)
    init_file = os.path.join(package_dir, '__init__.py')
    if not os.path.isfile(init_file):
      logging.warning('%s does not have __init__.py file, generating ...',
                      package_dir)
      files.WriteFileContents(init_file, _INIT_FILE_CONTENT)
Exemple #2
0
 def _CheckGeneratedFiles(self, api_name, api_version):
     prefix = api_name + '_' + api_version
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--generate_cli',
             '--init-file', 'empty',
             '--infile',
             GetSampleClientPath(api_name, prefix + '.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package',
             'samples.{0}_sample.{0}_{1}'.format(api_name, api_version),
             'client'
         ])
         expected_files = (
             set([prefix + '.py']) |  # CLI files
             set([prefix + '_client.py',
                  prefix + '_messages.py',
                  '__init__.py']))
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
         for expected_file in expected_files:
             self.AssertDiffEqual(
                 _GetContent(GetSampleClientPath(
                     api_name, prefix, expected_file)),
                 _GetContent(os.path.join(tmp_dir_path, expected_file)))
Exemple #3
0
def GenerateApi(base_dir, root_dir, api_name, api_version, api_config):
    """Invokes apitools generator for given api."""
    discovery_doc = api_config['discovery_doc']

    args = [gen_client.__file__]

    unelidable_request_methods = api_config.get('unelidable_request_methods')
    if unelidable_request_methods:
        args.append('--unelidable_request_methods={0}'.format(','.join(
            api_config['unelidable_request_methods'])))

    api_version_in_targets = api_config.get('version', api_version)
    args.extend([
        # TODO(b/25710611) enable empty init files.
        # '--init-file=empty',
        '--nogenerate_cli',
        '--infile={0}'.format(os.path.join(base_dir, root_dir, discovery_doc)),
        '--outdir={0}'.format(
            os.path.join(base_dir, root_dir, api_name,
                         api_version_in_targets)),
        '--overwrite',
        '--apitools_version=CloudSDK',
        '--root_package',
        '{0}.{1}.{2}'.format(root_dir.replace('/', '.'), api_name,
                             api_version_in_targets),
        'client',
    ])
    logging.debug('Apitools gen %s', args)
    gen_client.main(args)
 def _CheckGeneratedFiles(self, api_name, api_version):
     prefix = api_name + '_' + api_version
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--generate_cli',
             '--init-file', 'empty',
             '--infile',
             GetSampleClientPath(api_name, prefix + '.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package',
             'samples.{0}_sample.{0}_{1}'.format(api_name, api_version),
             'client'
         ])
         expected_files = (
             set([prefix + '.py']) |  # CLI files
             set([prefix + '_client.py',
                  prefix + '_messages.py',
                  '__init__.py']))
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
         for expected_file in expected_files:
             self.AssertDiffEqual(
                 _GetContent(GetSampleClientPath(
                     api_name, prefix, expected_file)),
                 _GetContent(os.path.join(tmp_dir_path, expected_file)))
Exemple #5
0
    def testGenClient_SimpleDocEmptyInit(self):
        with test_utils.TempDir() as tmp_dir_path:
            gen_client.main(
                [
                    gen_client.__file__,
                    "--generate_cli",
                    "--init-file",
                    "empty",
                    "--infile",
                    GetTestDataPath("dns", "dns_v1.json"),
                    "--outdir",
                    tmp_dir_path,
                    "--overwrite",
                    "--root_package",
                    "google.apis",
                    "client",
                ]
            )
            expected_files = set(["dns_v1.py"]) | set(  # CLI files
                ["dns_v1_client.py", "dns_v1_messages.py", "__init__.py"]
            )
            self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
            init_file = _GetContent(os.path.join(tmp_dir_path, "__init__.py"))
            self.assertEqual(
                """\"""Package marker file.\"""

import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
""",
                init_file,
            )
 def testHelp_NotEnoughArguments(self):
     with self.assertRaisesRegexp(SystemExit, '0'):
         with test_utils.CaptureOutput() as (_, err):
             gen_client.main([gen_client.__file__, '-h'])
             err_output = err.getvalue()
             self.assertIn('usage:', err_output)
             self.assertIn('error: too few arguments', err_output)
 def testHelp_NotEnoughArguments(self):
     with self.assertRaisesRegexp(SystemExit, '0'):
         with test_utils.CaptureOutput() as (_, err):
             gen_client.main([gen_client.__file__, '-h'])
             err_output = err.getvalue()
             self.assertIn('usage:', err_output)
             self.assertIn('error: too few arguments', err_output)
Exemple #8
0
 def testGenPipPackage_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__, '--nogenerate_cli', '--infile',
             GetDocPath('dns_v1.json'), '--outdir', tmp_dir_path,
             '--overwrite', '--root_package', 'google.apis', 'pip_package'
         ])
         self.assertEquals(set(['apitools', 'setup.py']),
                           set(os.listdir(tmp_dir_path)))
Exemple #9
0
 def testGenProto_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__, '--nogenerate_cli', '--infile',
             GetDocPath('dns_v1.json'), '--outdir', tmp_dir_path,
             '--overwrite', '--root_package', 'google.apis', 'proto'
         ])
         self.assertEquals(
             set(['dns_v1_messages.proto', 'dns_v1_services.proto']),
             set(os.listdir(tmp_dir_path)))
 def testGenClient_SimpleDocNoInit(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__, '--init-file', 'none', '--infile',
             GetTestDataPath('dns',
                             'dns_v1.json'), '--outdir', tmp_dir_path,
             '--overwrite', '--root_package', 'google.apis', 'client'
         ])
         expected_files = (set(['dns_v1_client.py', 'dns_v1_messages.py']))
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
 def testGenClient_SimpleDocWithV5(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__, '--infile',
             GetTestDataPath('dns', 'dns_v1.json'), '--outdir',
             tmp_dir_path, '--overwrite', '--apitools_version', '0.5.0',
             '--root_package', 'google.apis', 'client'
         ])
         self.assertEquals(
             set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
             set(os.listdir(tmp_dir_path)))
 def testGenPipPackage_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--infile', GetTestDataPath('dns', 'dns_v1.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package', 'google.apis',
             'pip_package'
         ])
         self.assertEquals(
             set(['apitools', 'setup.py']),
             set(os.listdir(tmp_dir_path)))
 def testGenProto_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--infile', GetTestDataPath('dns', 'dns_v1.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package', 'google.apis',
             'proto'
         ])
         self.assertEquals(
             set(['dns_v1_messages.proto', 'dns_v1_services.proto']),
             set(os.listdir(tmp_dir_path)))
 def testGenClient_SimpleDocNoInit(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--init-file', 'none',
             '--infile', GetTestDataPath('dns', 'dns_v1.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package', 'google.apis',
             'client'
         ])
         expected_files = (
             set(['dns_v1_client.py', 'dns_v1_messages.py']))
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
 def testGenClient_SimpleDocWithV5(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--infile', GetTestDataPath('dns', 'dns_v1.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--apitools_version', '0.5.0',
             '--root_package', 'google.apis',
             'client'
         ])
         self.assertEquals(
             set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
             set(os.listdir(tmp_dir_path)))
Exemple #16
0
 def testGenPipPackage_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main(
             [
                 gen_client.__file__,
                 "--nogenerate_cli",
                 "--infile",
                 GetTestDataPath("dns", "dns_v1.json"),
                 "--outdir",
                 tmp_dir_path,
                 "--overwrite",
                 "--root_package",
                 "google.apis",
                 "pip_package",
             ]
         )
         self.assertEquals(set(["apitools", "setup.py"]), set(os.listdir(tmp_dir_path)))
Exemple #17
0
 def testGenProto_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main(
             [
                 gen_client.__file__,
                 "--nogenerate_cli",
                 "--infile",
                 GetTestDataPath("dns", "dns_v1.json"),
                 "--outdir",
                 tmp_dir_path,
                 "--overwrite",
                 "--root_package",
                 "google.apis",
                 "proto",
             ]
         )
         self.assertEquals(set(["dns_v1_messages.proto", "dns_v1_services.proto"]), set(os.listdir(tmp_dir_path)))
    def testGenClient_SimpleDocEmptyInit(self):
        with test_utils.TempDir() as tmp_dir_path:
            gen_client.main([
                gen_client.__file__, '--init-file', 'empty', '--infile',
                GetTestDataPath('dns',
                                'dns_v1.json'), '--outdir', tmp_dir_path,
                '--overwrite', '--root_package', 'google.apis', 'client'
            ])
            expected_files = (set(
                ['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
            self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
            init_file = _GetContent(os.path.join(tmp_dir_path, '__init__.py'))
            self.assertEqual(
                """\"""Package marker file.\"""

import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
""", init_file)
Exemple #19
0
 def testGenClient_SimpleDoc(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main([
             gen_client.__file__,
             '--generate_cli',
             '--infile', GetTestDataPath('dns_v1.json'),
             '--outdir', tmp_dir_path,
             '--overwrite',
             '--root_package', 'google.apis',
             'client'
         ])
         expected_files = (
             set(['dns_v1.py']) |  # CLI files
             set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
         for expected_file in expected_files:
             self.assertMultiLineEqual(
                 _GetContent(GetTestDataPath('gen_' + expected_file)),
                 _GetContent(os.path.join(tmp_dir_path, expected_file)))
Exemple #20
0
 def testGenClient_SimpleDocNoInit(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main(
             [
                 gen_client.__file__,
                 "--generate_cli",
                 "--init-file",
                 "none",
                 "--infile",
                 GetTestDataPath("dns", "dns_v1.json"),
                 "--outdir",
                 tmp_dir_path,
                 "--overwrite",
                 "--root_package",
                 "google.apis",
                 "client",
             ]
         )
         expected_files = set(["dns_v1.py"]) | set(["dns_v1_client.py", "dns_v1_messages.py"])  # CLI files
         self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
Exemple #21
0
 def testGenClient_SimpleDocWithV4(self):
     with test_utils.TempDir() as tmp_dir_path:
         gen_client.main(
             [
                 gen_client.__file__,
                 "--nogenerate_cli",
                 "--infile",
                 GetTestDataPath("dns", "dns_v1.json"),
                 "--outdir",
                 tmp_dir_path,
                 "--overwrite",
                 "--apitools_version",
                 "0.4.12",
                 "--root_package",
                 "google.apis",
                 "client",
             ]
         )
         self.assertEquals(
             set(["dns_v1_client.py", "dns_v1_messages.py", "__init__.py"]), set(os.listdir(tmp_dir_path))
         )
    def testGenClient_SimpleDocEmptyInit(self):
        with test_utils.TempDir() as tmp_dir_path:
            gen_client.main([
                gen_client.__file__,
                '--init-file', 'empty',
                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
                '--outdir', tmp_dir_path,
                '--overwrite',
                '--root_package', 'google.apis',
                'client'
            ])
            expected_files = (
                set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
            self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
            init_file = _GetContent(os.path.join(tmp_dir_path, '__init__.py'))
            self.assertEqual("""\"""Package marker file.\"""

import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
""", init_file)
Exemple #23
0
def GenerateApi(base_dir, root_dir, api_name, api_version, api_config):
  """Invokes apitools generator for given api."""
  discovery_doc = api_config['discovery_doc']

  args = [gen_client.__file__]

  unelidable_request_methods = api_config.get('unelidable_request_methods')
  if unelidable_request_methods:
    args.append('--unelidable_request_methods={0}'.format(
        ','.join(api_config['unelidable_request_methods'])))

  args.extend([
      '--init-file=empty',
      '--nogenerate_cli',
      '--infile={0}'.format(os.path.join(base_dir, root_dir, discovery_doc)),
      '--outdir={0}'.format(os.path.join(base_dir, root_dir, api_name,
                                         api_version)),
      '--overwrite',
      '--apitools_version=CloudSDK',
      '--root_package',
      '{0}.{1}.{2}'.format(
          root_dir.replace('/', '.'), api_name, api_version),
      'client',
  ])
  logging.debug('Apitools gen %s', args)
  gen_client.main(args)

  package_dir = base_dir
  for subdir in [root_dir, api_name, api_version]:
    package_dir = os.path.join(package_dir, subdir)
    init_file = os.path.join(package_dir, '__init__.py')
    if not os.path.isfile(init_file):
      logging.warning('%s does not have __init__.py file, generating ...',
                      package_dir)
      with open(init_file, 'w') as f:
        f.write(_INIT_FILE_CONTENT)
    def testGeneration(self):
        for api in _API_LIST:
            with test_utils.TempDir(change_to=True):
                args = [
                    self.gen_client_binary,
                    '--client_id=12345',
                    '--client_secret=67890',
                    '--discovery_url=%s' % api,
                    '--outdir=generated',
                    '--overwrite',
                    'client',
                ]
                logging.info('Testing API %s with command line: %s', api,
                             ' '.join(args))
                retcode = gen_client.main(args)
                if retcode == 128:
                    logging.error('Failed to fetch discovery doc, continuing.')
                    continue
                self.assertEqual(0, retcode)

                sys.path.insert(0, os.path.join(os.getcwd(), 'generated'))
                # Ensure we can import the generated client.
                importlib.import_module('{}_{}_client'.format(*api.split('.')))
    def testGeneration(self):
        for api in _API_LIST:
            with test_utils.TempDir(change_to=True):
                args = [
                    self.gen_client_binary,
                    '--client_id=12345',
                    '--client_secret=67890',
                    '--discovery_url=%s' % api,
                    '--outdir=generated',
                    '--overwrite',
                    'client',
                ]
                logging.info('Testing API %s with command line: %s',
                             api, ' '.join(args))
                retcode = gen_client.main(args)
                if retcode == 128:
                    logging.error('Failed to fetch discovery doc, continuing.')
                    continue
                self.assertEqual(0, retcode)

                with tempfile.NamedTemporaryFile() as out:
                    with tempfile.NamedTemporaryFile() as err:
                        cmdline_args = [
                            os.path.join(
                                'generated', api.replace('.', '_') + '.py'),
                            'help',
                        ]
                        retcode = subprocess.call(
                            cmdline_args, stdout=out, stderr=err)
                        with open(err.name, 'rb') as f:
                            err_output = f.read()
                # appcommands returns 1 on help
                self.assertEqual(1, retcode)
                if 'Traceback (most recent call last):' in err_output:
                    err = '\n======\n%s======\n' % err_output
                    self.fail(
                        'Error raised in generated client:' + err)
    def testGeneration(self):
        for api in _API_LIST:
            with test_utils.TempDir(change_to=True):
                args = [
                    self.gen_client_binary,
                    '--client_id=12345',
                    '--client_secret=67890',
                    '--discovery_url=%s' % api,
                    '--outdir=generated',
                    '--overwrite',
                    'client',
                ]
                logging.info('Testing API %s with command line: %s',
                             api, ' '.join(args))
                retcode = gen_client.main(args)
                if retcode == 128:
                    logging.error('Failed to fetch discovery doc, continuing.')
                    continue
                self.assertEqual(0, retcode)

                with tempfile.NamedTemporaryFile() as out:
                    with tempfile.NamedTemporaryFile() as err:
                        cmdline_args = [
                            os.path.join(
                                'generated', api.replace('.', '_') + '.py'),
                            'help',
                        ]
                        retcode = subprocess.call(
                            cmdline_args, stdout=out, stderr=err)
                        with open(err.name, 'rb') as f:
                            err_output = f.read()
                # appcommands returns 1 on help
                self.assertEqual(1, retcode)
                if 'Traceback (most recent call last):' in err_output:
                    err = '\n======\n%s======\n' % err_output
                    self.fail(
                        'Error raised in generated client:' + err)
    def testGeneration(self):
        for api in _API_LIST:
            with test_utils.TempDir(change_to=True):
                args = [
                    self.gen_client_binary,
                    '--client_id=12345',
                    '--client_secret=67890',
                    '--discovery_url=%s' % api,
                    '--outdir=generated',
                    '--overwrite',
                    'client',
                ]
                logging.info('Testing API %s with command line: %s',
                             api, ' '.join(args))
                retcode = gen_client.main(args)
                if retcode == 128:
                    logging.error('Failed to fetch discovery doc, continuing.')
                    continue
                self.assertEqual(0, retcode)

                sys.path.insert(0, os.path.join(os.getcwd(), 'generated'))
                # Ensure we can import the generated client.
                importlib.import_module('{}_{}_client'.format(
                    *api.split('.')))