diff -r cb44fef5ea1d -r 7db732ac6796 Lib/packaging/command/cmd.py --- a/Lib/packaging/command/cmd.py Thu Jul 21 01:11:30 2011 +0200 +++ b/Lib/packaging/command/cmd.py Wed Jul 20 22:55:22 2011 -0400 @@ -317,9 +317,16 @@ cmd_obj.ensure_finalized() return cmd_obj - def get_reinitialized_command(self, command, reinit_subcommands=False): + def get_reinitialized_command(self, command, reinit_subcommands=False, **kwargs): + """Wrapper around Distributions 'get_reinitialized_command()' + method: returns a reinitialized command object based on a command + string or command options. Allows the addition of extra option + values or modifications of defaults. Usage of this method requires + the returned commaned to be finalized (by calling 'finalize_options()' + or 'ensure_finalized()'). + """ return self.distribution.get_reinitialized_command( - command, reinit_subcommands) + command, reinit_subcommands, **kwargs) def run_command(self, command): """Run some other command: uses the 'run_command()' method of diff -r cb44fef5ea1d -r 7db732ac6796 Lib/packaging/dist.py --- a/Lib/packaging/dist.py Thu Jul 21 01:11:30 2011 +0200 +++ b/Lib/packaging/dist.py Wed Jul 20 22:55:22 2011 -0400 @@ -688,7 +688,8 @@ except ValueError as msg: raise PackagingOptionError(msg) - def get_reinitialized_command(self, command, reinit_subcommands=False): + def get_reinitialized_command(self, command, reinit_subcommands=False, + **kwargs): """Reinitializes a command to the state it was in when first returned by 'get_command_obj()': ie., initialized but not yet finalized. This provides the opportunity to sneak option @@ -714,8 +715,12 @@ else: command_name = command.get_command_name() + for k, v in kwargs.items(): + setattr(command, k, v) + if not command.finalized: return command + command.initialize_options() self.have_run[command_name] = 0 command.finalized = False @@ -723,7 +728,8 @@ if reinit_subcommands: for sub in command.get_sub_commands(): - self.get_reinitialized_command(sub, reinit_subcommands) + self.get_reinitialized_command(sub, reinit_subcommands, + **kwargs) return command diff -r cb44fef5ea1d -r 7db732ac6796 Lib/packaging/tests/test_command_cmd.py --- a/Lib/packaging/tests/test_command_cmd.py Thu Jul 21 01:11:30 2011 +0200 +++ b/Lib/packaging/tests/test_command_cmd.py Wed Jul 20 22:55:22 2011 -0400 @@ -8,9 +8,22 @@ class MyCmd(Command): + + user_options = [ + ('test-option-1=', None, + "An option for testing defaults"), + ('test-option-2=', None, + "A second option for testing defaults"), + ] def initialize_options(self): - pass + self.test_option_1 = None + self.test_option_2 = None + def finalize_options(self): + if self.test_option_1 is None: + self.test_option_1 = "Kitten" + if self.test_option_2 is None: + self.test_option_2 = "Puppy" class CommandTestCase(support.LoggingCatcher, unittest.TestCase): @@ -94,6 +107,26 @@ self.assertRaises(PackagingOptionError, cmd.ensure_dirname, 'option2') + def test_get_reinitialized_command(self): + cmd = self.cmd + cmd.test_option_1 = "Horse" + cmd.test_option_2 = "Goat" + cmd.ensure_finalized() + cmd = cmd.get_reinitialized_command(cmd) + cmd.ensure_finalized() + self.assertEqual(cmd.test_option_1, "Kitten") + self.assertEqual(cmd.test_option_2, "Puppy") + + dist = Distribution() + cmd = MyCmd(dist) + cmd.ensure_finalized() + cmd = cmd.get_reinitialized_command(cmd, test_option_1="Rabbit") + cmd.ensure_finalized() + self.assertEqual(cmd.test_option_1, "Rabbit") + self.assertEqual(cmd.test_option_2, "Puppy") + + + def test_suite(): return unittest.makeSuite(CommandTestCase)