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
17import os
18import subprocess
19
20_GEN_CLIENT_BINARY = 'gen_client'
21
22_SAMPLES = [
23    'bigquery_sample/bigquery_v2.json',
24    'dns_sample/dns_v1.json',
25    'iam_sample/iam_v1.json',
26    'fusiontables_sample/fusiontables_v1.json',
27    'servicemanagement_sample/servicemanagement_v1.json',
28    'storage_sample/storage_v1.json',
29]
30
31
32def _Generate(samples):
33    for sample in samples:
34        sample_dir, sample_doc = os.path.split(sample)
35        name, ext = os.path.splitext(sample_doc)
36        if ext != '.json':
37            raise RuntimeError('Expected .json discovery doc [{0}]'
38                               .format(sample))
39        api_name, api_version = name.split('_')
40        args = [
41            _GEN_CLIENT_BINARY,
42            '--infile', sample,
43            '--init-file', 'empty',
44            '--outdir={0}'.format(os.path.join(sample_dir, name)),
45            '--overwrite',
46            '--root_package',
47            'samples.{0}_sample.{0}_{1}'.format(api_name, api_version),
48            'client',
49        ]
50        subprocess.check_call(args)
51
52
53if __name__ == '__main__':
54    _Generate(_SAMPLES)
55