annotate Tools/pybench/CommandLine.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents c57d99a8fc42
children
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
51443
901cb207b1d3 Merged revisions 69411 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47103
diff changeset
14 from __future__ import print_function
901cb207b1d3 Merged revisions 69411 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47103
diff changeset
15
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
16 __copyright__ = """\
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
17 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
18 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
19 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
20 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
21 """
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 __version__ = '1.2'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
24
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
25 import sys, getopt, glob, os, re, traceback
37857
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 ### Helpers
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 def _getopt_flags(options):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
30
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
31 """ 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
32 list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
33
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
34 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
35 s = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
36 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
37 for o in options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
38 if o.prefix == '-':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
39 # short option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
40 s.append(o.name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
41 if o.takes_argument:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
42 s.append(':')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
43 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
44 # long option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
45 if o.takes_argument:
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 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
48 l.append(o.name)
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
49 return ''.join(s), l
37857
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 def invisible_input(prompt='>>> '):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
52
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
53 """ 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
54 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
55
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
56 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
57 import getpass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
58 entry = getpass.getpass(prompt)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
59 if entry is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
60 raise KeyboardInterrupt
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
61 return entry
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 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
64
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
65 """ Open a file using mode.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
66
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
67 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
68 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
69 transparently encoded using the given encoding.
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 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
72
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
73 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
74 if name == 'stdout':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
75 return sys.stdout
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
76 elif name == 'stderr':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
77 return sys.stderr
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
78 elif name == 'stdin':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
79 return sys.stdin
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
80 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
81 if encoding is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
82 import codecs
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
83 f = codecs.open(name, mode, encoding)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
84 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
85 f = open(name, mode)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
86 if 'w' in mode:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40624
diff changeset
87 os.chmod(name, 0o600)
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
88 return f
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 def option_dict(options):
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 """ 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
93 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
94 d = {}
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
95 for option in options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
96 d[option.name] = option
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
97 return d
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
98
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
99 # Alias
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
100 getpasswd = invisible_input
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
101
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
102 _integerRE = re.compile('\s*(-?\d+)\s*$')
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
103 _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
104
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
105 def srange(s,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
106
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
107 integer=_integerRE,
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
108 integerRange=_integerRangeRE):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
109
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
110 """ 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
111 to a Python list.
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 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
114
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
115 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
116 in the string.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
117
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
118 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
119 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
120 append = l.append
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
121 for entry in s.split(','):
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
122 m = integer.match(entry)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
123 if m:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
124 append(int(m.groups()[0]))
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
125 continue
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
126 m = integerRange.match(entry)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
127 if m:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
128 start,end = map(int,m.groups())
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
129 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
130 return l
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 def abspath(path,
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
133
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
134 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
135 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
136
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
137 """ 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
138
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
139 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
140 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
141
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
142 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
143 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
144 path = expandvars(path)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
145 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
146 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
147 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
148 path = expanduser(path)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
149 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
150 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
151 return join(getcwd(), path)
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 ### Option classes
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 class Option:
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 """ Option base class. Takes no argument.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
158
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
159 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
160 default = None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
161 helptext = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
162 prefix = '-'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
163 takes_argument = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
164 has_default = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
165 tab = 15
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 def __init__(self,name,help=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
168
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
169 if not name[:1] == '-':
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
170 raise TypeError('option names must start with "-"')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
171 if name[1:2] == '-':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
172 self.prefix = '--'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
173 self.name = name[2:]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
174 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
175 self.name = name[1:]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
176 if help:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
177 self.help = help
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 def __str__(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
180
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
181 o = self
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
182 name = o.prefix + o.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
183 if o.takes_argument:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
184 name = name + ' arg'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
185 if len(name) > self.tab:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
186 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
187 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
188 name = '%-*s ' % (self.tab, name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
189 description = o.help
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
190 if o.has_default:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
191 description = description + ' (%s)' % o.default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
192 return '%s %s' % (name, description)
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 class ArgumentOption(Option):
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 """ Option that takes an argument.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
197
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
198 An optional default argument can be given.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
199
37857
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 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
202
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
203 # Basemethod
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
204 Option.__init__(self,name,help)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
205
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
206 if default is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
207 self.default = default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
208 self.has_default = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
209 self.takes_argument = 1
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 class SwitchOption(Option):
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 """ 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
214
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 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
217
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
218 # Basemethod
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
219 Option.__init__(self,name,help)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
220
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
221 if default is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
222 self.default = default
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
223 self.has_default = 1
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 ### Application baseclass
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 class Application:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
228
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
229 """ 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
230 parsing.
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
231
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
232 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
233 # Options the program accepts (Option instances)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
234 options = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
235
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
236 # 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
237 preset_options = [SwitchOption('-v',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
238 'generate verbose output'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
239 SwitchOption('-h',
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('--help',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
242 'show this help text'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
243 SwitchOption('--debug',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
244 'enable debugging'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
245 SwitchOption('--copyright',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
246 'show copyright'),
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
247 SwitchOption('--examples',
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
248 'show examples of usage')]
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
249
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
250 # The help layout looks like this:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
251 # [header] - defaults to ''
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 # [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
254 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
255 # options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
256 # [options] - formatted from self.options
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 # [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
259 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
260 # [about] - defaults to ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
261 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
262 # 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
263 # 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
264 # 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
265 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
266
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
267 # Header (default to program name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
268 header = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
269
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
270 # Name (defaults to program name)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
271 name = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
272
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
273 # 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
274 synopsis = '%(name)s [option] files...'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
275
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
276 # Version (optional)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
277 version = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
278
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
279 # 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
280 about = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
281
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
282 # 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
283 examples = ''
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
284
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
285 # Copyright to show
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
286 copyright = __copyright__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
287
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
288 # Apply file globbing ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
289 globbing = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
290
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
291 # Generate debug output ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
292 debug = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
293
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
294 # Generate verbose output ?
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
295 verbose = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
296
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
297 # Internal errors to catch
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
298 InternalError = BaseException
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
299
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
300 # Instance variables:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
301 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
302 # 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
303 files = None # List of passed filenames
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
304 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
305
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
306 def __init__(self,argv=None):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
307
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
308 # Setup application specs
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
309 if argv is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
310 argv = sys.argv
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
311 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
312 if not self.name:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
313 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
314 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
315 self.name = self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
316 if not self.header:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
317 self.header = self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
318 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
319 self.header = self.header
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
320
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
321 # Init .arguments list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
322 self.arguments = argv[1:]
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
323
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
324 # Setup Option mapping
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
325 self.option_map = option_dict(self.options)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
326
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
327 # Append preset options
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
328 for option in self.preset_options:
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
329 if not option.name in self.option_map:
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
330 self.add_option(option)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
331
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
332 # Init .files list
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
333 self.files = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
334
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
335 # Start Application
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
336 rc = 0
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
337 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
338 # Process startup
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
339 rc = self.startup()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
340 if rc is not None:
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
341 raise SystemExit(rc)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
342
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
343 # Parse command line
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
344 rc = self.parse()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
345 if rc is not None:
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
346 raise SystemExit(rc)
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
347
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
348 # Start application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
349 rc = self.main()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
350 if rc is None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
351 rc = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
352
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
353 except SystemExit as rcException:
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
354 rc = rcException
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
355 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
356
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
357 except KeyboardInterrupt:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
358 print()
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
359 print('* User Break')
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
360 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
361 rc = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
362
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
363 except self.InternalError:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
364 print()
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
365 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
366 if self.debug:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
367 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
368 traceback.print_exc(20, sys.stdout)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
369 elif self.verbose:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
370 print(' %s: %s' % sys.exc_info()[:2])
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
371 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
372 rc = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
373
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
374 raise SystemExit(rc)
37857
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 def add_option(self, option):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
377
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
378 """ 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
379
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
380 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
381 executed.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
382
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
383 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
384 self.options.append(option)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
385 self.option_map[option.name] = option
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 def startup(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
388
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
389 """ Set user defined instance variables.
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 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
392 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
393
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 return None
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 def exit(self, rc=0):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
398
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
399 """ Exit the program.
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 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
402 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
403
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
404 """
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
405 raise SystemExit(rc)
37857
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 def parse(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
408
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
409 """ 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
410
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
411 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
412 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
413 for processing.
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 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
416 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
417
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
418 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
419 # Parse arguments
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
420 self.values = values = {}
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
421 for o in self.options:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
422 if o.has_default:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
423 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
424 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
425 values[o.prefix+o.name] = 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
426 flags,lflags = _getopt_flags(self.options)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
427 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
428 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
429 if self.globbing:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
430 l = []
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
431 for f in files:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
432 gf = glob.glob(f)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
433 if not gf:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
434 l.append(f)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
435 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
436 l[len(l):] = gf
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
437 files = l
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
438 self.optionlist = optlist
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
439 self.files = files + self.files
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 38801
diff changeset
440 except getopt.error as why:
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
441 self.help(why)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
442 sys.exit(1)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
443
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
444 # Call file handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
445 rc = self.handle_files(self.files)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
446 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
447 sys.exit(rc)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
448
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
449 # Call option handlers
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
450 for optionname, value in optlist:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
451
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
452 # Try to convert value to integer
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
453 try:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
454 value = int(value)
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
455 except ValueError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
456 pass
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
457
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
458 # 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
459 # instances on the command line)
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
460 handlername = 'handle' + optionname.replace('-', '_')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
461 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
462 handler = getattr(self, handlername)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
463 except AttributeError:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
464 if value == '':
85406
c57d99a8fc42 #18803: fix more typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51443
diff changeset
465 # count the number of occurrences
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
466 if optionname in values:
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
467 values[optionname] = values[optionname] + 1
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 values[optionname] = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
470 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
471 values[optionname] = value
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
472 else:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
473 rc = handler(value)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
474 if rc is not None:
40624
426f1d373cdd Fix pybench so it works -- Larry Hastings.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
475 raise SystemExit(rc)
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
476
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
477 # 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
478 rc = self.check_files(self.files)
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
479 if rc is not None:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
480 sys.exit(rc)
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 def check_files(self,filelist):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
483
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
484 """ 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
485
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
486 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
487 is checking that at least n files are given.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
488
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
489 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
490 process is terminated with the return value as exit code.
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
491
37857
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 return None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
494
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
495 def help(self,note=''):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
496
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
497 self.print_header()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
498 if self.synopsis:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
499 print('Synopsis:')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
500 # To remain backward compatible:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
501 try:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
502 synopsis = self.synopsis % self.name
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
503 except (NameError, KeyError, TypeError):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
504 synopsis = self.synopsis % self.__dict__
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
505 print(' ' + synopsis)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
506 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
507 self.print_options()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
508 if self.version:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
509 print('Version:')
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
510 print(' %s' % self.version)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
511 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
512 if self.about:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
513 about = self.about % self.__dict__
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
514 print(about.strip())
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
515 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
516 if note:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
517 print('-'*72)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
518 print('Note:',note)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
519 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
520
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
521 def notice(self,note):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
522
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
523 print('-'*72)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
524 print('Note:',note)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
525 print('-'*72)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
526 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
527
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
528 def print_header(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
529
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
530 print('-'*72)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
531 print(self.header % self.__dict__)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
532 print('-'*72)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
533 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
534
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
535 def print_options(self):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
536
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
537 options = self.options
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
538 print('Options and default settings:')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
539 if not options:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
540 print(' None')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
541 return
47103
63741c7521aa More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents: 42132
diff changeset
542 int = [x for x in options if x.prefix == '--']
63741c7521aa More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents: 42132
diff changeset
543 short = [x for x in options if x.prefix == '-']
63741c7521aa More 2to3 fixes in the Tools directory. Fixes #2893.
Georg Brandl <georg@python.org>
parents: 42132
diff changeset
544 items = short + int
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
545 for o in options:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
546 print(' ',o)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
547 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
548
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 # Example handlers:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
551 #
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
552 # 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
553 # 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
554 #
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 # File handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
557 def handle_files(self,files):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
558
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
559 """ 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
560 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
561 return None
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
562
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
563 # Short option handler
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
564 def handle_h(self,arg):
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 self.help()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
567 return 0
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
568
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
569 def handle_v(self, value):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
570
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
571 """ Turn on verbose output.
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.verbose = 1
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
574
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
575 # 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
576 def handle__help(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.help()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
579 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
580
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
581 def handle__debug(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
582
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
583 self.debug = 1
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
584 # We don't want to catch internal errors:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
585 class NoErrorToCatch(Exception): pass
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
586 self.InternalError = NoErrorToCatch
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
587
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
588 def handle__copyright(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
589
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
590 self.print_header()
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
591 copyright = self.copyright % self.__dict__
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
592 print(copyright.strip())
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
593 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
594 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
595
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
596 def handle__examples(self,arg):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
597
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
598 self.print_header()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
599 if self.examples:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
600 print('Examples:')
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
601 print()
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
602 examples = self.examples % self.__dict__
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
603 print(examples.strip())
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
604 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
605 else:
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
606 print('No examples available.')
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
607 print()
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
608 return 0
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
609
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
610 def main(self):
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 """ 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
613
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
614 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
615 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
616 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
617 __init__ for further details).
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
618
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
619 """
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
620 return None
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
621
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
622 # Alias
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
623 CommandLine = Application
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
624
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
625 def _test():
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 class MyApplication(Application):
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
628 header = 'Test Application'
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
629 version = __version__
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
630 options = [Option('-v','verbose')]
37903
56089faba056 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37857
diff changeset
631
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
632 def handle_v(self,arg):
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
633 print('VERBOSE, Yeah !')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
634
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
635 cmd = MyApplication()
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
636 if not cmd.values['-h']:
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
637 cmd.help()
42132
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
638 print('files:',cmd.files)
863ed7cd45dc Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
639 print('Bye...')
37857
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
640
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
641 if __name__ == '__main__':
a4d5cefc2de4 Adding pybench 1.3 to the Tools/ directory.
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
642 _test()