def create(self): """Create a new test environment. This sets up Trac, calls :meth:`create_repo` and sets up authentication. """ os.mkdir(self.dirname) # testing.log gets any unused output from subprocesses self.logfile = open(os.path.join(self.dirname, 'testing.log'), 'w') self.create_repo() config_file = os.path.join(self.dirname, 'config.ini') config = Configuration(config_file) config.set('repositories', '.dir', self.repo_path_for_initenv()) config.set('repositories', '.type', self.repotype) config.save() self._tracadmin('initenv', self.tracdir, self.dburi, '--config=%s' % config_file) if call([ sys.executable, os.path.join(self.trac_src, 'contrib', 'htpasswd.py'), "-c", "-b", self.htpasswd, "admin", "admin" ], close_fds=close_fds, cwd=self.command_cwd): raise Exception('Unable to setup admin password') self.adduser('user') self.adduser('joe') self.grant_perm('admin', 'TRAC_ADMIN') env = self.get_trac_environment() for component in self.get_enabled_components(): env.config.set('components', component, 'enabled') env.config.save() self.post_create(env)
def setup_config(self): """Load the configuration file.""" self.config = Configuration(self.config_file_path, {'envname': os.path.basename(self.path)}) self.setup_log() plugins_dir = self.shared_plugins_dir load_components(self, plugins_dir and (plugins_dir,))
def __init__(self, default_data=False, enable=None): ComponentManager.__init__(self) self.enabled_components = enable self.db = InMemoryDatabase() from trac.config import Configuration self.config = Configuration(None) from trac.log import logger_factory self.log = logger_factory('test') from trac.web.href import Href self.href = Href('/trac.cgi') self.abs_href = Href('http://example.org/trac.cgi') from trac import db_default for section, name, value in db_default.default_config: self.config.set(section, name, value) if default_data: cursor = self.db.cursor() for table, cols, vals in db_default.data: cursor.executemany( "INSERT INTO %s (%s) VALUES (%s)" % (table, ','.join(cols), ','.join(['%s' for c in cols])), vals) self.db.commit()
def test_default(self): config = Configuration(self.filename) self.assertEquals('', config.get('a', 'option')) self.assertEquals('value', config.get('a', 'option', 'value')) config.setdefault('a', 'option', 'value') self.assertEquals('value', config.get('a', 'option'))
def create(self, options=[], default_data=True): """Create the basic directory structure of the environment, initialize the database and populate the configuration file with default values. If options contains ('inherit', 'file'), default values will not be loaded; they are expected to be provided by that file or other options. :raises TracError: if the base directory of `path` does not exist. :raises TracError: if `path` exists and is not empty. """ base_dir = os.path.dirname(self.path) if not os.path.exists(base_dir): raise TracError( _( "Base directory '%(env)s' does not exist. Please create it " "and retry.", env=base_dir)) if os.path.exists(self.path) and os.listdir(self.path): raise TracError(_("Directory exists and is not empty.")) # Create the directory structure if not os.path.exists(self.path): os.mkdir(self.path) os.mkdir(self.htdocs_dir) os.mkdir(self.log_dir) os.mkdir(self.plugins_dir) os.mkdir(self.templates_dir) # Create a few files create_file(os.path.join(self.path, 'VERSION'), _VERSION + '\n') create_file( os.path.join(self.path, 'README'), 'This directory contains a Trac environment.\n' 'Visit https://trac.edgewall.org/ for more information.\n') # Setup the default configuration os.mkdir(self.conf_dir) config = Configuration(self.config_file_path) for section, name, value in options: config.set(section, name, value) config.save() self.setup_config() if not any((section, option) == ('inherit', 'file') for section, option, value in options): self.config.set_defaults(self) self.config.save() # Create the sample configuration create_file(self.config_file_path + '.sample') self._update_sample_config() # Create the database dbm = DatabaseManager(self) dbm.init_db() if default_data: dbm.insert_default_data()
def __init__(self, enable=[]): ComponentManager.__init__(self) from trac.config import Configuration from trac.log import logger_factory self.config = Configuration(None) self.log = logger_factory('test') self.href = Href('/trac.cgi') self.enabled_components = enable
def test_sections(self): configfile = open(self.filename, 'w') configfile.writelines( ['[a]\n', 'option = x\n', '[b]\n', 'option = y\n']) configfile.close() config = Configuration(self.filename) self.assertEquals(['a', 'b'], config.sections())
def test_read_and_get(self): configfile = open(self.filename, 'w') configfile.writelines(['[a]\n', 'option = x\n', '\n']) configfile.close() config = Configuration(self.filename) self.assertEquals('x', config.get('a', 'option')) self.assertEquals('x', config.get('a', 'option', 'y'))
def load_workflow_config_snippet(config, filename): """Loads the ticket-workflow section from the given file (expected to be in the 'workflows' tree) into the provided config. """ filename = resource_filename('trac.ticket', 'workflows/%s' % filename) new_config = Configuration(filename) for name, value in new_config.options('ticket-workflow'): config.set('ticket-workflow', name, value)
def __init__(self): ComponentManager.__init__(self) self.log = logger_factory('null') self.config = Configuration(None) self.href = Href('/') self.abs_href = Href('http://www.example.com/') self._wiki_pages = {} self.path = ''
def setup_config(self): """Load the configuration file.""" self.config = Configuration(os.path.join(self.path, 'conf', 'trac.ini'), {'envname': os.path.basename(self.path)}) self.setup_log() from trac.loader import load_components plugins_dir = self.shared_plugins_dir load_components(self, plugins_dir and (plugins_dir,))
def _config(self): filename = self.filename or self._get_password_file() if self._userconf is None or filename != self._userconf.filename: self._userconf = Configuration(filename) # Overwrite default with str class to preserve case. self._userconf.parser.optionxform = str self._userconf.parse_if_needed(force=True) else: self._userconf.parse_if_needed() return self._userconf
def test_options(self): configfile = open(self.filename, 'w') configfile.writelines( ['[a]\n', 'option = x\n', '[b]\n', 'option = y\n']) configfile.close() config = Configuration(self.filename) self.assertEquals(('option', 'x'), iter(config.options('a')).next()) self.assertEquals(('option', 'y'), iter(config.options('b')).next()) self.assertRaises(StopIteration, iter(config.options('c')).next)
def readconfig(filename): """Returns a list of raw config options""" config = Configuration(filename) rawactions = list(config.options('ticket-workflow')) debug("%s\n" % str(rawactions)) if not rawactions: sys.stderr.write("ERROR: You don't seem to have a [ticket-workflow] " "section.\n") sys.exit(1) return rawactions
def set_home_config(self, values): syspath = self.conf.getEnvironmentSysPath("home") setconf = Configuration(syspath + '/conf/trac.ini') try: for (main, sub, value) in values: setconf.set(main, sub, value) setconf.save() except: return False return True
def _config(self): filename = self.filename if not filename: self._svnserve_conf.parse_if_needed() filename = self._svnserve_conf['general'].getpath('password-db') if self._userconf is None or filename != self._userconf.filename: self._userconf = Configuration(filename) else: self._userconf.parse_if_needed() return self._userconf
def setup_config(self): """Load the configuration file.""" self.config = Configuration(self.config_file_path, {'envname': self.name}) if not self.config.exists: raise TracError(_("The configuration file is not found at " "%(path)s", path=self.config_file_path)) self.setup_log() plugins_dir = self.shared_plugins_dir load_components(self, plugins_dir and (plugins_dir,))
def _get_password_file(self): repos = RepositoryManager(self.env).get_repository('') if not repos: return None if isinstance(repos, CachedRepository): repos = repos.repos if repos.params['type'] in ('svn', 'svnfs', 'direct-svnfs'): conf = Configuration(os.path.join(repos.path, 'conf', 'svnserve.conf')) return conf['general'].getpath('password-db')
def diff(file1, file2, ignored_sections=None, ignore_absent=False): """ :param file1: Filename :param file2: Filename :param list ignored_sections: List of ignored sections :param bool ignore_absent: Disables absent key reporting """ if ignored_sections is None: ignored_sections = [] if not os.path.exists(file1): raise ValueError('file %s does not exists' % file1) if not os.path.exists(file2): raise ValueError('file %s does not exists' % file2) conf1 = Configuration(file1) conf2 = Configuration(file2) fn1 = os.path.split(file1)[1] fn2 = os.path.split(file2)[1] conf1_sections = set(conf1.sections()) - set(ignored_sections) conf2_sections = set(conf2.sections()) - set(ignored_sections) for section in conf1.sections(): if section not in conf2_sections: print 'SECTION: %s not in %s' % (section, fn2) default = object() for section in conf1_sections: for key, value1 in conf1.options(section): if not conf2.has_option(section, key): if not ignore_absent: print '[%s] %s = %s is ABSENT from %s (but exists in %s)' % ( section, key, value1, fn2, fn1) else: value2 = conf2.get(section, key, default) if value2 != value1 and value2 is not default: print '[%s] %s = %s -> %s (%s -> %s)' % ( section, key, value1, value2, fn1, fn2)
def test_set_and_save(self): configfile = open(self.filename, 'w') configfile.close() config = Configuration(self.filename) config.set('a', 'option', 'x') self.assertEquals('x', config.get('a', 'option')) config.save() configfile = open(self.filename, 'r') self.assertEquals(['[a]\n', 'option = x\n', '\n'], configfile.readlines()) configfile.close()
def _config(self): filename = self.filename if not filename: self._svnserve_conf.parse_if_needed() filename = self._svnserve_conf['general'].getpath('password-db') if self._userconf is None or filename != self._userconf.filename: self._userconf = Configuration(filename) # Overwrite default with str class to preserve case. self._userconf.parser.optionxform = str self._userconf.parse_if_needed(force=True) else: self._userconf.parse_if_needed() return self._userconf
def __init__(self, default_data=False, enable=None): """Construct a new Environment stub object. :param default_data: If True, populate the database with some defaults. :param enable: A list of component classes or name globs to activate in the stub environment. """ ComponentManager.__init__(self) Component.__init__(self) self.systeminfo = [] import trac self.path = os.path.dirname(trac.__file__) if not os.path.isabs(self.path): self.path = os.path.join(os.getcwd(), self.path) # -- configuration self.config = Configuration(None) # We have to have a ticket-workflow config for ''lots'' of things to # work. So insert the basic-workflow config here. There may be a # better solution than this. load_workflow_config_snippet(self.config, 'basic-workflow.ini') self.config.set('logging', 'log_level', 'DEBUG') self.config.set('logging', 'log_type', 'stderr') if enable is not None: self.config.set('components', 'trac.*', 'disabled') for name_or_class in enable or (): config_key = self._component_name(name_or_class) self.config.set('components', config_key, 'enabled') # -- logging from trac.log import logger_handler_factory self.log, self._log_handler = logger_handler_factory('test') # -- database self.dburi = get_dburi() if self.dburi.startswith('sqlite'): self.config.set('trac', 'database', 'sqlite::memory:') self.db = InMemoryDatabase() if default_data: self.reset_db(default_data) from trac.web.href import Href self.href = Href('/trac.cgi') self.abs_href = Href('http://example.org/trac.cgi') self.known_users = [] translation.activate(Locale and Locale('en', 'US'))
def test_reparse(self): configfile = open(self.filename, 'w') configfile.writelines(['[a]\n', 'option = x\n', '\n']) configfile.close() config = Configuration(self.filename) self.assertEquals('x', config.get('a', 'option')) time.sleep(1) # needed because of low mtime granularity configfile = open(self.filename, 'w') configfile.write('[a]\noption = y') configfile.close() config.parse_if_needed() self.assertEquals('y', config.get('a', 'option'))
def _update_sample_config(self): filename = os.path.join(self.config_file_path + '.sample') if not os.path.isfile(filename): return config = Configuration(filename) config.set_defaults() try: config.save() except EnvironmentError as e: self.log.warning("Couldn't write sample configuration file (%s)%s", e, exception_to_unicode(e, traceback=True)) else: self.log.info( "Wrote sample configuration file with the new " "settings and their default values: %s", filename)
def _update_sample_config(self): filename = os.path.join(self.env.path, 'conf', 'trac.ini.sample') if not os.path.isfile(filename): return config = Configuration(filename) for (section, name), option in Option.get_registry().iteritems(): config.set(section, name, option.dumps(option.default)) try: config.save() self.log.info("Wrote sample configuration file with the new " "settings and their default values: %s", filename) except IOError as e: self.log.warn("Couldn't write sample configuration file (%s)", e, exc_info=True)
def do(self): # Make backup of trac.ini before configuring it try: shutil.copy(self.conf_file, self.conf_file_back) except Exception: conf.log.exception("Could not create trac.ini backup") return False # Open trac.ini for configuration config = None try: config = Configuration(self.conf_file) except Exception: conf.log.exception("Error while reading config file!") return False # Enable correct plugin for repository try: vcs_plugin = self.__vcs_plugin() if vcs_plugin: config.set('components', vcs_plugin, 'enabled') config.set('trac', 'repository_type', self.vcs_type) except Exception: conf.log.exception("Could not set static settings for trac") return False try: config.set('project', 'descr', self.project.description) except Exception: conf.log.exception("Couldn't set description") return False # Remove attachment size (to enable global setting) try: config.remove("attachment", "max_size") except Exception: conf.log.exception("Could not remove attachment config property for a new project") # Save configuration try: config.save() except Exception: conf.log.exception("Failed to save configuration") return False self.success = True return True
def writeconfig(self, filepath, dicts=[]): """Writes or updates a config file. A list of dictionaries is used so that options for different aspects of the configuration can be kept separate while being able to update the same sections. Note that the result is order dependent where two dictionaries update the same option. """ config = Configuration(filepath) file_changed = False for data in dicts: for section, options in data.iteritems(): for key, value in options.iteritems(): if config.get(section, key, None) != value: # This should be expected to generate a false positive # when two dictionaries update the same option file_changed = True config.set(section, key, value) if file_changed: if os.path.exists(filepath): backupfile(filepath) config.save()
def create(self, options=[]): """Create the basic directory structure of the environment, initialize the database and populate the configuration file with default values. If options contains ('inherit', 'file'), default values will not be loaded; they are expected to be provided by that file or other options. """ # Create the directory structure if not os.path.exists(self.path): os.mkdir(self.path) os.mkdir(self.get_log_dir()) os.mkdir(self.get_htdocs_dir()) os.mkdir(os.path.join(self.path, 'plugins')) # Create a few files create_file(os.path.join(self.path, 'VERSION'), _VERSION + '\n') create_file( os.path.join(self.path, 'README'), 'This directory contains a Trac environment.\n' 'Visit http://trac.edgewall.org/ for more information.\n') # Setup the default configuration os.mkdir(os.path.join(self.path, 'conf')) create_file(os.path.join(self.path, 'conf', 'trac.ini.sample')) config = Configuration(os.path.join(self.path, 'conf', 'trac.ini')) for section, name, value in options: config.set(section, name, value) config.save() self.setup_config() if not any((section, option) == ('inherit', 'file') for section, option, value in options): self.config.set_defaults(self) self.config.save() # Create the database DatabaseManager(self).init_db()
def post_process_request(self, req, template, data, content_type): config = Configuration(self.message_file) frame_width = self.frame_width if frame_width == '': frame_width = '500' frame_height = self.frame_height if frame_height == '': frame_height = '400' d = self._read_messages(req, config) #self.log.debug('post_process_request: dictionary-len: ' + str(len(d))) if (len(d) > 0): frame_data = { 'TracMotdFrame': { 'frame_width': frame_width, 'frame_height': frame_height } } add_script_data(req, frame_data) add_script_data(req, d) add_script(req, 'tracmotd/js/date.js') add_script(req, 'tracmotd/js/motd.js') add_stylesheet(req, 'tracmotd/css/motd.css') return (template, data, content_type)
def getSettings(self, cls, store): if not isinstance(cls, basestring): mymodule = cls.__module__.lower() else: mymodule = cls.lower() # Do not add duplicates for object in store: if mymodule in object['module']: return mycount = len(mymodule.split('.')) prjconf = Configuration(conf.global_conf_path) rules = [(name.lower(), value.lower()) for name, value in prjconf.options('components')] rules.sort(lambda a, b: -cmp(len(a[0]), len(b[0]))) for pattern, value in rules: if pattern.startswith(mymodule + '.'): item = pattern.split(".") count = len(item) if count > mycount: header = item[mycount] if count > (mycount + 1): store.append({ 'module': mymodule, 'name': header + '.' + item[mycount + 1], 'default': self.parse_value(0, value), 'static': self.parse_boolean_value(1, value, True) })