aboutsummaryrefslogtreecommitdiff
blob: 4550e8142e540e6a669aa22cc7172fd0e827c3bf (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
# R overlay -- roverlay package, core functions
# -*- coding: utf-8 -*-
# Copyright (C) 2012, 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.

"""R overlay package

Provides roverlay initialization helpers (setup_initial_logger,
load_config_file) and some information vars (version, name, ...).
"""

__all__ = [
   'DIE', 'die', 'setup_initial_logger', 'load_config_file',
   'locate_config_file', 'default_helper_setup', 'load_locate_config_file',
]

import os
import sys
import logging

import roverlay.config
import roverlay.recipe.easylogger
import roverlay.tools.shenv
import roverlay.util.common


name        = "R_overlay"
version     = "0.3.1"

description_str = "R overlay creation (roverlay) " + version
license_str     = (
   'Copyright (C) 2012, 2013 Andr\xe9 Erdmann\n'
   'Distributed under the terms of the GNU General Public License;\n'
   'either version 2 of the License, or (at your option) any later version.\n'
)

DEFAULT_CONFIG_FILE_NAME = "R-overlay.conf"

# directories where the config file could be found if roverlay has been
# installed, in order:
# * user roverlay dir (${HOME}/roverlay)
# * system config dir /etc/roverlay
#
# Note: $PWD has been removed
#
CONFIG_DIRS = tuple ((
   (
      ( os.getenv ( 'HOME' ) or roverlay.util.common.get_home_dir ( None ) )
      + os.sep + 'roverlay'
   ),
   # os.sep is '/' if /etc exists, so don't care about that
   '/etc/roverlay',
))

class DIE ( object ):
   """Container class for various system exit 'events'."""
   NOP          =  os.EX_OK
   ERR          =  1
   BAD_USAGE    =  os.EX_USAGE
   USAGE        =  os.EX_USAGE
   ARG          =  9
   CONFIG       =  os.EX_CONFIG
   OV_CREATE    =  20
   SYNC         =  30
   CMD_LEFTOVER =  90
   IMPORT       =  91
   UNKNOWN      =  95
   INTERRUPT    = 130

   @staticmethod
   def die ( msg=None, code=None ):
      """
      Calls syst.exit (code:=DIE.ERR) after printing a message (if any).
      """
      code = DIE.ERR 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 (...) ---

# --- DIE: exit codes ---
die = DIE.die


def setup_initial_logger():
   """Sets up initial logging."""
   roverlay.recipe.easylogger.setup_initial()
# --- end of setup_initial_logger (...) ---

def force_console_logging ( *args, **kwargs ):
   return roverlay.recipe.easylogger.force_console_logging ( *args, **kwargs )
# --- end of force_console_logging (...) ---

def load_config_file (
   cfile, extraconf=None, setup_logger=True, load_main_only=False
):
   """
   Loads the config, including the field definition file.
   Sets up the logger afterwards.
   (Don't call this method more than once.)

   arguments:
   * cfile          -- path to the config file
   * extraconf      -- a dict with additional config entries that will override
                        entries read from cfile
   * setup_logger   -- set up logger (defaults to True)
   * load_main_only -- if set and True: load main config file only
                        (= do not load field def, ...)
   """
   roverlay_config = roverlay.config.access()

   confloader = roverlay_config.get_loader()

   if cfile:
      confloader.load_config ( cfile )

   if extraconf is not None:
      roverlay_config.merge_with ( extraconf )

   if setup_logger:
      roverlay.recipe.easylogger.setup ( roverlay_config )
      my_logger = logging.getLogger()
      my_logger.debug ( "roverlay version " + version )
      my_logger.debug (
         "python version {0}.{1}.{2}".format ( *sys.version_info )
      )
      del my_logger

   if not load_main_only:
      confloader.load_field_definition (
         roverlay_config.get_or_fail ( "DESCRIPTION.field_definition_file" )
      )

      confloader.load_use_expand_map (
         roverlay_config.get ( "EBUILD.USE_EXPAND.rename_file" )
      )

   return roverlay_config

# --- end of load_config_file (...) ---

def locate_config_file (
   ROVERLAY_INSTALLED, CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME
):
   # search for the config file if roverlay has been installed
   if ROVERLAY_INSTALLED:
      cfg        = None
      config_dir = None

      for config_dir in CONFIG_DIRS:
         cfg = config_dir + os.sep + CONFIG_FILE_NAME
         if os.path.isfile ( cfg ):
            return cfg

   else:
      config_dir = os.environ.get ( 'ROVERLAY_PRJROOT', os.getcwd() )

      for fname in ( CONFIG_FILE_NAME + '.local', CONFIG_FILE_NAME ):
         fpath = config_dir + os.sep + fname
         if os.path.exists ( fpath ):
            return fpath

   return None
# --- end of locate_config_file (...) ---

def load_locate_config_file (
   ROVERLAY_INSTALLED, CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME, **kw
):
   return load_config_file (
      locate_config_file ( ROVERLAY_INSTALLED, CONFIG_FILE_NAME ), **kw
   )
# --- end of load_locate_config_file (...) ---

def default_helper_setup ( ROVERLAY_INSTALLED, log_to_console=True ):
   if log_to_console is True:
      roverlay.recipe.easylogger.force_console_logging (
         log_level=logging.WARNING
      )
   elif log_to_console or (
      log_to_console == 0 and log_to_console is not False
   ):
      roverlay.recipe.easylogger.force_console_logging (
         log_level=log_to_console
      )
   else:
      setup_initial_logger()

   config = load_locate_config_file (
      ROVERLAY_INSTALLED, extraconf={ 'installed': ROVERLAY_INSTALLED, },
      setup_logger=False, load_main_only=True,
   )

   roverlay.tools.shenv.setup_env()
   return config
# --- end of default_helper_setup (...) ---