Ignore:
Timestamp:
25-05-07 15:27:31 (6 years ago)
Author:
thomas
Message:
  • moap/util/deps.py:
  • moap/util/distro.py: Rewrite Distro class. Rewrite code to use lsb_release, seems more generic.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/moap/util/distro.py

    r231 r250  
    22# vi:si:et:sw=4:sts=4:ts=4 
    33 
     4import commands 
    45import os 
    5 import re 
    66 
    77import distutils.version 
     
    1515class Distro: 
    1616    """ 
    17     @cvar tag:     a short lower-case identifier for the distro 
    18     @type tag:    str 
    19     @cvar name:    a longer human-readable name for the distro 
    20     @type name:    str 
    21     @cvar version: the version of the distro 
    22     @type version: str 
    23     @cvar arch:    the architecture of the distro 
    24     @type arch:    str 
     17    @cvar description: a longer human-readable name for the distro 
     18    @type description: str 
     19    @cvar distributor: a short lower-case identifier for the distro 
     20    @type distributor: str 
     21    @cvar release:    the version of the distro 
     22    @type release:    str 
     23    @cvar arch:        the architecture of the distro 
     24    @type arch:        str 
    2525    """ 
    26     tag = None 
    27     name = None 
    28     version = None 
     26    description = None 
     27    distributor = None 
     28    release = None 
    2929    arch = None 
    3030 
    31     def __init__(self, tag, name, version, arch): 
    32         self.tag = tag 
    33         self.name = name 
    34         self.version = version 
     31    def __init__(self, description, distributor, release, arch): 
     32        self.description = description 
     33        self.distributor = distributor 
     34        self.release = release 
    3535        self.arch = arch 
    3636 
    37     def atLeast(self, version): 
     37    def atLeast(self, release): 
    3838        """ 
    39         @param version: version to compare with 
    40         @type version:  str 
     39        @param release: release to compare with 
     40        @type  release:  str 
    4141 
    42         Returns: whether the distro is at least as new as the given version, 
     42        Returns: whether the distro is at least as new as the given release, 
    4343                 taking non-numbers into account. 
    4444        """ 
    45         mine = distutils.version.LooseVersion(self.version) 
    46         theirs = distutils.version.LooseVersion(version) 
     45        mine = distutils.version.LooseVersion(self.release) 
     46        theirs = distutils.version.LooseVersion(release) 
    4747        return mine >= theirs 
    4848 
     
    6666    rtype: L{Distro} or None. 
    6767    """ 
    68 # note: keep redhat-release towards the end, since mandrake has it too (sic) 
    69 # it's a tuple of tuples so we conserve the order 
    70     mappings = ( 
    71         ('/etc/fedora-release',        'fedora'), 
    72         ('/etc/mandrake-release',      'mandriva'), 
    73         ('/etc/mandriva-release',      'mandriva'), 
    74         ('/etc/mandrakelinux-release', 'mandriva'), 
    75         ('/etc/SuSE-release',          'suse'), 
    76         ('/etc/novell-release',        'suse'), 
    77         ('/etc/redhat-release',        'redhat'), 
    78         ('/etc/debian_version',        'debian'), 
    79         ('/etc/netware-release',       'netware'), 
    80         ('/etc/slackware-version',     'slackware'), 
    81         ('/etc/yellowdog-release',     'yellowdog'), 
    82     ) 
     68    # start with lsb_release 
     69    output = commands.getoutput("lsb_release -i") 
     70    if output and output.startswith('Distributor ID:'): 
     71        distributor = output.split(':', 2)[1].strip() 
     72        output = commands.getoutput("lsb_release -d") 
     73        description = output.split(':', 2)[1].strip() 
     74        output = commands.getoutput("lsb_release -r") 
     75        release = output.split(':', 2)[1].strip() 
    8376 
    84     for path, distro in mappings: 
    85         if os.path.exists(path): 
    86             log.debug('distro', "Found release file %s" % path) 
    87             h = open(path) 
    88             contents = h.read() 
    89             h.close() 
    90             version = None 
    91             f = '_%s_getNameVersionFromRelease' % distro 
    92             if f in globals().keys(): 
    93                 log.debug('distro', "Found version function %r" % f) 
    94                 name, version = globals()[f](contents) 
    95                 return Distro(distro, name, version, None) 
    96  
    97     return None 
    98  
    99 # distro-specific name and version getters 
    100 # each getter should return a list of [full name, version] 
    101 def _fedora_getNameVersionFromRelease(contents): 
    102     matcher = re.compile("^(Fedora Core) release (\d+) .*") 
    103     m = matcher.search(contents) 
    104     if m: 
    105         log.debug('distro', "Found fedora version") 
    106         return (m.expand("\\1"), m.expand("\\2")) 
    107  
    108     log.debug('distro', "Did not find fedora version") 
    109     return None 
     77        return Distro(description, distributor, release, None) 
Note: See TracChangeset for help on using the changeset viewer.