Skip to content

Instantly share code, notes, and snippets.

Created November 23, 2014 17:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/de85156a10f0041a9496 to your computer and use it in GitHub Desktop.
Save anonymous/de85156a10f0041a9496 to your computer and use it in GitHub Desktop.
drive-in extension for distutils of python 2.7: use [metadata]-section of setup.cfg to "auto-fill" the keyword-arguments of core.setup function.
[metadata]
name = foo
version = 0.1
author = Don Question
author-email = donquestion@example.com
description = Demonstrates a declarative setup setup.
description-file = README.md
classifiers = Development Status :: 4 - Beta
License :: OSI Approved :: MIT License
Operating System :: POSIX :: Linux
Programming Language :: Python :: 2.7
Topic :: Software Development
#!/usr/bin/env python # -*- coding: utf-8 -*-
"""Install this package.
"""
from distutils import core
execfile('src/setupmeta.py') # pseudo-import of: metadata_from_setupcfg()
def start():
""" Start thread of execution.
"""
setup_options = metadata_from_setupcfg()
core.setup(**setup_options)
if __name__ == "__main__":
start()
# vim: ai ts=4 sts=4 sw=4 et=4 tw=79 fenc=utf-8 ft=python
#!/usr/bin/env python # -*- coding: utf-8 -*-
""" Setup extension: read options from section [metadata] from file: setup.cfg.
"""
from ConfigParser import ConfigParser
from functools import partial
from os import path
def metadata_from_setupcfg():
"""
Read the [metadata] section of setup.cfg and return it as a dict.
This is the main-function. All other functions in this file can be viewed
as helpers.
"""
cfgparser = ConfigParser()
section_name = 'metadata'
cfgparser.read(get_cfg_fname())
options = dict(cfgparser.items(section_name))
return normalize(options)
def get_cfg_fname():
(base, pyext), cfgext = path.splitext(path.abspath(__file__)), '.cfg'
return base + cfgext
def change_keyname_dashes_to_underscore(dic):
return dict([(k.replace('-', '_'), dic[k]) for k in dic])
def long_descrition_from_file(fname):
with open(fname) as fdesc: return fdesc.read()
def pop_item(dic, key):
""" Returns tuple: either (key, item) if key exists, or (key, None).
"""
return (key, dic.pop(key, None))
def normalize(options):
""" Return correct kwargs for setup() from provided options-dict.
1. make copy of options-dict and use the copy
2. change all dashes to underscores in keynames
3. go over keys which needs attention and make them setup-compatible
4. return n(ormalized)options-dict
"""
noptions = change_keyname_dashes_to_underscore(options.copy())
pop = partial(pop_item, noptions)
(key, value) = pop('classifier')
if value and isinstance(value, basestring):
noptions[key] = value.splitlines()
# there is no else; either we know how to handle it, or leave it
(key, value) = pop('description_file')
if value:
noptions[key] = long_descrition_from_file(value)
return noptions
"""
Example usage in a setup.py:
from distutils import core
execfile('src/setupmeta.py')
core.setup(**metadata_from_setupcfg())
"""
# vim: ai ts=4 sts=4 et sw=4 tw=79 fenc=utf-8 ft=python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment