arch.py :  » Installer » Zero-Install » zeroinstall-injector-0.47 » zeroinstall » injector » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Installer » Zero Install 
Zero Install » zeroinstall injector 0.47 » zeroinstall » injector » arch.py
"""
Information about the current system's architecture.

This module provides information about the current system. It is used to determine
whether an implementation is suitable for this machine, and to compare different implementations.

For example, it will indicate that:

 - An i486 machine cannot run an i686 binary.
 - An i686 machine can run an i486 binary, but would prefer an i586 one.
 - A Windows binary cannot run on a Linux machine.

Each dictionary maps from a supported architecture type to a preference level. Lower numbers are
better, Unsupported architectures are not listed at all.
"""

# Copyright (C) 2009, Thomas Leonard
# See the README file for details, or visit http://0install.net.

from zeroinstall import _
import os

# os_ranks and mapping are mappings from names to how good they are.
# 1 => Native (best)
# Higher numbers are worse but usable.
try:
  _uname = os.uname()
except AttributeError:
  # No uname. Probably Windows.
  import sys
  p = sys.platform
  import platform
  bits, linkage = platform.architecture()
  if p == 'win32' and (bits == '' or bits == '32bit'):
    _uname = ('Windows', 'i486')
  elif p == 'win64' or (p == 'win32' and bits == '64bit'):
    _uname = ('Windows', 'x86_64')
  else:
    _uname = (p, 'i486')

def _get_os_ranks(target_os):
  if target_os.startswith('CYGWIN_NT'):
    target_os = 'Cygwin'
  elif target_os == 'SunOS':
    target_os = 'Solaris'

  # Special case Mac OS X, to separate it from Darwin/X
  # (Mac OS X also includes the closed Apple frameworks)
  if os.path.exists('/System/Library/Frameworks/Carbon.framework'):
    target_os = 'MacOSX'

  # Binaries compiled for _this_ OS are best...
  os_ranks = {target_os : 1}

  # If target_os appears in the first column of this table, all
  # following OS types on the line will also run on this one
  # (earlier ones preferred):
  _os_matrix = {
    'Cygwin': ['Windows'],
    'MacOSX': ['Darwin'],
  }

  for supported in _os_matrix.get(target_os, []):
    os_ranks[supported] = len(os_ranks) + 1

  # At the lowest priority, try an OS-independent implementation
  os_ranks[None] = len(os_ranks) + 1
  return os_ranks

os_ranks = _get_os_ranks(_uname[0])

# All chosen machine-specific implementations must come from the same group
# Unlisted archs are in group 0
machine_groups = {
  'x86_64': 64,
  'ppc64': 64,
}

def canonicalize_machine(machine):
  if machine == 'x86':
    machine = 'i386'
  elif machine == 'amd64':
    machine = 'x86_64'
  elif machine == 'Power Macintosh':
    machine = 'ppc'
  elif machine == 'i86pc':
    machine = 'i686'
  return machine

def _get_machine_ranks(target_machine):
  target_machine = canonicalize_machine(target_machine)

  # Binaries compiled for _this_machine are best...
  machine_ranks = {target_machine : 0}

  # If target_machine appears in the first column of this table, all
  # following machine types on the line will also run on this one
  # (earlier ones preferred):
  _machine_matrix = {
    'i486': ['i386'],
    'i586': ['i486', 'i386'],
    'i686': ['i586', 'i486', 'i386'],
    'x86_64': ['i686', 'i586', 'i486', 'i386'],
    'ppc': ['ppc32'],
    'ppc64': ['ppc'],
  }
  for supported in _machine_matrix.get(target_machine, []):
    machine_ranks[supported] = len(machine_ranks)

  # At the lowest priority, try a machine-independant implementation
  machine_ranks[None] = len(machine_ranks)
  return machine_ranks

machine_ranks = _get_machine_ranks(_uname[-1])

class Architecture:
  """A description of an architecture. Use by L{solver} to make sure it chooses
  compatible versions.
  @ivar os_ranks: supported operating systems and their desirability
  @type os_ranks: {str: int}
  @ivar machine_ranks: supported CPU types and their desirability
  @type machine_ranks: {str: int}
  @ivar child_arch: architecture for dependencies (usually C{self})
  @type child_arch: L{Architecture}
  @ivar use: matching values for <requires use='...'>; otherwise the dependency is ignored
  @type use: set(str)
  """

  use = frozenset([None])

  def __init__(self, os_ranks, machine_ranks):
    self.os_ranks = os_ranks
    self.machine_ranks = machine_ranks
    self.child_arch = self

  def __str__(self):
    return _("<Arch: %(os_ranks)s %(machine_ranks)s>") % {'os_ranks': self.os_ranks, 'machine_ranks': self.machine_ranks}

class SourceArchitecture(Architecture):
  """Matches source code that creates binaries for a particular architecture.
  Note that the L{child_arch} here is the binary; source code depends on binary tools,
  not on other source packages.
  """
  def __init__(self, binary_arch):
    Architecture.__init__(self, binary_arch.os_ranks, {'src': 1})
    self.child_arch = binary_arch

def get_host_architecture():
  """Get an Architecture that matches implementations that will run on the host machine.
  @rtype: L{Architecture}"""
  return Architecture(os_ranks, machine_ranks)

def get_architecture(os, machine):
  """Get an Architecture that matches binaries that will work on the given system.
  @param os: OS type, or None for host's type
  @param machine: CPU type, or None for host's type
  @return: an Architecture object
  @rtype: L{Architecture}"""

  if os is None:
    target_os_ranks = os_ranks
  else:
    target_os_ranks = _get_os_ranks(os)
  if machine is None:
    target_machine_ranks = machine_ranks
  else:
    target_machine_ranks = _get_machine_ranks(machine)

  return Architecture(target_os_ranks, target_machine_ranks)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.