aboutsummaryrefslogtreecommitdiff
blob: 383bb9695ed7d71171270c0b9ff6888c2506e0d1 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# R overlay -- config package, data for config file creation
# -*- 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.path
import re
import textwrap

from .entryutil import deref_entry_safe

from . import entrymap
from . import tree


ENTRY_MAP = entrymap.CONFIG_ENTRY_MAP


EMPTY_STR = ""

RE_WS           = re.compile ( '\s+' )
COMMENT_WRAPPER = textwrap.TextWrapper (
   initial_indent='# ', subsequent_indent='#  ', width=70,
)

wrap_comment = lambda a: '\n'.join (
   COMMENT_WRAPPER.wrap ( RE_WS.sub ( ' ', str ( a ) ) )
)

listlike   = lambda b: (
   hasattr ( b, '__iter__' ) and not isinstance ( b, str )
)

do_iterate   = lambda c: c if listlike ( c ) else ( c, )
iter_lines   = lambda d: map ( str, do_iterate ( d ) )
wrap_comment_lines = lambda e: '\n'.join (
   map ( wrap_comment, do_iterate ( e ) )
)

def _fspath_prefix_func ( *prefix ):
   _PREFIX = os.path.join ( *prefix )
   def wrapped ( *p ):
      if p:
         return os.path.join ( _PREFIX, *p ).rstrip ( os.path.sep )
      else:
         return _PREFIX
   # --- end of wrapped (...) ---

   return wrapped
# --- end of _fspath_prefix_func (...) ---


class ConfigOptionMissing ( KeyError ):
   def __init__ ( self, key ):
      super ( ConfigOptionMissing, self ).__init__ (
         "{!r} has to be added first".format ( key )
      )

class ConfigValueError ( ValueError ):
   def __init__ ( self, key, value ):
      super ( ConfigValueError, self ).__init__ (
         "invalid value {!r} for option {!r}".format ( value, key )
      )

class ConfigOption ( object ):
   def __init__ ( self,
      name, default=None, required=None, recommended=False, description=None,
      use_default_desc=True, comment_default=False, append_newline=True,
      defaults_to=None
   ):
      self.name             = name
      self.map_entry        = deref_entry_safe ( name )
      self.default          = default
      self.required         = required or (
         not recommended if required is None else False
      )
      self.recommended      = recommended
      self.description      = description
      self.use_default_desc = use_default_desc
      self.comment_default  = comment_default
      self.append_newline   = append_newline
      self.value            = None
      self.defaults_to      = defaults_to
      self.comment_value    = False

      #self.is_set = False
   # --- end of __init__ (...) ---

   def set_value ( self, value ):
      self.value  = value
      #self.is_set = True

   def gen_str ( self ):
      entry = self.map_entry[1]

      if self.value is None:
         using_default = True
         self.value = self.default
#      elif self.value == self.default:
#         using_default = True
      else:
         using_default = False


      if using_default and self.comment_default:
         if self.required:
            yield wrap_comment ( "{} has to be set".format ( self.name ) )
         elif self.recommended:
            yield wrap_comment ( "{} should be set".format ( self.name ) )


      if self.use_default_desc:
         desc = entry.get ( 'description' ) or entry.get ( 'desc' )
         if desc:
            yield wrap_comment_lines ( desc )

      if self.description is True:
         yield wrap_comment ( self.name )
      elif self.description:
         yield wrap_comment_lines ( self.description )

      if self.defaults_to:
         if self.defaults_to is True:
            yield '#  Defaults to \"{}\".'.format ( self.default )
         elif listlike ( self.defaults_to ):
            yield '#  Defaults to \"{}\" ({}).'.format ( *self.defaults_to )
         else:
            yield '#  Defaults to \"{}\".'.format ( self.defaults_to )


      if self.comment_value or ( self.comment_default and using_default ):
         yield "#{k}=\"{v}\"".format ( k=self.name, v=self.value )
      else:
         yield "{k}=\"{v}\"".format ( k=self.name, v=self.value )

      if self.append_newline:
         yield EMPTY_STR
   # --- end of gen_str (...) ---

   def __str__ ( self ):
      return '\n'.join ( self.gen_str() )
   # --- end of __str__ (...) ---

# --- end of ConfigOption ---


class RoverlayConfigCreation ( object ):

   def __init__ ( self,
      is_installed, target_type,
      work_root     = '~/roverlay',
      data_root     = '/usr/share/roverlay',
      conf_root     = '/etc/roverlay',
      additions_dir = '/etc/roverlay/files',
   ):
      self.work_root         = work_root
      self.data_root         = data_root
      self.conf_root         = conf_root
      self.additions_dir     = additions_dir

      self.get_workdir       = _fspath_prefix_func ( self.work_root )
      self.get_datadir       = _fspath_prefix_func ( self.data_root )
      self.get_confdir       = _fspath_prefix_func ( self.conf_root )
      self.get_additions_dir = _fspath_prefix_func ( self.additions_dir )

      self._ctree            = tree.ConfigTree()
      self._cloader          = self._ctree.get_loader()
      self._verify_value     = self._cloader._make_and_verify_value
      self.reset ( is_installed=is_installed, target_type=target_type )
   # --- end of __init__ (...) ---

   def iter_options ( self ):
      for item in self.config:
         if isinstance ( item, ConfigOption ):
            yield item

   def set_option ( self, key, value ):
      if key in self._cmap:
         option = self._cmap [key]

         if isinstance ( value, str ):
            if len ( value ) > 2 and value[1] == ',':
               v = value[0].lower()

               if v == 'w':
                  svalue = self.get_workdir ( value[2:] )
               elif v == 'd':
                  svalue = self.get_datadir ( value[2:] )
               elif v == 'c':
                  svalue = self.get_confdir ( value[2:] )
               elif v == 'a':
                  svalue = self.get_additions_dir ( value[2:] )
               else:
                  svalue = value
            else:
               svalue = value
         elif value:
            svalue = str ( value )
         else:
            svalue = None
         # -- end if, get value

         try:
            converted_value = self._verify_value (
               option.map_entry[1].get ( 'value_type' ), svalue
            )
         except ( TypeError, ValueError ):
            raise ConfigValueError ( key, value )

         if converted_value is not None:
            option.set_value ( svalue )
         elif isinstance ( value, str ) and not value:
            option.comment_value = True
         else:
            raise ConfigValueError ( key, value )
      else:
         raise ConfigOptionMissing ( key )

   def reset ( self, is_installed, target_type ):
      workdir       = self.get_workdir
      datadir       = self.get_datadir
      confdir       = self.get_confdir
      additions_dir = self.get_additions_dir

      cachedir = _fspath_prefix_func ( self.work_root, 'cache' )

      _NULLCONF = lambda *a, **b: None
      _GET_A    = lambda a, b: a
      _GET_B    = lambda a, b: b
      _T_TRUE   = ( ConfigOption, _NULLCONF,    _GET_A )
      _T_FALSE  = ( _NULLCONF,    ConfigOption, _GET_B )
      _get_setup_triple = lambda cond: ( _T_TRUE if cond else _T_FALSE )

      IF_INSTALLED, UNLESS_INSTALLED, get_inst_val = (
         _get_setup_triple ( is_installed )
      )
      IF_PORTDIR, UNLESS_PORTDIR,  get_portdir_val = (
         _get_setup_triple ( target_type.portdir )
      )
      IF_PORTAGE, UNLESS_PORTAGE, get_portage_val = (
         _get_setup_triple ( target_type.has_portage )
      )


      self.config = [
         '# R-overlay.conf',
         '#  This is roverlay\'s main config file',
         '#',
         '',
         '# --- Required Configuration ---',
         '',
         ConfigOption ( 'DISTFILES',   workdir ( 'distfiles' ) ),
         ConfigOption ( 'OVERLAY_DIR', workdir ( 'overlay' ) ),
         ConfigOption ( 'DISTDIR',     workdir ( 'mirror' ) ),
         ConfigOption (
            'LOG_FILE', workdir ( 'log/roverlay.log' ), recommended=True,
            use_default_desc=False
         ),
         ConfigOption ( 'CACHEDIR', cachedir() ),
         IF_PORTDIR (
            'PORTDIR', target_type.portdir, use_default_desc=False,
            description=(
               "portage directory",
               " used to scan for valid licenses",
            ),
         ),
         UNLESS_PORTAGE (
            'EBUILD_PROG', '/usr/bin/ebuild',
            comment_default=True, required=False, recommended=None,
            description=(
               ' optional, but required for importing hand-written ebuilds'
               ' and MANIFEST_IMPLEMENTATION=ebuild (see below)'
            ),
         ),
         '',
         '# --- Logging Configuration (optional) ---',
         '',
         ConfigOption (
            'LOG_LEVEL', get_inst_val ( 'WARNING', 'INFO' ), required=False,
            comment_default=is_installed,
         ),
         ConfigOption (
            'LOG_LEVEL_CONSOLE', get_inst_val ( 'INFO', 'WARNING' ),
            required=False, comment_default=is_installed,
            use_default_desc=False, append_newline=False,
         ),
         ConfigOption (
            'LOG_LEVEL_FILE', get_inst_val ( 'ERROR', 'WARNING' ),
            required=False, comment_default=is_installed,
            use_default_desc=False, append_newline=False,
         ),
         '',
         ConfigOption (
            'LOG_FILE_ROTATE', 'yes', required=False,
            comment_default=is_installed, use_default_desc=False,
            description='this enables per-run log files',
            # defaults_to="no"
         ),
         ConfigOption (
            'LOG_FILE_ROTATE_COUNT', '5', required=False,
            comment_default=True, use_default_desc=False,
            description='number of backup log files to keep',
            defaults_to="3"
         ),
         ConfigOption (
            'LOG_FILE_UNRESOLVABLE',
            workdir ( 'log', 'dep_unresolvable.log' ), required=False,
            comment_default=is_installed,
         ),
         '',
         '# --- Other Configuration Options ---',
         '',
         # ADDITIONS_DIR: confdir or workdir?
         ConfigOption ( 'ADDITIONS_DIR', additions_dir() ),
         ConfigOption (
            'USE_EXPAND_RENAME', additions_dir ( 'use_expand.rename' ),
            comment_default=True, required=False,
         ),
         ConfigOption (
            'USE_EXPAND_DESC', additions_dir ( 'use_expand.desc' ),
            comment_default=True, required=False,
         ),
         ConfigOption (
            'SIMPLE_RULES_FILE', confdir ( 'simple-deprules.d' ),
            use_default_desc=True, recommended=True,
            description=(
               'using the default dependency rule files',
               'Can be extended by appending other directories/files'
            ),
         ),
         ConfigOption (
            'PACKAGE_RULES', confdir ( 'package_rules' ),
         ),
         ConfigOption (
            'STATS_DB', cachedir ( 'stats.db' ),
            defaults_to=( "", "disable persistent stats" ),
         ),
         ConfigOption (
            'EVENT_HOOK', datadir ( 'hooks/mux.sh' ),
         ),
         ConfigOption (
            'EVENT_HOOK_RC', confdir ( 'hookrc' ),
         ),
         ConfigOption (
            'EVENT_HOOK_RESTRICT', '-* db_written overlay_success user',
            description=(
               'Note:',
               ' setting -user is highly recommended when running roverlay as root',
            ),
            comment_default=False, required=False,
            defaults_to=( "*", "allow all" ),
         ),
         UNLESS_INSTALLED ( 'TEMPLATE_ROOT', datadir ( 'mako_templates' ) ),
         ConfigOption (
            'LICENSE_MAP', confdir ( 'license.map' )
         ),
         ConfigOption (
            'OVERLAY_ECLASS', datadir ( 'eclass/R-packages.eclass' ),
            use_default_desc=False, recommended=True,
            description=(
               'Not required but ebuilds won\'t be functional '
               'without the eclass'
            )
         ),
         ConfigOption (
            'OVERLAY_CATEGORY', 'sci-R', defaults_to=True,
            comment_default=True, required=False, use_default_desc=False,
            description=(
               "default category for created ebuilds",
               " (usually overridden by package rules)"
            )
         ),
         ConfigOption (
            'REPO_CONFIG', confdir ( 'repo.list' ),
            use_default_desc=False,
            description='using the default repo list'
         ),
         ConfigOption (
            'FIELD_DEFINITION', confdir ( 'description_fields.conf' ),
            use_default_desc=False,
            description='using the default field definition file',
         ),
         UNLESS_INSTALLED (
            'DESCRIPTION_DIR', cachedir ( 'desc-files' ),
            comment_default=True, required=False,
            description='Note that this slows overlay creation down.',
         ),
         ConfigOption (
            'DISTDIR_STRATEGY', 'hardlink symlink',
            use_default_desc=False,
            description=(
               'using the default distdir strategy',
               ' try hard links first, then fall back to symbolic ones'
            ),
         ),
         ConfigOption (
            'DISTDIR_VERIFY', 'no', required=False, comment_default=True,
            description=' usually not needed',
         ),
         ConfigOption (
            'DISTMAP_COMPRESSION', 'bzip2', required=False,
            comment_default=True, defaults_to=True,
         ),
         ConfigOption (
            'DISTMAP_FILE', '', required=False, comment_default=True,
            defaults_to="<CACHEDIR>/distmap.db",
         ),
         ConfigOption (
            'USE_PORTAGE_LICENSES', 'no', required=False,
            comment_default=target_type.portdir, defaults_to="yes"
         ),
         UNLESS_PORTDIR (
            'LICENSES_FILE', datadir ( 'licenses' ), required=True,
            use_default_desc=False, defaults_to="<CACHEDIR>/licenses",
            description=(
               "file that lists all known licenses (one per line)"
            ),
         ),
         ConfigOption (
            'CREATE_LICENSES_FILE', 'no', required=False,
            comment_default=target_type.portdir, defaults_to="yes"
         ),
         ConfigOption (
            'NOSYNC', 'yes', required=False, comment_default=True,
            defaults_to="no",
         ),
         ConfigOption (
            'MANIFEST_IMPLEMENTATION', 'ebuild', required=False,
            use_default_desc=False, comment_default=True, defaults_to="next",
            description=(
               "Manifest file creation",
               ' Available choices are \'next\' (internal, fast)',
               ' and \'ebuild\' (using ebuild(1), slow, but failsafe).',
            ) + get_portage_val (
               (), ( ' *** \'ebuild\' needs a valid EBUILD_PROG ***', )
            )
         ),
      ]

      self._cmap = {
         c.name: c for c in self.config if isinstance ( c, ConfigOption )
      }
   # --- end of reset (...) ---

   def gen_lines ( self ):
      for item in self.config:
         if item is not None:
            yield str ( item )
   # --- end of gen_lines (...) ---

   def get_lines ( self ):
      return list ( self.gen_lines() )
   # --- end of get_lines (...) ---

   def get_str ( self, append_newline=True ):
      ret = '\n'.join ( self.gen_lines() ).rstrip()
      return ( ret + '\n' ) if append_newline else ret
   # --- end of get_str (...) ---

   def __str__ ( self ):
      return self.get_str ( False )
   # --- end of __str__ (...) ---

# --- end of RoverlayConfigCreation ---