Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05f842b

Browse files
committed
Basic dependency checking. setup() has two new optional arguments
requires and provides. requires is a sequence of strings, of the form 'packagename-version'. The dependency checking so far merely does an '__import__(packagename)' and checks for packagename.__version__ You can also leave off the version, and any version of the package will be installed. There's a special case for the package 'python' - sys.version_info is used, so requires= ( 'python-2.3', ) just works. Provides is of the same format as requires - but if it's not supplied, a provides is generated by adding the version to each entry in packages, or modules if packages isn't there. Provides is currently only used in the PKG-INFO file. Shortly, PyPI will grow the ability to accept these lines, and register will be updated to send them. There's a new command 'checkdep' command that runs these checks. For this version, only greater-than-or-equal checking is done. We'll add the ability to specify an optional operator later.
1 parent a3837a0 commit 05f842b

5 files changed

Lines changed: 144 additions & 3 deletions

File tree

Lib/distutils/command/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'bdist_dumb',
2525
'bdist_rpm',
2626
'bdist_wininst',
27+
'checkdep',
2728
# These two are reserved for future use:
2829
#'bdist_sdux',
2930
#'bdist_pkgtool',

Lib/distutils/command/checkdep.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""distutils.command.x
2+
3+
Implements the Distutils 'x' command.
4+
"""
5+
6+
# created 2000/mm/dd, John Doe
7+
8+
__revision__ = "$Id$"
9+
10+
from distutils.core import Command
11+
12+
class DependencyFailure(Exception): pass
13+
14+
class VersionTooOld(DependencyFailure): pass
15+
16+
class VersionNotKnown(DependencyFailure): pass
17+
18+
class checkdep (Command):
19+
20+
# Brief (40-50 characters) description of the command
21+
description = "check package dependencies"
22+
23+
# List of option tuples: long name, short name (None if no short
24+
# name), and help string.
25+
# Later on, we might have auto-fetch and the like here. Feel free.
26+
user_options = []
27+
28+
def initialize_options (self):
29+
self.debug = None
30+
31+
# initialize_options()
32+
33+
34+
def finalize_options (self):
35+
pass
36+
# finalize_options()
37+
38+
39+
def run (self):
40+
from distutils.version import LooseVersion
41+
failed = []
42+
for pkg, ver in self.distribution.metadata.requires:
43+
if pkg == 'python':
44+
if ver is not None:
45+
# Special case the 'python' package
46+
import sys
47+
thisver = LooseVersion('%d.%d.%d'%sys.version_info[:3])
48+
if thisver < ver:
49+
failed.append(((pkg,ver), VersionTooOld(thisver)))
50+
continue
51+
# Kinda hacky - we should do more here
52+
try:
53+
mod = __import__(pkg)
54+
except Exception, e:
55+
failed.append(((pkg,ver), e))
56+
continue
57+
if ver is not None:
58+
if hasattr(mod, '__version__'):
59+
thisver = LooseVersion(mod.__version__)
60+
if thisver < ver:
61+
failed.append(((pkg,ver), VersionTooOld(thisver)))
62+
else:
63+
failed.append(((pkg,ver), VersionNotKnown()))
64+
65+
if failed:
66+
raise DependencyFailure, failed
67+
68+
# run()
69+
70+
# class x

Lib/distutils/command/install.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class install (Command):
126126
"force installation (overwrite any existing files)"),
127127
('skip-build', None,
128128
"skip rebuilding everything (for testing/debugging)"),
129+
('skip-checkdep', None,
130+
"skip checking dependencies (use at own risk)"),
129131

130132
# Where to install documentation (eventually!)
131133
#('doc-format=', None, "format of documentation to generate"),
@@ -183,12 +185,15 @@ def initialize_options (self):
183185

184186
# 'force' forces installation, even if target files are not
185187
# out-of-date. 'skip_build' skips running the "build" command,
186-
# handy if you know it's not necessary. 'warn_dir' (which is *not*
188+
# handy if you know it's not necessary. 'skip_checkdep' skips
189+
# the 'checkdep' command, if you are sure you can work around the
190+
# dependency failure in another way. 'warn_dir' (which is *not*
187191
# a user option, it's just there so the bdist_* commands can turn
188192
# it off) determines whether we warn about installing to a
189193
# directory not in sys.path.
190194
self.force = 0
191195
self.skip_build = 0
196+
self.skip_checkdep = 0
192197
self.warn_dir = 1
193198

194199
# These are only here as a conduit from the 'build' command to the
@@ -500,6 +505,12 @@ def run (self):
500505
if not self.skip_build:
501506
self.run_command('build')
502507

508+
# We check dependencies before we install
509+
# For now, this is disabled. Before 2.4 is released, this will
510+
# be turned on.
511+
#if not self.skip_checkdep:
512+
# self.run_command('checkdep')
513+
503514
# Run all sub-commands (at least those that need to be run)
504515
for cmd_name in self.get_sub_commands():
505516
self.run_command(cmd_name)

Lib/distutils/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def gen_usage (script_name):
4747
'name', 'version', 'author', 'author_email',
4848
'maintainer', 'maintainer_email', 'url', 'license',
4949
'description', 'long_description', 'keywords',
50-
'platforms', 'classifiers', 'download_url')
50+
'platforms', 'classifiers', 'download_url',
51+
'provides', 'requires', )
5152

5253
# Legal keyword arguments for the Extension constructor
5354
extension_keywords = ('name', 'sources', 'include_dirs',

Lib/distutils/dist.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,51 @@ def __init__ (self, attrs=None):
214214
else:
215215
sys.stderr.write(msg + "\n")
216216

217+
# Build up the requires sequence
218+
from distutils.version import LooseVersion
219+
requires = attrs.get('requires')
220+
if requires:
221+
if isinstance(requires, type('')):
222+
raise DistutilsOptionError, 'requires should be a sequence'
223+
newreq = []
224+
for req in requires:
225+
if '-' not in req:
226+
# We have a plain package name - any version will do
227+
newreq.append((req,None))
228+
else:
229+
pkg, ver = string.split(req, '-', 1)
230+
newreq.append((pkg, LooseVersion(ver)))
231+
attrs['requires'] = newreq
232+
233+
# Build up the provides object. If the setup() has no
234+
# provides line, we use packages or modules and the version
235+
# to synthesise the provides. If no version is provided (no
236+
# pun intended) we don't have a provides entry at all.
237+
provides = attrs.get('provides')
238+
if provides:
239+
if isinstance(provides, type('')):
240+
raise DistutilsOptionError, 'provides should be a sequence'
241+
newprov = []
242+
for prov in provides:
243+
if '-' not in prov:
244+
# We have a plain package name - any version will do
245+
newprov.append((prov,None))
246+
else:
247+
pkg, ver = string.split(prov, '-', 1)
248+
newprov.append((pkg, LooseVersion(ver)))
249+
attrs['provides'] = newprov
250+
elif attrs.get('version'):
251+
# Build a provides line
252+
prov = []
253+
if attrs.get('packages'):
254+
for pkg in attrs['packages']:
255+
pkg = string.replace(pkg, '/', '.')
256+
prov.append('%s-%s'%(pkg, attrs['version']))
257+
elif attrs.get('modules'):
258+
for mod in attrs['modules']:
259+
prov.append('%s-%s'%(mod, attrs['version']))
260+
attrs['provides'] = prov
261+
217262
# Now work on the rest of the attributes. Any attribute that's
218263
# not already defined is invalid!
219264
for (key,val) in attrs.items():
@@ -974,7 +1019,7 @@ class DistributionMetadata:
9741019
"license", "description", "long_description",
9751020
"keywords", "platforms", "fullname", "contact",
9761021
"contact_email", "license", "classifiers",
977-
"download_url")
1022+
"download_url", "provides", "requires",)
9781023

9791024
def __init__ (self):
9801025
self.name = None
@@ -991,6 +1036,8 @@ def __init__ (self):
9911036
self.platforms = None
9921037
self.classifiers = None
9931038
self.download_url = None
1039+
self.requires = []
1040+
self.provides = []
9941041

9951042
def write_pkg_info (self, base_dir):
9961043
"""Write the PKG-INFO file into the release tree.
@@ -1006,6 +1053,10 @@ def write_pkg_info (self, base_dir):
10061053
pkg_info.write('Author: %s\n' % self.get_contact() )
10071054
pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
10081055
pkg_info.write('License: %s\n' % self.get_license() )
1056+
for req in self.get_requires():
1057+
pkg_info.write('Requires: %s\n' % req )
1058+
for prov in self.get_provides():
1059+
pkg_info.write('Provides: %s\n' % prov )
10091060
if self.download_url:
10101061
pkg_info.write('Download-URL: %s\n' % self.download_url)
10111062

@@ -1084,6 +1135,13 @@ def get_classifiers(self):
10841135
def get_download_url(self):
10851136
return self.download_url or "UNKNOWN"
10861137

1138+
def get_requires(self):
1139+
return [ '%s%s%s'%(x, (y and '-') or '', y or '')
1140+
for x,y in self.requires ]
1141+
1142+
def get_provides(self):
1143+
return self.provides
1144+
10871145
# class DistributionMetadata
10881146

10891147

0 commit comments

Comments
 (0)