aboutsummaryrefslogtreecommitdiff
blob: 6e795a6e23cf5eac3d029fcc9826d5e736f5c7d2 (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
# R overlay -- overlay package, overlay additions dir
# -*- coding: utf-8 -*-
# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
# Distributed under the terms of the GNU General Public License;
# either version 2 of the License, or (at your option) any later version.

import os
import re

EMPTY_TUPLE = ()

class AdditionsDir ( object ):
   """AdditionsDir represents a filesystem directory (that does not need
   to exist).
   """

   def __init__ ( self, fspath, name=None, parent=None ):
      self.root   = str ( fspath ) if fspath else None
      self.parent = parent
      self.name   = name
   # --- end of __init__ (...) ---

   def exists ( self ):
      return self.root is not None and os.path.isdir ( self.root )
   # --- end of exists (...) ---

   __bool__    = exists
   __nonzero__ = exists

   def iter_entries ( self ):
      """Generator that yields the directory content of this dir."""
      if self.exists():
         for name in os.listdir ( self.root ):
            yield ( name, ( self.root + os.sep + name ) )
   # --- end of iter_entries (...) ---

   def __iter__ ( self ):
      return iter ( self.iter_entries() )
   # --- end of __iter__ (...) ---

   def get_child ( self, fspath, name ):
      """Returns a new instance with the given fspath/name and this object
      as parent.
      arguments:

      * fspath --
      * name   --
      """
      return self.__class__ (
         fspath = fspath,
         name   = name,
         parent = self
      )
   # --- end of get_child (...) ---

   def get_subdir ( self, relpath ):
      """Returns a new instance which represents a subdirectory of this dir.

      arguments:
      * relpath -- path of the new instance, relative to the root of this dir
      """
      if self.root:
         return self.__class__ (
            fspath = ( self.root + os.sep + relpath ),
            name   = relpath,
            parent = self
         )
      else:
         return self
   # --- end of get_subdir (...) ---

   def get_obj_subdir ( self, obj ):
      """Like get_obj_subdir(), but uses obj.name as relpath.

      arguments:
      * obj --
      """
      return self.get_subdir ( obj.name )
   # --- end of get_obj_subdir (...) ---

   def __str__ ( self ):
      return self.root or ""
   # --- end of __str__ (...) ---

# --- end of AdditionsDir ---


class _AdditionsDirView ( object ):
   """view objects implement AdditionsDir actions, e.g. find certain files."""

   def __init__ ( self, additions_dir ):
      self._additions_dir = additions_dir
   # --- end of __init__ (...) ---

   def __bool__ ( self ):
      return bool ( self._additions_dir )
   # --- end of __bool__ (...) ---

   def __nonzero__ ( self ):
      return self.__bool__()
   # --- end of __nonzero__ (...) ---

   @property
   def name ( self ):
      return self._additions_dir.name
   # --- end of name (...) ---

   def _fs_iter_regex ( self, regex ):
      """Iterates over the content of the additions dir and yields
      3-tuples ( match, path, name ) for each name that matches the given
      regex.

      arguments:
      * regex --
      """
      fre = re.compile ( regex )

      for name, fspath in self._additions_dir:
         fmatch = fre.match ( name )
         if fmatch:
            yield ( fmatch, fspath, name )
   # --- end of _fs_iter_regex (...) ---

# --- end of _AdditionsDirView ---


class _EbuildAdditionsView ( _AdditionsDirView ):

   # with leading '-'
   RE_PVR = '[-](?P<pvr>[0-9].*?([-]r[0-9]+)?)'

   def __init__ ( self, additions_dir ):
      """Ebuild additions dir view constructor.
      Also calls prepare() if declared.

      arguments:
      * additions_dir --
      """
      super ( _EbuildAdditionsView, self ).__init__ (
         additions_dir=additions_dir
      )
      if hasattr ( self, 'prepare' ): self.prepare()
   # --- end of __init__ (...) ---

# --- end of _EbuildAdditionsView ---


class EbuildView ( _EbuildAdditionsView ):
   """View object for finding/importing ebuilds."""

   RE_EBUILD_SUFFIX = '[.]ebuild'

   def has_ebuilds ( self ):
      """Returns True if there are any ebuilds that could be imported."""
      return bool ( getattr ( self, '_ebuilds', None ) )
   # --- end of has_ebuilds (...) ---

   def get_ebuilds ( self ):
      """Returns all ebuilds as list of 3-tuples ( pvr, path, name )."""
      return self._ebuilds
   # --- end of get_ebuilds (...) ---

   def has_metadata_xml ( self ):
      return bool ( getattr ( self, '_metadata_xml', None ) )
   # --- end of has_metadata_xml (...) ---

   def get_metadata_xml ( self ):
      return self._metadata_xml
   # --- end of get_metadata_xml (...) ---

   def __iter__ ( self ):
      return iter ( self.get_ebuilds() )
   # --- end of __iter__ (...) ---

   def prepare ( self ):
      """Searches for ebuilds and create self._ebuilds."""
      if self._additions_dir.exists():
         ebuilds = list()

         for fmatch, fpath, fname in self._fs_iter_regex (
            self._additions_dir.name + self.RE_PVR + self.RE_EBUILD_SUFFIX
         ):
            # deref symlinks
            ebuilds.append (
               ( fmatch.group ( 'pvr' ), os.path.abspath ( fpath ), fname )
            )

         self._ebuilds = ebuilds

         metadata_xml = self._additions_dir.root + os.sep + 'metadata.xml'
         if os.path.isfile ( metadata_xml ):
            self._metadata_xml = metadata_xml
         else:
            self._metadata_xml = None
   # --- end of prepare (...) --

# --- end of EbuildView ---


class CategoryView ( _AdditionsDirView ):
   """View object that creates EbuildView objects."""

   def iter_packages ( self ):
      for name, fspath in self._additions_dir:
         if os.path.isdir ( fspath ):
            yield EbuildView (
               self._additions_dir.get_child ( fspath, name )
            )
   # --- end of iter_packages (...) ---

   def __iter__ ( self ):
      return iter ( self.iter_packages() )
   # --- end of __iter__ (...) ---

# --- end of CategoryView ---


class CategoryRootView ( _AdditionsDirView ):
   """View object that creates CategoryView objects."""

   def iter_categories ( self ):
      for name, fspath in self._additions_dir:
         if os.path.isdir ( fspath ) and (
            '-' in name or name == 'virtual'
         ):
            yield CategoryView (
               self._additions_dir.get_child ( fspath, name )
            )
   # --- end of iter_categories (...) ---

   def __iter__ ( self ):
      return iter ( self.iter_categories() )
   # --- end of __iter__ (...) ---

# --- end of CategoryRootView ---


class PatchView ( _EbuildAdditionsView ):
   """View object for finding ebuild patches."""

   RE_PATCH_SUFFIX = '(?P<patchno>[0-9]{4})?[.]patch'

   def has_patches ( self ):
      """Returns True if one or more patches are available."""
      return bool ( getattr ( self, '_patches', None ) )
   # --- end of has_patches (...) ---

   def get_patches ( self, pvr, fallback_to_default=True ):
      """Returns a list of patches that should be applied to the ebuild
      referenced by pvr.

      arguments:
      * pvr                 -- $PVR of the ebuild
      * fallback_to_default -- return default patches if no version-specific
                               ones are available (defaults to True)
      """
      patches = self._patches.get ( pvr, None )
      if patches:
         return patches
      elif fallback_to_default:
         return getattr ( self, '_default_patches', EMPTY_TUPLE )
      else:
         return EMPTY_TUPLE
   # --- end of get_patches (...) ---

   def get_default_patches ( self ):
      """Returns the default patches."""
      return getattr ( self, '_default_patches', EMPTY_TUPLE )
   # --- end of get_default_patches (...) ---

   def prepare ( self ):
      """Searches for ebuild patch files."""
      def patchno_sort ( iterable ):
         return list (
            v[1] for v in sorted ( iterable, key=lambda k: k[0] )
         )
      # --- end of patchno_sort (...) ---

      if self._additions_dir.exists():
         # dict { pvr => *(patch_no, patch_file) }
         patches = dict()


         # find version-specific patches
         for fmatch, fpath, fname in self._fs_iter_regex (
            self._additions_dir.name + self.RE_PVR + self.RE_PATCH_SUFFIX
         ):
            patchno = fmatch.group ( 'patchno' )
            patchno = -1 if patchno is None else int ( patchno )
            pvr     = fmatch.group ( 'pvr' )
            if pvr in patches:
               patches [pvr].append ( ( patchno, fpath ) )
            else:
               patches [pvr] = [ ( patchno, fpath ) ]

         # -- end for;

         self._patches = { k: patchno_sort ( v ) for k, v in patches.items() }


         # find default patches

         default_patches = []

         for fmatch, fpath, fname in self._fs_iter_regex (
            self._additions_dir.name + self.RE_PATCH_SUFFIX
         ):
            patchno = fmatch.group ( 'patchno' )

            if patchno in self._patches:
               if len ( self._patches [patchno] ) < 2:

                  del self._patches [patchno]
                  default_patches.append (
                     ( ( -1 if patchno is None else int ( patchno ) ), fpath )
                  )
               else:
                  pass
            else:
               default_patches.append (
                  ( ( -1 if patchno is None else int ( patchno ) ), fpath )
               )
         # -- end for;

         if default_patches:
            self._default_patches = patchno_sort ( default_patches )
   # --- end of prepare (...) ---

# --- end of PatchView ---