Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / build / package_version / packages_info.py
1 #!/usr/bin/python
2 # Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Encompasses a description of packages associated with package targets."""
7
8 import error
9 import json
10
11 def LoadJSONStripComments(file):
12   lines = []
13   for line in file.readlines():
14     line = line.rstrip()
15     comment_index = line.find('#')
16     if comment_index != -1:
17       lines.append(line[:comment_index])
18     else:
19       lines.append(line)
20
21   stripped_file = '\n'.join(lines)
22   return json.loads(stripped_file)
23
24
25 class PackagesInfo(object):
26   """A packages file is a JSON file describing package targets and packages."""
27   def __init__(self, packages_file):
28     if isinstance(packages_file, basestring):
29       with open(packages_file, 'rt') as f:
30         packages_json = LoadJSONStripComments(f)
31     elif isinstance(packages_file, file):
32       packages_json = LoadJSONStripComments(packages_file)
33     else:
34       raise error.Error('Invalid packages file type (%s): %s' %
35                         (type(packages_file), packages_file))
36
37     assert isinstance(packages_json, dict), (
38         "Invalid packages file: %s" % packages_file)
39
40     self._platform_targets = packages_json.get('package_targets', {})
41     self._shared_packages = set(packages_json.get('shared',[]))
42     self._packages = packages_json.get('packages',{})
43
44   def IsSharedPackage(self, package_name):
45     """Returns whether or not a package is shared between all host platforms.
46
47     Args:
48       package_name: Name of a package.
49     Returns:
50       True if a package is shared, False otherwise.
51     """
52     return package_name in self._shared_packages
53
54   def GetPackageTargets(self, host_platform, host_arch):
55     """Returns a list of package targets for a given host.
56
57     Args:
58       host_platform: Normalized host platform name from pynacl.platform.
59       host_arch: Normalized host architecture name from pynacl.platform.
60     Returns:
61       List of package targets for platform/arch, empty list if not defined.
62     """
63     return self._platform_targets.get(host_platform, {}).get(host_arch, [])
64
65   def GetPackages(self, package_target):
66     """Returns the list of packages for a given package target.
67
68     Args:
69       package_target: Valid package target as defined in the packages file.
70     Returns:
71       List of packages for a package target. None if invalid package target.
72     """
73     return self._packages.get(package_target, None)
74
75   def GetPackageTargetsForPackage(self, package):
76     """Returns a list of all host platforms which is a toolchain component.
77
78     Args:
79       component: A toolchain component.
80     Returns:
81       List of host platforms which use the toolchain component.
82     """
83     return [package_target
84             for package_target, packages
85             in self._packages.iteritems()
86             if package in packages]