Large-scale API cleanup
[zeroinstall.git] / zeroinstall / injector / arch.py
blobec82ba5602f9aba726e424ba356ce73b12799ab3
1 """
2 Information about the current system's architecture.
4 This module provides information about the current system. It is used to determine
5 whether an implementation is suitable for this machine, and to compare different implementations.
7 For example, it will indicate that:
9 - An i486 machine cannot run an i686 binary.
10 - An i686 machine can run an i486 binary, but would prefer an i586 one.
11 - A Windows binary cannot run on a Linux machine.
13 Each dictionary maps from a supported architecture type to a preference level. Lower numbers are
14 better, Unsupported architectures are not listed at all.
15 """
17 # Copyright (C) 2009, Thomas Leonard
18 # See the README file for details, or visit http://0install.net.
20 from zeroinstall import _
21 import os
23 # TODO: "import platform"?
25 # os_ranks and mapping are mappings from names to how good they are.
26 # 1 => Native (best)
27 # Higher numbers are worse but usable.
28 try:
29 _uname = os.uname()
30 except AttributeError:
31 # No uname. Probably Windows.
32 import sys
33 p = sys.platform
34 import platform
35 bits, linkage = platform.architecture()
36 if p == 'win32' and (bits == '' or bits == '32bit'):
37 _uname = ('Windows', 'i486')
38 elif p == 'win64' or (p == 'win32' and bits == '64bit'):
39 _uname = ('Windows', 'x86_64')
40 else:
41 _uname = (p, 'i486')
43 def canonicalize_os(os_):
44 if os_.startswith('CYGWIN_NT'):
45 os_ = 'Cygwin'
46 elif os_ == 'SunOS':
47 os_ = 'Solaris'
48 return os_
50 def _get_os_ranks(target_os):
51 target_os = canonicalize_os(target_os)
53 if target_os == 'Darwin':
54 # Special case Mac OS X, to separate it from Darwin/X
55 # (Mac OS X also includes the closed Apple frameworks)
56 if os.path.exists('/System/Library/Frameworks/Carbon.framework'):
57 target_os = 'MacOSX'
59 # Binaries compiled for _this_ OS are best...
60 os_ranks = {target_os : 1}
62 # If target_os appears in the first column of this table, all
63 # following OS types on the line will also run on this one
64 # (earlier ones preferred):
65 _os_matrix = {
66 'Cygwin': ['Windows'],
67 'MacOSX': ['Darwin'],
70 for supported in _os_matrix.get(target_os, []):
71 os_ranks[supported] = len(os_ranks) + 1
73 # At the lowest priority, try an OS-independent implementation
74 os_ranks[None] = len(os_ranks) + 1
75 return os_ranks
77 os_ranks = _get_os_ranks(_uname[0])
79 # All chosen machine-specific implementations must come from the same group
80 # Unlisted archs are in group 0
81 machine_groups = {
82 'x86_64': 64,
83 'ppc64': 64,
86 def canonicalize_machine(machine):
87 if machine == 'x86':
88 machine = 'i386'
89 elif machine == 'amd64':
90 machine = 'x86_64'
91 elif machine == 'Power Macintosh':
92 machine = 'ppc'
93 elif machine == 'i86pc':
94 machine = 'i686'
95 return machine
97 def _get_machine_ranks(target_machine):
98 target_machine = canonicalize_machine(target_machine)
100 # Binaries compiled for _this_machine are best...
101 machine_ranks = {target_machine : 0}
103 # If target_machine appears in the first column of this table, all
104 # following machine types on the line will also run on this one
105 # (earlier ones preferred):
106 _machine_matrix = {
107 'i486': ['i386'],
108 'i586': ['i486', 'i386'],
109 'i686': ['i586', 'i486', 'i386'],
110 'x86_64': ['i686', 'i586', 'i486', 'i386'],
111 'ppc': ['ppc32'],
112 'ppc64': ['ppc'],
114 for supported in _machine_matrix.get(target_machine, []):
115 machine_ranks[supported] = len(machine_ranks)
117 # At the lowest priority, try a machine-independant implementation
118 machine_ranks[None] = len(machine_ranks)
119 return machine_ranks
121 machine_ranks = _get_machine_ranks(_uname[-1])
123 class Architecture:
124 """A description of an architecture. Use by L{solver} to make sure it chooses
125 compatible versions.
126 @ivar os_ranks: supported operating systems and their desirability
127 @type os_ranks: {str: int}
128 @ivar machine_ranks: supported CPU types and their desirability
129 @type machine_ranks: {str: int}
130 @ivar child_arch: architecture for dependencies (usually C{self})
131 @type child_arch: L{Architecture}
132 @ivar use: matching values for <requires use='...'>; otherwise the dependency is ignored
133 @type use: set(str)
136 use = frozenset([None])
138 def __init__(self, os_ranks, machine_ranks):
139 self.os_ranks = os_ranks
140 self.machine_ranks = machine_ranks
141 self.child_arch = self
143 def __str__(self):
144 return _("<Arch: %(os_ranks)s %(machine_ranks)s>") % {'os_ranks': self.os_ranks, 'machine_ranks': self.machine_ranks}
146 class SourceArchitecture(Architecture):
147 """Matches source code that creates binaries for a particular architecture.
148 Note that the L{child_arch} here is the binary; source code depends on binary tools,
149 not on other source packages.
151 def __init__(self, binary_arch):
152 Architecture.__init__(self, binary_arch.os_ranks, {'src': 1})
153 self.child_arch = binary_arch
155 def get_host_architecture():
156 """Get an Architecture that matches implementations that will run on the host machine.
157 @rtype: L{Architecture}"""
158 return Architecture(os_ranks, machine_ranks)
160 def get_architecture(os, machine):
161 """Get an Architecture that matches binaries that will work on the given system.
162 @param os: OS type, or None for host's type
163 @param machine: CPU type, or None for host's type
164 @return: an Architecture object
165 @rtype: L{Architecture}"""
167 if os is None:
168 target_os_ranks = os_ranks
169 else:
170 target_os_ranks = _get_os_ranks(os)
171 if machine is None:
172 target_machine_ranks = machine_ranks
173 else:
174 target_machine_ranks = _get_machine_ranks(machine)
176 return Architecture(target_os_ranks, target_machine_ranks)