Package: setuphelpers
setuphelpersHelper functions for writing setup.py’s.Helper functionssetuphelpersprovides the following:git_versionUse to supply your package’s version string from git. If the latest
commit is tagged, the version will be that tag. If it is not tagged, it
will be the last version + 1 minor/patch version .devN number of commits
since last tag. If building off a branch other than master, the branch
name is used as a local version identifier.Examples:0.0.1,0.0.2.dev1,0.0.2.dev1+featureXfromsetuptoolsimportsetupfromsetuphelpersimportgit_versionsetup(name="my_thing",version=git_version())Note that you cannot deploy a sdist package usinggit_version, due
to needing access to.git. But please don’t start including.git’s in your packages. There is a pattern you can use to havegit_versionwrite out to a file to include, but I would argue that
there’s not a need to do that, as it’s already included in the written
package metadata anyway (wheels > *). But if you must:importosimportiofromsetuptoolsimportsetupfromsetuphelpersimportgit_versionfromsetuphelpersimportfind_version# VER_FILE should not exist in your checked in sourceVER_FILE=os.path.join("my_thing","__version__.py")ifos.path.isfile(VER_FILE):# this should only be run during rebuilding a sdist packageVERSION=find_version(VER_FILE)else:# this should be run only during the actual packagingVERSION=git_version()withio.open(VER_FILE,"w",encoding="utf-8")asopenversion:openversion.write('__version__ = "{}"'.format(VERSION))setup(name="my_thing",version=VERSION)long_descriptionUse to fill in the long_description field with the contents of your
README. If no README is found, will fallback to the docstring of your
setup.py.fromsetuptoolsimportsetupfromsetuphelpersimportlong_descriptionsetup(name="my_thing",version="1.0",long_description=long_description())find_versionUsed to find the value assigned to __version__ in the specified
filepath.fromsetuptoolsimportsetupfromsetuphelpersimportfind_versionsetup(name="my_thing",version=find_version("my_thing/__init__.py"))test_commandUsed to build a test command class for running unit tests withpython setup.py test. Default support is for py.test, but nose and
unittest are also both possible. The return fromtest_commandcan be
passed tocmdclass.Note that thetests_requiredependancies still need to be provided,
including coverage, if you’re using it (plus whatever else you need to
test with).fromsetuptoolsimportsetupfromsetuphelpersimporttest_commandsetup(name="my_thing",version="1.0",tests_require=["pytest","pytest-cov"],cmdclass=test_command(cover="my_thing"),)A note onsetup_requiresYou can (and should) putsetuphelpersin thesetup_requiresargument of your setup.py. But you also need to be careful to avoid
dependency problems during build. To accomplish this, you can use this
pattern (for each/any of the used functions):fromsetuptoolsimportsetuptry:fromsetuphelpersimport(find_version,# not used, but for completenessgit_version,long_description,test_command,)exceptImportError:find_version=lambdax:"0.0.0"git_version=lambda:"0.0.0"long_description=lambda:__doc__test_command=lambda**_:{}setup(name="my_thing",version=git_version(),# find_version("my_thing/__init__.py"),description=long_description(),tests_require=["pytest","pytest-cov"],setup_requires=["setuphelpers"],cmdclass=test_command(cover="my_thing"),)Nowpython setup.py installshould pull insetuphelpersif it’s
missing, and re-exec itself to fill in the arguments as expected. You
need to manually avoid both theImportErrorand theNameErrorthough, so mock callables need to be created in the case of missingsetuphelpers.Copyright and Licensesetuphelpers was written by Adam TalsmaCopyright (c) 2016 CCP hf.Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.