def coerce_file(fn):
"""Coerce content of given file to something useful for setup(), turn :
.py into mock object with description and version fields,
.md into rst text. Remove images with "nopypi" alt text along the way.
:url: https://github.com/Kraymer/setupgoon
"""
import ast
import os
import re
import subprocess # noqa
text = open(os.path.join(os.path.dirname(__file__), fn)).read()
if fn.endswith('.py'): # extract version, docstring etc out of python file
mock = type('mock', (object,), {})()
for attr in ('version', 'author', 'author_email', 'license'):
regex = r'^__%s__\s*=\s*[\'"]([^\'"]*)[\'"]$' % attr
m = re.search(regex, text, re.MULTILINE)
setattr(mock, attr, m.group(1) if m else None)
mock.docstring = ast.get_docstring(ast.parse(text))
return mock
if fn.endswith('md') and 'upload' in sys.argv: # convert md to rest on pypi package upload
text = '\n'.join([l for l in text.split('\n') if '![nopypi' not in l])
p = subprocess.Popen(['pandoc', '-t', 'rst'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
text, stderr = p.communicate(text)
return text
评论列表
文章目录