Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d17a7a

Browse files
committed
Patch from Rene Liebscher: this adds "--help-foo" options to list the
values that "--foo" can take for various commands: eg. what formats for "sdist" and "bdist", what compilers for "build_ext" and "build_clib". I have *not* reviewed this patch; I'm checking it in as-is because it also fixes a paper-bag-over-head bug in bdist.py, and because I won't have time to review it properly for several days: so someone else can test it for me, instead!
1 parent 1169687 commit 9d17a7a

8 files changed

Lines changed: 107 additions & 25 deletions

File tree

Lib/distutils/archive_util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def visit (z, dirname, names):
110110

111111

112112
ARCHIVE_FORMATS = {
113-
'gztar': (make_tarball, [('compress', 'gzip')]),
114-
'bztar': (make_tarball, [('compress', 'bzip2')]),
115-
'ztar': (make_tarball, [('compress', 'compress')]),
116-
'tar': (make_tarball, [('compress', None)]),
117-
'zip': (make_zipfile, [])
113+
'gztar': (make_tarball, [('compress', 'gzip')],"gzipped tar-file"),
114+
'bztar': (make_tarball, [('compress', 'bzip2')],"bzip2-ed tar-file"),
115+
'ztar': (make_tarball, [('compress', 'compress')],"compressed tar-file"),
116+
'tar': (make_tarball, [('compress', None)],"uncompressed tar-file"),
117+
'zip': (make_zipfile, [],"zip-file")
118118
}
119119

120120
def check_archive_formats (formats):

Lib/distutils/ccompiler.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,22 @@ def mkpath (self, name, mode=0777):
726726
# Map compiler types to (module_name, class_name) pairs -- ie. where to
727727
# find the code that implements an interface to this compiler. (The module
728728
# is assumed to be in the 'distutils' package.)
729-
compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler'),
730-
'msvc': ('msvccompiler', 'MSVCCompiler'),
729+
compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler',"standard UNIX-style compiler"),
730+
'msvc': ('msvccompiler', 'MSVCCompiler',"Microsoft Visual C++"),
731+
'cygwin': ('cygwinccompiler', 'CygwinCCompiler',"Cygwin-Gnu-Win32-C-Compiler"),
732+
'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',"MinGW32-C-Compiler (or cygwin in this mode)"),
731733
}
732734

735+
# prints all possible arguments to --compiler
736+
def show_compilers():
737+
from distutils.fancy_getopt import FancyGetopt
738+
list_of_compilers=[]
739+
for compiler in compiler_class.keys():
740+
list_of_compilers.append(("compiler="+compiler,None,compiler_class[compiler][2]))
741+
list_of_compilers.sort()
742+
pretty_printer=FancyGetopt(list_of_compilers)
743+
pretty_printer.print_help("List of available compilers:")
744+
733745

734746
def new_compiler (plat=None,
735747
compiler=None,
@@ -755,7 +767,7 @@ def new_compiler (plat=None,
755767
if compiler is None:
756768
compiler = default_compiler[plat]
757769

758-
(module_name, class_name) = compiler_class[compiler]
770+
(module_name, class_name,long_description) = compiler_class[compiler]
759771
except KeyError:
760772
msg = "don't know how to compile C/C++ code on platform '%s'" % plat
761773
if compiler is not None:

Lib/distutils/command/bdist.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class bdist (Command):
2121
user_options = [('bdist-base=', 'b',
2222
"temporary directory for creating built distributions"),
2323
('formats=', None,
24-
"formats for distribution " +
25-
"(gztar, bztar, zip, rpm, ... )"),
24+
"formats for distribution"),
2625
]
2726

2827
# The following commands do not take a format option from bdist
@@ -33,12 +32,28 @@ class bdist (Command):
3332
default_format = { 'posix': 'gztar',
3433
'nt': 'zip', }
3534

36-
format_command = { 'gztar': 'bdist_dumb',
37-
'bztar': 'bdist_dumb',
38-
'ztar': 'bdist_dumb',
39-
'tar': 'bdist_dumb',
40-
'rpm': 'bdist_rpm',
41-
'zip': 'bdist_dumb', }
35+
format_command = { 'gztar': ('bdist_dumb',"gzipped tar-file"),
36+
'bztar': ('bdist_dumb',"bzip2-ed tar-file"),
37+
'ztar': ('bdist_dumb',"compressed tar-file"),
38+
'tar': ('bdist_dumb',"tar-file"),
39+
'rpm': ('bdist_rpm',"rpm distribution"),
40+
'zip': ('bdist_dumb',"zip-file"),
41+
}
42+
43+
# prints all possible arguments to --format
44+
def show_formats():
45+
from distutils.fancy_getopt import FancyGetopt
46+
list_of_formats=[]
47+
for format in bdist.format_command.keys():
48+
list_of_formats.append(("formats="+format,None,bdist.format_command[format][1]))
49+
list_of_formats.sort()
50+
pretty_printer=FancyGetopt(list_of_formats)
51+
pretty_printer.print_help("List of available distribution formats:")
52+
53+
help_options = [
54+
('help-formats', None,
55+
"lists available distribution formats",show_formats),
56+
]
4257

4358

4459
def initialize_options (self):
@@ -74,14 +89,14 @@ def run (self):
7489
for format in self.formats:
7590

7691
try:
77-
cmd_name = self.format_command[self.format]
92+
cmd_name = self.format_command[format][0]
7893
except KeyError:
7994
raise DistutilsOptionError, \
80-
"invalid format '%s'" % self.format
95+
"invalid format '%s'" % format
8196

8297
sub_cmd = self.reinitialize_command(cmd_name)
8398
if cmd_name not in self.no_format_option:
84-
sub_cmd.format = self.format
99+
sub_cmd.format = format
85100
self.run_command (cmd_name)
86101

87102
# run()

Lib/distutils/command/build.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys, os
1010
from distutils.core import Command
1111
from distutils.util import get_platform
12+
from distutils.ccompiler import show_compilers
1213

1314
class build (Command):
1415

@@ -35,6 +36,10 @@ class build (Command):
3536
('force', 'f',
3637
"forcibly build everything (ignore file timestamps)"),
3738
]
39+
help_options = [
40+
('help-compiler', None,
41+
"lists available compilers",show_compilers),
42+
]
3843

3944
def initialize_options (self):
4045
self.build_base = 'build'

Lib/distutils/command/build_clib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from types import *
2424
from distutils.core import Command
2525
from distutils.errors import *
26-
from distutils.ccompiler import new_compiler
26+
from distutils.ccompiler import new_compiler,show_compilers
2727

2828

2929
class build_clib (Command):
@@ -42,6 +42,10 @@ class build_clib (Command):
4242
('compiler=', 'c',
4343
"specify the compiler type"),
4444
]
45+
help_options = [
46+
('help-compiler', None,
47+
"lists available compilers",show_compilers),
48+
]
4549

4650
def initialize_options (self):
4751
self.build_clib = None

Lib/distutils/command/build_ext.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from distutils.errors import *
1515
from distutils.dep_util import newer_group
1616
from distutils.extension import Extension
17+
from distutils.ccompiler import show_compilers
1718

1819
# An extension name is just a dot-separated list of Python NAMEs (ie.
1920
# the same as a fully-qualified module name).
@@ -72,6 +73,10 @@ class build_ext (Command):
7273
('compiler=', 'c',
7374
"specify the compiler type"),
7475
]
76+
help_options = [
77+
('help-compiler', None,
78+
"lists available compilers",show_compilers),
79+
]
7580

7681

7782
def initialize_options (self):

Lib/distutils/command/sdist.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from distutils.core import Command
1414
from distutils.util import newer, create_tree, remove_tree, convert_path, \
1515
write_file
16-
from distutils.archive_util import check_archive_formats
16+
from distutils.archive_util import check_archive_formats,ARCHIVE_FORMATS
1717
from distutils.text_file import TextFile
1818
from distutils.errors import DistutilsExecError, DistutilsOptionError
1919

@@ -35,11 +35,26 @@ class sdist (Command):
3535
('force-manifest', 'f',
3636
"forcibly regenerate the manifest and carry on as usual"),
3737
('formats=', None,
38-
"formats for source distribution (tar, ztar, gztar, bztar, or zip)"),
38+
"formats for source distribution"),
3939
('keep-tree', 'k',
4040
"keep the distribution tree around after creating " +
4141
"archive file(s)"),
4242
]
43+
# prints all possible arguments to --formats
44+
def show_formats():
45+
from distutils.fancy_getopt import FancyGetopt
46+
list_of_formats=[]
47+
for format in ARCHIVE_FORMATS.keys():
48+
list_of_formats.append(("formats="+format,None,ARCHIVE_FORMATS[format][2]))
49+
list_of_formats.sort()
50+
pretty_printer=FancyGetopt(list_of_formats)
51+
pretty_printer.print_help("List of available distribution formats:")
52+
53+
help_options = [
54+
('help-formats', None,
55+
"lists available distribution formats",show_formats),
56+
]
57+
4358
negative_opts = {'use-defaults': 'no-defaults'}
4459

4560
default_format = { 'posix': 'gztar',

Lib/distutils/dist.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,38 @@ def _parse_command_opts (self, parser, args):
437437
negative_opt = copy (negative_opt)
438438
negative_opt.update (cmd_class.negative_opt)
439439

440+
# Check for help_options in command class
441+
# They have a different format (tuple of four) so we need to preprocess them here
442+
help_options = []
443+
if hasattr(cmd_class,"help_options") and type (cmd_class.help_options) is ListType:
444+
help_options = map(lambda x:(x[0],x[1],x[2]),cmd_class.help_options)
445+
440446
# All commands support the global options too, just by adding
441447
# in 'global_options'.
442448
parser.set_option_table (self.global_options +
443-
cmd_class.user_options)
449+
cmd_class.user_options + help_options)
444450
parser.set_negative_aliases (negative_opt)
445451
(args, opts) = parser.getopt (args[1:])
446452
if hasattr(opts, 'help') and opts.help:
447453
self._show_help(parser, display_options=0, commands=[cmd_class])
448454
return
449455

456+
if hasattr(cmd_class,"help_options") and type (cmd_class.help_options) is ListType:
457+
help_option_found=0
458+
for help_option in cmd_class.help_options:
459+
if hasattr(opts, parser.get_attr_name(help_option[0])):
460+
help_option_found=1
461+
#print "showing help for option %s of command %s" % (help_option[0],cmd_class)
462+
if callable(help_option[3]):
463+
help_option[3]()
464+
else:
465+
raise DistutilsClassError, \
466+
("command class %s must provide " +
467+
"a callable object for help_option '%s'") % \
468+
(cmd_class,help_option[0])
469+
if help_option_found:
470+
return
471+
450472
# Put the options from the command-line into their official
451473
# holding pen, the 'command_options' dictionary.
452474
opt_dict = self.get_option_dict(command)
@@ -496,15 +518,19 @@ def _show_help (self,
496518
klass = command
497519
else:
498520
klass = self.get_command_class (command)
499-
parser.set_option_table (klass.user_options)
521+
if hasattr(klass,"help_options") and type (klass.help_options) is ListType:
522+
parser.set_option_table (klass.user_options+
523+
map(lambda x:(x[0],x[1],x[2]),klass.help_options))
524+
else:
525+
parser.set_option_table (klass.user_options)
500526
parser.print_help ("Options for '%s' command:" % klass.__name__)
501527
print
502528

503529
print usage
504530
return
505531

506532
# _show_help ()
507-
533+
508534

509535
def handle_display_options (self, option_order):
510536
"""If there were any non-global "display-only" options

0 commit comments

Comments
 (0)