1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#!/usr/bin/python
#-*- coding:utf-8 -*-
# Pybik -- A 3 dimensional magic cube game.
# Copyright © 2009, 2011 B. Clausius <barcc@gmx.de>
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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, see <http://www.gnu.org/licenses/>.
import sys
import getopt
import cProfile
import pstats
def main():
opt_vals = app_opts (sys.argv, file='profile.tmp')
if opt_vals['run']:
run(opt_vals)
if opt_vals['out']:
out(opt_vals)
return 0
def app_opts (argv, **opt_vals):
shortopts = "hn:f:"
shortoptmap = {'h':"help", 'n':"name", 'r':'run', 'o':'out',
'y':'pure-python', 'f':'file'}
longopts = []
for o,opt in shortoptmap.items():
if o not in shortopts:
shortopts += o
if o+':' in shortopts:
longopts.append(opt+'=')
else:
longopts.append(opt)
for o in longopts:
if o.endswith('='):
if not o[:-1] in opt_vals:
opt_vals[o[:-1]] = ''
else:
if not o in opt_vals:
opt_vals[o] = False
#print opt_vals
#print argv
try:
opts,unused_args = getopt.getopt(argv[1:], shortopts, longopts)
except getopt.GetoptError, e:
print e
exit(1)
#print opts,unused_args
for opt,optarg in opts:
if len(opt) == 2 and opt[1] in shortoptmap:
opt = '--'+shortoptmap[opt[1]]
if opt == "--help":
print 'usage:', argv[0], 'options...'
for so,lo in shortoptmap.items():
if o == ':':
continue
elif so+':' in shortopts:
print ' -%s arg, --%s=arg' % (so,lo)
else:
print ' -%s, --%s' % (so,lo)
exit (0)
elif opt[2:] in longopts:
opt_vals[opt[2:]] = True
elif opt[2:]+'=' in longopts:
opt_vals[opt[2:]] = optarg
else:
opt_vals[opt.lstrip('-')] = optarg
#print opt_vals
return opt_vals
def run(opts):
global main_module
import main as main_module
if opts['pure-python']:
statement = 'main_module.main(["", "--solved", "--pure-python"])'
cProfile.run(statement, opts['file'])
else:
statement = 'main_module.main(["", "--solved"])'
cProfile.run(statement, opts['file'])
def out(opts):
p = pstats.Stats(opts['file'])
p.strip_dirs().sort_stats(-1).print_stats(opts['name'])
if __name__ == '__main__':
sys.exit(main())
|