summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/build_tools/sdk_tools/config.py
blob: c87be584f1554463410f078d0d012b703a78b6a9 (plain)
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
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import logging
import urlparse
from sdk_update_common import Error

SOURCE_WHITELIST = [
  'http://localhost/',  # For testing.
  'https://storage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk',
]

def IsSourceValid(url):
  # E1101: Instance of 'ParseResult' has no 'scheme' member
  # pylint: disable=E1101

  given = urlparse.urlparse(url)
  for allowed_url in SOURCE_WHITELIST:
    allowed = urlparse.urlparse(allowed_url)
    if (given.scheme == allowed.scheme and
        given.hostname == allowed.hostname and
        given.path.startswith(allowed.path)):
      return True
  return False


class Config(dict):
  def __init__(self, data=None):
    dict.__init__(self)
    if data:
      self.update(data)
    else:
      self.sources = []

  def LoadJson(self, json_data):
    try:
      self.update(json.loads(json_data))
    except Exception as e:
      raise Error('Error reading json config:\n%s' % str(e))

  def ToJson(self):
    try:
      return json.dumps(self, sort_keys=False, indent=2)
    except Exception as e:
      raise Error('Json encoding error writing config:\n%s' % e)

  def __getattr__(self, name):
    if name in self:
      return self[name]
    else:
      raise AttributeError('Config does not contain: %s' % name)

  def __setattr__(self, name, value):
    self[name] = value

  def AddSource(self, source):
    if not IsSourceValid(source):
      logging.warn('Only whitelisted sources are allowed. Ignoring \"%s\".' % (
          source,))
      return

    if source in self.sources:
      logging.info('Source \"%s\" already in Config.' % (source,))
      return
    self.sources.append(source)

  def RemoveSource(self, source):
    if source not in self.sources:
      logging.warn('Source \"%s\" not in Config.' % (source,))
      return
    self.sources.remove(source)

  def RemoveAllSources(self):
    if not self.sources:
      logging.info('No sources to remove.')
      return
    self.sources = []