1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
8 from sdk_update_common
import Error
11 'http://localhost/', # For testing.
12 'https://storage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk',
15 def IsSourceValid(url
):
16 # E1101: Instance of 'ParseResult' has no 'scheme' member
17 # pylint: disable=E1101
19 given
= urlparse
.urlparse(url
)
20 for allowed_url
in SOURCE_WHITELIST
:
21 allowed
= urlparse
.urlparse(allowed_url
)
22 if (given
.scheme
== allowed
.scheme
and
23 given
.hostname
== allowed
.hostname
and
24 given
.path
.startswith(allowed
.path
)):
30 def __init__(self
, data
=None):
37 def LoadJson(self
, json_data
):
39 self
.update(json
.loads(json_data
))
40 except Exception as e
:
41 raise Error('Error reading json config:\n%s' % str(e
))
45 return json
.dumps(self
, sort_keys
=False, indent
=2)
46 except Exception as e
:
47 raise Error('Json encoding error writing config:\n%s' % e
)
49 def __getattr__(self
, name
):
53 raise AttributeError('Config does not contain: %s' % name
)
55 def __setattr__(self
, name
, value
):
58 def AddSource(self
, source
):
59 if not IsSourceValid(source
):
60 logging
.warn('Only whitelisted sources are allowed. Ignoring \"%s\".' % (
64 if source
in self
.sources
:
65 logging
.info('Source \"%s\" already in Config.' % (source
,))
67 self
.sources
.append(source
)
69 def RemoveSource(self
, source
):
70 if source
not in self
.sources
:
71 logging
.warn('Source \"%s\" not in Config.' % (source
,))
73 self
.sources
.remove(source
)
75 def RemoveAllSources(self
):
77 logging
.info('No sources to remove.')