Mercurial > public > mercurial-scm > hg
comparison mercurial/commands.py @ 4546:5c8130691692
dispatch: reduce nesting by moving parsing exceptions
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 11 Jun 2007 21:09:23 -0500 |
parents | aea8fd7fb5e2 |
children | 8774d2cafe4d |
comparison
equal
deleted
inserted
replaced
4545:aea8fd7fb5e2 | 4546:5c8130691692 |
---|---|
3239 extensions.loadall(u) | 3239 extensions.loadall(u) |
3240 u.addreadhook(extensions.loadall) | 3240 u.addreadhook(extensions.loadall) |
3241 | 3241 |
3242 try: | 3242 try: |
3243 cmd, func, args, options, cmdoptions = parse(u, args) | 3243 cmd, func, args, options, cmdoptions = parse(u, args) |
3244 if options["encoding"]: | |
3245 util._encoding = options["encoding"] | |
3246 if options["encodingmode"]: | |
3247 util._encodingmode = options["encodingmode"] | |
3248 if options["time"]: | |
3249 def get_times(): | |
3250 t = os.times() | |
3251 if t[4] == 0.0: # Windows leaves this as zero, so use time.clock() | |
3252 t = (t[0], t[1], t[2], t[3], time.clock()) | |
3253 return t | |
3254 s = get_times() | |
3255 def print_time(): | |
3256 t = get_times() | |
3257 u.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") % | |
3258 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])) | |
3259 atexit.register(print_time) | |
3260 | |
3261 # enter the debugger before command execution | |
3262 if options['debugger']: | |
3263 pdb.set_trace() | |
3264 | |
3265 try: | |
3266 if options['cwd']: | |
3267 os.chdir(options['cwd']) | |
3268 | |
3269 u.updateopts(options["verbose"], options["debug"], options["quiet"], | |
3270 not options["noninteractive"], options["traceback"], | |
3271 parseconfig(options["config"])) | |
3272 | |
3273 path = u.expandpath(options["repository"]) or "" | |
3274 repo = path and hg.repository(u, path=path) or None | |
3275 if repo and not repo.local(): | |
3276 raise util.Abort(_("repository '%s' is not local") % path) | |
3277 | |
3278 if options['help']: | |
3279 return help_(u, cmd, options['version']) | |
3280 elif options['version']: | |
3281 return version_(u) | |
3282 elif not cmd: | |
3283 return help_(u, 'shortlist') | |
3284 | |
3285 if cmd not in norepo.split(): | |
3286 try: | |
3287 if not repo: | |
3288 repo = hg.repository(u, path=path) | |
3289 u = repo.ui | |
3290 except hg.RepoError: | |
3291 if cmd not in optionalrepo.split(): | |
3292 raise | |
3293 d = lambda: func(u, repo, *args, **cmdoptions) | |
3294 else: | |
3295 d = lambda: func(u, *args, **cmdoptions) | |
3296 | |
3297 try: | |
3298 if options['profile']: | |
3299 import hotshot, hotshot.stats | |
3300 prof = hotshot.Profile("hg.prof") | |
3301 try: | |
3302 try: | |
3303 return prof.runcall(d) | |
3304 except: | |
3305 try: | |
3306 u.warn(_('exception raised - generating ' | |
3307 'profile anyway\n')) | |
3308 except: | |
3309 pass | |
3310 raise | |
3311 finally: | |
3312 prof.close() | |
3313 stats = hotshot.stats.load("hg.prof") | |
3314 stats.strip_dirs() | |
3315 stats.sort_stats('time', 'calls') | |
3316 stats.print_stats(40) | |
3317 elif options['lsprof']: | |
3318 try: | |
3319 from mercurial import lsprof | |
3320 except ImportError: | |
3321 raise util.Abort(_( | |
3322 'lsprof not available - install from ' | |
3323 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) | |
3324 p = lsprof.Profiler() | |
3325 p.enable(subcalls=True) | |
3326 try: | |
3327 return d() | |
3328 finally: | |
3329 p.disable() | |
3330 stats = lsprof.Stats(p.getstats()) | |
3331 stats.sort() | |
3332 stats.pprint(top=10, file=sys.stderr, climit=5) | |
3333 else: | |
3334 return d() | |
3335 finally: | |
3336 u.flush() | |
3337 except: | |
3338 # enter the debugger when we hit an exception | |
3339 if options['debugger']: | |
3340 pdb.post_mortem(sys.exc_info()[2]) | |
3341 u.print_exc() | |
3342 raise | |
3343 except ParseError, inst: | 3244 except ParseError, inst: |
3344 if inst.args[0]: | 3245 if inst.args[0]: |
3345 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) | 3246 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) |
3346 help_(u, inst.args[0]) | 3247 help_(u, inst.args[0]) |
3347 else: | 3248 else: |
3348 u.warn(_("hg: %s\n") % inst.args[1]) | 3249 u.warn(_("hg: %s\n") % inst.args[1]) |
3349 help_(u, 'shortlist') | 3250 help_(u, 'shortlist') |
3251 return -1 | |
3350 except AmbiguousCommand, inst: | 3252 except AmbiguousCommand, inst: |
3351 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") % | 3253 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") % |
3352 (inst.args[0], " ".join(inst.args[1]))) | 3254 (inst.args[0], " ".join(inst.args[1]))) |
3255 return -1 | |
3353 except UnknownCommand, inst: | 3256 except UnknownCommand, inst: |
3354 u.warn(_("hg: unknown command '%s'\n") % inst.args[0]) | 3257 u.warn(_("hg: unknown command '%s'\n") % inst.args[0]) |
3355 help_(u, 'shortlist') | 3258 help_(u, 'shortlist') |
3259 return -1 | |
3260 | |
3261 if options["encoding"]: | |
3262 util._encoding = options["encoding"] | |
3263 if options["encodingmode"]: | |
3264 util._encodingmode = options["encodingmode"] | |
3265 if options["time"]: | |
3266 def get_times(): | |
3267 t = os.times() | |
3268 if t[4] == 0.0: # Windows leaves this as zero, so use time.clock() | |
3269 t = (t[0], t[1], t[2], t[3], time.clock()) | |
3270 return t | |
3271 s = get_times() | |
3272 def print_time(): | |
3273 t = get_times() | |
3274 u.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") % | |
3275 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])) | |
3276 atexit.register(print_time) | |
3277 | |
3278 # enter the debugger before command execution | |
3279 if options['debugger']: | |
3280 pdb.set_trace() | |
3281 | |
3282 try: | |
3283 if options['cwd']: | |
3284 os.chdir(options['cwd']) | |
3285 | |
3286 u.updateopts(options["verbose"], options["debug"], options["quiet"], | |
3287 not options["noninteractive"], options["traceback"], | |
3288 parseconfig(options["config"])) | |
3289 | |
3290 path = u.expandpath(options["repository"]) or "" | |
3291 repo = path and hg.repository(u, path=path) or None | |
3292 if repo and not repo.local(): | |
3293 raise util.Abort(_("repository '%s' is not local") % path) | |
3294 | |
3295 if options['help']: | |
3296 return help_(u, cmd, options['version']) | |
3297 elif options['version']: | |
3298 return version_(u) | |
3299 elif not cmd: | |
3300 return help_(u, 'shortlist') | |
3301 | |
3302 if cmd not in norepo.split(): | |
3303 try: | |
3304 if not repo: | |
3305 repo = hg.repository(u, path=path) | |
3306 u = repo.ui | |
3307 except hg.RepoError: | |
3308 if cmd not in optionalrepo.split(): | |
3309 raise | |
3310 d = lambda: func(u, repo, *args, **cmdoptions) | |
3311 else: | |
3312 d = lambda: func(u, *args, **cmdoptions) | |
3313 | |
3314 try: | |
3315 if options['profile']: | |
3316 import hotshot, hotshot.stats | |
3317 prof = hotshot.Profile("hg.prof") | |
3318 try: | |
3319 try: | |
3320 return prof.runcall(d) | |
3321 except: | |
3322 try: | |
3323 u.warn(_('exception raised - generating ' | |
3324 'profile anyway\n')) | |
3325 except: | |
3326 pass | |
3327 raise | |
3328 finally: | |
3329 prof.close() | |
3330 stats = hotshot.stats.load("hg.prof") | |
3331 stats.strip_dirs() | |
3332 stats.sort_stats('time', 'calls') | |
3333 stats.print_stats(40) | |
3334 elif options['lsprof']: | |
3335 try: | |
3336 from mercurial import lsprof | |
3337 except ImportError: | |
3338 raise util.Abort(_( | |
3339 'lsprof not available - install from ' | |
3340 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) | |
3341 p = lsprof.Profiler() | |
3342 p.enable(subcalls=True) | |
3343 try: | |
3344 return d() | |
3345 finally: | |
3346 p.disable() | |
3347 stats = lsprof.Stats(p.getstats()) | |
3348 stats.sort() | |
3349 stats.pprint(top=10, file=sys.stderr, climit=5) | |
3350 else: | |
3351 return d() | |
3352 finally: | |
3353 u.flush() | |
3354 except: | |
3355 # enter the debugger when we hit an exception | |
3356 if options['debugger']: | |
3357 pdb.post_mortem(sys.exc_info()[2]) | |
3358 u.print_exc() | |
3359 raise |