@@ -72,6 +72,7 @@ def __init__(self):
7272 self .tracker_home = ''
7373 self .db = None
7474 self .db_uncommitted = False
75+ self .force = None
7576
7677 def get_class (self , classname ):
7778 """Get the class - raise an exception if it doesn't exist.
@@ -378,36 +379,35 @@ def do_install(self, tracker_home, args):
378379 # check for both old- and new-style configs
379380 if list (filter (os .path .exists , [config_ini_file ,
380381 os .path .join (tracker_home , 'config.py' )])):
381- ok = raw_input (_ (
382+ if not self .force :
383+ ok = raw_input (_ (
382384"""WARNING: There appears to be a tracker in "%(tracker_home)s"!
383385If you re-install it, you will lose all the data!
384386Erase it? Y/N: """ ) % locals ())
385- if ok .strip ().lower () != 'y' :
386- return 0
387+ if ok .strip ().lower () != 'y' :
388+ return 0
387389
388390 # clear it out so the install isn't confused
389391 shutil .rmtree (tracker_home )
390392
391393 # select template
392394 templates = self .listTemplates ()
393- template = len (args ) > 1 and args [1 ] or ''
394- if template not in templates :
395- print _ ('Templates:' ), ', ' .join (templates )
396- while template not in templates :
397- template = raw_input (_ ('Select template [classic]: ' )).strip ()
398- if not template :
399- template = 'classic'
395+ template = self ._get_choice (
396+ list_name = _ ('Templates:' ),
397+ prompt = _ ('Select template' ),
398+ options = templates ,
399+ argument = len (args ) > 1 and args [1 ] or '' ,
400+ default = 'classic' )
400401
401402 # select hyperdb backend
402403 import roundup .backends
403404 backends = roundup .backends .list_backends ()
404- backend = len (args ) > 2 and args [2 ] or ''
405- if backend not in backends :
406- print _ ('Back ends:' ), ', ' .join (backends )
407- while backend not in backends :
408- backend = raw_input (_ ('Select backend [anydbm]: ' )).strip ()
409- if not backend :
410- backend = 'anydbm'
405+ backend = self ._get_choice (
406+ list_name = _ ('Back ends:' ),
407+ prompt = _ ('Select backend' ),
408+ options = backends ,
409+ argument = len (args ) > 2 and args [2 ] or '' ,
410+ default = 'anydbm' )
411411 # XXX perform a unit test based on the user's selections
412412
413413 # Process configuration file definitions
@@ -456,6 +456,20 @@ def do_install(self, tracker_home, args):
456456}
457457 return 0
458458
459+ def _get_choice (self , list_name , prompt , options , argument , default = None ):
460+ if default is None :
461+ default = options [0 ] # just pick the first one
462+ if argument in options :
463+ return argument
464+ if self .force :
465+ return default
466+ sys .stdout .write ('%s: %s\n ' % (list_name , ', ' .join (options )))
467+ while argument not in options :
468+ argument = raw_input ('%s [%s]: ' % (prompt , default ))
469+ if not argument :
470+ return default
471+ return argument
472+
459473 def do_genconfig (self , args ):
460474 '' """Usage: genconfig <filename>
461475 Generate a new tracker config file (ini style) with default values
@@ -494,12 +508,13 @@ def do_initialise(self, tracker_home, args):
494508
495509 # is there already a database?
496510 if tracker .exists ():
497- ok = raw_input (_ (
511+ if not self .force :
512+ ok = raw_input (_ (
498513"""WARNING: The database is already initialised!
499514If you re-initialise it, you will lose all the data!
500515Erase it? Y/N: """ ))
501- if ok .strip ().lower () != 'y' :
502- return 0
516+ if ok .strip ().lower () != 'y' :
517+ return 0
503518
504519 # nuke it
505520 tracker .nuke ()
@@ -1449,7 +1464,10 @@ def run_command(self, args):
14491464
14501465 # make sure we have a tracker_home
14511466 while not self .tracker_home :
1452- self .tracker_home = raw_input (_ ('Enter tracker home: ' )).strip ()
1467+ if not self .force :
1468+ self .tracker_home = raw_input (_ ('Enter tracker home: ' )).strip ()
1469+ else :
1470+ self .tracker_home = os .curdir
14531471
14541472 # before we open the db, we may be doing an install or init
14551473 if command == 'initialise' :
0 commit comments