Package rhnpush :: Module rhnpush_confmanager
[hide private]
[frames] | no frames]

Source Code for Module rhnpush.rhnpush_confmanager

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import sys 
 17  import os 
 18  from rhnpush import rhnpush_config 
 19  from rhnpush import utils 
 20   
 21   
22 -class ConfManager:
23
24 - def __init__(self, optionparser, store_true_list):
25 sysdir = '/etc/sysconfig/rhn' 26 homedir = utils.get_home_dir() 27 default = 'rhnpushrc' 28 regular = '.rhnpushrc' 29 deffile = os.path.join(sysdir, default) 30 regfile = os.path.join(homedir, regular) 31 cwdfile = os.path.join(os.getcwd(), regular) 32 33 self.cfgFileList = [deffile, regfile, cwdfile] 34 self.defaultconfig = rhnpush_config.rhnpushConfigParser(ensure_consistency=True) 35 36 # Get a reference to the object containing command-line options 37 self.cmdconfig = optionparser 38 self.store_true_list = store_true_list
39 40 # Change the files options of the self.userconfig 41 # Change the exclude options of the self.userconfig
42 - def _files_to_list(self):
43 # Change the files options to lists. 44 if ('files' in self.defaultconfig.__dict__ and 45 not isinstance(self.defaultconfig.files, type([]))): 46 self.defaultconfig.files = [x.strip() for x in 47 self.defaultconfig.files.split(',')] 48 49 # Change the exclude options to list. 50 if ('exclude' in self.defaultconfig.__dict__ and 51 not isinstance(self.defaultconfig.__dict__['exclude'], type([]))): 52 self.defaultconfig.exclude = [x.strip() for x in 53 self.defaultconfig.exclude.split(',')]
54
55 - def get_config(self):
56 for f in self.cfgFileList: 57 if os.access(f, os.F_OK): 58 if not os.access(f, os.R_OK): 59 print(("rhnpush does not have read permission on %s" % f)) 60 sys.exit(1) 61 config2 = rhnpush_config.rhnpushConfigParser(f) 62 self.defaultconfig, config2 = utils.make_common_attr_equal(self.defaultconfig, config2) 63 64 self._files_to_list() 65 66 # Change the channel string into a list of strings. 67 # pylint: disable=E1103 68 if not self.defaultconfig.channel: 69 # if no channel then make it null array instead of 70 # an empty string array from of size 1 [''] . 71 self.defaultconfig.channel = [] 72 else: 73 self.defaultconfig.channel = [x.strip() for x in 74 self.defaultconfig.channel.split(',')] 75 76 # Get the command line arguments. These take precedence over the other settings 77 argoptions, files = self.cmdconfig.parse_args() 78 79 # Makes self.defaultconfig compatible with argoptions by changing all '0' value attributes to None. 80 _zero_to_none(self.defaultconfig, self.store_true_list) 81 82 # If verbose isn't set at the command-line, it automatically gets set to zero. If it's at zero, change it to 83 # None so the settings in the config files take precedence. 84 if argoptions.verbose == 0: 85 argoptions.verbose = None 86 87 # Orgid, count, cache_lifetime, and verbose all need to be integers, just like in argoptions. 88 if self.defaultconfig.orgid: 89 self.defaultconfig.orgid = int(self.defaultconfig.orgid) 90 91 if self.defaultconfig.count: 92 self.defaultconfig.count = int(self.defaultconfig.count) 93 94 if self.defaultconfig.cache_lifetime: 95 self.defaultconfig.cache_lifetime = int(self.defaultconfig.cache_lifetime) 96 97 if self.defaultconfig.verbose: 98 self.defaultconfig.verbose = int(self.defaultconfig.verbose) 99 100 if self.defaultconfig.timeout: 101 self.defaultconfig.timeout = int(self.defaultconfig.timeout) 102 103 # Copy the settings in argoptions into self.defaultconfig. 104 self.defaultconfig, argoptions = utils.make_common_attr_equal(self.defaultconfig, argoptions) 105 106 # Make sure files is in the correct format. 107 if self.defaultconfig.files != files: 108 self.defaultconfig.files = files 109 110 return self.defaultconfig
111 112 113 # Changes every option in config that is also in store_true_list that is set to '0' to None
114 -def _zero_to_none(config, store_true_list):
115 for opt in config.keys(): 116 for cmd in store_true_list: 117 if str(opt) == cmd and config.__dict__[opt] == '0': 118 config.__dict__[opt] = None
119