Skip to content

Commit a66f4fd

Browse files
author
tarek.ziade
committed
#3986 replacing string and types call (like in the Py3k branch), and put exec_msg call at the right place
git-svn-id: http://svn.python.org/projects/python/trunk@69385 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c2bb7ce commit a66f4fd

2 files changed

Lines changed: 55 additions & 24 deletions

File tree

Lib/distutils/cmd.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__revision__ = "$Id$"
88

9-
import sys, os, string, re
9+
import sys, os, re
1010
from distutils.errors import DistutilsOptionError
1111
from distutils import util, dir_util, file_util, archive_util, dep_util
1212
from distutils import log
@@ -156,19 +156,19 @@ def finalize_options (self):
156156
"abstract method -- subclass %s must override" % self.__class__
157157

158158

159-
def dump_options (self, header=None, indent=""):
159+
def dump_options(self, header=None, indent=""):
160160
from distutils.fancy_getopt import longopt_xlate
161161
if header is None:
162162
header = "command options for '%s':" % self.get_command_name()
163-
print indent + header
163+
self.announce(indent + header, level=log.INFO)
164164
indent = indent + " "
165165
for (option, _, _) in self.user_options:
166-
option = string.translate(option, longopt_xlate)
166+
option = option.translate(longopt_xlate)
167167
if option[-1] == "=":
168168
option = option[:-1]
169169
value = getattr(self, option)
170-
print indent + "%s = %s" % (option, value)
171-
170+
self.announce(indent + "%s = %s" % (option, value),
171+
level=log.INFO)
172172

173173
def run (self):
174174
"""A command's raison d'etre: carry out the action it exists to
@@ -405,8 +405,8 @@ def make_archive (self, base_name, format,
405405
base_name, format, root_dir, base_dir, dry_run=self.dry_run)
406406

407407

408-
def make_file (self, infiles, outfile, func, args,
409-
exec_msg=None, skip_msg=None, level=1):
408+
def make_file(self, infiles, outfile, func, args,
409+
exec_msg=None, skip_msg=None, level=1):
410410
"""Special case of 'execute()' for operations that process one or
411411
more input files and generate one output file. Works just like
412412
'execute()', except the operation is skipped and a different
@@ -415,24 +415,24 @@ def make_file (self, infiles, outfile, func, args,
415415
and it is true, then the command is unconditionally run -- does no
416416
timestamp checks.
417417
"""
418-
if exec_msg is None:
419-
exec_msg = "generating %s from %s" % \
420-
(outfile, string.join(infiles, ', '))
421418
if skip_msg is None:
422419
skip_msg = "skipping %s (inputs unchanged)" % outfile
423420

424-
425421
# Allow 'infiles' to be a single string
426-
if type(infiles) is StringType:
422+
if isinstance(infiles, str):
427423
infiles = (infiles,)
428-
elif type(infiles) not in (ListType, TupleType):
424+
elif not isinstance(infiles, (list, tuple)):
429425
raise TypeError, \
430426
"'infiles' must be a string, or a list or tuple of strings"
431427

428+
if exec_msg is None:
429+
exec_msg = "generating %s from %s" % \
430+
(outfile, ', '.join(infiles))
431+
432432
# If 'outfile' must be regenerated (either because it doesn't
433433
# exist, is out-of-date, or the 'force' flag is true) then
434434
# perform the action that presumably regenerates it
435-
if self.force or dep_util.newer_group (infiles, outfile):
435+
if self.force or dep_util.newer_group(infiles, outfile):
436436
self.execute(func, args, exec_msg, level)
437437

438438
# Otherwise, print the "skip" message

Lib/distutils/tests/test_cmd.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
from distutils.dist import Distribution
66
from distutils.errors import DistutilsOptionError
77

8-
class CommandTestCase(unittest.TestCase):
9-
10-
def test_ensure_string_list(self):
11-
12-
class MyCmd(Command):
8+
class MyCmd(Command):
9+
def initialize_options(self):
10+
pass
1311

14-
def initialize_options(self):
15-
pass
12+
class CommandTestCase(unittest.TestCase):
1613

14+
def setUp(self):
1715
dist = Distribution()
18-
cmd = MyCmd(dist)
16+
self.cmd = MyCmd(dist)
17+
18+
def test_ensure_string_list(self):
1919

20+
cmd = self.cmd
2021
cmd.not_string_list = ['one', 2, 'three']
2122
cmd.yes_string_list = ['one', 'two', 'three']
2223
cmd.not_string_list2 = object()
2324
cmd.yes_string_list2 = 'ok'
24-
2525
cmd.ensure_string_list('yes_string_list')
2626
cmd.ensure_string_list('yes_string_list2')
2727

@@ -31,6 +31,37 @@ def initialize_options(self):
3131
self.assertRaises(DistutilsOptionError,
3232
cmd.ensure_string_list, 'not_string_list2')
3333

34+
def test_make_file(self):
35+
36+
cmd = self.cmd
37+
38+
# making sure it raises when infiles is not a string or a list/tuple
39+
self.assertRaises(TypeError, cmd.make_file,
40+
infiles=1, outfile='', func='func', args=())
41+
42+
# making sure execute gets called properly
43+
def _execute(func, args, exec_msg, level):
44+
self.assertEquals(exec_msg, 'generating out from in')
45+
cmd.force = True
46+
cmd.execute = _execute
47+
cmd.make_file(infiles='in', outfile='out', func='func', args=())
48+
49+
def test_dump_options(self):
50+
51+
msgs = []
52+
def _announce(msg, level):
53+
msgs.append(msg)
54+
cmd = self.cmd
55+
cmd.announce = _announce
56+
cmd.option1 = 1
57+
cmd.option2 = 1
58+
cmd.user_options = [('option1', '', ''), ('option2', '', '')]
59+
cmd.dump_options()
60+
61+
wanted = ["command options for 'MyCmd':", ' option1 = 1',
62+
' option2 = 1']
63+
self.assertEquals(msgs, wanted)
64+
3465
def test_suite():
3566
return unittest.makeSuite(CommandTestCase)
3667

0 commit comments

Comments
 (0)