aboutsummaryrefslogtreecommitdiff
blob: df767ba46e1e294429aa6e1128c5dd34c47a6d92 (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
# R overlay -- simple dependency rules
# -*- coding: utf-8 -*-
# Copyright (C) 2012 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.

"""simple dependency rules

This module provides 4 simple dependency rules classes
* SimpleDependencyRule
* SimpleIgnoreDependencyRule
* SimpleFuzzyDependencyRule
* SimpleFuzzyIgnoreDependencyRule

(TODO NOTE: could describe rule matching here)
"""

__all__ = (
   'SimpleIgnoreDependencyRule', 'SimpleDependencyRule',
   'SimpleFuzzyDependencyRule', 'SimpleFuzzyIgnoreDependencyRule'
)


from roverlay.depres.simpledeprule.util import \
   RuleFileSyntaxError, get_slot_restrict, get_slot_parser

from roverlay.depres.simpledeprule.abstractrules import \
   SimpleRule, FuzzySimpleRule

import roverlay.depres.depenv




class RuleConstructor ( object ):

   def __init__ ( self, eapi ):
      ##self.eapi = eapi

      self.kw_ignore       = SimpleIgnoreDependencyRule.RULE_PREFIX
      self.kw_fuzzy        = SimpleFuzzyDependencyRule.RULE_PREFIX
      self.kw_fuzzy_ignore = SimpleFuzzyIgnoreDependencyRule.RULE_PREFIX
   # --- end of __init__ (...) ---

   def lookup ( self, keyworded_str ):
      get_kwless = lambda : keyworded_str[1:].lstrip() or None

      kw = keyworded_str[0]
      if kw == self.kw_ignore:
         return ( SimpleIgnoreDependencyRule, get_kwless(), {} )
      elif kw == self.kw_fuzzy_ignore:
         return ( SimpleFuzzyIgnoreDependencyRule, get_kwless(), {} )
      elif kw == self.kw_fuzzy:
         ## syntax
         ## ~<cat>/<pkg>[:[<slot option>]]*
         ##  where slot option is any of
         ##  * slot restrict (range, list)
         ##  * "with version", "open" ("*" and "=" slot operators)
         ##  * relevant version parts ("1.2.3.4" => "1" if 1, "1.2" if 2, ...)
         ##  * relevant subslot version parts ("1.2.3.4" => <SLOT>/<SUBSLOT?>)
         ##  * slot operator ("=")

         kwless          = get_kwless()
         line_components = kwless.split ( ':' )

         if len ( line_components ) < 2:
            # non-slot fuzzy rule
            return ( SimpleFuzzyDependencyRule, kwless, {} )
         else:
            kwargs = dict()
            lc_iter = iter ( line_components )
            # drop first item as it's the resolving string and not an option
            next ( lc_iter )
            for opt_str in lc_iter:
               opt, has_value, value = opt_str.partition ( '=' )

               if not opt_str:
                  # empty
                  pass
               elif opt == 'default':
                  kwargs ['slot_mode'] = 0

               elif opt == 'with_version' or opt == '+v':
                  kwargs ['slot_mode'] = 1

               elif opt == 'open':
                  kwargs ['slot_mode'] = 2

               elif ( opt == 'restrict' or opt == 'r' ) and value:
                  kwargs ['slot_restrict'] = get_slot_restrict ( value )

               elif ( opt == 'slotparts' or opt == 's' ) and value:
                  kwargs ['slotparts'] = get_slot_parser ( value )

               elif ( opt == 'subslotparts' or opt == '/' ) and value:
                  kwargs ['subslotparts'] = get_slot_parser ( value )

               elif opt_str[0] == '/' and not has_value:
                  kwargs ['subslotparts'] = get_slot_parser ( opt_str[1:] )

#               elif opt == 'operator' and value:
#                  # unsafe, could be used to inject "$(rm -rf /)" etc.
#                  kwargs ['slot_operator'] = value

               elif opt == 'wide_match':
                  if not has_value:
                     kwargs ['allow_wide_match'] = True
                  else:
                     raise NotImplementedError("wide_match value")

               elif opt == '*':
                  kwargs ['slot_operator'] = '*'

               elif not opt and has_value:
                  # "="
                  kwargs ['slot_operator'] = '='
                  pass

               else:
                  raise RuleFileSyntaxError (
                     "cannot parse option {!r} from {!r}".format (
                        opt_str, kwless
                     )
                  )
            # -- end for lc_iter

            if (
               kwargs.get ( 'slot_operator' ) == '*'
               and kwargs.get ( 'slot_mode' ) != 2
            ):
               raise RuleFileSyntaxError (
                  "The '*' slot operator needs an 'open' rule."
               )
            else:
               return (
                  SimpleFuzzySlotDependencyRule, line_components[0], kwargs
               )
         # -- end if line_components

      else:
         return ( SimpleDependencyRule, keyworded_str, {} )
   # --- end of lookup (...) ---

# --- end of RuleConstructor ---

class SimpleIgnoreDependencyRule ( SimpleRule ):

   RULE_PREFIX = '!'

   def __init__ ( self, priority=50, resolving_package=None, **kw ):
      super ( SimpleIgnoreDependencyRule, self ) . __init__ (
         logger_name = 'IGNORE_DEPS',
         resolving_package=None,
         priority=50,
         **kw
      )
# --- end of SimpleIgnoreDependencyRule ---


class SimpleDependencyRule ( SimpleRule ):

   def __init__ ( self, priority=70, resolving_package=None, **kw ):
      """Initializes a SimpleDependencyRule. This is
      a SimpleIgnoreDependencyRule extended by a portage package string.

      arguments:
      * resolving package --
      * dep_str --
      * priority --
      """
      super ( SimpleDependencyRule, self ) . __init__ (
         priority=priority,
         logger_name=resolving_package,
         resolving_package=resolving_package,
         **kw
      )
   # --- end of __init__ (...) ---

# --- end of SimpleDependencyRule ---


class SimpleFuzzyIgnoreDependencyRule ( FuzzySimpleRule ):

   RULE_PREFIX = '%'

   def __init__ ( self, priority=51, resolving_package=None, **kw ):
      super ( SimpleFuzzyIgnoreDependencyRule, self ) . __init__ (
         priority=priority,
         resolving_package=resolving_package,
         logger_name = 'FUZZY.IGNORE_DEPS',
         **kw
      )

   def handle_version_relative_match ( self, *args, **kwargs ):
      raise Exception ( "should-be unreachable code" )
   # --- end of handle_version_relative_match (...) ---

# --- end of SimpleFuzzyIgnoreDependencyRule ---


class SimpleFuzzyDependencyRule ( FuzzySimpleRule ):

   RULE_PREFIX = '~'

   def __init__ ( self, priority=72, resolving_package=None, **kw ):
      super ( SimpleFuzzyDependencyRule, self ) . __init__ (
         priority=priority,
         resolving_package=resolving_package,
         logger_name = 'FUZZY.' + resolving_package,
         **kw
      )
   # --- end of __init__ (...) ---

   def handle_version_relative_match ( self, dep_env, fuzzy ):
      ver_pkg  = self.resolving_package + '-' + fuzzy ['version']
      vmod_str = fuzzy ['version_modifier']
      vmod     = fuzzy ['vmod']

      #if vmod & dep_env.VMOD_NOT:
      if vmod == dep_env.VMOD_NE:
         # package matches, but specific version is forbidden
         # ( !<package>-<specific verion> <package> )
         return "( !={vres} {res} )".format (
            vres=ver_pkg, res=self.resolving_package
         )
      else:
         # std vmod: >=, <=, =, <, >
         return vmod_str + ver_pkg
   # --- end of handle_version_relative_match (...) ---

# --- end of SimpleFuzzyDependencyRule ---


class SimpleFuzzySlotDependencyRule ( FuzzySimpleRule ):
   # 2 slot variants
   # "slot only": resolve dep_str as <cat>/<pkg>:<slot>
   # "combined": resolve dep_str as <vmod><cat>/<pkg>-<ver>:<slot>

#   FMT_DICT = {
#      'slot' : '{slot}',
#   }

   #RULE_PREFIX        = '~'
   RULE_PREFIX         = SimpleFuzzyDependencyRule.RULE_PREFIX

   # version relation operators that should never be matched in slot dep rules
   #  (bitmask)
   VMOD_BASE_DENY_MASK = roverlay.depres.depenv.DepEnv.VMOD_NOT

   # operators that should not be matched in nonwide mode (bitmask)
   #  - in addition to the base mask -
   VMOD_WIDE_DENY_MASK = roverlay.depres.depenv.DepEnv.VMOD_GT


   def __init__ ( self,
      priority          = 71,
      resolving_package = None,
      slot_mode         = None,
      slot_restrict     = None,
      slotparts         = None,
      subslotparts      = None,
      slot_operator     = None,
      allow_wide_match  = None,
      **kw
   ):
      super ( SimpleFuzzySlotDependencyRule, self ) . __init__ (
         priority=priority,
         resolving_package = resolving_package,
         logger_name       = 'FUZZY_SLOT.' + resolving_package,
         **kw
      )

      self.mode             = 0 if slot_mode is None else slot_mode
      self.slot_restrict    = slot_restrict
      self.slot_operator    = slot_operator
      self.slotparts        = (
         get_slot_parser ("0") if slotparts is None else slotparts
      )
      self.subslotparts     = subslotparts
      self.allow_wide_match = allow_wide_match
      self.vmod_mask        = self.VMOD_BASE_DENY_MASK
      if not allow_wide_match:
         self.vmod_mask |= self.VMOD_WIDE_DENY_MASK

      if self.mode == 0:
         # "default"
         self._resolving_fmt = self.resolving_package + ':{slot}'
         if self.slot_operator:
            self._resolving_fmt += self.slot_operator

      elif self.mode == 1:
         # "with version"
         self._resolving_fmt = (
            '{vmod}' + self.resolving_package + '-{version}:{slot}'
         )
         if self.slot_operator:
            self._resolving_fmt += self.slot_operator

      elif self.mode == 2:
         # "open" slot
         if not self.slot_operator:
            self.slot_operator = '='

         del self.slot_restrict

         self._orig_resolving_package = self.resolving_package
         self.resolving_package += ':' + self.slot_operator
      else:
         raise Exception (
            "unknown fuzzy slot rule mode {}".format ( self.mode )
         )



      if self.is_selfdep:
         raise NotImplementedError ( "fuzzy slot rule must not be a selfdep." )
   # --- end of __init__ (...) ---

   def noexport ( self ):
      del self.slot_operator
      del self.allow_wide_match

      if self.slot_restrict:
         self.slot_restrict.noexport()
   # --- end of noexport (...) ---

   def get_resolving_str ( self ):
      def gen_opts():
         if self.allow_wide_match:
            yield "wide_match"

#         yield "vmod_mask={:#x}".format (
#            self.vmod_mask & ~self.VMOD_BASE_DENY_MASK
#         )

         if self.mode == 2:
            yield "open"
         else:
            if self.mode == 1:
               yield "with_version"

            if self.slot_restrict:
               yield "restrict=" + str ( self.slot_restrict )

            if self.slotparts and (
               not hasattr ( self.slotparts, '_index' )
               or  self.slotparts._index != 0
            ):
               yield "s=" + str ( self.slotparts )

            if self.subslotparts:
               yield "/" + str ( self.subslotparts )
         # -- end if
         if self.slot_operator:
            yield self.slot_operator

      return "{prefix}{resolv}:{opts}".format (
         prefix = self.RULE_PREFIX,
         resolv = (
            self._orig_resolving_package
            if hasattr ( self, '_orig_resolving_package' )
            else self.resolving_package
         ),
         opts   = ':'.join ( gen_opts() )
      )
   # --- end of get_resolving_str (...) ---

   def handle_version_relative_match ( self, dep_env, fuzzy ):
      def get_slotted_result ( dep_env, fuzzy, vmod ):
         slot_str  = None
         vslot_str = None
         slot      = self.slotparts.get_slot ( fuzzy )

         if slot is not None:
            if self.subslotparts:
               subslot = self.subslotparts.get_slot ( fuzzy )
               if subslot is not None:
                  slot_str  = slot + '/' + subslot
                  vslot_str = (
                     self.slotparts.calculate_slot ( fuzzy, slot )
                     + '/'
                     + self.subslotparts.calculate_slot ( fuzzy, subslot )
                  )
            else:
               vslot_str = self.slotparts.calculate_slot ( fuzzy, slot )
               slot_str  = slot

            if slot_str and (
               not self.slot_restrict
               or self.slot_restrict.accepts ( vslot_str )
            ):
               return self._resolving_fmt.format (
                  slot    = slot_str,
                  version = fuzzy ['version'],
                  vmod    = fuzzy ['version_modifier']
               )
            # -- end if <accepted slot>
         # -- end if <have slot>

         # explicit return
         return None
      # --- end of get_slot_result (...) ---

      res  = False
      vmod = fuzzy ['vmod']


      if vmod & self.vmod_mask:
         # can never be resolved as slot(ted) dep
         return False

##    MAYBE TODO
##    elif self.ident and dep_env.want_slotres_override(self.ident):
###        ^^^^^^^^^^?            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^?
##       want_slot_res, ... = dep_env.get_slotres_override(self.ident,...)
###                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^?
##       if not want_slot_res:
##          return False
##       elif ...:
##          ...
##

      # else might be resolvable as slot(ted) dep
      elif self.mode == 2:
         return self.resolving_package
      elif vmod & dep_env.VMOD_EQ:
         return get_slotted_result ( dep_env, fuzzy, vmod )
      else:
         return False
      # -- end if <vmod>

   # --- end of handle_version_relative_match (...) ---