Пример #1
0
 def __init__(self, path):
     self.env = Environment(path)
     self.loginNameCache = {}
     self.fieldNameCache = {}
     from trac.db.api import DatabaseManager
     self.using_postgres = \
         DatabaseManager(self.env).connection_uri.startswith("postgres:")
Пример #2
0
 def _init_env(self):
     self.__env = env = Environment(self.envname)
     # fix language according to env settings
     if has_babel:
         negotiated = get_console_locale(env)
         if negotiated:
             translation.activate(negotiated)
Пример #3
0
    def test_config_argument(self):
        """Options contained in file specified by the --config argument
        are written to trac.ini.
        """
        config_file = os.path.join(self.parent_dir, 'config.ini')
        create_file(
            config_file,
            textwrap.dedent("""\
            [the-plugin]
            option_a = 1
            option_b = 2
            [components]
            the_plugin.* = enabled
            [project]
            name = project2
            """))
        rv, output = self.execute('initenv project1 sqlite:db/sqlite.db '
                                  '--config=%s' % config_file)
        env = Environment(self.env_path)
        cfile = env.config.parser

        self.assertEqual(0, rv, output)
        self.assertEqual('1', cfile.get('the-plugin', 'option_a'))
        self.assertEqual('2', cfile.get('the-plugin', 'option_b'))
        self.assertEqual('enabled', cfile.get('components', 'the_plugin.*'))
        self.assertEqual('project1', cfile.get('project', 'name'))
        self.assertEqual('sqlite:db/sqlite.db', cfile.get('trac', 'database'))
        for (section, name), option in \
                Option.get_registry(env.compmgr).iteritems():
            if (section, name) not in \
                    (('trac', 'database'), ('project', 'name')):
                self.assertEqual(option.default, cfile.get(section, name))
Пример #4
0
 def test_version_file_empty(self):
     """TracError raised when environment version is empty."""
     create_file(os.path.join(self.env.path, 'VERSION'), '')
     with self.assertRaises(TracError) as cm:
         Environment(self.env.path)
     self.assertEqual("Unknown Trac environment type ''",
                      unicode(cm.exception))
Пример #5
0
def getSQLiteEnvironment(opts):
    """ Create an Environment connected to the SQLite database """

    dburi = opts.sqlite_uri
    env = Environment(opts.tracenv)
    env.config.set('trac', 'database', dburi)
    return env
Пример #6
0
    def ticket_changed(self, ticket, comment, author, old_values):
        """Called when a ticket is modified.
        
        `old_values` is a dictionary containing the previous values of the
        fields that have changed.
        """
        self.log.info(
            "DEBUG: ticket_changed(ticket, comment, author, old_values")
        self.log.info("DEBUG: owner: %s" % ticket['owner'])
        self.log.info("DEBUG: %s" % comment)
        self.log.info("DEBUG: %s" % author)
        self.log.info("DEBUG: %s" % old_values)
        self.log.info("DEBUG: %s" % ticket['anothertrac'])
        another_tracs = self._get_another_tracs()
        self.log.info("Another tracs dict: %s" % another_tracs)

        tkt_id = ticket.id

        # Check for [another-tracs] config section
        if another_tracs:
            for trac in another_tracs:
                env = None
                env = Environment(trac['path'])

                another_tkt_id = self.another_ticket_exist(self.env, tkt_id)

                # Check if ticket is "another".
                if ticket['reporter'] == trac['user']:
                    self.log.info("MATCH reporter!!! RAAAAAGHHHHH")
                    if another_tkt_id:
                        self.change_another_ticket(env, another_tkt_id,
                                                   comment, author, ticket,
                                                   trac, 'native')
                    return True
            # Check if ticket is "native".
                elif ticket['owner'] == trac['user']:
                    self.log.info("MATCH owner!!! ARGHHHHHHHHH")
                    if not another_tkt_id:
                        self.log.info("NATIVE_TICKET: new_another_ticket(%s)" %
                                      tkt_id)
                        another_tkt_id = self.new_another_ticket(
                            env, ticket, comment, author, trac)
                        if another_tkt_id:
                            self.log.info(
                                "NATIVE_TICKET: new ticket in another trac success created, id: %s"
                                % another_tkt_id)
                            self._insert_anotherticket(self.env, tkt_id,
                                                       trac['name'],
                                                       another_tkt_id)
                        else:
                            self.log.warning(
                                "NATIVE_TICKET: fail during create ticket in another trac"
                            )
                    else:
                        self.log.info("ANOTHER TICKET EXIST!!!")
                        self.change_another_ticket(env, another_tkt_id,
                                                   comment, author, ticket,
                                                   trac)
                    return True
Пример #7
0
    def _do_migrate(self, env_path, dburl):
        options = [('trac', 'database', dburl)]
        options.extend((section, name, value)
                       for section in self.config.sections()
                       for name, value in self.config.options(section)
                       if section != 'trac' or name != 'database')
        src_db = self.env.get_read_db()
        src_cursor = src_db.cursor()
        src_tables = set(
            self._get_tables(self.config.get('trac', 'database'), src_cursor))
        env = Environment(env_path, create=True, options=options)
        env.upgrade()
        env.config.save()  # remove comments

        db = env.get_read_db()
        cursor = db.cursor()
        tables = set(self._get_tables(dburl, cursor))
        tables = sorted(tables & src_tables)
        sequences = set(self._get_sequences(dburl, cursor, tables))
        directories = self._get_directories(src_db)

        printout('Copying tables:')
        for table in tables:
            if table == 'system':
                continue

            @env.with_transaction()
            def copy(db):
                cursor = db.cursor()
                printout('  %s table... ' % table, newline=False)
                src_cursor.execute('SELECT * FROM ' + src_db.quote(table))
                columns = get_column_names(src_cursor)
                query = 'INSERT INTO ' + db.quote(table) + \
                        ' (' + ','.join(db.quote(c) for c in columns) + ')' + \
                        ' VALUES (' + ','.join(['%s'] * len(columns)) + ')'
                cursor.execute('DELETE FROM ' + db.quote(table))
                count = 0
                while True:
                    rows = src_cursor.fetchmany(100)
                    if not rows:
                        break
                    cursor.executemany(query, rows)
                    count += len(rows)
                printout('%d records.' % count)

            if table in sequences:
                db.update_sequence(cursor, table)

        printout('Copying directories:')
        for name in directories:
            printout('  %s directory... ' % name, newline=False)
            src = os.path.join(self.env.path, name)
            dst = os.path.join(env.path, name)
            if os.path.isdir(dst):
                shutil.rmtree(dst)
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            printout('done.')
Пример #8
0
    def test_convert_to_sqlite_inplace(self):
        dburi = get_dburi()
        if dburi in ('sqlite::memory:', 'sqlite:db/trac.db'):
            dburi = 'sqlite:db/trac-convert.db'
        self._create_env(self.src_path, dburi)

        self.src_env = Environment(self.src_path)
        src_options = self._get_options(self.src_env)
        src_records = self._get_all_records(self.src_env)
        self._convert_db_inplace(self.src_env, 'sqlite:db/trac.db')
        self.src_env.shutdown()
        self.src_env = Environment(self.src_path)
        dst_options = self._get_options(self.src_env)
        dst_records = self._get_all_records(self.src_env)
        self.assertEqual({'name': 'initial_database_version', 'value': '21'},
                         dst_records['system']['initial_database_version'])
        self._compare_records(src_records, dst_records)
        self.assertEqual(src_options, dst_options)
Пример #9
0
 def _init_env(self):
     self.__env = env = Environment(self.envname)
     negotiated = None
     # fixup language according to env settings
     if has_babel:
         negotiated = get_console_locale(env)
         if negotiated:
             translation.activate(negotiated)
     self.cmd_mgr = AdminCommandManager(env)
Пример #10
0
 def __init__(self, path):
     self.env = Environment(path)
     self._db = self.env.get_db_cnx()
     self._db.autocommit = False
     self.loginNameCache = {}
     self.fieldNameCache = {}
     from trac.db.api import DatabaseManager
     self.using_postgres = DatabaseManager(
         self.env).connection_uri.startswith("postgres:")
Пример #11
0
 def test_no_permissions(self):
     self._create_env()
     os.chmod(self.db_path, 0444)
     env = Environment(self.env_path)
     try:
         self._db_query(env)
         self.fail('ConfigurationError not raised')
     except ConfigurationError, e:
         self.assertIn('requires read _and_ write permissions', unicode(e))
Пример #12
0
    def test_import_7(self):
        if trac.__version__.startswith('0.12'):
            # Not working on 0.12, because of configuration reasons...
            return

        env = self._setup()
        #print dir(env)
        instancedir = os.path.join(tempfile.gettempdir(), 'test-importer._preview_%d' % self.index[0])
        self.index[0] += 1
        if os.path.exists(instancedir):
           shutil.rmtree(instancedir, False)
        _dbfile = os.path.join(os.path.join(instancedir, 'db'), 'trac.db')
        env = Environment(instancedir, create=True)
        os.remove(_dbfile)
        shutil.copyfile(os.path.join(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(testfolder))), 'test'), 'tickets.db'), _dbfile)
        open(os.path.join(os.path.join(instancedir, 'conf'), 'trac.ini'), 'a').write('\n[ticket-custom]\ndomain = text\ndomain.label = Domain\nstage = text\nstage.label = Stage\nusers = text\nusers.label = Users\n')
        env = Environment(instancedir)
        self._do_test(env, 'ticket-13.xls', self._test_import)
Пример #13
0
    def test_invalid_version_raises_trac_error(self):
        """TracError raised when environment version is invalid."""
        version = 'Trac Environment Version 0'
        create_file(os.path.join(self.env.path, 'VERSION'), version + '\n')

        with self.assertRaises(TracError) as cm:
            Environment(self.env.path)
        self.assertEqual(
            "Unknown Trac environment type 'Trac Environment Version 0'",
            unicode(cm.exception))
Пример #14
0
 def test_missing_tracdb(self):
     self._create_env()
     os.remove(self.db_path)
     env = Environment(self.env_path)
     try:
         self._db_query(env)
         self.fail('ConfigurationError not raised')
     except ConfigurationError, e:
         self.assertIn('Database "', unicode(e))
         self.assertIn('" not found.', unicode(e))
Пример #15
0
 def test_version_file_not_found(self):
     """TracError raised when environment version file not found."""
     version_file = os.path.join(self.env.path, 'VERSION')
     os.remove(version_file)
     with self.assertRaises(TracError) as cm:
         Environment(self.env.path)
     self.assertEqual(
         "No Trac environment found at %s\n"
         "IOError: [Errno 2] No such file or directory: '%s'" %
         (self.env.path, version_file), unicode(cm.exception))
Пример #16
0
 def _create_env(self, path, dburi):
     env = Environment(path, True,
                       [('trac', 'database', dburi),
                        ('trac', 'base_url', 'http://localhost/'),
                        ('project', 'name', u'Pŕójéćŧ Ńáḿé')])
     dbm = DatabaseManager(env)
     dbm.set_database_version(21, 'initial_database_version')
     att = Attachment(env, 'wiki', 'WikiStart')
     att.insert('filename.txt', io.BytesIO('test'), 4)
     env.shutdown()
Пример #17
0
 def __init__(self, path):
     '''Basic object that supports the wiki and all that jazz'''
     self._env = Environment(os.path.abspath(path))
     purl = self._env.project_url
     if purl.endswith('/'):
         purl = purl[:-1]
     if purl:
         self.name = purl.split('/')[-1]
     else:
         self.name = self._env.project_name
Пример #18
0
    def test_convert_to_sqlite_env(self):
        dburi = get_dburi()
        if dburi == 'sqlite::memory:':
            dburi = 'sqlite:db/trac.db'
        self._create_env(self.src_path, dburi)

        self.src_env = Environment(self.src_path)
        src_options = self._get_options(self.src_env)
        src_records = self._get_all_records(self.src_env)
        self._convert_db(self.src_env, 'sqlite:db/trac.db', self.dst_path)
        self.dst_env = Environment(self.dst_path)
        dst_options = self._get_options(self.dst_env)
        dst_records = self._get_all_records(self.dst_env)
        self.assertEqual({'name': 'initial_database_version', 'value': '21'},
                         dst_records['system']['initial_database_version'])
        self._compare_records(src_records, dst_records)
        self.assertEqual(src_options, dst_options)
        att = Attachment(self.dst_env, 'wiki', 'WikiStart', 'filename.txt')
        self.assertEqual('test', read_file(att.path))
Пример #19
0
def Main(opts):
    """ Cross your fingers and pray """
    env = Environment(opts.envpath)
    from tractags.api import TagSystem

    tlist = opts.tags or split_tags(
        env.config.get('blog', 'default_tag', 'blog'))
    tags = TagSystem(env)
    req = Mock(perm=MockPerm())
    blog = tags.query(req, ' '.join(tlist + ['realm:wiki']))

    cnx = env.get_db_cnx()
    for resource, page_tags in list(blog):
        try:
            page = WikiPage(env, version=1, name=resource.id)
            _, publish_time, author, _, _ = page.get_history().next()
            if opts.deleteonly:
                page.delete()
                continue
            categories = ' '.join([t for t in page_tags if t not in tlist])
            page = WikiPage(env, name=resource.id)
            for version, version_time, version_author, version_comment, \
                _ in page.get_history():
                # Currently the basename of the post url is used due to
                # http://trac-hacks.org/ticket/2956
                #name = resource.id.replace('/', '_')
                name = resource.id
                # extract title from text:
                fulltext = page.text
                match = _title_split_match(fulltext)
                if match:
                    title = match.group(1)
                    fulltext = match.group(2)
                else:
                    title = name
                body = fulltext
                print "Adding post %s, v%s: %s" % (name, version, title)
                insert_blog_post(cnx, name, version, title, body, publish_time,
                                 version_time, version_comment, version_author,
                                 author, categories)
                reparent_blog_attachments(env, resource.id, name)
                continue
            cnx.commit()
            if opts.delete:
                page.delete()
                continue
        except:
            env.log.debug("Error loading wiki page %s" % resource.id,
                          exc_info=True)
            print "Failed to add post %s, v%s: %s" % (name, version, title)
            cnx.rollback()
            cnx.close()
            return 1
    cnx.close()
    return 0
Пример #20
0
 def __init__(self, project, rev, repo, env=None):
     """Initialize the class with the project path and the revision"""
     try:
         self.env = env or Environment(project)
         self.tm = AgiloTicketModelManager(self.env)
         self.repo = repo
         repos = self.env.get_repository(self.repo)
         repos.sync()
     except Exception, e:
         print >> sys.stderr, "An Error occurred while opening Trac project: %s => %s" % (project, to_unicode(e))
         sys.exit(1)
Пример #21
0
    def __init__(self, config, dry_run=False):
        self._dry_run = dry_run

        self._milestones = config['milestones']
        self._min_year = config['min_year']

        self._env = Environment(config['env'])
        self._users = {
            key: value.strip()
            for key, _, value in self._env.get_known_users() if value
        }
Пример #22
0
def update_irc_search():
    args = sys.argv
    if len(args) < 2:
        print 'Usage: %s <environment path>'%(args[0])
    else:
        from trac.env import Environment
        from irclogs import api
        env = Environment(sys.argv[1])
        chmgr = api.IRCChannelManager(env)
        for indexer in chmgr.indexers:
            indexer.update_index()
Пример #23
0
    def create(self, project):
        """Create a Trac environment for the project
        
        This will NOT call trac-admin but interfaces with Trac's API 
        directly. It performs the same steps as trac-admin"""

        if self.has_env(project):
            return True

        # When creating environments, you can supply a list
        # of (section, option, value) tuples to trac which serve as a default
        options = self.get_default_options(project)

        # If an (inherit, file, xy) option is present, trac will omit the default values
        if 'inherit_config' in self.component_config:
            options.append(
                ('inherit', 'file', self.component_config['inherit_config']))

        try:
            # create environment
            env = Environment(self.get_env_path(project),
                              create=True,
                              options=options)

            # preload wiki pages
            wiki_pages = None
            if 'preload_wiki' in self.component_config:
                wiki_pages = self.component_config['preload_wiki']
            else:
                try:
                    wiki_pages = pkg_resources.resource_filename(
                        'trac.wiki', 'default-pages')
                except Exception as e:
                    logger.exception(
                        "Exception while trying to find wiki pages")
                    wiki_pages = None

            if wiki_pages:
                try:
                    WikiAdmin(env).load_pages(wiki_pages)
                except Exception as e:
                    logger.exception("Unable to load wiki pages from %s",
                                     wiki_pages)
            else:
                logger.warning("Not wiki pages found for preloading")

            # all done
            return True
        except Exception as e:
            logger.exception(
                "Caught exception while creating Trac environment in " +
                self.get_env_path(project))

        return False
def send_mail(message, receiver):
    env = Environment(conf.getEnvironmentSysPath(conf.sys_home_project_name))
    notifier = NotificationSystem(env)
    from_email = env.config['notification'].get('smtp_from')
    replyto_email = env.config['notification'].get('smtp_replyto')
    sender = from_email or replyto_email

    try:
        notifier.send_email(sender, receiver, message)
    except Exception as e:
        error_exit(e)
    return True
Пример #25
0
def fetchRecipes(trac_env):
    env = Environment(trac_env)
    db = env.get_db_cnx()
    cursor = db.cursor()
    cursor.execute(
        "SELECT path,active,recipe,min_rev,max_rev,label,description,name FROM bitten_config"
    )
    for row in cursor:
        (path, active, recipe, min_rev, max_rev, label, description,
         name) = row
        writeFile(trac_env, name,
                  (path, active, recipe, min_rev, max_rev, label, description))
Пример #26
0
 def __init__(self, project, log, env=None):
     """Initialize the class with the project path and the revision"""
     try:
         self.env = env or Environment(project)
         self.parser = CommitMessageCommandParser(self.env, log)
     except Exception, e:
         #print_exc()
         print >> sys.stderr, "An Error occured while opening Trac project: '%s' => %s" % \
                              (project, to_unicode(e))
         print >> sys.stderr, "AgiloSVNPreCommit initialized with: '%s' %s, '%s' %s" % \
                              (project, type(project), log, type(log))
         sys.exit(1)
Пример #27
0
    def setUp(self):
        #        self.env = EnvironmentStub()

        env_path = os.path.join(tempfile.gettempdir(), 'trac-tempenv')
        self.env = Environment(env_path, create=True)
        self.db = self.env.get_db_cnx()

        self.compmgr = ComponentManager()

        # init TicketTemplateModule
        self.tt = ttadmin.TicketTemplateModule(self.compmgr)
        setattr(self.tt, "env", self.env)
Пример #28
0
 def __init__(self,
              trac_env,
              repo_dir,
              target_ref,
              data_file,
              verbose=True):
     self.env = Environment(trac_env)
     self.repo_dir = repo_dir.rstrip('/')
     self.reponame = os.path.basename(self.repo_dir).lower()
     self.target_ref = target_ref
     self.data_file = data_file
     self.verbose = verbose
Пример #29
0
    def _setup(self, configuration=None):
        configuration = configuration or '[ticket-custom]\nmycustomfield = text\nmycustomfield.label = My Custom Field\nmycustomfield.order = 1'

        instancedir = os.path.join(tempfile.gettempdir(),
                                   'test-importer._preview')
        if os.path.exists(instancedir):
            shutil.rmtree(instancedir, False)
        env = Environment(instancedir, create=True)
        open(os.path.join(os.path.join(instancedir, 'conf'), 'trac.ini'),
             'a').write('\n' + configuration + '\n')
        db = env.get_db_cnx()
        _exec(
            db.cursor(),
            "INSERT INTO permission VALUES ('anonymous', 'REPORT_ADMIN')        "
        )
        _exec(
            db.cursor(),
            "INSERT INTO permission VALUES ('anonymous', 'IMPORT_EXECUTE')        "
        )
        db.commit()
        ImporterTestCase.TICKET_TIME = 1190909220
        return Environment(instancedir)
Пример #30
0
    def register_repository(self, repository, name=None):
        """Register a repository with trac"""

        project = repository.project
        tracname = name if name is not None else repository.name

        if repository.name in project.data.get('plugins',
                                               {}).get('trac', {}).get(
                                                   repository.type, {}):
            logger.error(
                "Repository %s:%s is already registered in project %s",
                repository.type, repository.name, project.name)
            return False

        if repository.type not in self.typemap:
            logger.error("Repository type %s is not supported in Trac",
                         repository.type)
            return False

        if not self.has_env(project):
            logger.warning(
                "Tried to add repository %s:%s to Trac of project %s, but there is no environment",
                repository.type, repository.name, project.name)
            return False

        try:
            env = Environment(self.get_env_path(project))

            DbRepositoryProvider(env).add_repository(
                tracname, repository.path, self.typemap[repository.type])

            # save mapping in project
            project.data.setdefault('plugins',
                                    {}).setdefault('trac', {}).setdefault(
                                        repository.type,
                                        {})[repository.name] = tracname
            project.save()

            # Synchronise repository
            rm = RepositoryManager(env)
            repos = rm.get_repository(tracname)
            repos.sync(lambda rev: logger.debug("Synced revision: %s", rev),
                       clean=True)

            return True
        except Exception as e:
            logger.exception(
                "Exception occured while addingrepository %s:%s to Trac of project %s",
                repository.type, repository.name, project.name)
            return False