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.
6 """Encompasses a description of packages associated with package targets."""
11 def LoadJSONStripComments(file):
13 for line in file.readlines():
15 comment_index = line.find('#')
16 if comment_index != -1:
17 lines.append(line[:comment_index])
21 stripped_file = '\n'.join(lines)
22 return json.loads(stripped_file)
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)
34 raise error.Error('Invalid packages file type (%s): %s' %
35 (type(packages_file), packages_file))
37 assert isinstance(packages_json, dict), (
38 "Invalid packages file: %s" % packages_file)
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',{})
44 def IsSharedPackage(self, package_name):
45 """Returns whether or not a package is shared between all host platforms.
48 package_name: Name of a package.
50 True if a package is shared, False otherwise.
52 return package_name in self._shared_packages
54 def GetPackageTargets(self, host_platform, host_arch):
55 """Returns a list of package targets for a given host.
58 host_platform: Normalized host platform name from pynacl.platform.
59 host_arch: Normalized host architecture name from pynacl.platform.
61 List of package targets for platform/arch, empty list if not defined.
63 return self._platform_targets.get(host_platform, {}).get(host_arch, [])
65 def GetPackages(self, package_target):
66 """Returns the list of packages for a given package target.
69 package_target: Valid package target as defined in the packages file.
71 List of packages for a package target. None if invalid package target.
73 return self._packages.get(package_target, None)
75 def GetPackageTargetsForPackage(self, package):
76 """Returns a list of all host platforms which is a toolchain component.
79 component: A toolchain component.
81 List of host platforms which use the toolchain component.
83 return [package_target
84 for package_target, packages
85 in self._packages.iteritems()
86 if package in packages]