xonsh/setup.py

199 lines
6.8 KiB
Python
Raw Normal View History

2015-03-07 13:35:01 -06:00
#!/usr/bin/env python
# -*- coding: ascii -*-
2015-03-07 12:02:04 -06:00
"""The xonsh installer."""
# Note: Do not embed any non-ASCII characters in this file until pip has been
# fixed. See https://github.com/scopatz/xonsh/issues/487.
2015-03-07 13:35:01 -06:00
from __future__ import print_function, unicode_literals
2015-03-07 12:02:04 -06:00
import os
import sys
2015-09-30 00:56:14 -04:00
import json
2015-03-07 12:02:04 -06:00
try:
from tempfile import TemporaryDirectory
except ImportError:
pass
try:
2015-03-07 12:02:04 -06:00
from setuptools import setup
2015-03-09 22:42:34 -05:00
from setuptools.command.sdist import sdist
2015-03-15 20:48:13 -05:00
from setuptools.command.install import install
2015-04-06 23:21:03 -05:00
from setuptools.command.develop import develop
from setuptools.command.install_scripts import install_scripts
2015-03-07 12:02:04 -06:00
HAVE_SETUPTOOLS = True
except ImportError:
from distutils.core import setup
2015-03-15 20:48:13 -05:00
from distutils.command.sdist import sdist as sdist
from distutils.command.install import install as install
from distutils.command.install_scripts import install_scripts
2015-03-07 12:02:04 -06:00
HAVE_SETUPTOOLS = False
2015-09-30 00:56:14 -04:00
try:
from jupyter_client.kernelspec import KernelSpecManager
2015-09-30 00:56:14 -04:00
HAVE_JUPYTER = True
except ImportError:
HAVE_JUPYTER = False
2015-03-07 12:09:30 -06:00
2016-06-13 03:02:24 -04:00
TABLES = ['xonsh/lexer_table.py', 'xonsh/parser_table.py', 'xonsh/__amalgam__.py']
2015-03-07 13:24:44 -06:00
2015-03-07 13:24:44 -06:00
def clean_tables():
"""Remove the lexer/parser modules that are dynamically created."""
2015-03-07 13:24:44 -06:00
for f in TABLES:
if os.path.isfile(f):
os.remove(f)
2016-06-12 11:07:09 -04:00
print('Removed ' + f)
2015-03-07 13:24:44 -06:00
2015-09-30 00:56:14 -04:00
2016-06-16 00:24:35 -04:00
os.environ['DEBUG'] = '1'
2016-06-15 00:44:01 -04:00
from xonsh import __version__ as XONSH_VERSION
2015-03-07 13:24:44 -06:00
def build_tables():
"""Build the lexer/parser modules."""
2015-03-07 13:24:44 -06:00
print('Building lexer and parser tables.')
sys.path.insert(0, os.path.dirname(__file__))
from xonsh.parser import Parser
Parser(lexer_table='lexer_table', yacc_table='parser_table',
outputdir='xonsh')
2016-06-13 03:02:24 -04:00
import amalgamate
amalgamate.main(['amalgamate', 'xonsh'])
2015-03-07 13:24:44 -06:00
sys.path.pop(0)
2015-09-30 00:56:14 -04:00
2016-05-22 17:51:42 -04:00
def install_jupyter_hook(prefix=None, root=None):
"""Make xonsh available as a Jupyter kernel."""
2015-09-30 00:56:14 -04:00
if not HAVE_JUPYTER:
print('Could not install Jupyter kernel spec, please install '
'Jupyter/IPython.')
2015-09-30 00:56:14 -04:00
return
spec = {"argv": [sys.executable, "-m", "xonsh.jupyter_kernel",
"-f", "{connection_file}"],
"display_name": "Xonsh",
"language": "xonsh",
"codemirror_mode": "shell",
}
with TemporaryDirectory() as d:
os.chmod(d, 0o755) # Starts off as 700, not user readable
if sys.platform == 'win32':
# Ensure that conda-build detects the hard coded prefix
spec['argv'][0] = spec['argv'][0].replace(os.sep, os.altsep)
2015-09-30 00:56:14 -04:00
with open(os.path.join(d, 'kernel.json'), 'w') as f:
json.dump(spec, f, sort_keys=True)
if 'CONDA_BUILD' in os.environ:
2016-05-22 17:51:42 -04:00
prefix = sys.prefix
if sys.platform == 'win32':
2016-05-22 17:51:42 -04:00
prefix = prefix.replace(os.sep, os.altsep)
user = ('--user' in sys.argv)
print('Installing Jupyter kernel spec:')
print(' root: {0!r}'.format(root))
print(' prefix: {0!r}'.format(prefix))
print(' as user: {0}'.format(user))
KernelSpecManager().install_kernel_spec(
2016-05-22 17:51:42 -04:00
d, 'xonsh', user=user, replace=True, prefix=prefix)
2015-09-30 00:56:14 -04:00
2015-03-09 22:42:34 -05:00
class xinstall(install):
"""Xonsh specialization of setuptools install class."""
2015-03-09 22:42:34 -05:00
def run(self):
clean_tables()
build_tables()
2016-05-22 17:51:42 -04:00
# install Jupyter hook
root = self.root if self.root else None
prefix = self.prefix if self.prefix else None
try:
install_jupyter_hook(prefix=prefix, root=root)
except Exception:
import traceback
traceback.print_exc()
print('Installing Jupyter hook failed.')
2015-03-09 22:42:34 -05:00
install.run(self)
2015-09-30 00:56:14 -04:00
2015-03-09 22:42:34 -05:00
class xsdist(sdist):
"""Xonsh specialization of setuptools sdist class."""
2015-03-09 22:42:34 -05:00
def make_release_tree(self, basedir, files):
clean_tables()
build_tables()
sdist.make_release_tree(self, basedir, files)
2015-09-30 00:56:14 -04:00
#-----------------------------------------------------------------------------
# Hack to overcome pip/setuptools problem on Win 10. See:
# https://github.com/tomduck/pandoc-eqnos/issues/6
# https://github.com/pypa/pip/issues/2783
# Custom install_scripts command class for setup()
class install_scripts_quoted_shebang(install_scripts):
"""Ensure there are quotes around shebang paths with spaces."""
def write_script(self, script_name, contents, mode="t", *ignored):
shebang = str(contents.splitlines()[0])
if shebang.startswith('#!') and ' ' in shebang[2:].strip() \
and '"' not in shebang:
quoted_shebang = '#!"%s"' % shebang[2:].strip()
contents = contents.replace(shebang, quoted_shebang)
super().write_script(script_name, contents, mode, *ignored)
# The custom install needs to be used on Windows machines
if os.name == 'nt':
cmdclass = {'install': xinstall, 'sdist': xsdist, 'install_scripts': install_scripts_quoted_shebang}
else:
cmdclass = {'install': xinstall, 'sdist': xsdist}
2015-04-06 23:21:03 -05:00
if HAVE_SETUPTOOLS:
class xdevelop(develop):
"""Xonsh specialization of setuptools develop class."""
2015-04-06 23:21:03 -05:00
def run(self):
clean_tables()
build_tables()
2015-04-06 23:57:38 -05:00
develop.run(self)
2015-04-06 23:21:03 -05:00
2015-03-07 12:02:04 -06:00
def main():
"""The main entry point."""
if sys.version_info[:2] < (3, 4):
2015-03-12 20:43:06 -05:00
sys.exit('xonsh currently requires Python 3.4+')
2015-03-23 01:40:44 -05:00
try:
if '--name' not in sys.argv:
logo_fname = os.path.join(os.path.dirname(__file__), 'logo.txt')
with open(logo_fname, 'rb') as f:
logo = f.read().decode('utf-8')
print(logo)
2015-03-23 01:40:44 -05:00
except UnicodeEncodeError:
pass
with open(os.path.join(os.path.dirname(__file__), 'README.rst'), 'r') as f:
2015-03-07 12:09:30 -06:00
readme = f.read()
skw = dict(
name='xonsh',
2016-01-07 15:49:35 -08:00
description='A general purpose, Python-ish shell',
2015-03-07 12:09:30 -06:00
long_description=readme,
license='BSD',
2015-03-24 16:07:37 -04:00
version=XONSH_VERSION,
2015-03-07 12:09:30 -06:00
author='Anthony Scopatz',
maintainer='Anthony Scopatz',
author_email='scopatz@gmail.com',
url='https://github.com/scopatz/xonsh',
platforms='Cross Platform',
2015-04-02 21:08:17 -05:00
classifiers=['Programming Language :: Python :: 3'],
2016-05-23 17:24:31 -04:00
packages=['xonsh', 'xonsh.ply', 'xonsh.ptk', 'xonsh.parsers',
2016-05-28 13:56:56 -04:00
'xonsh.xoreutils', 'xontrib', 'xonsh.completers'],
2016-05-09 23:40:45 -04:00
package_dir={'xonsh': 'xonsh', 'xontrib': 'xontrib'},
package_data={'xonsh': ['*.json'], 'xontrib': ['*.xsh']},
cmdclass=cmdclass
2015-03-07 12:09:30 -06:00
)
2015-03-09 22:42:34 -05:00
if HAVE_SETUPTOOLS:
2015-03-30 20:50:20 -05:00
skw['entry_points'] = {
'pygments.lexers': ['xonsh = xonsh.pyghooks:XonshLexer',
'xonshcon = xonsh.pyghooks:XonshConsoleLexer'],
'console_scripts': ['xonsh = xonsh.main:main'],
2015-03-30 20:50:20 -05:00
}
2015-04-06 23:21:03 -05:00
skw['cmdclass']['develop'] = xdevelop
else:
2016-05-31 22:55:11 -05:00
skw['scripts'] = ['scripts/xonsh'] if 'win' not in sys.platform else ['scripts/xonsh.bat']
2015-11-18 21:19:23 +01:00
2015-03-07 12:02:04 -06:00
setup(**skw)
2015-03-07 17:32:13 -06:00
2015-03-07 12:02:04 -06:00
if __name__ == '__main__':
main()