summaryrefslogtreecommitdiff
path: root/component.py
blob: e99dd6c449ec5bd0557c05841537d29178691dd7 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import urllib2
import re
import commands
import os
import git

try:
    import elementtree.ElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

from version import Version
try:
    from PPARequest import PPARequest
except ImportError:
    PPARequest = None
import config

LOCAL_GIT_REPOS_PATH = os.path.join(config.check_versions_dir, 'git')

class Component(object):
    """abstract class"""
    def __init__(self, name, debian_name=None):
        print name
        self.name = name

        if debian_name is None:
            self.debian_name = name
        else:
            self.debian_name = debian_name

        self.upstream = self._get_upstream_version()
        self.ubuntu = self._get_ubuntu_versions()
        self.debian = self._get_debian_versions()
        #self.jhbuild = self._get_jhbuild_versions()
        self.extras = self._get_extras_versions()

        if Component.ppa_enabled():
            self.ppa = self._get_ppa_versions()
        else:
            self.ppa = None

    def _get_upstream_version(self):
        raise NotImplemented

    def _get_jhbuild_versions(self):
        module_set = JhbuildModulesetCache.get_module_set()
        for child in module_set.getchildren():
            if child.attrib.get('id') != self.name:
                continue
            if child.attrib.get('version'):
                return {'trunk': Version(child.attrib.get('version'))}
            return {'trunk': Version(child.find('branch').attrib.get('version'))}
        return {}

    def _get_ubuntu_versions(self):
        return self._rmadison('ubuntu')

    def _get_debian_versions(self):
        return self._rmadison('debian')

    def _rmadison(self, distro):
        versions = {}
        (s, o) = commands.getstatusoutput('rmadison -u %s %s' % (distro, self.debian_name))

        for l in o.split('\n'):
            try:
                pkg, ver, distro, archi = l.split('|')
            except ValueError:
                continue

            ver = ver.strip()
            distro = distro.strip()
            distro = distro.split('/')[0]

            versions[distro] = Version(ver, debian=True)

        return versions

    def _get_ppa_versions(self):
        return PPARequest().query(self.debian_name)

    def __str__(self):
        return self.name

    @staticmethod
    def ppa_enabled():
        return 'CHECK_VERSION_LAUNCHPAD' in os.environ and PPARequest is not None

    def _get_extras_versions(self):
        out = {}
        for level in ['extras', 'extras-testing', 'extras-devel']:
            out[level] = None
            max = None

            if self.name.startswith('lib'):
                d = self.name[:4]
            else:
                d = self.name[0]

            repo = 'http://repository.maemo.org/%s/pool/fremantle/free/source/%s/%s/' % (level, d, self.name)
            reg_exp = re.compile('%s_.*(?=.orig.tar.gz">)' % self.name)

            try:
                listing = urllib2.urlopen(repo)
            except urllib2.HTTPError: # 404
                continue

            for release in reg_exp.findall(listing.read()):
                version = re.split('%s_' % self.name, release)[1]
                v = Version(version)
                if max is None or v > max:
                    max = v

            out[level] = max

        return out

class HttpComponent(Component):
    def __init__(self, name, repo, debian_name=None):
        self.repo = repo

        Component.__init__(self, name, debian_name)

    def _get_upstream_version(self):
        # FIXME: there is probably a better regexp
        reg_exp = re.compile('%s-(.*?).tar.(?:bz2|gz)"' % self.name)
        listing = urllib2.urlopen(self.repo)

        versions = []
        for version in reg_exp.findall(listing.read()):
            versions.append(Version(version))

        return max(versions)

class GitComponent(Component):
    def __init__(self, name, remote_repo, debian_name=None):
        self.remote_repo = remote_repo
        self.local_repo_path = os.path.join(LOCAL_GIT_REPOS_PATH, name)

        if os.path.exists(self.local_repo_path):
            # update repo
            os.chdir(self.local_repo_path)
            commands.getstatusoutput('git pull')
            self.local_repo = git.Repo(self.local_repo_path)
        else:
            self.local_repo = self._clone_repo()

        Component.__init__(self, name, debian_name)

    def _clone_repo(self):
        if not os.path.exists(LOCAL_GIT_REPOS_PATH):
            print "create", LOCAL_GIT_REPOS_PATH
            os.makedirs(LOCAL_GIT_REPOS_PATH)

        print "clone", self.remote_repo
        cmd = 'git clone %s %s' % (self.remote_repo, self.local_repo_path)
        status, out = commands.getstatusoutput(cmd)

        return git.Repo(self.local_repo_path)

    def _get_upstream_version(self):
        versions = []

        for tag in self.local_repo.tags:
            try:
                version = tag.name.rsplit('-', 1)[1]
            except IndexError:
                # assume '_' is used to separate version (as Empathy does)
                # FIXME: this is horrible
                version = tag.name.split('_', 1)[1]
                version = version.replace('_', '.')

            versions.append(Version(version))

        return max(versions)

class TelepathyComponent(HttpComponent):
    def __init__(self, name, debian_name=None):
        repo = 'http://telepathy.freedesktop.org/releases/%s' % name

        HttpComponent.__init__(self, name, repo, debian_name)

class FarsightComponent(HttpComponent):
    def __init__(self, name, debian_name=None):
        repo = 'http://farsight.freedesktop.org/releases/%s' % name

        HttpComponent.__init__(self, name, repo, debian_name)

class NiceComponent(HttpComponent):
    def __init__(self, name, debian_name=None):
        repo = 'http://nice.freedesktop.org/releases/'

        HttpComponent.__init__(self, name, repo, debian_name)

class PapyonComponent(HttpComponent):
    def __init__(self, name, debian_name=None):
        repo = 'http://www.freedesktop.org/software/papyon/releases/'

        HttpComponent.__init__(self, name, repo, debian_name)

class JhbuildModulesetCache:
    module_set = None
    def get_module_set(cls):
        if cls.module_set:
            return cls.module_set
        external_deps = urllib2.urlopen('http://git.gnome.org/browse/jhbuild/plain/modulesets/gnome-external-deps-2.32.modules').read()
        module_set = ET.fromstring(external_deps)
        cls.module_set = module_set
        return module_set
    get_module_set = classmethod(get_module_set)

if __name__ == '__main__':
    def display(comp):
        print comp, comp.upstream, comp.ubuntu, comp.debian, comp.ppa, comp.jhbuild, comp.extras

    display(TelepathyComponent('telepathy-gabble'))
    display(FarsightComponent('farsight2'))
    display(NiceComponent('libnice'))
    display(GitComponent('telepathy-sofiasip', 'git://git.collabora.co.uk/git/telepathy-sofiasip.git'))
    display(GitComponent('empathy', 'git://git.gnome.org/empathy'))
    display(TelepathyComponent('telepathy-mission-control', 'telepathy-mission-control-5'))
    display(PapyonComponent('papyon'))
    display(TelepathyComponent('telepathy-logger'))
    display(GitComponent('folks', 'git://git.gnome.org/folks'))