Steve Piercy
2016-11-27 b7f994c9d13e70b597f42bce49ca6389cd21e2b2
convert pcreate and pdistreport to use argparse
- add sphinxcontrib-autoprogram
- attempt to render output of pcreate, but be unsuccessful
6 files modified
82 ■■■■ changed files
docs/conf.py 7 ●●●●● patch | view | raw | blame | history
docs/pscripts/pcreate.rst 5 ●●●●● patch | view | raw | blame | history
pyramid/scaffolds/template.py 8 ●●●● patch | view | raw | blame | history
pyramid/scripts/pcreate.py 56 ●●●● patch | view | raw | blame | history
pyramid/scripts/pdistreport.py 4 ●●●● patch | view | raw | blame | history
setup.py 2 ●●● patch | view | raw | blame | history
docs/conf.py
@@ -51,11 +51,10 @@
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.doctest',
    'sphinx.ext.intersphinx',
    'sphinx.ext.todo',
    'sphinx.ext.viewcode',
    'repoze.sphinx.autointerface',
    'sphinxcontrib.programoutput',
    'sphinx.ext.viewcode',
    'sphinx.ext.intersphinx',
    'sphinxcontrib.autoprogram',
    # enable pylons_sphinx_latesturl when this branch is no longer "latest"
    # 'pylons_sphinx_latesturl',
    ]
docs/pscripts/pcreate.rst
@@ -6,8 +6,7 @@
``pcreate``
-----------
.. program-output:: pcreate --help
   :prompt:
   :shell:
.. autoprogram:: pyramid.scripts.pcreate:PCreateCommand
   :prog: pcreate.py
.. seealso:: :ref:`creating_a_project`
pyramid/scaffolds/template.py
@@ -81,7 +81,7 @@
        template_dir = self.template_dir()
        if not self.exists(output_dir):
            self.out("Creating directory %s" % output_dir)
            if not command.options.simulate:
            if not command.args.simulate:
                # Don't let copydir create this top-level directory,
                # since copydir will svn add it sometimes:
                self.makedirs(output_dir)
@@ -90,9 +90,9 @@
            output_dir,
            vars,
            verbosity=command.verbosity,
            simulate=command.options.simulate,
            interactive=command.options.interactive,
            overwrite=command.options.overwrite,
            simulate=command.args.simulate,
            interactive=command.args.interactive,
            overwrite=command.args.overwrite,
            indent=1,
            template_renderer=self.render_template,
            )
pyramid/scripts/pcreate.py
@@ -2,7 +2,7 @@
# (http://pythonpaste.org) Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
import optparse
import argparse
import os
import os.path
import pkg_resources
@@ -22,79 +22,79 @@
class PCreateCommand(object):
    verbosity = 1 # required
    description = """\
    parser = argparse.ArgumentParser(description="""\
Render Pyramid scaffolding to an output directory.
Note: As of Pyramid 1.8, this command is deprecated. Use a specific
cookiecutter instead:
https://github.com/Pylons/?q=cookiecutter
"""
    usage = "usage: %prog [options] -s <scaffold> output_directory"
    parser = optparse.OptionParser(usage, description=description)
    parser.add_option('-s', '--scaffold',
""")
    parser.add_argument('-s', '--scaffold',
                      dest='scaffold_name',
                      action='append',
                      help=("Add a scaffold to the create process "
                            "(multiple -s args accepted)"))
    parser.add_option('-t', '--template',
    parser.add_argument('-t', '--template',
                      dest='scaffold_name',
                      action='append',
                      help=('A backwards compatibility alias for '
                            '-s/--scaffold.  Add a scaffold to the '
                            'create process (multiple -t args accepted)'))
    parser.add_option('-l', '--list',
    parser.add_argument('-l', '--list',
                      dest='list',
                      action='store_true',
                      help="List all available scaffold names")
    parser.add_option('--list-templates',
    parser.add_argument('--list-templates',
                      dest='list',
                      action='store_true',
                      help=("A backwards compatibility alias for -l/--list.  "
                            "List all available scaffold names."))
    parser.add_option('--package-name',
    parser.add_argument('--package-name',
                      dest='package_name',
                      action='store',
                      type='string',
                      help='Package name to use. The name provided is assumed '
                           'to be a valid Python package name, and will not '
                           'be validated. By default the package name is '
                           'derived from the value of output_directory.')
    parser.add_option('--simulate',
    parser.add_argument('--simulate',
                      dest='simulate',
                      action='store_true',
                      help='Simulate but do no work')
    parser.add_option('--overwrite',
    parser.add_argument('--overwrite',
                      dest='overwrite',
                      action='store_true',
                      help='Always overwrite')
    parser.add_option('--interactive',
    parser.add_argument('--interactive',
                      dest='interactive',
                      action='store_true',
                      help='When a file would be overwritten, interrogate '
                           '(this is the default, but you may specify it to '
                           'override --overwrite)')
    parser.add_option('--ignore-conflicting-name',
    parser.add_argument('--ignore-conflicting-name',
                      dest='force_bad_name',
                      action='store_true',
                      default=False,
                      help='Do create a project even if the chosen name '
                           'is the name of an already existing / importable '
                           'package.')
    parser.add_argument('output_directory',
                        help='The directory where the project will be '
                             'created.')
    pyramid_dist = pkg_resources.get_distribution("pyramid")
    def __init__(self, argv, quiet=False):
        self.quiet = quiet
        self.options, self.args = self.parser.parse_args(argv[1:])
        if not self.options.interactive and not self.options.overwrite:
            self.options.interactive = True
        self.args = self.parser.parse_args()
        if not self.args.interactive and not self.args.overwrite:
            self.args.interactive = True
        self.scaffolds = self.all_scaffolds()
    def run(self):
        self._warn_pcreate_deprecated()
        if self.options.list:
        if self.args.list:
            return self.show_scaffolds()
        if not self.options.scaffold_name and not self.args:
        if not self.args.scaffold_name and not self.args:
            if not self.quiet: # pragma: no cover
                self.parser.print_help()
                self.out('')
@@ -108,18 +108,18 @@
    @property
    def output_path(self):
        return os.path.abspath(os.path.normpath(self.args[0]))
        return os.path.abspath(os.path.normpath(self.args.output_directory))
    @property
    def project_vars(self):
        output_dir = self.output_path
        project_name = os.path.basename(os.path.split(output_dir)[1])
        if self.options.package_name is None:
        if self.args.package_name is None:
            pkg_name = _bad_chars_re.sub(
                '', project_name.lower().replace('-', '_'))
            safe_name = pkg_resources.safe_name(project_name)
        else:
            pkg_name = self.options.package_name
            pkg_name = self.args.package_name
            safe_name = pkg_name
        egg_name = pkg_resources.to_filename(safe_name)
@@ -152,7 +152,7 @@
    def render_scaffolds(self):
        props = self.project_vars
        output_dir = self.output_path
        for scaffold_name in self.options.scaffold_name:
        for scaffold_name in self.args.scaffold_name:
            for scaffold in self.scaffolds:
                if scaffold.name == scaffold_name:
                    scaffold.run(self, output_dir, props)
@@ -189,7 +189,7 @@
            print(msg)
    def validate_input(self):
        if not self.options.scaffold_name:
        if not self.args.scaffold_name:
            self.out('You must provide at least one scaffold name: -s <scaffold name>')
            self.out('')
            self.show_scaffolds()
@@ -198,14 +198,14 @@
            self.out('You must provide a project name')
            return False
        available = [x.name for x in self.scaffolds]
        diff = set(self.options.scaffold_name).difference(available)
        diff = set(self.args.scaffold_name).difference(available)
        if diff:
            self.out('Unavailable scaffolds: %s' % ", ".join(sorted(diff)))
            return False
        pkg_name = self.project_vars['package']
        if pkg_name == 'site' and not self.options.force_bad_name:
        if pkg_name == 'site' and not self.args.force_bad_name:
            self.out('The package name "site" has a special meaning in '
                     'Python. Are you sure you want to use it as your '
                     'project\'s name?')
@@ -221,7 +221,7 @@
        if not pkg_exists:
            return True
        if self.options.force_bad_name:
        if self.args.force_bad_name:
            return True
        self.out('A package named "{0}" already exists, are you sure you want '
                 'to use it as your project\'s name?'.format(pkg_name))
pyramid/scripts/pdistreport.py
@@ -1,7 +1,7 @@
import sys
import platform
import pkg_resources
import optparse
import argparse
from operator import itemgetter
def out(*args): # pragma: no cover
@@ -15,7 +15,7 @@
    # all args except argv are for unit testing purposes only
    description = "Show Python distribution versions and locations in use"
    usage = "usage: %prog"
    parser = optparse.OptionParser(usage, description=description)
    parser = argparse.ArgumentParser(usage, description=description)
    parser.parse_args(argv[1:])
    packages = []
    for distribution in pkg_resources.working_set:
setup.py
@@ -64,7 +64,7 @@
    'repoze.sphinx.autointerface',
    'pylons_sphinx_latesturl',
    'pylons-sphinx-themes',
    'sphinxcontrib-programoutput',
    'sphinxcontrib-autoprogram',
    ]
testing_extras = tests_require + [