Ticket #7241: 7241-trac-admin-wiki-export-r7376.2.diff
| File 7241-trac-admin-wiki-export-r7376.2.diff, 18.2 KB (added by cboos, 4 months ago) |
|---|
-
trac/admin/console.py
37 37 from trac.util.html import html 38 38 from trac.util.text import to_unicode, wrap, unicode_quote, unicode_unquote, \ 39 39 print_table, console_print 40 from trac.util.translation import _ 40 from trac.util.translation import _, ngettext 41 41 from trac.wiki import WikiPage 42 42 from trac.wiki.api import WikiSystem 43 43 from trac.wiki.macros import WikiMacroBase 44 44 45 45 TRAC_VERSION = pkg_resources.get_distribution('Trac').version 46 46 47 def printout(*args): 48 console_print(sys.stdout, *args) 49 50 def printerr(*args): 51 console_print(sys.stderr, *args) 52 47 53 def copytree(src, dst, symlinks=False, skip=[]): 48 54 """Recursively copy a directory tree using copy2() (from shutil.copytree.) 49 55 … … 117 123 except SystemExit: 118 124 raise 119 125 except TracError, e: 120 console_print(sys.stderr, 'Command failed:', e)126 printerr(_("Command failed:", e)) 121 127 rv = 2 122 128 if not self.interactive: 123 129 return rv 124 130 125 131 def run(self): 126 132 self.interactive = True 127 print """Welcome to trac-admin %(version)s133 printout(_("""Welcome to trac-admin %(version)s 128 134 Interactive Trac administration console. 129 135 Copyright (c) 2003-2008 Edgewall Software 130 136 131 137 Type: '?' or 'help' for help on commands. 132 """ % {'version': TRAC_VERSION}138 """, version=TRAC_VERSION)) 133 139 self.cmdloop() 134 140 135 141 ## … … 155 161 self.__env = Environment(self.envname) 156 162 return self.__env 157 163 except Exception, e: 158 console_print(sys.stderr, 'Failed to open environment.', e)164 printerr(_("Failed to open environment.", e)) 159 165 traceback.print_exc() 160 166 sys.exit(1) 161 167 … … 282 288 doc = getattr(self, "_help_" + arg[0]) 283 289 self.print_doc(doc) 284 290 except AttributeError: 285 console_print(sys.stderr, "No documentation found for '%s'" % 286 arg[0]) 291 printerr(_("No documentation found for '%(cmd)s'", cmd=arg[0])) 287 292 else: 288 print 'trac-admin - The Trac Administration Console %s' \289 % TRAC_VERSION293 printout(_("trac-admin - The Trac Administration Console " 294 "%(version)s", version=TRAC_VERSION)) 290 295 if not self.interactive: 291 296 print 292 print "Usage: trac-admin </path/to/projenv> [command [subcommand] [option ...]]\n" 293 print "Invoking trac-admin without command starts "\ 294 "interactive mode." 297 printout(_("Usage: trac-admin </path/to/projenv> " 298 "[command [subcommand] [option ...]]\n") 299 ) 300 printout(_("Invoking trac-admin without command starts " 301 "interactive mode.")) 295 302 self.print_doc(self.all_docs()) 296 303 297 304 … … 427 434 rows.sort() 428 435 print_table(rows, ['User', 'Action']) 429 436 print 430 print 'Available actions:'437 printout(_("Available actions:")) 431 438 actions = self._permsys.get_actions() 432 439 actions.sort() 433 440 text = ', '.join(actions) 434 print wrap(text, initial_indent=' ', subsequent_indent=' ',435 linesep='\n')441 printout(wrap(text, initial_indent=' ', subsequent_indent=' ', 442 linesep='\n')) 436 443 print 437 444 438 445 def _do_permission_add(self, user, action): 439 446 if not self._permsys: 440 447 self._permsys = PermissionSystem(self.env_open()) 441 448 if not action.islower() and not action.isupper(): 442 print 'Group names must be in lower case and actions in upper case' 449 printout(_("Group names must be in lower case and actions in " 450 "upper case")) 443 451 return 444 452 self._permsys.grant_permission(user, action) 445 453 … … 471 479 472 480 def get_initenv_args(self): 473 481 returnvals = [] 474 print 'Creating a new Trac environment at %s' % self.envname 475 print 476 print 'Trac will first ask a few questions about your environment ' 477 print 'in order to initalize and prepare the project database.' 478 print 479 print " Please enter the name of your project." 480 print " This name will be used in page titles and descriptions." 481 print 482 printout(_("Creating a new Trac environment at %(envname)s", 483 envname=self.envname)) 484 printout(_(""" 485 Trac will first ask a few questions about your environment 486 in order to initialize and prepare the project database. 487 488 Please enter the name of your project. 489 This name will be used in page titles and descriptions. 490 """)) 482 491 dp = 'My Project' 483 returnvals.append(raw_input('Project Name [%s]> ' % dp).strip() or dp) 484 print 485 print ' Please specify the connection string for the database to use.' 486 print ' By default, a local SQLite database is created in the environment ' 487 print ' directory. It is also possible to use an already existing ' 488 print ' PostgreSQL database (check the Trac documentation for the exact ' 489 print ' connection string syntax).' 490 print 492 returnvals.append(raw_input(_("Project Name [%(default)s]> ", 493 default=dp)).strip() or dp) 494 printout(_(""" 495 Please specify the connection string for the database to use. 496 By default, a local SQLite database is created in the environment 497 directory. It is also possible to use an already existing 498 PostgreSQL database (check the Trac documentation for the exact 499 connection string syntax). 500 501 """)) 491 502 ddb = 'sqlite:db/trac.db' 492 prompt = 'Database connection string [%s]> ' % ddb503 prompt = _("Database connection string [%(default)s]> ", default=ddb) 493 504 returnvals.append(raw_input(prompt).strip() or ddb) 494 print 495 print ' Please specify the type of version control system,' 496 print ' By default, it will be svn.' 497 print 498 print ' If you don\'t want to use Trac with version control integration, ' 499 print ' choose the default here and don\'t specify a repository directory. ' 500 print ' in the next question.' 501 print 505 printout(_(""" 506 Please specify the type of version control system, 507 By default, it will be svn. 508 509 If you don't want to use Trac with version control integration, 510 choose the default here and don\'t specify a repository directory. 511 in the next question. 512 513 """)) 502 514 drpt = 'svn' 503 prompt = 'Repository type [%s]> ' % drpt515 prompt = _("Repository type [%(default)s]> ", default=drpt) 504 516 returnvals.append(raw_input(prompt).strip() or drpt) 505 print 506 print ' Please specify the absolute path to the version control ' 507 print ' repository, or leave it blank to use Trac without a repository.' 508 print ' You can also set the repository location later.' 509 print 510 prompt = 'Path to repository [/path/to/repos]> ' 517 printout(_(""" 518 Please specify the absolute path to the version control 519 repository, or leave it blank to use Trac without a repository. 520 You can also set the repository location later. 521 522 """)) 523 prompt = _("Path to repository [/path/to/repos]> ") 511 524 returnvals.append(raw_input(prompt).strip()) 512 525 print 513 526 return returnvals 514 527 515 528 def do_initenv(self, line): 516 529 def initenv_error(msg): 517 console_print(sys.stderr, "Initenv for '%s' failed.\n%s" %518 (self.envname, msg))530 printerr(_("Initenv for '%(env)s' failed.", env=self.envname), 531 "\n", msg) 519 532 if self.env_check(): 520 533 initenv_error("Does an environment already exist?") 521 534 return 2 … … 543 556 project_name, db_str, repository_type, repository_dir = arg[:4] 544 557 545 558 try: 546 print 'Creating and Initializing Project'559 printout(_("Creating and Initializing Project")) 547 560 options = [ 548 561 ('trac', 'database', db_str), 549 562 ('trac', 'repository_type', repository_type), … … 557 570 options=options) 558 571 except Exception, e: 559 572 initenv_error('Failed to create environment.') 560 console_print(sys.stderr,e)573 printerr(e) 561 574 traceback.print_exc() 562 575 sys.exit(1) 563 576 564 577 # Add a few default wiki pages 565 print ' Installing default wiki pages'578 printout(_(" Installing default wiki pages")) 566 579 cnx = self.__env.get_db_cnx() 567 580 cursor = cnx.cursor() 568 581 pages_dir = pkg_resources.resource_filename('trac.wiki', … … 574 587 try: 575 588 repos = self.__env.get_repository() 576 589 if repos: 577 print ' Indexing repository'590 printout(_(" Indexing repository")) 578 591 repos.sync(self._resync_feedback) 579 592 except TracError, e: 580 console_print(sys.stderr, "\nWarning:\n")593 printerr("\n", _("Warning:"), "\n") 581 594 if repository_type == "svn": 582 console_print(sys.stderr, 583 "You should install the SVN bindings") 595 printerr(_("You should install the SVN bindings")) 584 596 else: 585 console_print(sys.stderr, 586 "Repository type %s not supported" % 587 repository_type) 597 printerr(_("Repository type %(type)s not supported", 598 type=repository_type)) 588 599 except Exception, e: 589 600 initenv_error(to_unicode(e)) 590 601 traceback.print_exc() 591 602 return 2 592 603 593 print """604 printout(_(""" 594 605 --------------------------------------------------------------------- 595 606 Project environment for '%(project_name)s' created. 596 607 … … 614 625 http://trac.edgewall.org/ 615 626 616 627 Congratulations! 617 """ % dict(project_name=project_name, project_path=self.envname,628 """, project_name=project_name, project_path=self.envname, 618 629 project_dir=os.path.basename(self.envname), 619 config_path=os.path.join(self.envname, 'conf', 'trac.ini')) 630 config_path=os.path.join(self.envname, 'conf', 'trac.ini'))) 620 631 621 632 _help_resync = [('resync', 'Re-synchronize trac with the repository'), 622 633 ('resync <rev>', 'Re-synchronize only the given <rev>')] … … 633 644 rev = argv[0] 634 645 if rev: 635 646 env.get_repository().sync_changeset(rev) 636 print '%s resynced.' % rev647 printout(_("%(rev)s resynced.", rev=rev)) 637 648 return 638 649 from trac.versioncontrol.cache import CACHE_METADATA_KEYS 639 print 'Resyncing repository history... '650 printout(_("Resyncing repository history... ")) 640 651 cnx = self.db_open() 641 652 cursor = cnx.cursor() 642 653 cursor.execute("DELETE FROM revision") … … 649 660 repos = env.get_repository().sync(self._resync_feedback) 650 661 cursor.execute("SELECT count(rev) FROM revision") 651 662 for cnt, in cursor: 652 print cnt, 'revisions cached.', 653 print 'Done.' 663 printout(ngettext("%(num)s revision cached.", 664 "%(num)s revisions cached.", cnt, num=cnt)) 665 printout(_("Done.")) 654 666 655 667 ## Wiki 656 668 _help_wiki = [('wiki list', 'List wiki pages'), … … 749 761 params=(title,)) 750 762 old = list(rows) 751 763 if old and title in create_only: 752 console_print(sys.stdout,' %s already exists.' % title)764 printout(' %s already exists.' % title) 753 765 return False 754 766 if old and data == old[0][0]: 755 console_print(sys.stdout,' %s already up to date.' % title)767 printout(' %s already up to date.' % title) 756 768 return False 757 769 f.close() 758 770 … … 768 780 "ORDER BY version DESC LIMIT 1", params=[page]) 769 781 text = data.next()[0] 770 782 if not filename: 771 print text783 printout(text) 772 784 else: 773 785 if os.path.isfile(filename): 774 786 raise Exception("File '%s' exists" % filename) … … 785 797 raise TracError("%s is not a directory" % dir) 786 798 for p in pages: 787 799 dst = os.path.join(dir, unicode_quote(p, '')) 788 console_print(sys.stdout, " %s => %s" % (p,dst))800 printout(_(" %(src)s => %(dst)s", src=p, dst=dst)) 789 801 self._do_wiki_export(p, dst) 790 802 791 803 def _do_wiki_load(self, dir, cursor=None, ignore=[], create_only=[]): … … 797 809 page = unicode_unquote(page.encode('utf-8')) 798 810 if os.path.isfile(filename): 799 811 if self._do_wiki_import(filename, page, cursor, create_only): 800 print (" %s imported from %s" %801 (filename, page)).encode(cons_charset)812 printout(_(" %(page)s imported from %(filename)s", 813 filename=filename, page=page)) 802 814 803 815 ## Ticket 804 816 _help_ticket = [('ticket remove <number>', 'Remove ticket')] … … 819 831 try: 820 832 number = int(arg[1]) 821 833 except ValueError: 822 console_print(sys.stderr, "<number> must be a number")834 printerr(_("<number> must be a number")) 823 835 return 824 836 self._do_ticket_remove(number) 825 837 else: 826 838 self.do_help ('ticket') 827 839 828 def _do_ticket_remove(self, num ber):829 ticket = Ticket(self.env_open(), num ber)840 def _do_ticket_remove(self, num): 841 ticket = Ticket(self.env_open(), num) 830 842 ticket.delete() 831 print "Ticket %d and all associated data removed." % number843 printout(_("Ticket %(num)s and all associated data removed.", num=num)) 832 844 833 845 834 846 ## (Ticket) Type … … 1121 1133 self.db_open() 1122 1134 1123 1135 if not self.__env.needs_upgrade(): 1124 print "Database is up to date, no upgrade necessary."1136 printout(_("Database is up to date, no upgrade necessary.")) 1125 1137 return 1126 1138 1127 1139 try: … … 1133 1145 "upgrade without doing a backup." % msg) 1134 1146 else: 1135 1147 raise 1136 print 'Upgrade done.'1148 printout(_("Upgrade done.")) 1137 1149 1138 1150 _help_hotcopy = [('hotcopy <backupdir>', 1139 1151 'Make a hot backup copy of an environment')] … … 1155 1167 cursor.execute("UPDATE system SET name=NULL WHERE name IS NULL") 1156 1168 1157 1169 try: 1158 print 'Hotcopying %s to %s ...' % (self.__env.path, dest), 1170 printout(_('Hotcopying %(src)s to %(dst)s ...', 1171 src=self.__env.path, dst=dest)) 1159 1172 db_str = self.__env.config.get('trac', 'database') 1160 1173 prefix, db_path = db_str.split(':', 1) 1161 1174 if prefix == 'sqlite': … … 1169 1182 # Unlock database 1170 1183 cnx.rollback() 1171 1184 1172 print 'Hotcopy done.'1185 printout(_("Hotcopy done.")) 1173 1186 1174 1187 _help_deploy = [('deploy <directory>', 1175 1188 'Extract static resources from Trac and all plugins.')] … … 1190 1203 os.makedirs(target) 1191 1204 os.makedirs(chrome_target) 1192 1205 from trac.web.chrome import Chrome 1193 print 'Copying resources from:'1206 printout(_("Copying resources from:")) 1194 1207 for provider in Chrome(self.env_open()).template_providers: 1195 1208 paths = list(provider.get_htdocs_dirs()) 1196 1209 if not len(paths): 1197 1210 continue 1198 print ' %s.%s' % (provider.__module__, provider.__class__.__name__) 1211 printout(' %s.%s' % (provider.__module__, 1212 provider.__class__.__name__)) 1199 1213 for key, root in paths: 1200 1214 source = os.path.normpath(root) 1201 print ' ', source1215 printout(' ', source) 1202 1216 if os.path.exists(source): 1203 1217 dest = os.path.join(chrome_target, key) 1204 1218 copytree(source, dest) 1205 1219 1206 1220 # Create and copy scripts 1207 1221 os.makedirs(script_target) 1208 print 'Creating scripts.'1222 printout(_("Creating scripts.")) 1209 1223 data = {'env': self.env_open(), 'executable': sys.executable} 1210 1224 for script in ('cgi', 'fcgi', 'wsgi'): 1211 1225 dest = os.path.join(script_target, 'trac.'+script) … … 1260 1274 if args[0] in ('-h', '--help', 'help'): 1261 1275 return admin.onecmd('help') 1262 1276 elif args[0] in ('-v','--version'): 1263 print '%s %s' %(os.path.basename(sys.argv[0]), TRAC_VERSION)1277 printout(os.path.basename(sys.argv[0]), TRAC_VERSION) 1264 1278 else: 1265 1279 env_path = os.path.abspath(args[0]) 1266 1280 try: 1267 1281 unicode(env_path, 'ascii') 1268 1282 except UnicodeDecodeError: 1269 console_print(sys.stderr, _("non-ascii environment path " 1270 "'%(path)s' not supported.", 1271 path=env_path)) 1283 printerr(_("non-ascii environment path '%(path)s' not " 1284 "supported.", path=env_path)) 1272 1285 sys.exit(2) 1273 1286 admin.env_set(env_path) 1274 1287 if len(args) > 1: -
trac/util/text.py
112 112 113 113 def console_print(out, *args): 114 114 cons_charset = getattr(out, 'encoding', None) or 'utf-8' 115 out.write(' '.join([to_unicode(a).encode(cons_charset ) for a in args])+116 '\n')115 out.write(' '.join([to_unicode(a).encode(cons_charset, 'replace') 116 for a in args])+ '\n') 117 117 118 118 # -- Plain text formatting 119 119
