1# Copyright 2015 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Script to regenerate samples with latest client generator. 16 17To run: 18 19 python samples/regenerate_samples.py 20 21""" 22 23import os 24import subprocess 25import sys 26 27_SAMPLES = [ 28 'bigquery_sample/bigquery_v2.json', 29 'dns_sample/dns_v1.json', 30 'iam_sample/iam_v1.json', 31 'fusiontables_sample/fusiontables_v1.json', 32 'servicemanagement_sample/servicemanagement_v1.json', 33 'storage_sample/storage_v1.json', 34] 35 36 37def _Generate(samples): 38 # insert $PWD onto PYTHONPATH 39 insert_python_dir = os.getcwd() 40 python_path = os.environ.get('PYTHONPATH') 41 if python_path: 42 python_path = os.pathsep.join([insert_python_dir, python_path]) 43 else: 44 python_path = insert_python_dir 45 os.environ['PYTHONPATH'] = python_path 46 47 for sample in samples: 48 sample_dir, sample_doc = os.path.split(sample) 49 sample_dir = 'samples/' + sample_dir 50 name, ext = os.path.splitext(sample_doc) 51 if ext != '.json': 52 raise RuntimeError('Expected .json discovery doc [{0}]' 53 .format(sample)) 54 api_name, api_version = name.split('_') 55 args = [ 56 'python', 57 'apitools/gen/gen_client.py', 58 '--infile', 'samples/' + sample, 59 '--init-file', 'empty', 60 '--outdir={0}'.format(os.path.join(sample_dir, name)), 61 '--overwrite', 62 '--root_package', 63 'samples.{0}_sample.{0}_{1}'.format(api_name, api_version), 64 'client', 65 ] 66 sys.stderr.write('Running: {}\n'.format(' '.join(args))) 67 subprocess.check_call(args) 68 69 70if __name__ == '__main__': 71 _Generate(_SAMPLES) 72