setup.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:flinck 作者: Kraymer 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号