def _parse_build_repos(self): """ Make list of urls using repox.url, repox.user and repox.passwd configuration file parameters from 'build' section. Validate configuration parameters. """ repos = {} # get repo settings form build section for opt in self.options('build'): if opt.startswith('repo'): try: key, name = opt.split('.') except ValueError: raise errors.ConfigError("invalid repo option: %s" % opt) if name not in ('url', 'user', 'passwdx'): raise errors.ConfigError("invalid repo option: %s" % opt) if key not in repos: repos[key] = {} if name in repos[key]: raise errors.ConfigError('Duplicate entry %s' % opt) value = self.get(opt, 'build') if name == 'passwdx': try: value = decode_passwdx(value) except (TypeError, IOError), err: raise errors.ConfigError('Error decoding %s: %s' % \ (opt, err)) repos[key]['passwd'] = value else: repos[key][name] = value
def build_profile_by_name(self, name): '''return profile object by a given section''' if not name.startswith('profile.'): raise errors.ConfigError( 'section name specified by general.profile' ' must start with string "profile.": %s' % name) if not self.has_section(name): raise errors.ConfigError('no such section: %s' % name) user = self.get_optional_item(name, 'user') password = self.get_optional_item(name, 'passwd') profile = Profile(name, user, password) obs = self.get_optional_item(name, 'obs') if obs: if not obs.startswith('obs.'): raise errors.ConfigError('obs section name should start ' 'with string "obs.": %s' % obs) obsconf = SectionConf(profile, obs, self._get_url_options(obs), self.get_optional_item(obs, 'base_prj'), self.get_optional_item(obs, 'target_prj')) profile.set_obs(obsconf) repos = self.get_optional_item(name, 'repos') if repos: for repo in repos.split(','): repo = repo.strip() if not repo.startswith('repo.'): log.warning('ignore %s, repo section name should start ' 'with string "repo."' % repo) continue repoconf = SectionConf(profile, repo, self._get_url_options(repo)) profile.add_repo(repoconf) profile.buildroot = self.get_optional_item(name, 'buildroot') if self.get_optional_item(name, 'buildconf'): profile.buildconf = os.path.expanduser( self._interpolate(self.get_optional_item(name, 'buildconf'))) if self.get_optional_item(name, 'exclude_packages'): exclude_val = self.get_optional_item(name, 'exclude_packages') for pkg in exclude_val.split(','): if pkg.strip(): profile.exclude_packages.append(pkg.strip()) return profile
def _build_profile_by_subcommand(self): '''return profile object from subcommand oriented style of config''' profile = Profile('profile.current', None, None) sec = 'remotebuild' addr = self.get_optional_item(sec, 'build_server') if addr: user = self.get_optional_item(sec, 'user') password = self.get_optional_item(sec, 'passwd') url = URL(addr, user, password) obsconf = SectionConf( profile, 'obs.%s' % sec, url, self.get_optional_item('remotebuild', 'base_prj'), self.get_optional_item('remotebuild', 'target_prj')) profile.set_obs(obsconf) repos = self._parse_build_repos() for key, item in repos: if 'url' not in item: raise errors.ConfigError("URL is not specified for %s" % key) url = URL(item['url'], item.get('user'), item.get('passwd')) repoconf = SectionConf(profile, 'repo.%s' % key, url) profile.add_repo(repoconf) return profile
def get(self, opt, section='general'): 'get item value. return plain text of password if item is passwd' if opt == 'passwd': val = self._get('passwdx', section) try: return decode_passwdx(val) except (TypeError, IOError), err: raise errors.ConfigError('passwdx:%s' % err)
def __init__(self, parent, name, url, base=None, target=None): self.parent = parent self.name = name self.base = base self.target = target user = url.user or parent.common_user password = url.password or parent.common_password try: self.url = SafeURL(url.url, user, password) except ValueError, err: raise errors.ConfigError('%s for %s' % (str(err), url.url))
def add_conf(self, fpath): """ Add new config to configmgr, and new added config file has highest priority """ if not fpath: return if not os.path.exists(fpath): raise errors.ConfigError('Configuration file %s does not '\ 'exist' % fpath) # new added conf has highest priority self._cfgfiles.insert(0, fpath) # reload config files self.load_confs()
def load_confs(self): 'reset all config values by files passed in' self._cfgparsers = [] for fpath in self._cfgfiles: cfgparser = BrainConfigParser() try: cfgparser.read_one(fpath) if cfgparser.has_section('general') and \ cfgparser.has_option('general', 'work_dir') and \ cfgparser.get('general', 'work_dir') == '.': cfgparser.set('general', 'work_dir', os.path.abspath(os.path.dirname(fpath))) except Error, err: raise errors.ConfigError('config file error:%s' % err) self._cfgparsers.append(cfgparser)
def __init__(self, fpath=None): self._cfgfiles = [] self._cfgparsers = [] if fpath: if not os.path.exists(fpath): raise errors.ConfigError('Configuration file %s does not '\ 'exist' % fpath) self._cfgfiles.append(fpath) # find the default path fpaths = self._lookfor_confs() if not fpaths: self._new_conf() fpaths = self._lookfor_confs() self._cfgfiles.extend(fpaths) self.load_confs()
def set_into_file(self, section, option, value, replace_opt=None): """When set new value, need to update the readin file lines, which can be saved back to file later. """ if not self.has_section(section): self.add_section(section) SafeConfigParser.set(self, section, option, value) if replace_opt: SafeConfigParser.remove_option(self, section, replace_opt) # If the code reach here, it means the section and key are ok try: self._set_into_file(section, option, value, replace_opt) except Exception as err: # This really shouldn't happen, we've already once parsed the file # contents successfully. raise errors.ConfigError('BUG: ' + str(err))
def _interpolate(self, value): '''do string interpolation''' general_keys = {} for opt in self.DEFAULTS['general']: if opt == 'work_dir' and self.get(opt, 'general') == '.': general_keys[opt] = os.getcwd() else: general_keys[opt] = self.get(opt, 'general') value = re.sub(r'\$\{([^}]+)\}', r'%(\1)s', value) try: value = value % general_keys except KeyError, err: raise errors.ConfigError('unknown key: %s. Supportted '\ 'keys are %s' % (str(err), ' '.join( \ self.DEFAULTS['general'].keys())))
def convert_to_new_style(self, profile): 'convert ~/.gbs.conf to new style' def dump_general(fhandler): 'dump options in general section' parser = BrainConfigParser() parser.add_section('general') parser.set('general', 'profile', profile.name) for opt in self.options('general'): val = self.get(opt) if val != self.DEFAULTS['general'].get(opt): parser.set('general', opt, val) parser.write(fhandler) fname = '~/.gbs.conf.template' try: tmp = Temp() with open(tmp.path, 'w') as fhandler: dump_general(fhandler) profile.dump(fhandler) shutil.move(tmp.path, os.path.expanduser(fname)) except IOError, err: raise errors.ConfigError(err)
key) dirty.add(cfgparser) if dirty: log.warning('plaintext password in config files will ' 'be replaced by encoded ones') self.update(dirty) def _get(self, opt, section='general'): 'get value from multi-levels of config file' for cfgparser in self._cfgparsers: try: return cfgparser.get(section, opt) except Error, err: pass raise errors.ConfigError(err) def options(self, section='general'): 'merge and return options of certain section from multi-levels' sect_found = False options = set() for cfgparser in self._cfgparsers: try: options.update(cfgparser.options(section)) sect_found = True except Error, err: pass if not sect_found: raise errors.ConfigError(err)