Skip to content

Commit

Permalink
Reduce duplicate code in wheel and install commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtcollins committed Mar 27, 2015
1 parent 0e2e7a5 commit 19d88b0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 65 deletions.
48 changes: 48 additions & 0 deletions pip/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pip.commands.unzip import UnzipCommand
from pip.commands.zip import ZipCommand
from pip.commands.wheel import WheelCommand
from pip.req import InstallRequirement, parse_requirements


commands_dict = {
Expand Down Expand Up @@ -83,3 +84,50 @@ def keyfn(key):
return 0xff

return sorted(cmddict.items(), key=keyfn)


class BadOptions(Exception):
pass


def populate_requirement_set(
requirement_set, args, options, logger, finder, session, name):
"""Marshal cmd line args into a requirement set.
If no requirements are given an error is output to logger and an
exception raised.
:raises BadOptions: if no requirements are supplied.
"""
# parse args and/or requirements files
for name in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
name, None, isolated=options.isolated_mode,
)
)
for name in options.editables:
requirement_set.add_requirement(
InstallRequirement.from_editable(
name,
default_vcs=options.default_vcs,
isolated=options.isolated_mode,
)
)
for filename in options.requirements:
for req in parse_requirements(
filename,
finder=finder, options=options, session=session):
requirement_set.add_requirement(req)
if not requirement_set.has_requirements:
opts = {'name': name}
if options.find_links:
msg = ('You must give at least one requirement to '
'%(name)s (maybe you meant "pip %(name)s '
'%(links)s"?)' %
dict(opts, links=' '.join(options.find_links)))
else:
msg = ('You must give at least one requirement '
'to %(name)s (see "pip help %(name)s")' % opts)
logger.warning(msg)
raise BadOptions()
41 changes: 7 additions & 34 deletions pip/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import shutil
import warnings

from pip.req import InstallRequirement, RequirementSet, parse_requirements
from pip.commands import populate_requirement_set, BadOptions
from pip.req import RequirementSet
from pip.locations import build_prefix, virtualenv_no_global, distutils_scheme
from pip.basecommand import Command
from pip.index import PackageFinder
Expand Down Expand Up @@ -302,39 +303,11 @@ def run(self, options, args):
isolated=options.isolated_mode,
)

for name in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
name, None, isolated=options.isolated_mode,
)
)

for name in options.editables:
requirement_set.add_requirement(
InstallRequirement.from_editable(
name,
default_vcs=options.default_vcs,
isolated=options.isolated_mode,
)
)

for filename in options.requirements:
for req in parse_requirements(
filename,
finder=finder, options=options, session=session):
requirement_set.add_requirement(req)

if not requirement_set.has_requirements:
opts = {'name': self.name}
if options.find_links:
msg = ('You must give at least one requirement to '
'%(name)s (maybe you meant "pip %(name)s '
'%(links)s"?)' %
dict(opts, links=' '.join(options.find_links)))
else:
msg = ('You must give at least one requirement '
'to %(name)s (see "pip help %(name)s")' % opts)
logger.warning(msg)
try:
populate_requirement_set(
requirement_set, args, options, logger, finder,
session, self.name)
except BadOptions:
return

try:
Expand Down
38 changes: 7 additions & 31 deletions pip/commands/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import warnings

from pip.basecommand import Command
from pip.commands import populate_requirement_set, BadOptions
from pip.index import PackageFinder
from pip.exceptions import CommandError, PreviousBuildDirError
from pip.req import InstallRequirement, RequirementSet, parse_requirements
from pip.req import RequirementSet
from pip.utils import import_or_raise, normalize_path
from pip.utils.build import BuildDirectory
from pip.utils.deprecation import RemovedInPip7Warning, RemovedInPip8Warning
Expand Down Expand Up @@ -190,36 +191,11 @@ def run(self, options, args):
if not os.path.exists(options.wheel_dir):
os.makedirs(options.wheel_dir)

# parse args and/or requirements files
for name in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
name, None, isolated=options.isolated_mode,
)
)
for name in options.editables:
requirement_set.add_requirement(
InstallRequirement.from_editable(
name,
default_vcs=options.default_vcs,
isolated=options.isolated_mode,
)
)
for filename in options.requirements:
for req in parse_requirements(
filename,
finder=finder,
options=options,
session=session):
requirement_set.add_requirement(req)

# fail if no requirements
if not requirement_set.has_requirements:
logger.error(
"You must give at least one requirement to %s "
"(see \"pip help %s\")",
self.name, self.name,
)
try:
populate_requirement_set(
requirement_set, args, options, logger, finder,
session, self.name)
except BadOptions:
return

try:
Expand Down

0 comments on commit 19d88b0

Please sign in to comment.