Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/toolkit/lsb_release.py
blob: 704c5573817d25801bf31d14afeaa8b716562b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (C) 2011 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Get LSB (Linux Standard Base) Distribution information."""

import re
import math
import subprocess
from os.path import exists


_distributor_id = None
_release = None

_DERIVATES = {
        'Trisquel': (
            'Ubuntu', [
                lambda x: '%02d.%02d' %
                    (int(float(x)) + 6, 4
                        if float(x) - int(float(x)) < 0.5 else 10),
                ],
            ),
        'LinuxMint': (
            'Ubuntu', [
                lambda x: '%02d.%02d' %
                    (math.ceil(int(x) / 2.) + 5, [4, 10][(int(x) - 1) % 2]),
                ],
            ),
        'Tuquito': (
            'Ubuntu', [
                lambda x: '%02d.%02d' %
                    (int(float(x)) + 6, 4 if float(x) == int(float(x)) else 10)
                ],
            ),
        }


def distributor_id():
    """Current distribution LSB `Distributor ID`.

    :returns:
        string value

    """
    if _distributor_id is None:
        _init()
    return _distributor_id


def release():
    """Current distribution LSB `Release`.

    :returns:
        string value

    """
    if _release is None:
        _init()
    return _release


def _init():
    global _distributor_id, _release

    def check_derivates():
        global _distributor_id, _release

        if _distributor_id not in _DERIVATES:
            return
        _distributor_id, releases = _DERIVATES[_distributor_id]
        for i in releases:
            release_value = i(_release)
            if release_value:
                break
        else:
            release_value = ''
        _release = release_value

    try:
        _distributor_id, _release = _lsb_release()
        check_derivates()
    except OSError:
        if exists('/etc/lsb-release'):
            _distributor_id, _release = _parse_lsb_release()
            check_derivates()
        if not _release:
            _distributor_id, _release = _find_lsb_release()

    return _distributor_id, _release


def _lsb_release():
    lsb_id, lsb_release = '', ''

    process = subprocess.Popen(['lsb_release', '--all'],
            stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, __ = process.communicate()

    if process.returncode == 0:
        for line in str(stdout).split('\n'):
            if ':' not in line:
                continue
            key, value = line.split(':', 1)
            if key.strip() == 'Distributor ID':
                lsb_id = value.strip()
            elif key.strip() == 'Release':
                lsb_release = value.strip()

    return lsb_id, lsb_release


def _parse_lsb_release():
    lsb_id, lsb_release = '', ''

    for line in file('/etc/lsb-release').readlines():
        key, value = line.split('=')
        value = value.strip().strip('\'"')
        if key == 'DISTRIB_ID':
            lsb_id = value
        elif key == 'DISTRIB_RELEASE':
            lsb_release = value

    return lsb_id, lsb_release


def _find_lsb_release():
    if exists('/etc/debian_version'):
        return 'Debian', file('/etc/debian_version').read().strip()

    elif exists('/etc/redhat-release'):
        line = file('/etc/redhat-release').read().strip()

        match = re.search('Fedora.*?\\W([0-9.]+)', line)
        if match is not None:
            return 'Fedora', match.group(1)

        match = re.search('CentOS.*?\\W([0-9.]+)', line)
        if match is not None:
            return 'CentOS', match.group(1)

        match = re.search('\\W([0-9.]+)', line)
        if match is not None:
            return 'RHEL', match.group(1)

    else:
        # TODO http://linuxmafia.com/faq/Admin/release-files.html
        return '', ''


if __name__ == '__main__':
    print distributor_id(), release()