Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/smc_pyutil/fastentrypoints.py
Views: 687
# -*- coding: utf-8 -*-12# Copyright (c) 2016, Aaron Christianson3# All rights reserved.4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions are7# met:8#9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11#12# 2. Redistributions in binary form must reproduce the above copyright13# notice, this list of conditions and the following disclaimer in the14# documentation and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS17# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED22# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR23# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS26# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27'''28Monkey patch setuptools to write faster console_scripts with this format:2930import sys31from mymodule import entry_function32sys.exit(entry_function())3334This is better.3536(c) 2016, Aaron Christianson37http://github.com/ninjaaron/fast-entry_points38'''39from __future__ import absolute_import40from __future__ import print_function41from setuptools.command import easy_install42import re43TEMPLATE = '''\44# -*- coding: utf-8 -*-45# EASY-INSTALL-ENTRY-SCRIPT: '{3}','{4}','{5}'46__requires__ = '{3}'47import re48import sys4950from {0} import {1}5152if __name__ == '__main__':53sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])54sys.exit({2}())'''555657@classmethod58def get_args(cls, dist, header=None):59"""60Yield write_script() argument tuples for a distribution's61console_scripts and gui_scripts entry points.62"""63if header is None:64header = cls.get_header()65spec = str(dist.as_requirement())66for type_ in 'console', 'gui':67group = type_ + '_scripts'68for name, ep in dist.get_entry_map(group).items():69# ensure_safe_name70if re.search(r'[\\/]', name):71raise ValueError("Path separators not allowed in script names")72script_text = TEMPLATE.format(ep.module_name, ep.attrs[0],73'.'.join(ep.attrs), spec, group,74name)75args = cls._get_script_args(type_, name, header, script_text)76for res in args:77yield res787980easy_install.ScriptWriter.get_args = get_args818283def main():84import os85import re86import shutil87import sys88dests = sys.argv[1:] or ['.']89filename = re.sub('\.pyc$', '.py', __file__)9091for dst in dests:92shutil.copy(filename, dst)93manifest_path = os.path.join(dst, 'MANIFEST.in')94setup_path = os.path.join(dst, 'setup.py')9596# Insert the include statement to MANIFEST.in if not present97with open(manifest_path, 'a+') as manifest:98manifest.seek(0)99manifest_content = manifest.read()100if not 'include fastentrypoints.py' in manifest_content:101manifest.write(('\n' if manifest_content else '') +102'include fastentrypoints.py')103104# Insert the import statement to setup.py if not present105with open(setup_path, 'a+') as setup:106setup.seek(0)107setup_content = setup.read()108if not 'import fastentrypoints' in setup_content:109setup.seek(0)110setup.truncate()111setup.write('import fastentrypoints\n' + setup_content)112113114print(__name__)115116117