| 1 | # -*- Mode: Python; test-case-name: moap.test.test_util_distro -*- |
|---|
| 2 | # vi:si:et:sw=4:sts=4:ts=4 |
|---|
| 3 | |
|---|
| 4 | import commands |
|---|
| 5 | import os |
|---|
| 6 | |
|---|
| 7 | import distutils.version |
|---|
| 8 | |
|---|
| 9 | from moap.util import log |
|---|
| 10 | |
|---|
| 11 | """ |
|---|
| 12 | Figure out what distribution, architecture and version the user is on. |
|---|
| 13 | """ |
|---|
| 14 | |
|---|
| 15 | class Distro: |
|---|
| 16 | """ |
|---|
| 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 |
|---|
| 25 | """ |
|---|
| 26 | description = None |
|---|
| 27 | distributor = None |
|---|
| 28 | release = None |
|---|
| 29 | arch = None |
|---|
| 30 | |
|---|
| 31 | def __init__(self, description, distributor, release, arch): |
|---|
| 32 | self.description = description |
|---|
| 33 | self.distributor = distributor |
|---|
| 34 | self.release = release |
|---|
| 35 | self.arch = arch |
|---|
| 36 | |
|---|
| 37 | def atLeast(self, release): |
|---|
| 38 | """ |
|---|
| 39 | @param release: release to compare with |
|---|
| 40 | @type release: str |
|---|
| 41 | |
|---|
| 42 | Returns: whether the distro is at least as new as the given release, |
|---|
| 43 | taking non-numbers into account. |
|---|
| 44 | """ |
|---|
| 45 | mine = distutils.version.LooseVersion(self.release) |
|---|
| 46 | theirs = distutils.version.LooseVersion(release) |
|---|
| 47 | return mine >= theirs |
|---|
| 48 | |
|---|
| 49 | def getSysName(): |
|---|
| 50 | """ |
|---|
| 51 | Get the system name. Typically the first item in the os.uname tuple. |
|---|
| 52 | """ |
|---|
| 53 | return os.uname()[1] |
|---|
| 54 | |
|---|
| 55 | def getMachine(): |
|---|
| 56 | """ |
|---|
| 57 | Get the machine architecture. Typically the fifth item in os.uname. |
|---|
| 58 | """ |
|---|
| 59 | return os.uname()[4] |
|---|
| 60 | |
|---|
| 61 | def getDistroFromRelease(): |
|---|
| 62 | """ |
|---|
| 63 | Decide on the distro based on the presence of a distro-specific release |
|---|
| 64 | file. |
|---|
| 65 | |
|---|
| 66 | rtype: L{Distro} or None. |
|---|
| 67 | """ |
|---|
| 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() |
|---|
| 76 | |
|---|
| 77 | return Distro(description, distributor, release, None) |
|---|