~ubuntu-branches/ubuntu/vivid/python-pex/vivid

« back to all changes in this revision

Viewing changes to pex/platforms.py

  • Committer: Package Import Robot
  • Author(s): Barry Warsaw
  • Date: 2015-02-19 14:13:25 UTC
  • Revision ID: package-import@ubuntu.com-20150219141325-w62bie95l6rawuuv
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
 
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
 
3
 
 
4
from __future__ import absolute_import
 
5
 
 
6
import re
 
7
import sys
 
8
 
 
9
from pkg_resources import compatible_platforms, get_supported_platform
 
10
 
 
11
 
 
12
class Platform(object):
 
13
  class UnknownPlatformError(Exception):
 
14
    def __init__(self, platform):
 
15
      super(Platform.UnknownPlatformError, self).__init__('Unknown platform: %s' % platform)  # noqa
 
16
 
 
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 = {
 
20
    'i386': ('i386',),
 
21
    'ppc': ('ppc',),
 
22
    'x86_64': ('x86_64',),
 
23
    'ppc64': ('ppc64',),
 
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')
 
29
  }
 
30
 
 
31
  @staticmethod
 
32
  def current():
 
33
    return get_supported_platform()
 
34
 
 
35
  @staticmethod
 
36
  def python():
 
37
    return sys.version[:3]
 
38
 
 
39
  @classmethod
 
40
  def compatible(cls, package, platform):
 
41
    if package is None or platform is None or package == platform:
 
42
      return True
 
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):
 
49
      return False
 
50
    if int(package_match.group(MINOR)) > int(platform_match.group(MINOR)):
 
51
      return False
 
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))
 
61
 
 
62
  @staticmethod
 
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