1
0

fastentrypoints.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # flake8: noqa
  2. # pylint: skip-file
  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. import sys
  31. from mymodule import entry_function
  32. sys.exit(entry_function())
  33. This is better.
  34. (c) 2016, Aaron Christianson
  35. http://github.com/ninjaaron/fast-entry_points
  36. '''
  37. from setuptools.command import easy_install
  38. import re
  39. TEMPLATE = r'''
  40. # -*- coding: utf-8 -*-
  41. # EASY-INSTALL-ENTRY-SCRIPT: '{3}','{4}','{5}'
  42. __requires__ = '{3}'
  43. import re
  44. import sys
  45. from {0} import {1}
  46. if __name__ == '__main__':
  47. sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
  48. sys.exit({2}())'''.lstrip()
  49. @classmethod
  50. def get_args(cls, dist, header=None): # noqa: D205,D400
  51. """
  52. Yield write_script() argument tuples for a distribution's
  53. console_scripts and gui_scripts entry points.
  54. """
  55. if header is None:
  56. # pylint: disable=E1101
  57. header = cls.get_header()
  58. spec = str(dist.as_requirement())
  59. for type_ in 'console', 'gui':
  60. group = type_ + '_scripts'
  61. for name, ep in dist.get_entry_map(group).items():
  62. # ensure_safe_name
  63. if re.search(r'[\\/]', name):
  64. raise ValueError("Path separators not allowed in script names")
  65. script_text = TEMPLATE.format(
  66. ep.module_name, ep.attrs[0], '.'.join(ep.attrs),
  67. spec, group, name)
  68. # pylint: disable=E1101
  69. args = cls._get_script_args(type_, name, header, script_text)
  70. for res in args:
  71. yield res
  72. # pylint: disable=E1101
  73. easy_install.ScriptWriter.get_args = get_args
  74. def main():
  75. import os
  76. import re
  77. import shutil
  78. import sys
  79. dests = sys.argv[1:] or ['.']
  80. filename = re.sub(r'\.pyc$', '.py', __file__)
  81. for dst in dests:
  82. shutil.copy(filename, dst)
  83. manifest_path = os.path.join(dst, 'MANIFEST.in')
  84. setup_path = os.path.join(dst, 'setup.py')
  85. # Insert the include statement to MANIFEST.in if not present
  86. with open(manifest_path, 'a+') as manifest:
  87. manifest.seek(0)
  88. manifest_content = manifest.read()
  89. if 'include fastentrypoints.py' not in manifest_content:
  90. manifest.write(('\n' if manifest_content else '') + 'include fastentrypoints.py')
  91. # Insert the import statement to setup.py if not present
  92. with open(setup_path, 'a+') as setup:
  93. setup.seek(0)
  94. setup_content = setup.read()
  95. if 'import fastentrypoints' not in setup_content:
  96. setup.seek(0)
  97. setup.truncate()
  98. setup.write('import fastentrypoints\n' + setup_content)