Checks which imports are done and compares them to what's in setup.py and warns when discovering missing or unneeded dependencies.
Checks which imports are done and compares them to what’s in setup.py and warn when discovering missing or unneeded dependencies.
- Usage of z3c.dependencychecker
- Changelog of z3c.dependencychecker
- 1.11 (2013-04-16)
- 1.10 (2013-02-24)
- 1.9 (2013-02-13)
- 1.8 (2013-02-13)
- 1.7.1 (2012-11-26)
- 1.7 (2012-11-26)
- 1.6 (2012-11-01)
- 1.5 (2012-07-03)
- 1.4 (2012-07-03)
- 1.3.2 (2012-06-29)
- 1.3.1 (2012-06-29)
- 1.3 (2012-06-29)
- 1.2 (2011-09-19)
- 1.1 (2010-01-06)
- 1.0 (2009-12-10)
- 0.5 (2009-12-10)
- 0.4 (2009-12-10)
- 0.3 (2009-12-08)
- 0.2 (2009-12-08)
- 0.1 (2009-12-02)
z3c.dependencychecker reports on:
Unused imports: pyflakes is another tool that does this (and that also reports on missing variables inside the files).
Missing (test) requirements: imports without a corresponding requirement in the setup.py. There might be false alarms, but at least you’ve got a (hopefully short) list of items to check.
Watch out for packages that have a different name than how they’re imported. For instance a requirement on pydns which is used as import DNS in your code: pydns and DNS lead to separate “missing requirements: DNS” and “unneeded requirements: pydns” warnings.
Unneeded (test) requirements: requirements in your setup.py that aren’t imported anywhere in your code. You might need them because not everything needs to be imported. It at least gives you a much smaller list to check by hand.
Requirements that should be test-only: if something is only imported in a test file, it shouldn’t be in the generic defaults. So you get a separate list of requirements that should be moved from the regular to the test requirements.
It checks the following locations:
- Python files for regular imports.
- Zcml files for package="some.thing" attributes. It also supports Plone’s generic setup files.
- Python files, .txt and .rst files for imports in doctests.
The tests are quite sensitive to other python packages being available. If the tests do not run, first wrap the buildout in a virtualenv to make double sure there are no interfering packages. Or make sure you use a clean (system) python.
z3c.dependencychecker is a different application/packaging of zope’s importchecker utility. It has been used in quite some projects, I grabbed a copy from lovely.recipe’s checkout.
Either install z3c.dependencychecker globally (easy_install z3c.dependencychecker) or install it in your buildout.
Run the dependencychecker or bin/dependencychecker script from your project’s root folder and it will report on your dependencies.
By default, it looks in the src/ directory for your sources. Alternatively, you can specify a start directory yourself, for instance '.' if there’s no src/ directory.
We have a sample project in a temp directory:
>>> sample1_dir '/TESTTEMP/sample1' >>> ls(sample1_dir) setup.py src
For our test, we call the main() method, just like the dependencychecker script would:
>>> import os >>> os.chdir(sample1_dir) >>> from z3c.dependencychecker import dependencychecker >>> dependencychecker.main() Unused imports ============== /TESTTEMP/sample1/src/sample1/unusedimports.py:7: tempfile /TESTTEMP/sample1/src/sample1/unusedimports.py:4: zest.releaser /TESTTEMP/sample1/src/sample1/unusedimports.py:6: os <BLANKLINE> Missing requirements ==================== Products.GenericSetup.interfaces.EXTENSION missing.req other.generic.setup.dependency some_django_app something.origname zope.interface <BLANKLINE> Missing test requirements ========================= reinout.hurray <BLANKLINE> Unneeded requirements ===================== some.other.extension unneeded.req <BLANKLINE> Requirements that should be test requirements ============================================= Needed.By.Test <BLANKLINE> Unneeded test requirements ========================== zope.testing <BLANKLINE> Note: requirements are taken from the egginfo dir, so you need to re-run buildout (or setup.py or whatever) for changes in setup.py to have effect. <BLANKLINE>
- Improve test coverage of original import checker module.
- Try it on more projects and gather feedback.
- Try to handle local (so non-absolute) imports.
- Display impacted files for each section (like Unused imports). Maybe with a option (-v)?
- Improve documentation on commandline usage (“you can pass a directoryname”).
- Support python installations without global setuptools installed by searching the name in the setup.py as fallback.
- Treat non-test extras_require like normal install_requires.
- Improved detection for “Django-style” package names with a dash in them. Django doesn’t deal well with namespace packages, so instead of zc.something, you’ll see packages like zc-something. The import then uses an underscore, zc_something.
- Added support for Django settings files. Anything that matches *settings.py is searched for Django settings like INSTALLED_APPS = [...] or MIDDLEWARE_CLASSES = (...).
- Detect ZCML “provides”, as used for generic setup profile registration.
- Added travis.ci configuration. We’re tested there, too, now!
- Lookup package name for ZCML modules too, as it is done for python modules.
- Detect generic setup dependencies in metadata.xml files.
- Fix AttributeError when “magic modules” like email.Header are imported.
- Add support for zipped dists when looking up pkg name.
- Lookup pkg name from egg-infos if possible (python >= 2.5). This helps for instance with the PIL problem (which can be Imaging instead when you import it).
- Fixed broken 1.3.0 and 1.3.0 release: the MANIFEST.in was missing…
- Documentation updates because we moved to github: https://github.com/reinout/z3c.dependencychecker .
- Added fix for standard library detection on OSX when using the python buildout. (Patch by Jonas Baumann, as is the next item).
- Supporting [tests] in addition to [test] for test requirements.
- Looking for a package directory named after the package name in preference to the src/ directory.
- Compensating for django-style ‘django-something’ package names with ‘django_something’ package directories. Dash versus underscore.
- Zcml files are also searched for ‘component=’ patterns as that can be used by securitypolicy declarations.
- Dependencychecker is now case insensitive as pypi is too.
- Using optparse for parsing commandline now. Added –help and –version.
- Documentation update.
- Improved test coverage. The dependencychecker module self is at 100%, the original import checker module is at 91% coverage.
- Searching in doctests (.py, .txt, .rst) for imports, too. Regex-based by necessity, but it seems to catch what I can test it with.
- Supporting “from zope import interface”-style imports where you really want to be told you’re missing an “zope.interface” dependency instead of just “zope” (which is just a namespace package).
- Sorted “unneeded requirements” reports and filtered out duplicates.
- Reporting separately on dependencies that should be moved from the regular to the test dependencies.
- Added tests. Initial quick test puts coverage at 86%.
- Fixed bug in test requirement detection.
- Added documentation.
- Moved source code to zope’s svn repository.
- Also reporting on unneeded imports.
- Added note on re-running buildout after a setup.py change.
- Added zcml lookup to detect even more missing imports.
- Added reporting on missing regular and test imports.
- Grabbing existing requirements from egginfo directory.
- Copied over Martijn Faassen’s zope importchecker script.