1
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
4
from __future__ import absolute_import
9
from pkg_resources import compatible_platforms, get_supported_platform
12
class Platform(object):
13
class UnknownPlatformError(Exception):
14
def __init__(self, platform):
15
super(Platform.UnknownPlatformError, self).__init__('Unknown platform: %s' % platform) # noqa
17
# It blows my mind this code is not in distutils or distribute.
18
MACOSX_VERSION_STRING = re.compile(r"macosx-(\d+)\.(\d+)-(\S+)")
19
MACOSX_PLATFORM_COMPATIBILITY = {
22
'x86_64': ('x86_64',),
24
'fat': ('i386', 'ppc'),
25
'intel': ('i386', 'x86_64'),
26
'fat3': ('i386', 'ppc', 'x86_64'),
27
'fat64': ('ppc64', 'x86_64'),
28
'universal': ('i386', 'ppc', 'ppc64', 'x86_64')
33
return get_supported_platform()
37
return sys.version[:3]
40
def compatible(cls, package, platform):
41
if package is None or platform is None or package == platform:
43
MAJOR, MINOR, PLATFORM = range(1, 4)
44
package_match = cls.MACOSX_VERSION_STRING.match(package)
45
platform_match = cls.MACOSX_VERSION_STRING.match(platform)
46
if not (package_match and platform_match):
47
return compatible_platforms(package, platform)
48
if package_match.group(MAJOR) != platform_match.group(MAJOR):
50
if int(package_match.group(MINOR)) > int(platform_match.group(MINOR)):
52
package_platform = package_match.group(PLATFORM)
53
if package_platform not in cls.MACOSX_PLATFORM_COMPATIBILITY:
54
raise cls.UnknownPlatformError(package_platform)
55
sys_platform = platform_match.group(PLATFORM)
56
if sys_platform not in cls.MACOSX_PLATFORM_COMPATIBILITY:
57
raise cls.UnknownPlatformError(sys_platform)
58
package_compatibility = set(cls.MACOSX_PLATFORM_COMPATIBILITY[package_platform])
59
system_compatibility = set(cls.MACOSX_PLATFORM_COMPATIBILITY[sys_platform])
60
return bool(package_compatibility.intersection(system_compatibility))
63
def version_compatible(package_py_version, py_version):
64
return package_py_version is None or py_version is None or package_py_version == py_version