Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: third_party/gsutil/boto/boto/pyami/config.py

Issue 12317103: Added gsutil to depot tools (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
2 # Copyright (c) 2011 Chris Moyer http://coredumped.org/
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22 #
23 import StringIO, os, re
24 import warnings
25 import ConfigParser
26 import boto
27
28 # If running in Google App Engine there is no "user" and
29 # os.path.expanduser() will fail. Attempt to detect this case and use a
30 # no-op expanduser function in this case.
31 try:
32 os.path.expanduser('~')
33 expanduser = os.path.expanduser
34 except (AttributeError, ImportError):
35 # This is probably running on App Engine.
36 expanduser = (lambda x: x)
37
38 # By default we use two locations for the boto configurations,
39 # /etc/boto.cfg and ~/.boto (which works on Windows and Unix).
40 BotoConfigPath = '/etc/boto.cfg'
41 BotoConfigLocations = [BotoConfigPath]
42 UserConfigPath = os.path.join(expanduser('~'), '.boto')
43 BotoConfigLocations.append(UserConfigPath)
44
45 # If there's a BOTO_CONFIG variable set, we load ONLY
46 # that variable
47 if 'BOTO_CONFIG' in os.environ:
48 BotoConfigLocations = [expanduser(os.environ['BOTO_CONFIG'])]
49
50 # If there's a BOTO_PATH variable set, we use anything there
51 # as the current configuration locations, split with colons
52 elif 'BOTO_PATH' in os.environ:
53 BotoConfigLocations = []
54 for path in os.environ['BOTO_PATH'].split(":"):
55 BotoConfigLocations.append(expanduser(path))
56
57
58 class Config(ConfigParser.SafeConfigParser):
59
60 def __init__(self, path=None, fp=None, do_load=True):
61 ConfigParser.SafeConfigParser.__init__(self, {'working_dir' : '/mnt/pyam i',
62 'debug' : '0'})
63 if do_load:
64 if path:
65 self.load_from_path(path)
66 elif fp:
67 self.readfp(fp)
68 else:
69 self.read(BotoConfigLocations)
70 if "AWS_CREDENTIAL_FILE" in os.environ:
71 full_path = expanduser(os.environ['AWS_CREDENTIAL_FILE'])
72 try:
73 self.load_credential_file(full_path)
74 except IOError:
75 warnings.warn('Unable to load AWS_CREDENTIAL_FILE (%s)' % fu ll_path)
76
77 def load_credential_file(self, path):
78 """Load a credential file as is setup like the Java utilities"""
79 c_data = StringIO.StringIO()
80 c_data.write("[Credentials]\n")
81 for line in open(path, "r").readlines():
82 c_data.write(line.replace("AWSAccessKeyId", "aws_access_key_id").rep lace("AWSSecretKey", "aws_secret_access_key"))
83 c_data.seek(0)
84 self.readfp(c_data)
85
86 def load_from_path(self, path):
87 file = open(path)
88 for line in file.readlines():
89 match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line)
90 if match:
91 extended_file = match.group(1)
92 (dir, file) = os.path.split(path)
93 self.load_from_path(os.path.join(dir, extended_file))
94 self.read(path)
95
96 def save_option(self, path, section, option, value):
97 """
98 Write the specified Section.Option to the config file specified by path.
99 Replace any previous value. If the path doesn't exist, create it.
100 Also add the option the the in-memory config.
101 """
102 config = ConfigParser.SafeConfigParser()
103 config.read(path)
104 if not config.has_section(section):
105 config.add_section(section)
106 config.set(section, option, value)
107 fp = open(path, 'w')
108 config.write(fp)
109 fp.close()
110 if not self.has_section(section):
111 self.add_section(section)
112 self.set(section, option, value)
113
114 def save_user_option(self, section, option, value):
115 self.save_option(UserConfigPath, section, option, value)
116
117 def save_system_option(self, section, option, value):
118 self.save_option(BotoConfigPath, section, option, value)
119
120 def get_instance(self, name, default=None):
121 try:
122 val = self.get('Instance', name)
123 except:
124 val = default
125 return val
126
127 def get_user(self, name, default=None):
128 try:
129 val = self.get('User', name)
130 except:
131 val = default
132 return val
133
134 def getint_user(self, name, default=0):
135 try:
136 val = self.getint('User', name)
137 except:
138 val = default
139 return val
140
141 def get_value(self, section, name, default=None):
142 return self.get(section, name, default)
143
144 def get(self, section, name, default=None):
145 try:
146 val = ConfigParser.SafeConfigParser.get(self, section, name)
147 except:
148 val = default
149 return val
150
151 def getint(self, section, name, default=0):
152 try:
153 val = ConfigParser.SafeConfigParser.getint(self, section, name)
154 except:
155 val = int(default)
156 return val
157
158 def getfloat(self, section, name, default=0.0):
159 try:
160 val = ConfigParser.SafeConfigParser.getfloat(self, section, name)
161 except:
162 val = float(default)
163 return val
164
165 def getbool(self, section, name, default=False):
166 if self.has_option(section, name):
167 val = self.get(section, name)
168 if val.lower() == 'true':
169 val = True
170 else:
171 val = False
172 else:
173 val = default
174 return val
175
176 def setbool(self, section, name, value):
177 if value:
178 self.set(section, name, 'true')
179 else:
180 self.set(section, name, 'false')
181
182 def dump(self):
183 s = StringIO.StringIO()
184 self.write(s)
185 print s.getvalue()
186
187 def dump_safe(self, fp=None):
188 if not fp:
189 fp = StringIO.StringIO()
190 for section in self.sections():
191 fp.write('[%s]\n' % section)
192 for option in self.options(section):
193 if option == 'aws_secret_access_key':
194 fp.write('%s = xxxxxxxxxxxxxxxxxx\n' % option)
195 else:
196 fp.write('%s = %s\n' % (option, self.get(section, option)))
197
198 def dump_to_sdb(self, domain_name, item_name):
199 from boto.compat import json
200 sdb = boto.connect_sdb()
201 domain = sdb.lookup(domain_name)
202 if not domain:
203 domain = sdb.create_domain(domain_name)
204 item = domain.new_item(item_name)
205 item.active = False
206 for section in self.sections():
207 d = {}
208 for option in self.options(section):
209 d[option] = self.get(section, option)
210 item[section] = json.dumps(d)
211 item.save()
212
213 def load_from_sdb(self, domain_name, item_name):
214 from boto.compat import json
215 sdb = boto.connect_sdb()
216 domain = sdb.lookup(domain_name)
217 item = domain.get_item(item_name)
218 for section in item.keys():
219 if not self.has_section(section):
220 self.add_section(section)
221 d = json.loads(item[section])
222 for attr_name in d.keys():
223 attr_value = d[attr_name]
224 if attr_value == None:
225 attr_value = 'None'
226 if isinstance(attr_value, bool):
227 self.setbool(section, attr_name, attr_value)
228 else:
229 self.set(section, attr_name, attr_value)
OLDNEW
« no previous file with comments | « third_party/gsutil/boto/boto/pyami/bootstrap.py ('k') | third_party/gsutil/boto/boto/pyami/copybot.cfg » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698