Mailing List Archive

patch for trac-admin initenv
Hi All, as part of my work to get a web page up and running that will
automatically create projects (SVN and TRAC) I have made changes to the
trac-admin script (patch file attached). The changes allow the initenv
command to be invoked with arguments in a non-interactive mode. The
main change to the way the interactive mode works is that the script
collects all the user input before testing any of it. The ability to
abort the database creation by hitting a key has also been removed.

WARNING: This is the only the second small bit of Python I have ever
written and it has not been extensively tested. Use at your own risk.
If someone decides to merge this patch in they should probably do a bit
of testing and make a careful inspection of the source.

Type "help initenv" at the trac-admin prompt for info.

All feedback on coding style or improvements welcome....

Cheers,
Felix
-------------- next part --------------
#!/usr/bin/env python
# -*- coding: iso8859-1 -*-
__author__ = 'Daniel Lundin <daniel@edgewall.com>, Jonas Borgstr?m <jonas@edgewall.com>'
__copyright__ = 'Copyright (c) 2004 Edgewall Software'
__license__ = """
Copyright (C) 2003, 2004 Edgewall Software
Copyright (C) 2003, 2004 Jonas Borgstr?m <jonas@edgewall.com>
Copyright (C) 2003, 2004 Daniel Lundin <daniel@edgewall.com>

Trac is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.

Trac is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."""

import os
import os.path
import sys
import time
import cmd
import shlex
import sqlite
import StringIO

from trac import util
import trac.siteconfig
import trac.Environment

def my_sum(list):
"""Python2.1 doesn't have sum()"""
tot = 0
for item in list:
tot += item
return tot


class TracAdmin(cmd.Cmd):
intro = ''
license = trac.__license_long__
credits = trac.__credits__
doc_header = 'Trac Admin Console %(ver)s\n' \
'Available Commands:\n' \
% {'ver':trac.__version__ }
ruler = ''
prompt = "Trac> "
__env = None

def __init__(self,envdir=None):
cmd.Cmd.__init__(self)
self.interactive = 0
if envdir:
self.env_set(envdir)

def docmd(self, cmd='help'):
self.onecmd(cmd)

def run(self):
self.interactive = 1
print 'Welcome to trac-admin %(ver)s\n' \
'Interactive Trac adminstration console.\n' \
'%(copy)s\n\n' \
"Type: '?' or 'help' for help on commands.\n" % \
{'ver':trac.__version__,'copy':__copyright__}
while 1:
try:
self.cmdloop()
break
except KeyboardInterrupt:
print "\n** Interrupt. Use 'quit' to exit **"

##
## Environment methods
##

def env_set(self, envname):
self.envname = envname
self.prompt = "Trac [%s]> " % self.envname

def env_check(self):
try:
self.__db = trac.Environment.Environment(self.envname)
except:
return 0
return 1

def env_create(self):
try:
self.__env = trac.Environment.Environment (self.envname, create=1)
return self.__env
except Exception, e:
print 'Failed to create environment.', e
sys.exit(1)

def db_open(self):
try:
if not self.__env:
self.__env = trac.Environment.Environment (self.envname)
return self.__env.get_db_cnx()
except Exception, e:
print 'Failed to open environment.', e
sys.exit(1)

def db_execsql (self, sql, cursor=None):
data = []
if not cursor:
cnx=self.db_open()
cursor = cnx.cursor()
else:
cnx = None
cursor.execute(sql)
while 1:
row = cursor.fetchone()
if row == None:
break
data.append(row)
if cnx:
cnx.commit()
return data

##
## Utility methods
##

def arg_tokenize (self, argstr):
if hasattr(shlex, 'split'):
toks = shlex.split(argstr)
else:
def my_strip(s, c):
"""string::strip in python2.1 doesn't support arguments"""
i = j = 0
for i in range(len(s)):
if not s[i] in c:
break
for j in range(len(s), 0, -1):
if not s[j-1] in c:
break
return s[i:j]

lexer = shlex.shlex(StringIO.StringIO(argstr))
lexer.wordchars = lexer.wordchars + ".,_/"
toks = []
while 1:
token = my_strip(lexer.get_token(), '"\'')
if not token:
break
toks.append(token)
return toks or ['']

def word_complete (self, text, words):
return [a for a in words if a.startswith (text)]

def print_listing(self, headers, data, sep=' ',decor=1):
ldata = data
if decor:
ldata.insert (0, headers)
print
colw=[]
ncols = len(ldata[0]) # assumes all rows are of equal length
for cnum in xrange(0, ncols):
mw = 0
for cell in [str(d[cnum]) or '' for d in ldata]:
if len(cell) > mw:
mw = len(cell)
colw.append(mw)
for rnum in xrange(0, len(ldata)):
for cnum in xrange(0, ncols):
if decor and rnum == 0:
sp = ('%%%ds' % len(sep)) % ' ' # No separator in header
else:
sp = sep
if cnum+1 == ncols: sp = '' # No separator after last column
print ("%%-%ds%s" % (colw[cnum], sp)) % (ldata[rnum][cnum] or ''),
print
if rnum == 0 and decor:
print ''.join(['-' for x in xrange(0,(1+len(sep))*cnum+my_sum(colw))])
print

def print_doc(self,doc,decor=0):
if not doc: return
self.print_listing (['Command','Description'], doc, ' --', decor)

def get_component_list (self):
data = self.db_execsql ("SELECT name FROM component")
return [r[0] for r in data]

# def get_config_list (self):
# data = self.db_execsql ("SELECT section||'.'||name FROM config")
# return [r[0] for r in data]

def get_user_list (self):
data = self.db_execsql ("SELECT DISTINCT username FROM permission")
return [r[0] for r in data]

def get_wiki_list (self):
data = self.db_execsql('SELECT DISTINCT name FROM wiki')
return [r[0] for r in data]

def get_dir_list (self, pathstr,justdirs=0):
dname = os.path.dirname(pathstr)
d = os.path.join(os.getcwd(), dname)
dlist = os.listdir(d)
if justdirs:
result = []
for entry in dlist:
try:
if os.path.isdir(entry):
result.append(entry)
except:
pass
else:
result = dlist
return result

def get_enum_list (self, type):
data = self.db_execsql("SELECT name FROM enum WHERE type='%s'" % type)
return [r[0] for r in data]

def get_milestone_list (self):
data = self.db_execsql("SELECT name FROM milestone")
return [r[0] for r in data]

def get_version_list (self):
data = self.db_execsql("SELECT name FROM version")
return [r[0] for r in data]


##
## Available Commands
##

## Help
_help_help = [('help', 'Show documentation')]

def do_help(self, line=None):
arg = self.arg_tokenize(line)
if arg[0]:
try:
doc = getattr(self, "_help_" + arg[0])
self.print_doc (doc)
except AttributeError:
print "No documentation found for '%s'" % arg[0]
else:
docs = (self._help_about + self._help_help +
self._help_initenv + self._help_upgrade +
self._help_wiki +
# self._help_config + self._help_wiki +
self._help_permission + self._help_component +
self._help_priority + self._help_severity +
self._help_version + self._help_milestone)
print 'trac-admin - The Trac Administration Console %s' % trac.__version__
if not self.interactive:
print
print "Usage: trac-admin <dbfile> [command [subcommand] [option ...]]\n"
print "Invoking trac-admin without command starts "\
"interactive mode."
self.print_doc (docs)
print self.credits


## About / Version
_help_about = [('about', 'Shows information about trac-admin')]

def do_about(self, line):
print
print 'Trac Admin Console %s' % trac.__version__
print '================================================================='
print self.license
print self.credits


## Quit / EOF
_help_quit = [['quit', 'Exit the program']]
_help_EOF = _help_quit

def do_quit(self,line):
print
sys.exit()

do_EOF = do_quit # Alias

# ## Component
_help_component = [.('component list', 'Show available components'),
('component add <name> <owner>', 'Add a new component'),
('component rename <name> <newname>', 'Rename a component'),
('component remove <name>', 'Remove/uninstall component'),
('component chown <name> <owner>', 'Change component ownership')]

def complete_component (self, text, line, begidx, endidx):
if begidx in [16,17]:
comp = self.get_component_list()
elif begidx > 15 and line.startswith('component chown '):
comp = self.get_user_list()
else:
comp = ['list','add','rename','remove','chown']
return self.word_complete(text, comp)

def do_component(self, line):
arg = self.arg_tokenize(line)
try:
if arg[0] == 'list':
self._do_component_list()
elif arg[0] == 'add' and len(arg)==3:
name = arg[1]
owner = arg[2]
self._do_component_add(name, owner)
elif arg[0] == 'rename' and len(arg)==3:
name = arg[1]
newname = arg[2]
self._do_component_rename(name, newname)
elif arg[0] == 'remove' and len(arg)==2:
name = arg[1]
self._do_component_remove(name)
elif arg[0] == 'chown' and len(arg)==3:
name = arg[1]
owner = arg[2]
self._do_component_set_owner(name, owner)
else:
self.do_help ('component')
except Exception, e:
print 'Component %s failed:' % arg[0], e

def _do_component_list(self):
data = self.db_execsql('SELECT name, owner FROM component')
self.print_listing(['Name', 'Owner'], data)

def _do_component_add(self, name, owner):
data = self.db_execsql("INSERT INTO component VALUES('%s', '%s')"
% (name, owner))

def _do_component_rename(self, name, newname):
cnx = self.db_open()
cursor = cnx.cursor ()
self.db_execsql("UPDATE component SET name='%s' WHERE name='%s'"
% (newname,name), cursor)
self.db_execsql("UPDATE ticket SET component='%s' WHERE component='%s'"
% (newname,name), cursor)
cnx.commit()

def _do_component_remove(self, name):
data = self.db_execsql("DELETE FROM component WHERE name='%s'"
% (name))

def _do_component_set_owner(self, name, owner):
data = self.db_execsql("UPDATE component SET owner='%s' WHERE name='%s'"
% (owner,name))


## Permission
_help_permission = [('permission list', 'List permission rules'),
('permission add <user> <action> [action] [...]', 'Add a new permission rule'),
('permission remove <user> <action> [action] [...]', 'Remove permission rule')]

def do_permission(self, line):
arg = self.arg_tokenize(line)
try:
if arg[0] == 'list':
self._do_permission_list()
elif arg[0] == 'add' and len(arg) >= 3:
user = arg[1]
for action in arg[2:]:
self._do_permission_add(user, action)
elif arg[0] == 'remove' and len(arg) >= 3:
user = arg[1]
for action in arg[2:]:
self._do_permission_remove(user, action)
else:
self.do_help ('permission')
except Exception, e:
print 'Permission %s failed:' % arg[0], e

def _do_permission_list(self):
data = self.db_execsql('SELECT username, action FROM permission')
self.print_listing(['User', 'Action'], data)
print
print 'Available actions:'
print ' LOG_VIEW, FILE_VIEW, CHANGESET_VIEW, BROWSER_VIEW, '
print ' TICKET_VIEW, TICKET_CREATE, TICKET_MODIFY, TICKET_ADMIN, '
print ' REPORT_VIEW, REPORT_CREATE, REPORT_MODIFY, REPORT_DELETE, REPORT_ADMIN, '
print ' WIKI_VIEW, WIKI_CREATE, WIKI_MODIFY, WIKI_DELETE, WIKI_ADMIN, '
print ' MILESTONE_VIEW, MILESTONE_CREATE, MILESTONE_MODIFY, MILESTONE_DELETE, '
print ' ROADMAP_VIEW, ROADMAP_ADMIN, TIMELINE_VIEW, SEARCH_VIEW, '
print ' CONFIG_VIEW and TRAC_ADMIN.'
print

def _do_permission_add(self, user, action):
self.db_execsql("INSERT INTO permission VALUES('%s', '%s')" % (user, action.upper()))

def _do_permission_remove(self, user, action):
self.db_execsql("DELETE FROM permission WHERE username='%s' AND action='%s'" %
(user, action.upper()))

## Initdb
_help_initenv = [.('initenv', 'Creates and initializes a new environment interactively'),
('initenv <projectname> <repospath> <templatepath>', 'Create new environment from arguments given')]

def do_initdb(self, line):
self.do_initenv(line)

def collect_init_args(self):
returnvals = []
print 'Creating a new Trac environment at %s' % self.envname
print
print 'Trac will first ask a few questions about your environment '
print 'in order to initalize and prepare the project database.'
print
print " Please enter the name of your project."
print " This name will be used in page titles and descriptions."
print
dp = 'My Project'
returnvals.append(raw_input('Project Name [%s]> ' % dp) or dp)

print
print ' Please specify the absolute path to the project Subversion repository.'
print ' Repository must be local, and trac-admin requires read+write'
print ' permission to initialize the Trac database.'
print
drp = '/var/svn/test'
prompt = 'Path to repository [%s]> ' % drp
returnvals.append(raw_input(prompt) or drp)

print
print ' Please enter location of Trac page templates.'
print ' Default is the location of the site-wide templates' \
'installed with Trac.'
print
dt = trac.siteconfig.__default_templates_dir__
prompt = 'Templates directory [%s]> ' % dt
returnvals.append(raw_input(prompt) or dt)

return returnvals

def do_initenv(self, line):
if self.env_check():
print "Initdb/initenv for '%s' failed.\nDoes an environment already exist?" % self.envname
return

arg = self.arg_tokenize(line)
project_name = None
repository_dir = None
templates_dir = None

if len(arg) == 1:
returnvals = self.collect_init_args()
project_name = returnvals[0]
repository_dir = returnvals[1]
templates_dir = returnvals[2]
elif len(arg)!= 3:
print 'Wrong number of arguments to initenv %d' % len(arg)
return
else:
project_name = arg[0]
repository_dir = arg[1]
templates_dir = arg[2]

from svn import util, repos, core
core.apr_initialize()
pool = core.svn_pool_create(None)
try:

# Remove any trailing slash or else subversion might abort
if not os.path.split(repository_dir)[1]:
repository_dir = os.path.split(repository_dir)[0]

rep = repos.svn_repos_open(repository_dir, pool)
fs_ptr = repos.svn_repos_fs(rep)
except Exception, e:
print repository_dir, 'Repository access error: %s' % e
return

if not os.access(os.path.join(templates_dir, 'browser.cs'), os.F_OK) or \
not os.access(os.path.join(templates_dir, 'ticket.cs'), os.F_OK):
print templates_dir, 'doesn\'t look like a Trac templates directory'
return

try:
print 'Creating and Initializing Project'
self.env_create()
cnx = self.__env.get_db_cnx()
print ' Inserting default data'
self.__env.insert_default_data()

print ' Configuring Project'
print ' trac.repository_dir'
self.__env.set_config('trac', 'repository_dir', repository_dir)
print ' trac.templates_dir'
self.__env.set_config('trac', 'templates_dir', templates_dir)
print ' project.name'
self.__env.set_config('project', 'name', project_name)
self.__env.save_config()
# Add a few default wiki pages
print ' Installing wiki pages'
cursor = cnx.cursor()
self._do_wiki_load(trac.siteconfig.__default_wiki_dir__,cursor)

from trac import sync
print ' Indexing repository'
sync.sync(cnx, rep, fs_ptr, pool)
print 'Done.'
except Exception, e:
print 'Failed to initialize database.', e
sys.exit(2)


print "---------------------------------------------------------------------"
print
print 'Project database for \'%s\' created.' % project_name
print
print ' Customize settings for your project using the command:'
print
print ' trac-admin %s' % self.envname
print
print ' Don\'t forget, you also need to copy (or symlink) "trac/cgi-bin/trac.cgi"'
print ' to you web server\'s /cgi-bin/ directory, and then configure the server.'
print
print ' If you\'re using Apache, this config example snippet might be helpful:'
print
print ' Alias /trac "/wherever/you/installed/trac/htdocs/"'
print ' <Location "/cgi-bin/trac.cgi">'
print ' SetEnv TRAC_ENV "%s"' % self.envname
print ' </Location>'
print
print ' # You need something like this to authenticate users'
print ' <Location "/cgi-bin/trac.cgi/login">'
print ' AuthType Basic'
print ' AuthName "%s"' % project_name
print ' AuthUserFile /somewhere/trac.htpasswd'
print ' Require valid-user'
print ' </Location>'
print

print ' The latest documentation can also always be found on the project website:'
print ' http://projects.edgewall.com/trac/'
print
print 'Congratulations!'
print

## Wiki
_help_wiki = [('wiki list', 'List wiki pages'),
('wiki export <page> [file]',
'Export wiki page to file or stdout'),
('wiki import <page> [file]',
'Import wiki page from file or stdin'),
('wiki dump <directory>',
'Export all wiki pages to files named by title'),
('wiki load <directory>',
'Import all wiki pages from directory')]

def complete_wiki (self, text, line, begidx, endidx):
argv = self.arg_tokenize(line)
argc = len(argv)
if line[-1] == ' ': # Space starts new argument
argc += 1
if argc==2:
comp = ['list','import','export','dump','load']
else:
if argv[1] in ['dump','load']:
comp = self.get_dir_list(argv[-1], 1)
elif argv[1] in ['export', 'import']:
if argc==3:
comp = self.get_wiki_list()
elif argc==4:
comp = self.get_dir_list(argv[-1])
return self.word_complete(text, comp)

def do_wiki(self, line):
arg = self.arg_tokenize(line)
try:
if arg[0] == 'list':
self._do_wiki_list()
elif arg[0] == 'import' and len(arg) == 3:
title = arg[1]
file = arg[2]
self._do_wiki_import(file, title)
elif arg[0] == 'export' and len(arg) in [2,3]:
page = arg[1]
file = (len(arg) == 3 and arg[2]) or None
self._do_wiki_export(page, file)
elif arg[0] == 'dump' and len(arg) in [1,2]:
dir = (len(arg) == 2 and arg[1]) or ''
self._do_wiki_dump(dir)
elif arg[0] == 'load' and len(arg) in [1,2]:
dir = (len(arg) == 2 and arg[1]) or ''
self._do_wiki_load(dir)
elif arg[0] == 'upgrade' and len(arg) == 1:
self._do_wiki_load(trac.siteconfig.__default_wiki_dir__,
ignore=['WikiStart'])
else:
self.do_help ('wiki')
except Exception, e:
print 'Wiki %s failed:' % arg[0], e

def _do_wiki_list(self):
data = self.db_execsql('SELECT name,max(version),time'
' FROM wiki GROUP BY name ORDER BY name')
ldata = [(d[0], d[1], time.ctime(d[2])) for d in data]
self.print_listing(['Title', 'Edits', 'Modified'], ldata)

def _do_wiki_import(self, filename, title, cursor=None):
if not os.path.isfile(filename):
print "%s is not a file" % filename
return
f = open(filename,'r')
data = util.to_utf8(f.read())

# Make sure we don't insert the exact same page twice
old = self.db_execsql("SELECT text FROM wiki "
"WHERE name='%s' "
"ORDER BY version DESC LIMIT 1" % title, cursor)
if old and data == old[0][0]:
print ' %s already up to date.' % title
return

data = data.replace("'", "''") # Escape ' for safe SQL
f.close()

sql = ("INSERT INTO wiki('version','name','time','author','ipnr','text') "
" SELECT 1+ifnull(max(version),0),'%(title)s','%(time)s','%(author)s',"
" '%(ipnr)s','%(text)s' FROM wiki WHERE name='%(title)s'"
% {'title':title,
'time':int(time.time()),
'author':'trac',
'ipnr':'127.0.0.1',
'locked':'0',
'text':data})
self.db_execsql(sql, cursor)

def _do_wiki_export(self, page,filename=''):
data=self.db_execsql("SELECT text FROM wiki "
" WHERE name='%s'"
" ORDER BY version DESC LIMIT 1" % page)
text = data[0][0]
if not filename:
print text
else:
if os.path.isfile(filename):
raise Exception("File '%s' exists" % filename)
f = open(filename,'w')
f.write(text)
f.close()

def _do_wiki_dump(self, dir):
pages = self.get_wiki_list()
for p in pages:
dst = os.path.join(dir,p)
print " %s => %s" % (p, dst)
self._do_wiki_export(p, dst)

def _do_wiki_load(self, dir,cursor=None, ignore=[]):
for page in os.listdir(dir):
if page in ignore:
continue
filename = os.path.join(dir, page)
if os.path.isfile(filename):
print " %s => %s" % (filename, page)
self._do_wiki_import(filename, page, cursor)


## (Ticket) Priority
_help_priority = [.('priority list', 'Show possible ticket priorities'),
('priority add <value>', 'Add a priority value option'),
('priority change <value> <newvalue>',
'Change a priority value'),
('priority remove <value>', 'Remove priority value')]

def complete_priority (self, text, line, begidx, endidx):
if begidx == 16:
comp = self.get_enum_list ('priority')
elif begidx < 15:
comp = ['list','add','change','remove']
return self.word_complete(text, comp)

def do_priority(self, line):
self._do_enum('priority', line)

## (Ticket) Severity
_help_severity = [.('severity list', 'Show possible ticket priorities'),
('severity add <value>', 'Add a severity value option'),
('severity change <value> <newvalue>',
'Change a severity value'),
('severity remove <value>', 'Remove severity value')]

def complete_severity (self, text, line, begidx, endidx):
if begidx == 16:
comp = self.get_enum_list ('severity')
elif begidx < 15:
comp = ['list','add','change','remove']
return self.word_complete(text, comp)

def do_severity(self, line):
self._do_enum('severity', line)

# Priority and Severity share the same datastructure and methods:

def _do_enum(self, type, line):
arg = self.arg_tokenize(line)
try:
if arg[0] == 'list':
self._do_enum_list(type)
elif arg[0] == 'add' and len(arg)==2:
name = arg[1]
self._do_enum_add(type, name)
elif arg[0] == 'change' and len(arg)==3:
name = arg[1]
newname = arg[2]
self._do_enum_change(type, name, newname)
elif arg[0] == 'remove' and len(arg)==2:
name = arg[1]
self._do_enum_remove(type, name)
else:
self.do_help (type)
except Exception, e:
print 'Priority %s failed:' % arg[0], e

def _do_enum_list(self, type):
data = self.db_execsql("SELECT name FROM enum WHERE type='%s'"
% type)
self.print_listing(['Possible Values'], data)

def _do_enum_add(self, type, name):
sql = ("INSERT INTO enum('value','type','name') "
" SELECT 1+ifnull(max(value),0),'%(type)s','%(name)s'"
" FROM enum WHERE type='%(type)s'"
% {'type':type,
'name':name})
data = self.db_execsql(sql)

def _do_enum_change(self, type, name, newname):
d = {'name':name, 'newname':newname, 'type':type}
data = self.db_execsql("SELECT name FROM enum"
" WHERE type='%(type)s' AND name='%(name)s'" % d)
if not data:
raise Exception, "No such value '%s'" % name
data = self.db_execsql("UPDATE enum SET name='%(newname)s'"
" WHERE type='%(type)s' AND name='%(name)s'" % d)

def _do_enum_remove(self, type, name):
data = self.db_execsql("SELECT name FROM enum"
" WHERE type='%s' AND name='%s'" % (type, name))
if not data:
raise Exception, "No such value '%s'" % name
data = self.db_execsql("DELETE FROM enum WHERE type='%s' AND name='%s'"
% (type, name))


## Milestone
_help_milestone = [('milestone list', 'Show milestones'),
('milestone add <name> [time]', 'Add milestone'),
('milestone rename <name> <newname>',
'Rename milestone'),
('milestone time <name> <time>', 'Set milestone date (Format: "Jun 3, 2003")'),
('milestone remove <name>', 'Remove milestone')]

def complete_milestone (self, text, line, begidx, endidx):

if begidx in [15,17]:
comp = self.get_milestone_list ()
elif begidx < 15:
comp = ['list','add','rename','time','remove']
return self.word_complete(text, comp)

def do_milestone(self, line):
self._do_mile_ver('milestone', line)


## Version
_help_version = [('version list', 'Show versions'),
('version add <name> [time]', 'Add version'),
('version rename <name> <newname>',
'Rename version'),
('version time <name> <time>', 'Set version date (Format: "Jun 3, 2003")'),
('version remove <name>', 'Remove version')]

def complete_version (self, text, line, begidx, endidx):

if begidx in [15,17]:
comp = self.get_version_list ()
elif begidx < 15:
comp = ['list','add','rename','time','remove']
return self.word_complete(text, comp)

def do_version(self, line):
self._do_mile_ver('version', line)

# Milestone and Version are identical, methods

def _do_mile_ver(self, type, line):
arg = self.arg_tokenize(line)
try:
if arg[0] == 'list':
self._do_mile_ver_list(type)
elif arg[0] == 'add' and len(arg) in [2,3]:
name = arg[1]
self._do_mile_ver_add(type, name)
if len(arg) == 3:
time = arg[2]
self._do_mile_ver_time(type, name, time)
elif arg[0] == 'rename' and len(arg)==3:
name = arg[1]
newname = arg[2]
self._do_mile_ver_rename(type, name, newname)
elif arg[0] == 'time' and len(arg)==3:
name = arg[1]
time = arg[2]
self._do_mile_ver_time(type, name, time)
elif arg[0] == 'remove' and len(arg)==2:
name = arg[1]
self._do_mile_ver_remove(type, name)
else:
self.do_help (type)
except Exception, e:
print 'Command %s failed:' % arg[0], e

def _do_mile_ver_list(self, type):
data = self.db_execsql("SELECT name,time FROM %s ORDER BY time,name"
% type)
data = map(lambda x: (x[0], x[1] and time.strftime('%c', time.localtime(x[1]))), data)
print data
self.print_listing(['Name', 'Time'], data)

def _do_mile_ver_rename(self, type, name, newname):
d = {'name':name, 'newname':newname, 'type':type}
data = self.db_execsql("SELECT name FROM %(type)s"
" WHERE name='%(name)s'" % d)
if not data:
raise Exception, "No such %s '%s'" % (type, name)
data = self.db_execsql("UPDATE %(type)s SET name='%(newname)s'"
" WHERE name='%(name)s'" % d)

def _do_mile_ver_add(self, type, name):
sql = ("INSERT INTO %(type)s('name', 'time') "
" VALUES('%(name)s', 0)"
% {'type':type, 'name':name})
data = self.db_execsql(sql)

def _do_mile_ver_remove(self, type, name):
d = {'name':name, 'type':type}
data = self.db_execsql("SELECT name FROM %(type)s"
" WHERE name='%(name)s'" % d)
if not data:
raise Exception, "No such %s '%s'" % (type, name)
data = self.db_execsql("DELETE FROM %(type)s"
" WHERE name='%(name)s'" % d)

def _do_mile_ver_time(self, type, name, t):
seconds = None
t = t.strip()
if t == 'now':
seconds = int(time.time())
else:
for format in [.'%x %X', '%x, %X', '%X %x', '%X, %x', '%x', '%c',
'%b %d, %Y']:
try:
pt = time.strptime(t, format)
print pt
seconds = str(time.mktime(pt))
except ValueError:
continue
break
if seconds == None:
try:
seconds = int(t)
except ValueError:
pass
if seconds != None:
data = self.db_execsql("UPDATE %s SET time='%s'"
" WHERE name='%s'" % (type, seconds, name))
else:
print >> sys.stderr, 'Unknown time format'

_help_upgrade = [('upgrade', 'Upgrade database to current version.')]
def do_upgrade(self, line):
arg = self.arg_tokenize(line)
do_backup = 1
if arg[0] in ['-b', '--no-backup']:
do_backup = 0
db = self.db_open()
try:
curr = self.__env.get_version()
latest = trac.db_default.db_version
if curr < latest:
print "Upgrade: Upgrading %s to db version %i" % (self.envname, latest)
if do_backup:
print "Upgrade: Backup of old database saved in " \
"%s/db/trac.db.%i.bak" % (self.envname, curr)
else:
print "Upgrade: Backup disabled. Non-existant warranty voided."
self.__env.upgrade(do_backup)
else:
print "Upgrade: Database is up to date, no upgrade necessary."
except Exception, e:
print "Upgrade failed: ",e


## ---------------------------------------------------------------------------

##
## Main
##

def main():
tracadm = TracAdmin()
if len (sys.argv) > 1:
if sys.argv[1] in ['-h','--help','help']:
tracadm.docmd ("help")
elif sys.argv[1] in ['-v','--version','about']:
tracadm.docmd ("about")
else:
tracadm.env_set(sys.argv[1])
if len (sys.argv) > 2:
s_args = ' '.join(["'%s'" % c for c in sys.argv[3:]])
command = sys.argv[2] + ' ' +s_args
tracadm.docmd (command)
else:
while 1:
tracadm.run()
else:
tracadm.docmd ("help")




if __name__ == '__main__':
main()
patch for trac-admin initenv [ In reply to ]
Sorry! It seems that I attached the whole trac-admin script rather than
just the patch.

here is the patch...
-------------- next part --------------
Index: scripts/trac-admin
===================================================================
--- scripts/trac-admin (revision 734)
+++ scripts/trac-admin (working copy)
@@ -398,71 +398,88 @@
(user, action.upper()))

## Initdb
- _help_initenv = [('initenv', 'Create and initializes a new environment')]
+ _help_initenv = [.('initenv', 'Creates and initializes a new environment interactively'),
+ ('initenv <projectname> <repospath> <templatepath>', 'Create new environment from arguments given')]

def do_initdb(self, line):
self.do_initenv(line)
+
+ def collect_init_args(self):
+ returnvals = []
+ print 'Creating a new Trac environment at %s' % self.envname
+ print
+ print 'Trac will first ask a few questions about your environment '
+ print 'in order to initalize and prepare the project database.'
+ print
+ print " Please enter the name of your project."
+ print " This name will be used in page titles and descriptions."
+ print
+ dp = 'My Project'
+ returnvals.append(raw_input('Project Name [%s]> ' % dp) or dp)
+
+ print
+ print ' Please specify the absolute path to the project Subversion repository.'
+ print ' Repository must be local, and trac-admin requires read+write'
+ print ' permission to initialize the Trac database.'
+ print
+ drp = '/var/svn/test'
+ prompt = 'Path to repository [%s]> ' % drp
+ returnvals.append(raw_input(prompt) or drp)
+
+ print
+ print ' Please enter location of Trac page templates.'
+ print ' Default is the location of the site-wide templates' \
+ 'installed with Trac.'
+ print
+ dt = trac.siteconfig.__default_templates_dir__
+ prompt = 'Templates directory [%s]> ' % dt
+ returnvals.append(raw_input(prompt) or dt)
+
+ return returnvals

def do_initenv(self, line):
if self.env_check():
- print "Initdb for '%s' failed.\nDoes aa environment already exist?" % self.envname
+ print "Initdb/initenv for '%s' failed.\nDoes an environment already exist?" % self.envname
return
+
+ arg = self.arg_tokenize(line)
+ project_name = None
+ repository_dir = None
+ templates_dir = None
+
+ if len(arg) == 1:
+ returnvals = self.collect_init_args()
+ project_name = returnvals[0]
+ repository_dir = returnvals[1]
+ templates_dir = returnvals[2]
+ elif len(arg)!= 3:
+ print 'Wrong number of arguments to initenv %d' % len(arg)
+ return
+ else:
+ project_name = arg[0]
+ repository_dir = arg[1]
+ templates_dir = arg[2]
+
+ from svn import util, repos, core
+ core.apr_initialize()
+ pool = core.svn_pool_create(None)
try:
- print 'Creating a new Trac environment at %s' % self.envname
- print
- print 'Trac will first ask a few questions about your environment '
- print 'in order to initalize and prepare the project database.'
- print
- print " Please enter the name of your project."
- print " This name will be used in page titles and descriptions."
- print
- dp = 'My Project'
- project_name = raw_input('Project Name [%s]> ' % dp) or dp
+
+ # Remove any trailing slash or else subversion might abort
+ if not os.path.split(repository_dir)[1]:
+ repository_dir = os.path.split(repository_dir)[0]
+
+ rep = repos.svn_repos_open(repository_dir, pool)
+ fs_ptr = repos.svn_repos_fs(rep)
+ except Exception, e:
+ print repository_dir, 'Repository access error: %s' % e
+ return

- from svn import util, repos, core
- core.apr_initialize()
- pool = core.svn_pool_create(None)
- print
- print ' Please specify the absolute path to the project Subversion repository.'
- print ' Repository must be local, and trac-admin requires read+write'
- print ' permission to initialize the Trac database.'
- print
- while 1:
- try:
- drp = '/var/svn/test'
- prompt = 'Path to repository [%s]> ' % drp
- repository_dir = raw_input(prompt) or drp
-
- # Remove any trailing slash or else subversion might abort
- if not os.path.split(repository_dir)[1]:
- repository_dir = os.path.split(repository_dir)[0]
-
- rep = repos.svn_repos_open(repository_dir, pool)
- fs_ptr = repos.svn_repos_fs(rep)
- break
- except KeyboardInterrupt:
- raise KeyboardInterrupt
- except Exception, e:
- print repository_dir, 'Repository access error: %s' % e
-
- print
- print ' Please enter location of Trac page templates.'
- print ' Default is the location of the site-wide templates' \
- 'installed with Trac.'
+ if not os.access(os.path.join(templates_dir, 'browser.cs'), os.F_OK) or \
+ not os.access(os.path.join(templates_dir, 'ticket.cs'), os.F_OK):
+ print templates_dir, 'doesn\'t look like a Trac templates directory'
+ return

- print
- dt = trac.siteconfig.__default_templates_dir__
- prompt = 'Templates directory [%s]> ' % dt
- while 1:
- templates_dir = raw_input(prompt) or dt
- if os.access(os.path.join(templates_dir, 'browser.cs'), os.F_OK) and \
- os.access(os.path.join(templates_dir, 'ticket.cs'), os.F_OK):
- break
- print templates_dir, 'doesn\'t look like a Trac templates directory'
-
- except KeyboardInterrupt:
- print '\n* initdb aborted'
- return
try:
print 'Creating and Initializing Project'
self.env_create()
patch for trac-admin initenv [ In reply to ]
i think its quite useful, its probably best to put this in a issue in
the trac project website, if its not already there. i see an immediate
use for it in script integrating the creating of trac projects, svn
repos, and mailing lists.

thanks,

-kapil


On Jul 4, 2004, at 6:31 PM, Felix Collins wrote:

> Hi All,
> On the 9th of June I submitted a patch for the trac-admin script to
> allow non-interactive command line calling of the script with the
> initenv subcommand.
>
> This is useful for my situation because I allow users to automatically
> generate a new project (Trac Project and Subversion repository)
> without requiring admin intervention (hopefully).
>
> Nobody had any comments. Did anyone read the patch? Does anyone else
> need to call initenv like this? Was this totally irrelevant to the
> rest of the world?
>
> All comments welcome.
patch for trac-admin initenv [ In reply to ]
Hi All,
On the 9th of June I submitted a patch for the trac-admin script to
allow non-interactive command line calling of the script with the
initenv subcommand.

This is useful for my situation because I allow users to automatically
generate a new project (Trac Project and Subversion repository) without
requiring admin intervention (hopefully).

Nobody had any comments. Did anyone read the patch? Does anyone else
need to call initenv like this? Was this totally irrelevant to the rest
of the world?

All comments welcome.

Regards,
Felix

Updated patch attached.
-------------- next part --------------
Index: scripts/trac-admin
===================================================================
--- scripts/trac-admin (revision 802)
+++ scripts/trac-admin (working copy)
@@ -402,71 +402,88 @@
(user, action.upper()))

## Initdb
- _help_initenv = [('initenv', 'Create and initializes a new environment')]
+ _help_initenv = [.('initenv', 'Creates and initializes a new environment interactively'),
+ ('initenv <projectname> <repospath> <templatepath>', 'Create new environment from arguments given')]

def do_initdb(self, line):
self.do_initenv(line)
+
+ def collect_init_args(self):
+ returnvals = []
+ print 'Creating a new Trac environment at %s' % self.envname
+ print
+ print 'Trac will first ask a few questions about your environment '
+ print 'in order to initalize and prepare the project database.'
+ print
+ print " Please enter the name of your project."
+ print " This name will be used in page titles and descriptions."
+ print
+ dp = 'My Project'
+ returnvals.append(raw_input('Project Name [%s]> ' % dp) or dp)
+
+ print
+ print ' Please specify the absolute path to the project Subversion repository.'
+ print ' Repository must be local, and trac-admin requires read+write'
+ print ' permission to initialize the Trac database.'
+ print
+ drp = '/var/svn/test'
+ prompt = 'Path to repository [%s]> ' % drp
+ returnvals.append(raw_input(prompt) or drp)
+
+ print
+ print ' Please enter location of Trac page templates.'
+ print ' Default is the location of the site-wide templates' \
+ 'installed with Trac.'
+ print
+ dt = trac.siteconfig.__default_templates_dir__
+ prompt = 'Templates directory [%s]> ' % dt
+ returnvals.append(raw_input(prompt) or dt)
+
+ return returnvals

def do_initenv(self, line):
if self.env_check():
- print "Initdb for '%s' failed.\nDoes aa environment already exist?" % self.envname
+ print "Initdb/initenv for '%s' failed.\nDoes an environment already exist?" % self.envname
return
+
+ arg = self.arg_tokenize(line)
+ project_name = None
+ repository_dir = None
+ templates_dir = None
+
+ if len(arg) == 1:
+ returnvals = self.collect_init_args()
+ project_name = returnvals[0]
+ repository_dir = returnvals[1]
+ templates_dir = returnvals[2]
+ elif len(arg)!= 3:
+ print 'Wrong number of arguments to initenv %d' % len(arg)
+ return
+ else:
+ project_name = arg[0]
+ repository_dir = arg[1]
+ templates_dir = arg[2]
+
+ from svn import util, repos, core
+ core.apr_initialize()
+ pool = core.svn_pool_create(None)
try:
- print 'Creating a new Trac environment at %s' % self.envname
- print
- print 'Trac will first ask a few questions about your environment '
- print 'in order to initalize and prepare the project database.'
- print
- print " Please enter the name of your project."
- print " This name will be used in page titles and descriptions."
- print
- dp = 'My Project'
- project_name = raw_input('Project Name [%s]> ' % dp) or dp
+
+ # Remove any trailing slash or else subversion might abort
+ if not os.path.split(repository_dir)[1]:
+ repository_dir = os.path.split(repository_dir)[0]
+
+ rep = repos.svn_repos_open(repository_dir, pool)
+ fs_ptr = repos.svn_repos_fs(rep)
+ except Exception, e:
+ print repository_dir, 'Repository access error: %s' % e
+ return

- from svn import util, repos, core
- core.apr_initialize()
- pool = core.svn_pool_create(None)
- print
- print ' Please specify the absolute path to the project Subversion repository.'
- print ' Repository must be local, and trac-admin requires read+write'
- print ' permission to initialize the Trac database.'
- print
- while 1:
- try:
- drp = '/var/svn/test'
- prompt = 'Path to repository [%s]> ' % drp
- repository_dir = raw_input(prompt) or drp
-
- # Remove any trailing slash or else subversion might abort
- if not os.path.split(repository_dir)[1]:
- repository_dir = os.path.split(repository_dir)[0]
-
- rep = repos.svn_repos_open(repository_dir, pool)
- fs_ptr = repos.svn_repos_fs(rep)
- break
- except KeyboardInterrupt:
- raise KeyboardInterrupt
- except Exception, e:
- print repository_dir, 'Repository access error: %s' % e
-
- print
- print ' Please enter location of Trac page templates.'
- print ' Default is the location of the site-wide templates' \
- 'installed with Trac.'
+ if not os.access(os.path.join(templates_dir, 'browser.cs'), os.F_OK) or \
+ not os.access(os.path.join(templates_dir, 'ticket.cs'), os.F_OK):
+ print templates_dir, 'doesn\'t look like a Trac templates directory'
+ return

- print
- dt = trac.siteconfig.__default_templates_dir__
- prompt = 'Templates directory [%s]> ' % dt
- while 1:
- templates_dir = raw_input(prompt) or dt
- if os.access(os.path.join(templates_dir, 'browser.cs'), os.F_OK) and \
- os.access(os.path.join(templates_dir, 'ticket.cs'), os.F_OK):
- break
- print templates_dir, 'doesn\'t look like a Trac templates directory'
-
- except KeyboardInterrupt:
- print '\n* initdb aborted'
- return
try:
print 'Creating and Initializing Project'
self.env_create()
patch for trac-admin initenv [ In reply to ]
Felix Collins wrote:

> Hi All,
> On the 9th of June I submitted a patch for the trac-admin script to
> allow non-interactive command line calling of the script with the
> initenv subcommand.

I started using Trac just after this. Although I scanned the mailing
list archive, I must not have noticed your post. Sorry.

> This is useful for my situation because I allow users to automatically
> generate a new project (Trac Project and Subversion repository) without
> requiring admin intervention (hopefully).

I agree. I have posted changes which are a superset of yours that move
all of the functionality from trac-admin (just leaving the user
interface) to another file. The idea is that different user interfaces
or scripts can access the administration functions without having to
call trac-admin.

My latest post on this is here:
http://lists.edgewall.com/archive/trac/msg00442.html

> Nobody had any comments. Did anyone read the patch? Does anyone else
> need to call initenv like this? Was this totally irrelevant to the rest
> of the world?

I only received one comment on my changes which was positive. =)

> All comments welcome.

I plan on doing a little bit of cleanup on my changes and maybe even
write a GUI version of trac-admin. Before I do that, I'll take a
look at your patch to make sure that what I provide won't prevent
you from doing what you're doing now.

If you have the time, please take a look a my patch and let me know
if my changes also address your need.

Thanks.

--
Tim Moloney
ManTech Real-time Systems Laboratory
2015 Cattlemen Road \ /
Sarasota, FL 34232 .________\(O)/________.
(941) 377-6775 x208 ' ' O(.)O ' '
patch for trac-admin initenv [ In reply to ]
Tim Moloney wrote:
> I started using Trac just after this. Although I scanned the mailing
> list archive, I must not have noticed your post. Sorry.

Hey, no problem. Sorry if I sounded miffed. I guess there are just not
that many people reading this list yet. The edgewall people must be
busy with the next release of Trac too! ;-)


> I plan on doing a little bit of cleanup on my changes and maybe even
> write a GUI version of trac-admin. Before I do that, I'll take a
> look at your patch to make sure that what I provide won't prevent
> you from doing what you're doing now.

That sounds great! The only requirement that I have is that the tools
are fully scriptable. This is in keeping with the Subversion tools
philosophy and makes it easy to integrate Trac into larger systems in a
one off way.

I tried to look at your patch from the archive but I couldn't seem to
open it. I got the following response (On Win2k)...

D:\Downloads\temp>gzip -d gz00000.gz
gzip: gz00000.gz: not in gzip format


Can I get the patch from somewhere else?

Cheers,
Felix
patch for trac-admin initenv [ In reply to ]
Felix Collins wrote:
> Hi All,
> On the 9th of June I submitted a patch for the trac-admin script to
> allow non-interactive command line calling of the script with the
> initenv subcommand.
>
> This is useful for my situation because I allow users to automatically
> generate a new project (Trac Project and Subversion repository) without
> requiring admin intervention (hopefully).
>
> Nobody had any comments. Did anyone read the patch? Does anyone else
> need to call initenv like this? Was this totally irrelevant to the rest
> of the world?
>
> All comments welcome.
>

Hi,

I was not subscribed to this list at this date and like to know if
you have a webpage for distribution of this script. I'd like to test
it in our business and of course at home.

I think if would be much easier to send the link to the script
instead of the whole file to the complete list.
(I allow you to send it directly to me, of course)

Kind Regards,

Paul
patch for trac-admin initenv [ In reply to ]
Paul Puschmann wrote:
> Hi,
>
> I was not subscribed to this list at this date and like to know if you
> have a webpage for distribution of this script. I'd like to test it in
> our business and of course at home.
>
> I think if would be much easier to send the link to the script instead
> of the whole file to the complete list.
> (I allow you to send it directly to me, of course)
>
> Kind Regards,
>
> Paul


You are probably better off looking at Tim Moloney's work. My effort was
that of a first time Python hacker, although it is working and being
used on our system.

http://lists.edgewall.com/archive/trac/2004-June/000438.html

I think that is the code you are after. Have a read of Tim's posts in
the archive.

HTH,
Felix