Update year to 2009 in various places
[zeroinstall/zeroinstall-rsl.git] / zeroinstall / injector / arch.py
blob57f56d93ecd42436e38480cc77861633188712f2
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 import os
22 # os_ranks and mapping are mappings from names to how good they are.
23 # 1 => Native (best)
24 # Higher numbers are worse but usable.
25 try:
26 _uname = os.uname()
27 except AttributeError:
28 # No uname. Probably Windows (untested).
29 import sys
30 p = sys.platform
31 if p == 'win32':
32 _uname = ('Windows', 'i486')
33 elif p == 'win64':
34 _uname = ('Windows', 'x86_64')
35 else:
36 _uname = (p, 'i486')
38 os_ranks = {
39 # 'Linux' : 3, # Linux (lots of systems support emulation)
40 None : 2, # Any OS
41 _uname[0] : 1, # Current OS
44 # All chosen machine-specific implementations must come from the same group
45 # Unlisted archs are in group 0
46 machine_groups = {
47 'x86_64': 64,
48 'ppc64': 64,
51 def _get_machine_ranks(target_machine):
52 # Binaries compiled for _this_machine are best...
53 machine_ranks = {target_machine : 0}
55 # If target_machine appears in the first column of this table, all
56 # following machine types on the line will also run on this one
57 # (earlier ones preferred):
58 _machine_matrix = {
59 'i486': ['i386'],
60 'i586': ['i486', 'i386'],
61 'i686': ['i586', 'i486', 'i386'],
62 'x86_64': ['i686', 'i586', 'i486', 'i386'],
63 'ppc64': ['ppc32'],
65 for supported in _machine_matrix.get(target_machine, []):
66 machine_ranks[supported] = len(machine_ranks)
68 # At the lowest priority, try a machine-independant implementation
69 machine_ranks[None] = len(machine_ranks)
70 return machine_ranks
72 machine_ranks = _get_machine_ranks(_uname[-1])
73 #print machine_ranks
75 class Architecture:
76 """A description of an architecture. Use by L{solver} to make sure it chooses
77 compatible versions.
78 @ivar os_ranks: supported operating systems and their desirability
79 @type os_ranks: {str: int}
80 @ivar machine_ranks: supported CPU types and their desirability
81 @type machine_ranks: {str: int}
82 @ivar child_arch: architecture for dependencies (usually C{self})
83 @type child_arch: L{Architecture}"""
84 def __init__(self, os_ranks, machine_ranks):
85 self.os_ranks = os_ranks
86 self.machine_ranks = machine_ranks
87 self.child_arch = self
89 def __str__(self):
90 return "<Arch: %s %s>" % (self.os_ranks, self.machine_ranks)
92 class SourceArchitecture(Architecture):
93 """Matches source code that creates binaries for a particular architecture.
94 Note that the L{child_arch} here is the binary; source code depends on binary tools,
95 not on other source packages.
96 """
97 def __init__(self, binary_arch):
98 Architecture.__init__(self, binary_arch.os_ranks, {'src': 1})
99 self.child_arch = binary_arch
101 def get_host_architecture():
102 """Get an Architecture that matches implementations that will run on the host machine.
103 @rtype: L{Architecture}"""
104 return Architecture(os_ranks, machine_ranks)
106 def get_architecture(os, machine):
107 """Get an Architecture that matches binaries that will work on the given system.
108 @param os: OS type, or None for host's type
109 @param machine: CPU type, or None for host's type
110 @return: an Architecture object
111 @rtype: L{Architecture}"""
113 if os:
114 target_os_ranks = {
115 os : 1, # Perfer binaries for target OS
116 None : 2, # Otherwise, generic OS is fine
118 else:
119 target_os_ranks = os_ranks
120 if machine:
121 target_machine_ranks = _get_machine_ranks(machine)
122 else:
123 target_machine_ranks = machine_ranks
124 return Architecture(target_os_ranks, target_machine_ranks)