annotate Tools/pybench/CommandLine.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 2167105123e4
children 33b156e118c0 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1 """ CommandLine - Get and parse command line options
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
3 NOTE: This still is very much work in progress !!!
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
4
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
5 Different version are likely to be incompatible.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
6
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
7 TODO:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
8
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
9 * Incorporate the changes made by (see Inbox)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
10 * Add number range option using srange()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
11
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
12 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
13
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
14 __copyright__ = """\
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
15 Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
16 Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
17 See the documentation for further information on copyrights,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
18 or contact the author. All Rights Reserved.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
19 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
20
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
21 __version__ = '1.2'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
22
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
23 import sys, getopt, string, glob, os, re, exceptions, traceback
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
24
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
25 ### Helpers
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
26
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
27 def _getopt_flags(options):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
28
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
29 """ Convert the option list to a getopt flag string and long opt
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
30 list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
31
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
32 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
33 s = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
34 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
35 for o in options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
36 if o.prefix == '-':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
37 # short option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
38 s.append(o.name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
39 if o.takes_argument:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
40 s.append(':')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
41 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
42 # long option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
43 if o.takes_argument:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
44 l.append(o.name+'=')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
45 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
46 l.append(o.name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
47 return string.join(s,''),l
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
48
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
49 def invisible_input(prompt='>>> '):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
50
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
51 """ Get raw input from a terminal without echoing the characters to
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
52 the terminal, e.g. for password queries.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
53
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
54 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
55 import getpass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
56 entry = getpass.getpass(prompt)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
57 if entry is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
58 raise KeyboardInterrupt
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
59 return entry
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
60
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
61 def fileopen(name, mode='wb', encoding=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
62
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
63 """ Open a file using mode.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
64
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
65 Default mode is 'wb' meaning to open the file for writing in
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
66 binary mode. If encoding is given, I/O to and from the file is
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
67 transparently encoded using the given encoding.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
68
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
69 Files opened for writing are chmod()ed to 0600.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
70
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
71 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
72 if name == 'stdout':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
73 return sys.stdout
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
74 elif name == 'stderr':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
75 return sys.stderr
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
76 elif name == 'stdin':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
77 return sys.stdin
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
78 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
79 if encoding is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
80 import codecs
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
81 f = codecs.open(name, mode, encoding)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
82 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
83 f = open(name, mode)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
84 if 'w' in mode:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
85 os.chmod(name, 0600)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
86 return f
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
87
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
88 def option_dict(options):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
89
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
90 """ Return a dictionary mapping option names to Option instances.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
91 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
92 d = {}
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
93 for option in options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
94 d[option.name] = option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
95 return d
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
96
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
97 # Alias
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
98 getpasswd = invisible_input
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
99
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
100 _integerRE = re.compile('\s*(-?\d+)\s*$')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
101 _integerRangeRE = re.compile('\s*(-?\d+)\s*-\s*(-?\d+)\s*$')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
102
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
103 def srange(s,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
104
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
105 split=string.split,integer=_integerRE,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
106 integerRange=_integerRangeRE):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
107
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
108 """ Converts a textual representation of integer numbers and ranges
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
109 to a Python list.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
110
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
111 Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
112
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
113 Values are appended to the created list in the order specified
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
114 in the string.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
115
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
116 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
117 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
118 append = l.append
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
119 for entry in split(s,','):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
120 m = integer.match(entry)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
121 if m:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
122 append(int(m.groups()[0]))
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
123 continue
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
124 m = integerRange.match(entry)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
125 if m:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
126 start,end = map(int,m.groups())
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
127 l[len(l):] = range(start,end+1)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
128 return l
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
129
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
130 def abspath(path,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
131
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
132 expandvars=os.path.expandvars,expanduser=os.path.expanduser,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
133 join=os.path.join,getcwd=os.getcwd):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
134
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
135 """ Return the corresponding absolute path for path.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
136
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
137 path is expanded in the usual shell ways before
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
138 joining it with the current working directory.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
139
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
140 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
141 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
142 path = expandvars(path)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
143 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
144 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
145 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
146 path = expanduser(path)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
147 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
148 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
149 return join(getcwd(), path)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
150
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
151 ### Option classes
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
152
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
153 class Option:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
154
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
155 """ Option base class. Takes no argument.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
156
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
157 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
158 default = None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
159 helptext = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
160 prefix = '-'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
161 takes_argument = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
162 has_default = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
163 tab = 15
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
164
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
165 def __init__(self,name,help=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
166
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
167 if not name[:1] == '-':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
168 raise TypeError,'option names must start with "-"'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
169 if name[1:2] == '-':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
170 self.prefix = '--'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
171 self.name = name[2:]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
172 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
173 self.name = name[1:]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
174 if help:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
175 self.help = help
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
176
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
177 def __str__(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
178
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
179 o = self
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
180 name = o.prefix + o.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
181 if o.takes_argument:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
182 name = name + ' arg'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
183 if len(name) > self.tab:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
184 name = name + '\n' + ' ' * (self.tab + 1 + len(o.prefix))
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
185 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
186 name = '%-*s ' % (self.tab, name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
187 description = o.help
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
188 if o.has_default:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
189 description = description + ' (%s)' % o.default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
190 return '%s %s' % (name, description)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
191
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
192 class ArgumentOption(Option):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
193
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
194 """ Option that takes an argument.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
195
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
196 An optional default argument can be given.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
197
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
198 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
199 def __init__(self,name,help=None,default=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
200
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
201 # Basemethod
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
202 Option.__init__(self,name,help)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
203
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
204 if default is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
205 self.default = default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
206 self.has_default = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
207 self.takes_argument = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
208
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
209 class SwitchOption(Option):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
210
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
211 """ Options that can be on or off. Has an optional default value.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
212
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
213 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
214 def __init__(self,name,help=None,default=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
215
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
216 # Basemethod
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
217 Option.__init__(self,name,help)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
218
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
219 if default is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
220 self.default = default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
221 self.has_default = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
222
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
223 ### Application baseclass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
224
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
225 class Application:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
226
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
227 """ Command line application interface with builtin argument
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
228 parsing.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
229
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
230 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
231 # Options the program accepts (Option instances)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
232 options = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
233
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
234 # Standard settings; these are appended to options in __init__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
235 preset_options = [SwitchOption('-v',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
236 'generate verbose output'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
237 SwitchOption('-h',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
238 'show this help text'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
239 SwitchOption('--help',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
240 'show this help text'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
241 SwitchOption('--debug',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
242 'enable debugging'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
243 SwitchOption('--copyright',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
244 'show copyright'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
245 SwitchOption('--examples',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
246 'show examples of usage')]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
247
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
248 # The help layout looks like this:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
249 # [header] - defaults to ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
250 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
251 # [synopsis] - formatted as '<self.name> %s' % self.synopsis
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
252 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
253 # options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
254 # [options] - formatted from self.options
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
255 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
256 # [version] - formatted as 'Version:\n %s' % self.version, if given
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
257 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
258 # [about] - defaults to ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
259 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
260 # Note: all fields that do not behave as template are formatted
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
261 # using the instances dictionary as substitution namespace,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
262 # e.g. %(name)s will be replaced by the applications name.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
263 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
264
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
265 # Header (default to program name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
266 header = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
267
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
268 # Name (defaults to program name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
269 name = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
270
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
271 # Synopsis (%(name)s is replaced by the program name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
272 synopsis = '%(name)s [option] files...'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
273
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
274 # Version (optional)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
275 version = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
276
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
277 # General information printed after the possible options (optional)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
278 about = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
279
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
280 # Examples of usage to show when the --examples option is given (optional)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
281 examples = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
282
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
283 # Copyright to show
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
284 copyright = __copyright__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
285
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
286 # Apply file globbing ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
287 globbing = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
288
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
289 # Generate debug output ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
290 debug = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
291
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
292 # Generate verbose output ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
293 verbose = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
294
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
295 # Internal errors to catch
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
296 InternalError = exceptions.Exception
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
297
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
298 # Instance variables:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
299 values = None # Dictionary of passed options (or default values)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
300 # indexed by the options name, e.g. '-h'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
301 files = None # List of passed filenames
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
302 optionlist = None # List of passed options
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
303
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
304 def __init__(self,argv=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
305
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
306 # Setup application specs
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
307 if argv is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
308 argv = sys.argv
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
309 self.filename = os.path.split(argv[0])[1]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
310 if not self.name:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
311 self.name = os.path.split(self.filename)[1]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
312 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
313 self.name = self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
314 if not self.header:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
315 self.header = self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
316 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
317 self.header = self.header
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
318
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
319 # Init .arguments list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
320 self.arguments = argv[1:]
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
321
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
322 # Setup Option mapping
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
323 self.option_map = option_dict(self.options)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
324
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
325 # Append preset options
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
326 for option in self.preset_options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
327 if not self.option_map.has_key(option.name):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
328 self.add_option(option)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
329
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
330 # Init .files list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
331 self.files = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
332
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
333 # Start Application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
334 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
335 # Process startup
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
336 rc = self.startup()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
337 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
338 raise SystemExit,rc
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
339
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
340 # Parse command line
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
341 rc = self.parse()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
342 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
343 raise SystemExit,rc
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
344
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
345 # Start application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
346 rc = self.main()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
347 if rc is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
348 rc = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
349
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
350 except SystemExit,rc:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
351 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
352
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
353 except KeyboardInterrupt:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
354 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
355 print '* User Break'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
356 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
357 rc = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
358
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
359 except self.InternalError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
360 print
38801
2167105123e4 Updated to pybench 2.0.
Marc-André Lemburg <mal@egenix.com>
parents: 37903
diff changeset
361 print '* Internal Error (use --debug to display the traceback)'
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
362 if self.debug:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
363 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
364 traceback.print_exc(20, sys.stdout)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
365 elif self.verbose:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
366 print ' %s: %s' % sys.exc_info()[:2]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
367 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
368 rc = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
369
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
370 raise SystemExit,rc
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
371
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
372 def add_option(self, option):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
373
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
374 """ Add a new Option instance to the Application dynamically.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
375
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
376 Note that this has to be done *before* .parse() is being
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
377 executed.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
378
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
379 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
380 self.options.append(option)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
381 self.option_map[option.name] = option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
382
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
383 def startup(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
384
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
385 """ Set user defined instance variables.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
386
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
387 If this method returns anything other than None, the
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
388 process is terminated with the return value as exit code.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
389
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
390 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
391 return None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
392
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
393 def exit(self, rc=0):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
394
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
395 """ Exit the program.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
396
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
397 rc is used as exit code and passed back to the calling
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
398 program. It defaults to 0 which usually means: OK.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
399
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
400 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
401 raise SystemExit, rc
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
402
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
403 def parse(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
404
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
405 """ Parse the command line and fill in self.values and self.files.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
406
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
407 After having parsed the options, the remaining command line
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
408 arguments are interpreted as files and passed to .handle_files()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
409 for processing.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
410
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
411 As final step the option handlers are called in the order
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
412 of the options given on the command line.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
413
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
414 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
415 # Parse arguments
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
416 self.values = values = {}
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
417 for o in self.options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
418 if o.has_default:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
419 values[o.prefix+o.name] = o.default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
420 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
421 values[o.prefix+o.name] = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
422 flags,lflags = _getopt_flags(self.options)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
423 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
424 optlist,files = getopt.getopt(self.arguments,flags,lflags)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
425 if self.globbing:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
426 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
427 for f in files:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
428 gf = glob.glob(f)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
429 if not gf:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
430 l.append(f)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
431 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
432 l[len(l):] = gf
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
433 files = l
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
434 self.optionlist = optlist
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
435 self.files = files + self.files
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
436 except getopt.error,why:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
437 self.help(why)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
438 sys.exit(1)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
439
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
440 # Call file handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
441 rc = self.handle_files(self.files)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
442 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
443 sys.exit(rc)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
444
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
445 # Call option handlers
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
446 for optionname, value in optlist:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
447
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
448 # Try to convert value to integer
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
449 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
450 value = string.atoi(value)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
451 except ValueError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
452 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
453
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
454 # Find handler and call it (or count the number of option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
455 # instances on the command line)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
456 handlername = 'handle' + string.replace(optionname, '-', '_')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
457 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
458 handler = getattr(self, handlername)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
459 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
460 if value == '':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
461 # count the number of occurances
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
462 if values.has_key(optionname):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
463 values[optionname] = values[optionname] + 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
464 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
465 values[optionname] = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
466 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
467 values[optionname] = value
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
468 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
469 rc = handler(value)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
470 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
471 raise SystemExit, rc
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
472
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
473 # Apply final file check (for backward compatibility)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
474 rc = self.check_files(self.files)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
475 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
476 sys.exit(rc)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
477
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
478 def check_files(self,filelist):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
479
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
480 """ Apply some user defined checks on the files given in filelist.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
481
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
482 This may modify filelist in place. A typical application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
483 is checking that at least n files are given.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
484
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
485 If this method returns anything other than None, the
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
486 process is terminated with the return value as exit code.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
487
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
488 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
489 return None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
490
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
491 def help(self,note=''):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
492
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
493 self.print_header()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
494 if self.synopsis:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
495 print 'Synopsis:'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
496 # To remain backward compatible:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
497 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
498 synopsis = self.synopsis % self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
499 except (NameError, KeyError, TypeError):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
500 synopsis = self.synopsis % self.__dict__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
501 print ' ' + synopsis
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
502 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
503 self.print_options()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
504 if self.version:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
505 print 'Version:'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
506 print ' %s' % self.version
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
507 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
508 if self.about:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
509 print string.strip(self.about % self.__dict__)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
510 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
511 if note:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
512 print '-'*72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
513 print 'Note:',note
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
514 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
515
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
516 def notice(self,note):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
517
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
518 print '-'*72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
519 print 'Note:',note
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
520 print '-'*72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
521 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
522
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
523 def print_header(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
524
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
525 print '-'*72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
526 print self.header % self.__dict__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
527 print '-'*72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
528 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
529
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
530 def print_options(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
531
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
532 options = self.options
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
533 print 'Options and default settings:'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
534 if not options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
535 print ' None'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
536 return
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
537 long = filter(lambda x: x.prefix == '--', options)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
538 short = filter(lambda x: x.prefix == '-', options)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
539 items = short + long
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
540 for o in options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
541 print ' ',o
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
542 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
543
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
544 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
545 # Example handlers:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
546 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
547 # If a handler returns anything other than None, processing stops
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
548 # and the return value is passed to sys.exit() as argument.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
549 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
550
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
551 # File handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
552 def handle_files(self,files):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
553
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
554 """ This may process the files list in place.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
555 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
556 return None
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
557
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
558 # Short option handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
559 def handle_h(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
560
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
561 self.help()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
562 return 0
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
563
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
564 def handle_v(self, value):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
565
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
566 """ Turn on verbose output.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
567 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
568 self.verbose = 1
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
569
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
570 # Handlers for long options have two underscores in their name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
571 def handle__help(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
572
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
573 self.help()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
574 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
575
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
576 def handle__debug(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
577
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
578 self.debug = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
579 # We don't want to catch internal errors:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
580 self.InternalError = None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
581
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
582 def handle__copyright(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
583
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
584 self.print_header()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
585 print string.strip(self.copyright % self.__dict__)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
586 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
587 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
588
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
589 def handle__examples(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
590
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
591 self.print_header()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
592 if self.examples:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
593 print 'Examples:'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
594 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
595 print string.strip(self.examples % self.__dict__)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
596 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
597 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
598 print 'No examples available.'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
599 print
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
600 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
601
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
602 def main(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
603
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
604 """ Override this method as program entry point.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
605
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
606 The return value is passed to sys.exit() as argument. If
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
607 it is None, 0 is assumed (meaning OK). Unhandled
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
608 exceptions are reported with exit status code 1 (see
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
609 __init__ for further details).
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
610
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
611 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
612 return None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
613
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
614 # Alias
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
615 CommandLine = Application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
616
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
617 def _test():
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
618
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
619 class MyApplication(Application):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
620 header = 'Test Application'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
621 version = __version__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
622 options = [Option('-v','verbose')]
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
623
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
624 def handle_v(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
625 print 'VERBOSE, Yeah !'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
626
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
627 cmd = MyApplication()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
628 if not cmd.values['-h']:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
629 cmd.help()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
630 print 'files:',cmd.files
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
631 print 'Bye...'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
632
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
633 if __name__ == '__main__':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
634 _test()