Skip to content

Commit 94ca9be

Browse files
committed
setup.py: Use fastentrypoints to speed up things
This is not beautiful but closes #665 https://github.com/ninjaaron/fast-entry_points pypa/setuptools#510 pypa/setuptools#926
1 parent 79c339d commit 94ca9be

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ graft artiq/firmware
22
graft artiq/examples
33
include artiq/gui/logo*.svg
44
include versioneer.py
5+
include fastentrypoints.py
56
include artiq/_version.py

fastentrypoints.py

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

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from setuptools import setup, find_packages
44
import sys
55

6+
import fastentrypoints
67
import versioneer
78

89

0 commit comments

Comments
 (0)