summaryrefslogtreecommitdiff
path: root/portato/backend/portage/system.py
blob: b86a8f79587fccef7ff2a33a1ddcbde17f02605c (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# -*- coding: utf-8 -*-
#
# File: portato/backend/portage/system.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2010 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

from __future__ import absolute_import, with_statement

import re, os
import portage

from collections import defaultdict
import itertools as itt

from . import VERSION
from . import sets as syssets
from .package import PortagePackage
from .settings import PortageSettings
from ..system_interface import SystemInterface
from ...helper import debug, info, warning

class PortageSystem (SystemInterface):
    """This class provides access to the portage-system."""

    # pre-compile the RE removing the ".svn" and "CVS" entries
    unwantedPkgsRE = re.compile(r".*(\.svn|CVS)$")
    withBdepsRE = re.compile(r"--with-bdeps\s*( |=)\s*y")

    def __init__ (self):
        """Constructor."""
        self.settings = PortageSettings()
        portage.WORLD_FILE = os.path.join(self.settings.global_settings["ROOT"],portage.WORLD_FILE)

        self.use_descs = {}
        self.local_use_descs = defaultdict(dict)

        self.setmap = {
                self.SET_ALL : syssets.AllSet,
                self.SET_INSTALLED : syssets.InstalledSet,
                self.SET_UNINSTALLED : syssets.UninstalledSet,
                self.SET_TREE : syssets.TreeSet,
                "world" : syssets.WorldSet,
                "system" : syssets.SystemSet
                }

    def eapi_supported (self, eapi):
        return portage.eapi_is_supported(eapi)

    def has_set_support (self):
        return False

    def get_sets (self, description = False):
        if description:
            return (("world", "The world set."), ("system", "The system set."))
        else:
            return ("world", "system")

    def get_version (self):
        return "Portage %s" % portage.VERSION
    
    def new_package (self, cpv):
        return PortagePackage(cpv)

    def get_config_path (self):
        path = portage.USER_CONFIG_PATH

        if path[0] != "/":
            return os.path.join(self.settings.settings["ROOT"], path)
        else:
            return path

    def get_merge_command (self):
        return ["/usr/bin/python", "/usr/bin/emerge"]

    def get_sync_command (self):
        return self.get_merge_command()+["--sync"]

    def get_oneshot_option (self):
        return ["--oneshot"]

    def get_newuse_option (self):
        return ["--newuse"]

    def get_deep_option (self):
        return ["--deep"]

    def get_update_option (self):
        return ["--update"]

    def get_pretend_option (self):
        return ["--pretend", "--verbose"]

    def get_unmerge_option (self):
        return ["--unmerge"]

    def get_environment (self):
        default_opts = self.get_global_settings("EMERGE_DEFAULT_OPTS")
        opts = dict(os.environ)
        opts.update(TERM = "xterm") # emulate terminal :)
        opts.update(PAGER = "less") # force less

        if default_opts:
            opt_list = default_opts.split()
            changed = False

            for option in ["--ask", "-a", "--pretend", "-p"]:
                if option in opt_list:
                    opt_list.remove(option)
                    changed = True
            
            if changed:
                opts.update(EMERGE_DEFAULT_OPTS = " ".join(opt_list))

        return opts

    def cpv_matches (self, cpv, criterion):
        if portage.match_from_list(criterion, [cpv]) == []:
            return False
        else:
            return True

    def compare_versions(self, v1, v2):
        v1 = self.split_cpv(v1)
        v2 = self.split_cpv(v2)

        # if category is different
        if v1[0] != v2[0]:
            return cmp(v1[0],v2[0])
        # if name is different
        elif v1[1] != v2[1]:
            return cmp(v1[1],v2[1])
        # Compare versions
        else:
            return portage.pkgcmp(v1[1:],v2[1:])

    def with_bdeps(self):
        """Returns whether the "--with-bdeps" option is set to true.

        @returns: the value of --with-bdeps
        @rtype: boolean
        """

        settings = self.get_global_settings("EMERGE_DEFAULT_OPTS").split()
        for s in settings:
            if self.withBdepsRE.match(s):
                return True

        return False

    def find_lambda (self, name):
        """Returns the function needed by all the find_all_*-functions. Returns None if no name is given.
        
        @param name: name to build the function of
        @type name: string or RE
        @returns: 
                    1. None if no name is given
                    2. a lambda function
        @rtype: function
        """
        
        if name != None:
            if isinstance(name, str):
                return lambda x: re.match(".*"+name+".*",x, re.I)
            else: # assume regular expression
                return lambda x: name.match(x)
        else:
            return lambda x: True

    def geneticize_list (self, list_of_packages, only_cpv = False):
        """Convertes a list of cpv's into L{backend.Package}s.
        
        @param list_of_packages: the list of packages
        @type list_of_packages: string[]
        @param only_cpv: do nothing - return the passed list
        @type only_cpv: boolean
        @returns: converted list
        @rtype: PortagePackage[]
        """
        
        if not only_cpv:
            return [self.new_package(x) for x in list_of_packages]
        elif not isinstance(list_of_packages, list):
            return list(list_of_packages)
        else:
            return list_of_packages

    def get_global_settings (self, key):
        return self.settings.global_settings[key]

    def find_best (self, list, only_cpv = False):
        if only_cpv:
            return portage.best(list)
        else:
            return self.new_package(portage.best(list))

    def find_best_match (self, search_key, masked = False, only_installed = False, only_cpv = False):
        t = []
        
        if not only_installed:
            pkgSet = self.SET_TREE
        else:
            pkgSet = self.SET_INSTALLED

        t = self.find_packages(search_key, pkgSet = pkgSet, masked = masked, with_version = True, only_cpv = True)
        
        if not only_installed:
            if VERSION >= (2,1,5):
                t += [pkg.get_cpv() for pkg in self.find_packages(search_key, self.SET_INSTALLED) if not (pkg.is_testing(True) or pkg.is_masked())]
            else: # no need to run twice
                t += self.find_packages(search_key, self.SET_INSTALLED, only_cpv=True)

        if t:
            t = list(set(t))
            return self.find_best(t, only_cpv)

        return None

    def _get_set (self, pkgSet):
        pkgSet = pkgSet.lower()
        if pkgSet == "": pkgSet = self.SET_ALL

        return self.setmap[pkgSet]()

    def find_packages (self, key = "", pkgSet = SystemInterface.SET_ALL, masked = False, with_version = True, only_cpv = False):
        return self.geneticize_list(self._get_set(pkgSet).find(key, masked, with_version, only_cpv), only_cpv or not with_version)

    def list_categories (self, name = None):
        categories = self.settings.global_settings.categories
        return filter(self.find_lambda(name), categories)

    def split_cpv (self, cpv):
        try:
            cpv = portage.dep_getcpv(cpv)
        except portage.exception.InvalidAtom:
            pass

        return portage.catpkgsplit(cpv)

    def sort_package_list(self, pkglist, only_cpv = False):
        if only_cpv:
            pkglist.sort(self.compare_versions)
        else:
            pkglist.sort()
        return pkglist

    def reload_settings (self):
        self.settings.load()

    def get_new_packages (self, packages):
        """Gets a list of packages and returns the best choice for each in the portage tree.

        @param packages: the list of packages
        @type packages: string[]
        @returns: the list of packages
        @rtype: backend.Package[]
        """

        new_packages = []

        def append(crit, best, inst):
            if not best:
                return

            if not best.is_installed() and (best.is_masked() or best.is_testing(True)): # check to not update unnecessarily
                for i in inst:
                    if i.matches(crit):
                        debug("The installed %s matches %s. Discarding upgrade to masked version %s.", i.get_cpv(), crit, best.get_version())
                        return
            
            new_packages.append(best)

        for p in packages:
            inst = self.find_packages(p, self.SET_INSTALLED)
            
            best_p = self.find_best_match(p)
            if best_p is None:
                best_p = self.find_best_match(p, masked = True)
                if best_p is None:
                    warning(_("No best match for %s. It seems not to be in the tree anymore.") % p)
                    continue
                else:
                    debug("Best match for %s is masked" % p)

            if len(inst) > 1:
                myslots = set()
                splitp = p.split('[', 1) # split away the useflags
                for i in inst: # get the slots of the installed packages
                    myslots.add(i.get_package_settings("SLOT"))

                myslots.add(best_p.get_package_settings("SLOT")) # add the slot of the best package in portage
                for slot in myslots:
                    crit