forked from mbrukman/cloud-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_py.py
executable file
·89 lines (69 loc) · 2.43 KB
/
config_py.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
#
# Copyright 2014 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
#
# Config processing and expansion.
import json
import sys
# Local imports
import gce
def ImportModule(module):
config = __import__(module, globals={}, locals={}, fromlist=['*'])
return config.resources
def CompileAndEvalFile(path):
module_globals = {}
module_locals = {}
# We shouldn't use "from gce import *" here due to:
# SyntaxWarning: import * only allowed at module level
# so we emulate it instead by adding all top-level symbols to globals.
for key, val in gce.__dict__.iteritems():
module_globals[key] = val
execfile(path, module_globals, module_locals)
return module_locals['resources']
class ConfigExpander(object):
def __init__(self, **kwargs):
self.__kwargs = {}
for key, value in kwargs.iteritems():
self.__kwargs[key] = value
project = None
if 'project' in self.__kwargs:
project = self.__kwargs['project']
zone = None
if 'zone' in self.__kwargs:
zone = self.__kwargs['zone']
gce.GCE.setCurrent(project=project, zone=zone)
def ExpandFile(self, file_name):
return CompileAndEvalFile(file_name)
def main(argv):
if len(argv) < 2:
sys.stderr.write('''\
Syntax: %s [python module or file]
Note: using .py suffix simplifies your code and allows skipping the boilerplate
header, e.g., "from resources import *" but uses compile() + eval().
Skipping the .py suffix causes the file to be treated as a Python modile which
requires adding the import line manually and will use __import__() as the
mechanism.
''' % argv[0])
sys.exit(1)
path = argv[1]
if path.endswith('.py'):
resources = CompileAndEvalFile(path)
else:
resources = ImportModule(path)
print('%s' % json.dumps(resources, indent=2, separators=(',', ': '), sort_keys=True))
if __name__ == '__main__':
main(sys.argv)