Skip to content

Commit b261954

Browse files
committed
Merge pull request #2420 from msabramo/wheel_check_required_packages
Refactor wheel command check of required packages
2 parents 435d7b4 + 1a2c3f1 commit b261954

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

pip/commands/wheel.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pip.index import PackageFinder
1010
from pip.exceptions import CommandError, PreviousBuildDirError
1111
from pip.req import InstallRequirement, RequirementSet, parse_requirements
12-
from pip.utils import normalize_path
12+
from pip.utils import import_or_raise, normalize_path
1313
from pip.utils.build import BuildDirectory
1414
from pip.utils.deprecation import RemovedInPip7Warning, RemovedInPip8Warning
1515
from pip.wheel import WheelBuilder
@@ -100,33 +100,28 @@ def __init__(self, *args, **kw):
100100
self.parser.insert_option_group(0, index_opts)
101101
self.parser.insert_option_group(0, cmd_opts)
102102

103-
def run(self, options, args):
104-
105-
# confirm requirements
106-
try:
107-
import wheel.bdist_wheel
108-
# Hack to make flake8 not complain about an unused import
109-
wheel.bdist_wheel
110-
except ImportError:
103+
def check_required_packages(self):
104+
import_or_raise(
105+
'wheel.bdist_wheel',
106+
CommandError,
107+
"'pip wheel' requires the 'wheel' package. To fix this, run: "
108+
"pip install wheel"
109+
)
110+
pkg_resources = import_or_raise(
111+
'pkg_resources',
112+
CommandError,
113+
"'pip wheel' requires setuptools >= 0.8 for dist-info support."
114+
" To fix this, run: pip install --upgrade setuptools"
115+
)
116+
if not hasattr(pkg_resources, 'DistInfoDistribution'):
111117
raise CommandError(
112-
"'pip wheel' requires the 'wheel' package. To fix this, run: "
113-
"pip install wheel"
118+
"'pip wheel' requires setuptools >= 0.8 for dist-info "
119+
"support. To fix this, run: pip install --upgrade "
120+
"setuptools"
114121
)
115122

116-
try:
117-
import pkg_resources
118-
except ImportError:
119-
raise CommandError(
120-
"'pip wheel' requires setuptools >= 0.8 for dist-info support."
121-
" To fix this, run: pip install --upgrade setuptools"
122-
)
123-
else:
124-
if not hasattr(pkg_resources, 'DistInfoDistribution'):
125-
raise CommandError(
126-
"'pip wheel' requires setuptools >= 0.8 for dist-info "
127-
"support. To fix this, run: pip install --upgrade "
128-
"setuptools"
129-
)
123+
def run(self, options, args):
124+
self.check_required_packages()
130125

131126
index_urls = [options.index_url] + options.extra_index_urls
132127
if options.no_index:

pip/utils/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
logger = logging.getLogger(__name__)
4646

4747

48+
def import_or_raise(pkg_or_module_string, ExceptionType, *args, **kwargs):
49+
try:
50+
return __import__(pkg_or_module_string)
51+
except ImportError:
52+
raise ExceptionType(*args, **kwargs)
53+
54+
4855
def get_prog():
4956
try:
5057
if os.path.basename(sys.argv[0]) in ('__main__.py', '-c'):

0 commit comments

Comments
 (0)