本文整理汇总了Python中trac.env.Environment类的典型用法代码示例。如果您正苦于以下问题:Python Environment类的具体用法?Python Environment怎么用?Python Environment使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Environment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EnvironmentTestCase
class EnvironmentTestCase(unittest.TestCase):
def setUp(self):
env_path = os.path.join(tempfile.gettempdir(), 'trac-tempenv')
self.env = Environment(env_path, create=True)
self.db = self.env.get_db_cnx()
def tearDown(self):
self.db.close()
self.env.shutdown() # really closes the db connections
shutil.rmtree(self.env.path)
def test_get_version(self):
"""Testing env.get_version"""
assert self.env.get_version() == db_default.db_version
def test_get_known_users(self):
"""Testing env.get_known_users"""
cursor = self.db.cursor()
cursor.executemany("INSERT INTO session VALUES (%s,%s,0)",
[('123', 0),('tom', 1), ('joe', 1), ('jane', 1)])
cursor.executemany("INSERT INTO session_attribute VALUES (%s,%s,%s,%s)",
[('123', 0, 'email', '[email protected]'),
('tom', 1, 'name', 'Tom'),
('tom', 1, 'email', '[email protected]'),
('joe', 1, 'email', '[email protected]'),
('jane', 1, 'name', 'Jane')])
users = {}
for username,name,email in self.env.get_known_users(self.db):
users[username] = (name, email)
assert not users.has_key('anonymous')
self.assertEqual(('Tom', '[email protected]'), users['tom'])
self.assertEqual((None, '[email protected]'), users['joe'])
self.assertEqual(('Jane', None), users['jane'])
示例2: rename_user
def rename_user(envpath, oldname, newname):
"""Deletes all watchlist DB entries => Uninstaller"""
from trac.env import Environment
try:
env = Environment(envpath)
except:
print "Given path '%s' seems not to be a Trac environment." % envpath
sys.exit(3)
db = env.get_db_cnx()
cursor = db.cursor()
try:
cursor.execute("""
UPDATE watchlist
SET wluser=%s
WHERE wluser=%s
""", (newname,oldname))
cursor.execute("""
UPDATE watchlist_settings
SET wluser=%s
WHERE wluser=%s
""", (newname,oldname))
print "Renamed user '%s' to '%s'." % (oldname,newname)
db.commit()
except Exception as e:
db.rollback()
print "Could not rename user: " + unicode(e)
print "Does the new user already exists?"
sys.exit(3)
db.commit()
print "Finished."
示例3: fetchRecipes
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))
示例4: _do_migrate
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.')
示例5: Main
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
示例6: get_trac_user
def get_trac_user(path, username):
from trac.env import Environment
env = Environment(path)
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute(
"SELECT name, value" " FROM session_attribute" " WHERE sid='%s'" " AND (name='email' OR name='name')" % username
)
return dict((name, value) for name, value in cursor)
示例7: globally_execute_command
def globally_execute_command(self, *args):
offset = 0
max_index = -1
if args and args[0].isdigit():
offset = int(args[0])
if len(args) > 1 and args[1].isdigit():
limit = int(args[1])
max_index = limit + offset
args = args[2:]
else:
args = args[1:]
upgrade_check = False
env_list = False
if args and args[0] == 'upgrade-check':
upgrade_check = True
elif args and args[0] == 'list-env':
env_list = True
sys_home_project_name = self.config.get('multiproject', 'sys_home_project_name')
for index, row in enumerate(self.projects_iterator(['env_name'], batch_size=10)):
env_name, = row
if index < offset:
continue
if max_index != -1 and index >= max_index:
break
if env_name == sys_home_project_name:
continue
if env_list:
printout("{0:4} env:'{1}'".format(index, env_name))
continue
env = None
try:
env_path = safe_path(self.config.get('multiproject', 'sys_projects_root'),
env_name)
env = Environment(env_path)
except TracError as e:
printout(_('ERROR: Opening environment %(env_name)s failed', env_name=env_name))
continue
if upgrade_check:
if env.needs_upgrade():
printout("[+] {0:4} env:'{1}'".format(index, env_name))
else:
printout("[ ] {0:4} env:'{1}'".format(index, env_name))
continue
# To setup MultiProject specific things like 'project_identifier'
MultiProjectEnvironmentInit(env).environment_needs_upgrade(None)
try:
command_manager = AdminCommandManager(env)
printout(_("{0:4} Run in env:'{1}'".format(index, env_name)))
command_manager.execute_command(*args)
except AdminCommandError as e:
printout(_('ERROR: Executing command in environment %(env_name)s failed: ',
env_name=env_name) + str(e))
示例8: TicketTemplateTestCase
class TicketTemplateTestCase(unittest.TestCase):
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.db_transaction
self.db.__enter__()
self.compmgr = ComponentManager()
# init TicketTemplateModule
self.tt = ttadmin.TicketTemplateModule(self.compmgr)
setattr(self.tt, "env", self.env)
def tearDown(self):
self.db.__exit__(None, None, None)
self.env.shutdown() # really closes the db connections
shutil.rmtree(self.env.path)
def test_get_active_navigation_item(self):
req = Mock(path_info='/tickettemplate')
self.assertEqual('tickettemplate', self.tt.get_active_navigation_item(req))
req = Mock(path_info='/something')
self.assertNotEqual('tickettemplate', self.tt.match_request(req))
def test_get_navigation_items(self):
req = Mock(href=Mock(tickettemplate=lambda:"/trac-tempenv/tickettemplate"))
a, b, c= self.tt.get_navigation_items(req).next()
self.assertEqual('mainnav', a)
self.assertEqual('tickettemplate', b)
def test_match_request(self):
req = Mock(path_info='/tickettemplate')
self.assertEqual(True, self.tt.match_request(req))
req = Mock(path_info='/something')
self.assertEqual(False, self.tt.match_request(req))
def test_getTicketTypeNames(self):
options = self.tt._getTicketTypeNames()
self.assertEqual(["default", "defect", "enhancement", "task"], options)
def test_loadSaveTemplateText(self):
for tt_name, tt_text in [("default", "default text"),
("defect", "defect text"),
("enhancement", "enhancement text"),
("task", "task text"),
]:
self.tt._saveTemplateText(tt_name, tt_text)
self.assertEqual(tt_name + " text", self.tt._loadTemplateText(tt_name))
示例9: do_purge
def do_purge(self, req, path, users):
"""Purge obsolete data - i.e. environment data (sessions, preferences,
permissions) from users no longer existing
@param req
@param path path to the trac env to purge
@param users users to keep
@return boolean success
@return msg info
"""
self.env.log.debug('+ Purging obsolete data')
dryrun = self.env.config.getbool('user_sync','dryrun',True)
sql = []
envpath, tracenv = os.path.split(path)
try:
env = Environment(path)
except IOError:
self.env.log.debug('Could not initialize environment at %s' % (path,))
return False, 'Could not initialize environment at %s' % (path,)
perm = PermissionSystem(env)
if not 'TRAC_ADMIN' in perm.get_user_permissions(req.perm.username):
raise PermissionError
excludes = self.get_perm_groups(path)+users
protect = "'"+"','".join(excludes)+"'"
self.env.log.debug("Excluding from purge: %s" % (protect,))
db = env.get_db_cnx()
cursor = db.cursor()
if not dryrun:
self.env.log.debug('Updating database for %s' % (tracenv,))
cursor.execute('DELETE FROM auth_cookie WHERE name NOT IN (%s)' % (protect,))
cursor.execute('DELETE FROM session WHERE sid NOT IN (%s)' % (protect,))
cursor.execute('DELETE FROM session_attribute WHERE sid NOT IN (%s)' % (protect,))
cursor.execute('DELETE FROM permission WHERE username NOT IN (%s)' % (protect,))
db.commit()
sql_file_path = self.env.config.get('user_sync','sql_file_path') or os.path.join(self.env.path,'log')
if sql_file_path.lower() == 'none':
self.env.log.debug('SQLFile disabled (sql_file_path is "none")')
else:
sqlfile = '%s.sql' % (tracenv,)
sqlfile = os.path.join(sql_file_path,sqlfile)
self.env.log.debug('Writing SQL to %s' % (sqlfile,))
try:
f = open(sqlfile,'a')
f.write('\n--- SQL for purging Trac environment %s\n' % (tracenv,));
f.write('DELETE FROM auth_cookie WHERE name NOT IN (%s);\n' % (protect,))
f.write('DELETE FROM session WHERE sid NOT IN (%s);\n' % (protect,))
f.write('DELETE FROM session_attribute WHERE sid NOT IN (%s);\n' % (protect,))
f.write('DELETE FROM permission WHERE username NOT IN (%s);\n' % (protect,))
except IOError:
self.env.log.debug('Could not write SQL file %s!' % (sqlfile,))
return False, 'Could not write SQL file %s!' % (sqlfile,)
return True, 'Successfully purged environment %s' % (tracenv,)
示例10: _setup
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)
示例11: get_number_of_tickets_per_cr
def get_number_of_tickets_per_cr(self, project):
settings = get_current_registry().settings
if not settings:
return
tracenvs = settings.get('penelope.trac.envs')
for trac in project.tracs:
env = Environment('%s/%s' % (tracenvs, trac.trac_name))
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""SELECT c.value as cr, count(t.id) AS number FROM ticket t INNER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'customerrequest') group by cr;""")
tickets = cursor.fetchall()
db.rollback()
return dict(tickets)
示例12: _create_env
def _create_env(self, path, dburi):
env = Environment(path, True,
[('trac', 'database', dburi),
('trac', 'base_url', 'http://localhost/'),
('project', 'name', u'Pŕójéćŧ Ńáḿé')])
@env.with_transaction()
def fn(db):
cursor = db.cursor()
cursor.execute("UPDATE system SET value='21' "
"WHERE name='initial_database_version'")
pages_dir = resource_filename('trac.wiki', 'default-pages')
WikiAdmin(env).load_pages(pages_dir)
att = Attachment(env, 'wiki', 'WikiStart')
att.insert('filename.txt', StringIO('test'), 4)
env.shutdown()
示例13: __init__
def __init__(self, path, append):
self.append = _append
self.env = Environment(path)
self._db = self.env.get_db_cnx()
self._db.autocommit = False
self.loginNameCache = {}
self.fieldNameCache = {}
示例14: get_perm_groups
def get_perm_groups(self,path):
"""Get array of permission groups (e.g. anonymous,authenticated) defined in the given environment.
These 'users' should e.g. never be purged on cleanup
"""
users = []
env = Environment(path)
sids = []
self.env.log.debug('Get users to keep from environment path %s' % (path,))
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute('SELECT DISTINCT username FROM permission WHERE username NOT IN (SELECT DISTINCT sid FROM session_attribute UNION SELECT DISTINCT sid FROM session UNION SELECT DISTINCT name FROM auth_cookie)')
for row in cursor: users.append(row[0])
self.env.log.debug('Permission groups for %s: %s' % (path,','.join(users)))
for user in env.config.getlist('user_sync','users_keep'):
if not user in users: users.append(user)
return users
示例15: __init__
def __init__(self, project_name, path, db, host, user, password, append):
self.env = Environment(path)
self._append = append
self._tracdb = self.env.get_db_cnx()
self._tracdb.autocommit = False
self._trac_cursor = self._tracdb.cursor()
self._mantis_con = MySQLdb.connect(host=host,
user=user, passwd=password, db=db, compress=1,
cursorclass=MySQLdb.cursors.DictCursor, use_unicode=1)
self._mantis_cursor = self._mantis_con.cursor()
sql = "SELECT id FROM mantis_project_table WHERE name = %s" % (project_name)
print sql
self.mantisCursor().execute("SELECT id FROM mantis_project_table WHERE name = %s", (project_name))
result = self.mantisCursor().fetchall()
if len(result) > 1:
raise Exception("Ambiguous project name %s" % project_name)
elif len(result) == 0:
sql = """INSERT INTO mantis_project_table (name) VALUES (%s)""" % (project_name)
print sql
self.mantisCursor().execute("""INSERT INTO mantis_project_table (name) VALUES (%s)""" , (project_name))
self.mantisCommit()
self._project_id = int(self.mantisCursor().lastrowid)
else:
self._project_id = int(result[0]['id'])
self._bug_map = {}
self._user_map = {}
self._category_map = {}