Updated epydoc comments.
[zeroinstall/zeroinstall-mseaborn.git] / zeroinstall / injector / arch.py
blobd22e23e2a4cdd56b523ebffcefa218e62f717cde
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) 2006, 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 _uname = os.uname()
27 os_ranks = {
28 # 'Linux' : 3, # Linux (lots of systems support emulation)
29 None : 2, # Any OS
30 _uname[0] : 1, # Current OS
33 def _get_machine_ranks():
34 # Binaries compiled for _this_machine are best...
35 this_machine = _uname[-1]
36 machine_ranks = {this_machine : 0}
38 # If this_machine appears in the first column of this table, all
39 # following machine types on the line will also run on this one
40 # (earlier ones preferred):
41 _machine_matrix = {
42 'i486': ['i386'],
43 'i586': ['i486', 'i386'],
44 'i686': ['i586', 'i486', 'i386'],
45 'ppc64': ['ppc32'],
47 for supported in _machine_matrix.get(this_machine, []):
48 machine_ranks[supported] = len(machine_ranks)
50 # At the lowest priority, try a machine-independant implementation
51 machine_ranks[None] = len(machine_ranks)
52 return machine_ranks
54 machine_ranks = _get_machine_ranks()
55 #print machine_ranks
57 class Architecture:
58 """A description of an architecture. Use by L{solver} to make sure it chooses
59 compatible versions.
60 @ivar os_ranks: supported operating systems and their desirability
61 @type os_ranks: {str: int}
62 @ivar machine_ranks: supported CPU types and their desirability
63 @type machine_ranks: {str: int}
64 @ivar child_arch: architecture for dependencies (usually C{self})
65 @type child_arch: L{Architecture}"""
66 def __init__(self, os_ranks, machine_ranks):
67 self.os_ranks = os_ranks
68 self.machine_ranks = machine_ranks
69 self.child_arch = self
71 def __str__(self):
72 return "<Arch: %s %s>" % (self.os_ranks, self.machine_ranks)
74 class SourceArchitecture(Architecture):
75 """Matches source code that creates binaries for a particular architecture.
76 Note that the L{child_arch} here is the binary; source code depends on binary tools,
77 not on other source packages.
78 """
79 def __init__(self, binary_arch):
80 Architecture.__init__(self, binary_arch.os_ranks, {'src': 1})
81 self.child_arch = binary_arch
83 def get_host_architecture():
84 """Get an Architecture that matches implementations that will run on the host machine.
85 @rtype: L{Architecture}"""
86 return Architecture(os_ranks, machine_ranks)