import os
import shutil
import sys
py_version = sys.version_info
if py_version < (2, 3, 5):
import distutils as dist
from distutils import core
setup = core.setup
else:
import setuptools as dist
setup = dist.setup
VERSION = '0.5.4'
FILE_ENCODING = 'utf-8'
README_PATH = 'README.md'
HISTORY_PATH = 'HISTORY.md'
LICENSE_PATH = 'LICENSE'
RST_DESCRIPTION_PATH = 'setup_description.rst'
TEMP_EXTENSION = '.temp'
PREP_COMMAND = 'prep'
CLASSIFIERS = (
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.4',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: Implementation :: PyPy',
)
RST_LONG_DESCRIPTION_INTRO = """\
.. Do not edit this file. This file is auto-generated for PyPI by setup.py
.. using pandoc, so edits should go in the source files rather than here.
"""
def read(path):
f = open(path, 'rb')
try:
b = f.read()
finally:
f.close()
return b.decode(FILE_ENCODING)
def write(u, path):
print("writing to: %s" % path)
f = open(path, "wb")
try:
b = u.encode(FILE_ENCODING)
f.write(b)
finally:
f.close()
def make_temp_path(path, new_ext=None):
root, ext = os.path.splitext(path)
if new_ext is None:
new_ext = ext
temp_path = root + TEMP_EXTENSION + new_ext
return temp_path
def strip_html_comments(text):
lines = text.splitlines(True)
new_lines = filter(lambda line: not line.startswith("<!--"), lines)
return "".join(new_lines)
def convert_md_to_rst(md_path, rst_temp_path):
command = "pandoc --write=rst --output=%s %s" % (rst_temp_path, md_path)
print("converting with pandoc: %s to %s\n-->%s" % (md_path, rst_temp_path,
command))
if os.path.exists(rst_temp_path):
os.remove(rst_temp_path)
os.system(command)
if not os.path.exists(rst_temp_path):
s = ("Error running: %s\n"
" Did you install pandoc per the %s docstring?" % (command,
__file__))
sys.exit(s)
return read(rst_temp_path)
def make_long_description():
readme_path = README_PATH
readme_md = strip_html_comments(read(readme_path))
history_md = strip_html_comments(read(HISTORY_PATH))
license_md = """\
License
=======
""" + read(LICENSE_PATH)
sections = [readme_md, history_md, license_md]
md_description = '\n\n'.join(sections)
md_ext = os.path.splitext(readme_path)[1]
md_description_path = make_temp_path(RST_DESCRIPTION_PATH, new_ext=md_ext)
write(md_description, md_description_path)
rst_temp_path = make_temp_path(RST_DESCRIPTION_PATH)
long_description = convert_md_to_rst(md_path=md_description_path,
rst_temp_path=rst_temp_path)
return "\n".join([RST_LONG_DESCRIPTION_INTRO, long_description])
def prep():
long_description = make_long_description()
write(long_description, RST_DESCRIPTION_PATH)
def publish():
long_description = make_long_description()
if long_description != read(RST_DESCRIPTION_PATH):
print("""\
Description file not up-to-date: %s
Run the following command and commit the changes--
python setup.py %s
""" % (RST_DESCRIPTION_PATH, PREP_COMMAND))
sys.exit()
print("Description up-to-date: %s" % RST_DESCRIPTION_PATH)
answer = raw_input("Are you sure you want to publish to PyPI (yes/no)?")
if answer != "yes":
exit("Aborted: nothing published")
os.system('python setup.py sdist upload')
requires = []
if py_version < (2, 5):
requires.append('simplejson<2.1')
elif py_version < (2, 6):
requires.append('simplejson')
INSTALL_REQUIRES = requires
PACKAGES = [
'pystache',
'pystache.commands',
'pystache.tests',
'pystache.tests.data',
'pystache.tests.data.locator',
'pystache.tests.examples',
]
def get_extra_args():
extra = {}
if py_version >= (3, ):
extra['use_2to3'] = True
return extra
def main(sys_argv):
sys.stderr.write("pystache: using: version %s of %s\n" % (repr(dist.__version__), repr(dist)))
command = sys_argv[-1]
if command == 'publish':
publish()
sys.exit()
elif command == PREP_COMMAND:
prep()
sys.exit()
long_description = read(RST_DESCRIPTION_PATH)
template_files = ['*.mustache', '*.txt']
extra_args = get_extra_args()
setup(name='pystache',
version=VERSION,
license='MIT',
description='Mustache for Python',
long_description=long_description,
author='Chris Wanstrath',
author_email='chris@ozmm.org',
maintainer='Chris Jerdonek',
maintainer_email='chris.jerdonek@gmail.com',
url='http://github.com/defunkt/pystache',
install_requires=INSTALL_REQUIRES,
packages=PACKAGES,
package_data = {
'pystache.tests.data': template_files,
'pystache.tests.data.locator': template_files,
'pystache.tests.examples': template_files,
},
entry_points = {
'console_scripts': [
'pystache=pystache.commands.render:main',
'pystache-test=pystache.commands.test:main',
],
},
classifiers = CLASSIFIERS,
**extra_args
)
if __name__=='__main__':
main(sys.argv)