aboutsummaryrefslogtreecommitdiff
blob: 4d0e9c260262044a6141284864b5fed857949fea (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# R overlay -- runtime env
# -*- 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 functools
import logging
import errno
import os
import sys


import roverlay.argparser
import roverlay.core
import roverlay.fsutil
import roverlay.hook
import roverlay.remote.repolist
import roverlay.stats.collector
import roverlay.util.objects
import roverlay.recipe.easylogger

import roverlay.packagerules.generators.addition_control
from roverlay.packagerules.generators.addition_control import \
   create_addition_control_package_rule

import roverlay.config.tree
import roverlay.config.const

from roverlay.core import DIE, die

# TODO: move/merge roverlay.core.DIE into runtime env


class MinimalRuntimeEnvironment ( object ):

   HLINE = 79 * '-'

   def __init__ ( self ):
      super ( MinimalRuntimeEnvironment, self ).__init__()
      self.logger = None
      self.bind_logger ( logging.getLogger() )
   # -- end of __init__ (...) ---

   def bind_logger ( self, logger ):
      self.logger       = logger
      self.log_debug    = logger.debug
      self.log_info     = logger.info
      self.log_warn     = logger.warn
      self.log_warning  = logger.warning
      self.log_error    = logger.error
      self.log_critical = logger.critical
   # --- end of bind_logger (...) ---

   @roverlay.util.objects.abstractmethod
   def setup ( self ):
      pass

   def die ( self, msg=None, code=None ):
      """
      Calls syst.exit (code:=1) after printing a message (if any).
      """
      code = 1 if code is None else code
      if msg is not None:
         sys.stderr.write ( msg + "\n" )
#      else:
#         sys.stderr.write ( "died.\n" )
      sys.exit ( code )
   # --- end of die (...) ---

# --- end of MinimalRuntimeEnvironment ---


class RuntimeEnvironmentBase ( MinimalRuntimeEnvironment ):

   ARG_PARSER_CLS  = None
   KEEP_ARG_PARSER = False

   def __init__ ( self,
      installed,
      hide_exceptions=False,
      config_file_name=roverlay.core.DEFAULT_CONFIG_FILE_NAME
   ):
      super ( RuntimeEnvironmentBase, self ).__init__()
      self.installed         = bool ( installed )
      self.hide_exceptions   = bool ( hide_exceptions )
      self.config_file_name  = str ( config_file_name )

      self.stats             = roverlay.stats.collector.static
      self.config            = None
      self.additional_config = None
      self.options           = None
      self.command           = None
   # --- end of __init__ (...) ---

   def setup ( self ):
      roverlay.core.setup_initial_logger()
      self.stats.time.begin ( "setup" )
      if self.hide_exceptions:
         try:
            self.do_setup()
         except:
            die ( "failed to initialize runtime environment." )
      else:
         self.do_setup()
      self.stats.time.end ( "setup" )
   # --- end of setup (...) ---

   def do_setup_parser ( self ):
      parser = self.ARG_PARSER_CLS (
         defaults={
            'config_file': roverlay.core.locate_config_file (
               self.installed, self.config_file_name
            )
         }
      )
      parser.setup()
      parser.parse()
      parser.do_extraconf ( self.installed, 'installed' )

      self.command           = getattr ( parser, 'command', None )
      self.options           = parser.parsed
      self.additional_config = parser.extra_conf

      if self.KEEP_ARG_PARSER:
         self.parser = parser
   # --- end of do_setup_parser (...) ---

   def do_setup_config ( self ):
      # set console logging _before_ loading the config file so that
      # ConfigTree (debug) messages are visible
      #
      console_log_level = functools.reduce (
         ( lambda d, k: dict.get ( d, k ) if d else None ),
         [ "LOG", "CONSOLE", "level" ],
         self.additional_config
      )

      if console_log_level:
         roverlay.recipe.easylogger.force_reset()
         roverlay.recipe.easylogger.setup_initial ( log_level=console_log_level )

      try:
         self.config = roverlay.core.load_config_file (
            self.options ['config_file'],
            extraconf      = self.additional_config,
            setup_logger   = self.options.get ( 'want_logging', False ),
            load_main_only = self.options.get ( 'load_main_only', True ),
         )
      except:
         if self.hide_exceptions:
            die (
               "Cannot load config file {!r}".format (
                  self.options ['config_file']
               ),
               DIE.CONFIG
            )
         else:
            raise
   # --- end of do_setup_config (...) ---

   def do_setup ( self ):
      self.do_setup_parser()
      self.do_setup_config()
   # --- end of do_setup (...) ---

# --- end of RuntimeEnvironmentBase (...) ---

class RuntimeEnvironment ( RuntimeEnvironmentBase ):

   ARG_PARSER_CLS = (
      roverlay.argparser.RoverlayMainArgumentParser.create_new_parser
   )

   def __init__ ( self, installed, *args, **kw ):
      super ( RuntimeEnvironment, self ).__init__ ( installed, *args, **kw )

      self.actions_done     = set()
      self.command          = None

      self.stats_db_file    = None
      self.want_db_commit   = False

      self._repo_list       = None
      self._overlay_creator = None
   # --- end of __init__ (...) ---

   def get_repo_list ( self ):
      if self._repo_list is None:
         self._repo_list = roverlay.remote.repolist.RepoList (
            sync_enabled   = not self.config.get_or_fail ( 'nosync' ),
            force_distroot = self.options.get ( 'force_distroot' )
         )
      return self._repo_list
   # --- end of get_repo_list (...) ---

   def get_overlay_creator ( self ):
      if self._overlay_creator is None:
         self._overlay_creator = roverlay.overlay.creator.OverlayCreator (
            skip_manifest           = not self.options ['manifest'],
            incremental             = self.options ['incremental'],
            allow_write             = self.options ['write_overlay'],
            immediate_ebuild_writes = self.options ['immediate_ebuild_writes'],
            repo_id_map = self.get_repo_list().create_repo_identifier_map(),
         )
      return self._overlay_creator
   # --- end of get_overlay_creator (...) ---

   def create_addition_control_rules ( self, default_category=None ):
      kwargs = {}
      def add_key ( k, _kwargs=kwargs, _options=self.options ):
         _kwargs [k] = _options [k]

      add_key ( "cmdline_package_revbump_on_collision" )
      add_key ( "cmdline_package_force_replace" )
      add_key ( "cmdline_package_replace_only" )

      add_key ( "file_package_extended" )
      add_key ( "file_ebuild_extended"  )

      return create_addition_control_package_rule (
         (
            default_category
               or self.config.get_or_fail ( 'OVERLAY.category' )
         ),
         **kwargs
      )
   # --- end of create_addition_control_rules (...) ---

   def add_addition_control_rules (
      self, package_rules, default_category=None
   ):
      add_control_rule = self.create_addition_control_rules (
         default_category = default_category
      )
      package_rules.append_rule ( add_control_rule )
   # --- end of add_addition_control_rules (...) ---

   def add_addition_control_to_overlay_creator ( self ):
      if not self._overlay_creator:
         raise AssertionError ( "overlay creator not initialized." )
      elif not getattr ( self._overlay_creator, 'package_rules', None ):
         raise AssertionError ( "overlay creator has no package rules." )
      # --

      self.add_addition_control_rules (
         self._overlay_creator.package_rules,
         self._overlay_creator.overlay.default_category,
      )

      # + add addition_control object [FUTURE]

   # --- end of add_addition_control_to_overlay_creator (...) ---


   def do_setup ( self ):
      self.do_setup_parser()
      self.do_setup_config()

      self.stats_db_file = self.config.get ( 'STATS.dbfile', None )

      # want_logging <=> <have a command that uses hooks>
      if self.options ['want_logging']:
         roverlay.hook.setup()
   # --- end of do_setup (...) ---

   def setup_database ( self ):
      if self.stats_db_file:
         try:
            self.stats.setup_database ( self.config )

         except OSError as oserr:
            if oserr.errno == errno.ENOENT:
               self.stats_db_file = None
               self.logger.error (
                  'database not available. '
                  'Persistent stats collection has been disabled.'
               )
               return False
            else:
               raise

         else:
            return True
      else:
         return False
   # --- end of setup_database (...) ---

   def write_database ( self, hook_event=True ):
      if self.stats_db_file and self.want_db_commit:
         self.stats.write_database()
         if hook_event:
            roverlay.hook.run ( "db_written" )
         return True
      else:
         return False
   # --- end of write_database (...) ---

   def dump_stats ( self, stream=None, force=False ):
      if force or self.options ['dump_stats']:
         cout = sys.stdout.write if stream is None else stream.write

         cout ( "\n{:-^60}\n".format ( " stats dump " ) )
         cout ( str ( self.stats ) )
         cout ( "\n{:-^60}\n".format ( " end stats dump " ) )

         return True
      else:
         return False
   # --- end of dump_stats (...) ---

   def set_action_done ( self, action ):
      self.actions_done.add ( action )
   # --- end of set_action_done (...) ---

   def action_done ( self, action ):
      return action in self.actions_done
   # --- end of action_done (...) ---

   def want_command ( self, command ):
      return command == self.command
      ##and command not in self.actions_done
   # --- end of want_command (...) ---

   def option ( self, key, fallback=None ):
      return self.options.get ( key, fallback )
   # --- end of option (...) ---

   def optionally ( self, func, key, *args, **kwargs ):
      if self.options.get ( key, False ):
         return func ( *args, **kwargs )
      else:
         return None
   # --- end of optionally (...) ---

# --- end of RuntimeEnvironment ---


class IndependentRuntimeEnvironment ( MinimalRuntimeEnvironment ):

   LOG_FORMAT = None
   LOG_LEVEL  = None

   @classmethod
   def run_default_main ( cls, *args, **kwargs ):
      instance = cls ( *args, **kwargs )
      instance.setup()
      instance.default_main()
      return instance
   # --- end of run_default_main (...) ---

   def __init__ ( self, installed=True, stdout=None, stderr=None ):
      super ( IndependentRuntimeEnvironment, self ).__init__()

      if installed is None:
         installed_env = os.environ.get ( 'ROVERLAY_INSTALLED' )

         if installed_env:
            _installed = installed_env.lower() not in {
               '0', 'no', 'n', 'false',
            }
         else:
            _installed = False

      else:
         _installed = installed

      self.CONFIG_DEFAULTS = { 'installed': _installed, }
      self.PWD_INITIAL     = None

      self.config   = self.create_new_config()
      self.parser   = None
      self.options  = None
      self.commands = None
      self.prjroot  = None

      self.stdout   = stdout if stdout is not None else sys.stdout
      self.stderr   = stderr if stderr is not None else sys.stderr
      self._info    = self.stdout.write
      self._error   = self.stderr.write
      self.info     = self._info
      self.error    = self._error

      if _installed:
         self.INSTALLINFO = self.access_constant ( 'INSTALLINFO' )
      else:
         self.INSTALLINFO = None
   # --- end of __init__ (...) ---

   @roverlay.util.objects.abstractmethod
   def default_main ( self ):
      pass
   # --- end of default_main (...) ---

   def create_new_config ( self, config_str=None, apply_defaults=True ):
      ctree = roverlay.config.tree.ConfigTree ( register_static=False )

      if apply_defaults:
         ctree.merge_with ( self.CONFIG_DEFAULTS )

      if config_str:
         ctree.get_loader().parse_config ( config_str )

      return ctree
   # --- end of create_new_config (...) ---

   def reconfigure ( self, config_str=None ):
      self.reset_config()
      if config_str:
         self.config.get_loader().parse_config ( config_str )
   # --- end of reconfigure (...) ---

   def access_constant ( self, key ):
      return roverlay.config.const.lookup ( key )
   # --- end of access_constant (...) ---

   def wants_command ( self, *commands ):
      return any ( cmd in self.commands for cmd in commands )
   # --- end of wants_command (...) ---

   def extend_config ( self, additional_config ):
      self.config.merge_with ( additional_config )
   # --- end of extend_config (...) ---

   def reset_config ( self ):
      self.config.reset()
      self.extend_config ( self.CONFIG_DEFAULTS )
   # --- end of reset_config (...) ---

   def inject_config_path ( self, path, value ):
      return self.config.inject ( path, value, suppress_log=True )
   # --- end of inject_config_path (...) ---

   def locate_project_root ( self, ignore_missing=False ):
      prjroot = os.environ.get ( 'ROVERLAY_PRJROOT', None )

      couldbe_pydir = lambda a: (
         os.path.isdir ( a ) and os.path.isfile ( a + os.sep + '__init__.py' )
      )
      couldbe_prjroot = lambda b: couldbe_pydir ( b + os.sep + 'roverlay' )

      if prjroot:
         if not couldbe_prjroot ( prjroot ):
            self.error (
               'ROVERLAY_PRJROOT={p!r} does not seem to be correct - '
               'continuing.\n'.format ( p=prjroot )
            )
      else:
         if couldbe_prjroot ( self.PWD_INITIAL ):
            prjroot = self.PWD_INITIAL
         else:
            script_file = os.path.realpath ( sys.argv[0] )
            for root in roverlay.fsutil.walk_up (
               os.path.dirname ( script_file ), topdown=False, max_iter=7
            ):
               if couldbe_prjroot ( root ):
                  prjroot = root
                  break
               # -- end if
            else:
               msg = (
                  'cannot locate roverlay\'s project root - '
                  'please export ROVERLAY_PRJROOT=<dir>'
               )
               if ignore_missing:
                  self.error ( msg )
               else:
                  self.die ( msg )
            # -- end for
         # -- end if
      # -- end if not prjroot

      return prjroot
   # --- end of locate_project_root (...) ---

   @roverlay.util.objects.abstractmethod
   def create_argparser ( self ):
      pass
   # --- end of create_argparser (...) ---

   def setup_argparser ( self ):
      parser = self.create_argparser()
      if parser is not False:
         parser.setup()
         self.parser = parser

         parser.parse()

         self.options  = parser.get_options()
         self.commands = parser.get_commands()
   # --- end of do_setup_parser (...) ---

   def setup_common ( self, allow_prjroot_missing=True ):
      self.PWD_INITIAL = os.getcwd()
      if self.is_installed():
         self.prjroot = False
      else:
         self.prjroot = self.locate_project_root (
            ignore_missing=allow_prjroot_missing
         )

      roverlay.recipe.easylogger.force_console_logging (
         log_formatter = logging.Formatter ( self.LOG_FORMAT ),
         log_level     = (
            logging.DEBUG if self.LOG_LEVEL is None else self.LOG_LEVEL
         ),
      )
      self.setup_argparser()
   # --- end of setup_common (...) ---

   def setup ( self ):
      self.setup_common()
   # --- end of setup (...) ---

   def option ( self, key, fallback=None ):
      return self.options.get ( key, fallback )
   # --- end of option (...) ---

   def optionally ( self, func, key, *args, **kwargs ):
      if self.options.get ( key, False ):
         return func ( *args, **kwargs )
      else:
         return None
   # --- end of optionally (...) ---

   def is_installed ( self ):
      return self.config.get_or_fail ( 'installed' )
   # --- end of is_installed (...) ---

   installed = property ( is_installed )

# --- end of IndependentRuntimeEnvironment ---