improved distutils.dist test coverage, pep-8 compliancy
This commit is contained in:
parent
b02ceda3a5
commit
c01cbc42aa
|
@ -6,8 +6,7 @@ being built/installed/distributed.
|
||||||
|
|
||||||
__revision__ = "$Id$"
|
__revision__ = "$Id$"
|
||||||
|
|
||||||
import sys, os, string, re
|
import sys, os, re
|
||||||
from types import *
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -251,7 +250,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
|
|
||||||
# Now work on the rest of the attributes. Any attribute that's
|
# Now work on the rest of the attributes. Any attribute that's
|
||||||
# not already defined is invalid!
|
# not already defined is invalid!
|
||||||
for (key,val) in attrs.items():
|
for (key, val) in attrs.items():
|
||||||
if hasattr(self.metadata, "set_" + key):
|
if hasattr(self.metadata, "set_" + key):
|
||||||
getattr(self.metadata, "set_" + key)(val)
|
getattr(self.metadata, "set_" + key)(val)
|
||||||
elif hasattr(self.metadata, key):
|
elif hasattr(self.metadata, key):
|
||||||
|
@ -286,22 +285,24 @@ Common commands: (see '--help-commands' for more)
|
||||||
commands.sort()
|
commands.sort()
|
||||||
|
|
||||||
if header is not None:
|
if header is not None:
|
||||||
print indent + header
|
self.announce(indent + header)
|
||||||
indent = indent + " "
|
indent = indent + " "
|
||||||
|
|
||||||
if not commands:
|
if not commands:
|
||||||
print indent + "no commands known yet"
|
self.announce(indent + "no commands known yet")
|
||||||
return
|
return
|
||||||
|
|
||||||
for cmd_name in commands:
|
for cmd_name in commands:
|
||||||
opt_dict = self.command_options.get(cmd_name)
|
opt_dict = self.command_options.get(cmd_name)
|
||||||
if opt_dict is None:
|
if opt_dict is None:
|
||||||
print indent + "no option dict for '%s' command" % cmd_name
|
self.announce(indent +
|
||||||
|
"no option dict for '%s' command" % cmd_name)
|
||||||
else:
|
else:
|
||||||
print indent + "option dict for '%s' command:" % cmd_name
|
self.announce(indent +
|
||||||
|
"option dict for '%s' command:" % cmd_name)
|
||||||
out = pformat(opt_dict)
|
out = pformat(opt_dict)
|
||||||
for line in string.split(out, "\n"):
|
for line in out.split('\n'):
|
||||||
print indent + " " + line
|
self.announce(indent + " " + line)
|
||||||
|
|
||||||
# -- Config file finding/parsing methods ---------------------------
|
# -- Config file finding/parsing methods ---------------------------
|
||||||
|
|
||||||
|
@ -352,11 +353,13 @@ Common commands: (see '--help-commands' for more)
|
||||||
if filenames is None:
|
if filenames is None:
|
||||||
filenames = self.find_config_files()
|
filenames = self.find_config_files()
|
||||||
|
|
||||||
if DEBUG: print "Distribution.parse_config_files():"
|
if DEBUG:
|
||||||
|
self.announce("Distribution.parse_config_files():")
|
||||||
|
|
||||||
parser = ConfigParser()
|
parser = ConfigParser()
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
if DEBUG: print " reading", filename
|
if DEBUG:
|
||||||
|
self.announce(" reading", filename)
|
||||||
parser.read(filename)
|
parser.read(filename)
|
||||||
for section in parser.sections():
|
for section in parser.sections():
|
||||||
options = parser.options(section)
|
options = parser.options(section)
|
||||||
|
@ -365,7 +368,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
for opt in options:
|
for opt in options:
|
||||||
if opt != '__name__':
|
if opt != '__name__':
|
||||||
val = parser.get(section,opt)
|
val = parser.get(section,opt)
|
||||||
opt = string.replace(opt, '-', '_')
|
opt = opt.replace('-', '_')
|
||||||
opt_dict[opt] = (filename, val)
|
opt_dict[opt] = (filename, val)
|
||||||
|
|
||||||
# Make the ConfigParser forget everything (so we retain
|
# Make the ConfigParser forget everything (so we retain
|
||||||
|
@ -503,7 +506,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
# Also make sure that the command object provides a list of its
|
# Also make sure that the command object provides a list of its
|
||||||
# known options.
|
# known options.
|
||||||
if not (hasattr(cmd_class, 'user_options') and
|
if not (hasattr(cmd_class, 'user_options') and
|
||||||
type(cmd_class.user_options) is ListType):
|
isinstance(cmd_class.user_options, list)):
|
||||||
raise DistutilsClassError, \
|
raise DistutilsClassError, \
|
||||||
("command class %s must provide " +
|
("command class %s must provide " +
|
||||||
"'user_options' attribute (a list of tuples)") % \
|
"'user_options' attribute (a list of tuples)") % \
|
||||||
|
@ -519,7 +522,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
# Check for help_options in command class. They have a different
|
# Check for help_options in command class. They have a different
|
||||||
# format (tuple of four) so we need to preprocess them here.
|
# format (tuple of four) so we need to preprocess them here.
|
||||||
if (hasattr(cmd_class, 'help_options') and
|
if (hasattr(cmd_class, 'help_options') and
|
||||||
type(cmd_class.help_options) is ListType):
|
isinstance(cmd_class.help_options, list)):
|
||||||
help_options = fix_help_options(cmd_class.help_options)
|
help_options = fix_help_options(cmd_class.help_options)
|
||||||
else:
|
else:
|
||||||
help_options = []
|
help_options = []
|
||||||
|
@ -537,14 +540,11 @@ Common commands: (see '--help-commands' for more)
|
||||||
return
|
return
|
||||||
|
|
||||||
if (hasattr(cmd_class, 'help_options') and
|
if (hasattr(cmd_class, 'help_options') and
|
||||||
type(cmd_class.help_options) is ListType):
|
isinstance(cmd_class.help_options, list)):
|
||||||
help_option_found=0
|
help_option_found=0
|
||||||
for (help_option, short, desc, func) in cmd_class.help_options:
|
for (help_option, short, desc, func) in cmd_class.help_options:
|
||||||
if hasattr(opts, parser.get_attr_name(help_option)):
|
if hasattr(opts, parser.get_attr_name(help_option)):
|
||||||
help_option_found=1
|
help_option_found=1
|
||||||
#print "showing help for option %s of command %s" % \
|
|
||||||
# (help_option[0],cmd_class)
|
|
||||||
|
|
||||||
if callable(func):
|
if callable(func):
|
||||||
func()
|
func()
|
||||||
else:
|
else:
|
||||||
|
@ -569,17 +569,13 @@ Common commands: (see '--help-commands' for more)
|
||||||
instance, analogous to the .finalize_options() method of Command
|
instance, analogous to the .finalize_options() method of Command
|
||||||
objects.
|
objects.
|
||||||
"""
|
"""
|
||||||
keywords = self.metadata.keywords
|
for attr in ('keywords', 'platforms'):
|
||||||
if keywords is not None:
|
value = getattr(self.metadata, attr)
|
||||||
if type(keywords) is StringType:
|
if value is None:
|
||||||
keywordlist = string.split(keywords, ',')
|
continue
|
||||||
self.metadata.keywords = map(string.strip, keywordlist)
|
if isinstance(value, str):
|
||||||
|
value = [elm.strip() for elm in value.split(',')]
|
||||||
platforms = self.metadata.platforms
|
setattr(self.metadata, attr, value)
|
||||||
if platforms is not None:
|
|
||||||
if type(platforms) is StringType:
|
|
||||||
platformlist = string.split(platforms, ',')
|
|
||||||
self.metadata.platforms = map(string.strip, platformlist)
|
|
||||||
|
|
||||||
def _show_help(self, parser, global_options=1, display_options=1,
|
def _show_help(self, parser, global_options=1, display_options=1,
|
||||||
commands=[]):
|
commands=[]):
|
||||||
|
@ -606,31 +602,30 @@ Common commands: (see '--help-commands' for more)
|
||||||
options = self.global_options
|
options = self.global_options
|
||||||
parser.set_option_table(options)
|
parser.set_option_table(options)
|
||||||
parser.print_help(self.common_usage + "\nGlobal options:")
|
parser.print_help(self.common_usage + "\nGlobal options:")
|
||||||
print
|
self.announce('')
|
||||||
|
|
||||||
if display_options:
|
if display_options:
|
||||||
parser.set_option_table(self.display_options)
|
parser.set_option_table(self.display_options)
|
||||||
parser.print_help(
|
parser.print_help(
|
||||||
"Information display options (just display " +
|
"Information display options (just display " +
|
||||||
"information, ignore any commands)")
|
"information, ignore any commands)")
|
||||||
print
|
self.announce('')
|
||||||
|
|
||||||
for command in self.commands:
|
for command in self.commands:
|
||||||
if type(command) is ClassType and issubclass(command, Command):
|
if isinstance(command, type) and issubclass(command, Command):
|
||||||
klass = command
|
klass = command
|
||||||
else:
|
else:
|
||||||
klass = self.get_command_class(command)
|
klass = self.get_command_class(command)
|
||||||
if (hasattr(klass, 'help_options') and
|
if (hasattr(klass, 'help_options') and
|
||||||
type(klass.help_options) is ListType):
|
isinstance(klass.help_options, list)):
|
||||||
parser.set_option_table(klass.user_options +
|
parser.set_option_table(klass.user_options +
|
||||||
fix_help_options(klass.help_options))
|
fix_help_options(klass.help_options))
|
||||||
else:
|
else:
|
||||||
parser.set_option_table(klass.user_options)
|
parser.set_option_table(klass.user_options)
|
||||||
parser.print_help("Options for '%s' command:" % klass.__name__)
|
parser.print_help("Options for '%s' command:" % klass.__name__)
|
||||||
print
|
self.announce('')
|
||||||
|
|
||||||
print gen_usage(self.script_name)
|
self.announce(gen_usage(self.script_name))
|
||||||
return
|
|
||||||
|
|
||||||
def handle_display_options(self, option_order):
|
def handle_display_options(self, option_order):
|
||||||
"""If there were any non-global "display-only" options
|
"""If there were any non-global "display-only" options
|
||||||
|
@ -645,8 +640,8 @@ Common commands: (see '--help-commands' for more)
|
||||||
# we ignore "foo bar").
|
# we ignore "foo bar").
|
||||||
if self.help_commands:
|
if self.help_commands:
|
||||||
self.print_commands()
|
self.print_commands()
|
||||||
print
|
self.announce('')
|
||||||
print gen_usage(self.script_name)
|
self.announce(gen_usage(self.script_name))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
# If user supplied any of the "display metadata" options, then
|
# If user supplied any of the "display metadata" options, then
|
||||||
|
@ -662,12 +657,12 @@ Common commands: (see '--help-commands' for more)
|
||||||
opt = translate_longopt(opt)
|
opt = translate_longopt(opt)
|
||||||
value = getattr(self.metadata, "get_"+opt)()
|
value = getattr(self.metadata, "get_"+opt)()
|
||||||
if opt in ['keywords', 'platforms']:
|
if opt in ['keywords', 'platforms']:
|
||||||
print string.join(value, ',')
|
self.announce(','.join(value))
|
||||||
elif opt in ('classifiers', 'provides', 'requires',
|
elif opt in ('classifiers', 'provides', 'requires',
|
||||||
'obsoletes'):
|
'obsoletes'):
|
||||||
print string.join(value, '\n')
|
self.announce('\n'.join(value))
|
||||||
else:
|
else:
|
||||||
print value
|
self.announce(value)
|
||||||
any_display_options = 1
|
any_display_options = 1
|
||||||
|
|
||||||
return any_display_options
|
return any_display_options
|
||||||
|
@ -676,7 +671,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
"""Print a subset of the list of all commands -- used by
|
"""Print a subset of the list of all commands -- used by
|
||||||
'print_commands()'.
|
'print_commands()'.
|
||||||
"""
|
"""
|
||||||
print header + ":"
|
self.announce(header + ":")
|
||||||
|
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
klass = self.cmdclass.get(cmd)
|
klass = self.cmdclass.get(cmd)
|
||||||
|
@ -687,7 +682,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
description = "(no description available)"
|
description = "(no description available)"
|
||||||
|
|
||||||
print " %-*s %s" % (max_length, cmd, description)
|
self.announce(" %-*s %s" % (max_length, cmd, description))
|
||||||
|
|
||||||
def print_commands(self):
|
def print_commands(self):
|
||||||
"""Print out a help message listing all available commands with a
|
"""Print out a help message listing all available commands with a
|
||||||
|
@ -760,11 +755,10 @@ Common commands: (see '--help-commands' for more)
|
||||||
def get_command_packages(self):
|
def get_command_packages(self):
|
||||||
"""Return a list of packages from which commands are loaded."""
|
"""Return a list of packages from which commands are loaded."""
|
||||||
pkgs = self.command_packages
|
pkgs = self.command_packages
|
||||||
if not isinstance(pkgs, type([])):
|
if not isinstance(pkgs, list):
|
||||||
pkgs = string.split(pkgs or "", ",")
|
if pkgs is None:
|
||||||
for i in range(len(pkgs)):
|
pkgs = ''
|
||||||
pkgs[i] = string.strip(pkgs[i])
|
pkgs = [pkg.strip() for pkg in pkgs.split(',') if pkg != '']
|
||||||
pkgs = filter(None, pkgs)
|
|
||||||
if "distutils.command" not in pkgs:
|
if "distutils.command" not in pkgs:
|
||||||
pkgs.insert(0, "distutils.command")
|
pkgs.insert(0, "distutils.command")
|
||||||
self.command_packages = pkgs
|
self.command_packages = pkgs
|
||||||
|
@ -818,8 +812,8 @@ Common commands: (see '--help-commands' for more)
|
||||||
cmd_obj = self.command_obj.get(command)
|
cmd_obj = self.command_obj.get(command)
|
||||||
if not cmd_obj and create:
|
if not cmd_obj and create:
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print "Distribution.get_command_obj(): " \
|
self.announce("Distribution.get_command_obj(): " \
|
||||||
"creating '%s' command object" % command
|
"creating '%s' command object" % command)
|
||||||
|
|
||||||
klass = self.get_command_class(command)
|
klass = self.get_command_class(command)
|
||||||
cmd_obj = self.command_obj[command] = klass(self)
|
cmd_obj = self.command_obj[command] = klass(self)
|
||||||
|
@ -849,9 +843,12 @@ Common commands: (see '--help-commands' for more)
|
||||||
if option_dict is None:
|
if option_dict is None:
|
||||||
option_dict = self.get_option_dict(command_name)
|
option_dict = self.get_option_dict(command_name)
|
||||||
|
|
||||||
if DEBUG: print " setting options for '%s' command:" % command_name
|
if DEBUG:
|
||||||
|
self.announce(" setting options for '%s' command:" % command_name)
|
||||||
for (option, (source, value)) in option_dict.items():
|
for (option, (source, value)) in option_dict.items():
|
||||||
if DEBUG: print " %s = %s (from %s)" % (option, value, source)
|
if DEBUG:
|
||||||
|
self.announce(" %s = %s (from %s)" % (option, value,
|
||||||
|
source))
|
||||||
try:
|
try:
|
||||||
bool_opts = map(translate_longopt, command_obj.boolean_options)
|
bool_opts = map(translate_longopt, command_obj.boolean_options)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -862,7 +859,7 @@ Common commands: (see '--help-commands' for more)
|
||||||
neg_opt = {}
|
neg_opt = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
is_string = type(value) is StringType
|
is_string = isinstance(value, str)
|
||||||
if option in neg_opt and is_string:
|
if option in neg_opt and is_string:
|
||||||
setattr(command_obj, neg_opt[option], not strtobool(value))
|
setattr(command_obj, neg_opt[option], not strtobool(value))
|
||||||
elif option in bool_opts and is_string:
|
elif option in bool_opts and is_string:
|
||||||
|
@ -1044,10 +1041,10 @@ class DistributionMetadata:
|
||||||
if self.download_url:
|
if self.download_url:
|
||||||
self._write_field(file, 'Download-URL', self.download_url)
|
self._write_field(file, 'Download-URL', self.download_url)
|
||||||
|
|
||||||
long_desc = rfc822_escape( self.get_long_description())
|
long_desc = rfc822_escape(self.get_long_description())
|
||||||
self._write_field(file, 'Description', long_desc)
|
self._write_field(file, 'Description', long_desc)
|
||||||
|
|
||||||
keywords = string.join( self.get_keywords(), ',')
|
keywords = ','.join(self.get_keywords())
|
||||||
if keywords:
|
if keywords:
|
||||||
self._write_field(file, 'Keywords', keywords)
|
self._write_field(file, 'Keywords', keywords)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- coding: latin-1 -*-
|
# -*- coding: utf8 -*-
|
||||||
|
|
||||||
"""Tests for distutils.dist."""
|
"""Tests for distutils.dist."""
|
||||||
import os
|
import os
|
||||||
|
@ -36,7 +36,9 @@ class TestDistribution(Distribution):
|
||||||
return self._config_files
|
return self._config_files
|
||||||
|
|
||||||
|
|
||||||
class DistributionTestCase(support.TempdirManager, unittest.TestCase):
|
class DistributionTestCase(support.TempdirManager,
|
||||||
|
support.LoggingSilencer,
|
||||||
|
unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(DistributionTestCase, self).setUp()
|
super(DistributionTestCase, self).setUp()
|
||||||
|
@ -106,11 +108,11 @@ class DistributionTestCase(support.TempdirManager, unittest.TestCase):
|
||||||
my_file = os.path.join(tmp_dir, 'f')
|
my_file = os.path.join(tmp_dir, 'f')
|
||||||
klass = Distribution
|
klass = Distribution
|
||||||
|
|
||||||
dist = klass(attrs={'author': u'Mister Café',
|
dist = klass(attrs={'author': u'Mister Café',
|
||||||
'name': 'my.package',
|
'name': 'my.package',
|
||||||
'maintainer': u'Café Junior',
|
'maintainer': u'Café Junior',
|
||||||
'description': u'Café torréfié',
|
'description': u'Café torréfié',
|
||||||
'long_description': u'Héhéhé'})
|
'long_description': u'Héhéhé'})
|
||||||
|
|
||||||
|
|
||||||
# let's make sure the file can be written
|
# let's make sure the file can be written
|
||||||
|
@ -151,6 +153,49 @@ class DistributionTestCase(support.TempdirManager, unittest.TestCase):
|
||||||
|
|
||||||
self.assertEquals(len(warns), 0)
|
self.assertEquals(len(warns), 0)
|
||||||
|
|
||||||
|
def test_finalize_options(self):
|
||||||
|
|
||||||
|
attrs = {'keywords': 'one,two',
|
||||||
|
'platforms': 'one,two'}
|
||||||
|
|
||||||
|
dist = Distribution(attrs=attrs)
|
||||||
|
dist.finalize_options()
|
||||||
|
|
||||||
|
# finalize_option splits platforms and keywords
|
||||||
|
self.assertEquals(dist.metadata.platforms, ['one', 'two'])
|
||||||
|
self.assertEquals(dist.metadata.keywords, ['one', 'two'])
|
||||||
|
|
||||||
|
def test_show_help(self):
|
||||||
|
class FancyGetopt(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
|
def set_option_table(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def print_help(self, *args):
|
||||||
|
self.count += 1
|
||||||
|
|
||||||
|
parser = FancyGetopt()
|
||||||
|
dist = Distribution()
|
||||||
|
dist.commands = ['sdist']
|
||||||
|
dist.script_name = 'setup.py'
|
||||||
|
dist._show_help(parser)
|
||||||
|
self.assertEquals(parser.count, 3)
|
||||||
|
|
||||||
|
def test_get_command_packages(self):
|
||||||
|
dist = Distribution()
|
||||||
|
self.assertEquals(dist.command_packages, None)
|
||||||
|
cmds = dist.get_command_packages()
|
||||||
|
self.assertEquals(cmds, ['distutils.command'])
|
||||||
|
self.assertEquals(dist.command_packages,
|
||||||
|
['distutils.command'])
|
||||||
|
|
||||||
|
dist.command_packages = 'one,two'
|
||||||
|
cmds = dist.get_command_packages()
|
||||||
|
self.assertEquals(cmds, ['distutils.command', 'one', 'two'])
|
||||||
|
|
||||||
|
|
||||||
class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
|
class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
|
||||||
unittest.TestCase):
|
unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue