Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/smc_pyutil/fastentrypoints.py
Views: 272
1
# -*- coding: utf-8 -*-
2
3
# Copyright (c) 2016, Aaron Christianson
4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are
8
# met:
9
#
10
# 1. Redistributions of source code must retain the above copyright
11
# notice, this list of conditions and the following disclaimer.
12
#
13
# 2. Redistributions in binary form must reproduce the above copyright
14
# notice, this list of conditions and the following disclaimer in the
15
# documentation and/or other materials provided with the distribution.
16
#
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
'''
29
Monkey patch setuptools to write faster console_scripts with this format:
30
31
import sys
32
from mymodule import entry_function
33
sys.exit(entry_function())
34
35
This is better.
36
37
(c) 2016, Aaron Christianson
38
http://github.com/ninjaaron/fast-entry_points
39
'''
40
from __future__ import absolute_import
41
from __future__ import print_function
42
from setuptools.command import easy_install
43
import re
44
TEMPLATE = '''\
45
# -*- coding: utf-8 -*-
46
# EASY-INSTALL-ENTRY-SCRIPT: '{3}','{4}','{5}'
47
__requires__ = '{3}'
48
import re
49
import sys
50
51
from {0} import {1}
52
53
if __name__ == '__main__':
54
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
55
sys.exit({2}())'''
56
57
58
@classmethod
59
def get_args(cls, dist, header=None):
60
"""
61
Yield write_script() argument tuples for a distribution's
62
console_scripts and gui_scripts entry points.
63
"""
64
if header is None:
65
header = cls.get_header()
66
spec = str(dist.as_requirement())
67
for type_ in 'console', 'gui':
68
group = type_ + '_scripts'
69
for name, ep in dist.get_entry_map(group).items():
70
# ensure_safe_name
71
if re.search(r'[\\/]', name):
72
raise ValueError("Path separators not allowed in script names")
73
script_text = TEMPLATE.format(ep.module_name, ep.attrs[0],
74
'.'.join(ep.attrs), spec, group,
75
name)
76
args = cls._get_script_args(type_, name, header, script_text)
77
for res in args:
78
yield res
79
80
81
easy_install.ScriptWriter.get_args = get_args
82
83
84
def main():
85
import os
86
import re
87
import shutil
88
import sys
89
dests = sys.argv[1:] or ['.']
90
filename = re.sub('\.pyc$', '.py', __file__)
91
92
for dst in dests:
93
shutil.copy(filename, dst)
94
manifest_path = os.path.join(dst, 'MANIFEST.in')
95
setup_path = os.path.join(dst, 'setup.py')
96
97
# Insert the include statement to MANIFEST.in if not present
98
with open(manifest_path, 'a+') as manifest:
99
manifest.seek(0)
100
manifest_content = manifest.read()
101
if not 'include fastentrypoints.py' in manifest_content:
102
manifest.write(('\n' if manifest_content else '') +
103
'include fastentrypoints.py')
104
105
# Insert the import statement to setup.py if not present
106
with open(setup_path, 'a+') as setup:
107
setup.seek(0)
108
setup_content = setup.read()
109
if not 'import fastentrypoints' in setup_content:
110
setup.seek(0)
111
setup.truncate()
112
setup.write('import fastentrypoints\n' + setup_content)
113
114
115
print(__name__)
116
117