comparison lib/python3.8/site-packages/setuptools/command/develop.py @ 0:9e54283cc701 draft

"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author guerler
date Mon, 27 Jul 2020 03:47:31 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9e54283cc701
1 from distutils.util import convert_path
2 from distutils import log
3 from distutils.errors import DistutilsError, DistutilsOptionError
4 import os
5 import glob
6 import io
7
8 from setuptools.extern import six
9
10 import pkg_resources
11 from setuptools.command.easy_install import easy_install
12 from setuptools import namespaces
13 import setuptools
14
15 __metaclass__ = type
16
17
18 class develop(namespaces.DevelopInstaller, easy_install):
19 """Set up package for development"""
20
21 description = "install package in 'development mode'"
22
23 user_options = easy_install.user_options + [
24 ("uninstall", "u", "Uninstall this source package"),
25 ("egg-path=", None, "Set the path to be used in the .egg-link file"),
26 ]
27
28 boolean_options = easy_install.boolean_options + ['uninstall']
29
30 command_consumes_arguments = False # override base
31
32 def run(self):
33 if self.uninstall:
34 self.multi_version = True
35 self.uninstall_link()
36 self.uninstall_namespaces()
37 else:
38 self.install_for_development()
39 self.warn_deprecated_options()
40
41 def initialize_options(self):
42 self.uninstall = None
43 self.egg_path = None
44 easy_install.initialize_options(self)
45 self.setup_path = None
46 self.always_copy_from = '.' # always copy eggs installed in curdir
47
48 def finalize_options(self):
49 ei = self.get_finalized_command("egg_info")
50 if ei.broken_egg_info:
51 template = "Please rename %r to %r before using 'develop'"
52 args = ei.egg_info, ei.broken_egg_info
53 raise DistutilsError(template % args)
54 self.args = [ei.egg_name]
55
56 easy_install.finalize_options(self)
57 self.expand_basedirs()
58 self.expand_dirs()
59 # pick up setup-dir .egg files only: no .egg-info
60 self.package_index.scan(glob.glob('*.egg'))
61
62 egg_link_fn = ei.egg_name + '.egg-link'
63 self.egg_link = os.path.join(self.install_dir, egg_link_fn)
64 self.egg_base = ei.egg_base
65 if self.egg_path is None:
66 self.egg_path = os.path.abspath(ei.egg_base)
67
68 target = pkg_resources.normalize_path(self.egg_base)
69 egg_path = pkg_resources.normalize_path(
70 os.path.join(self.install_dir, self.egg_path))
71 if egg_path != target:
72 raise DistutilsOptionError(
73 "--egg-path must be a relative path from the install"
74 " directory to " + target
75 )
76
77 # Make a distribution for the package's source
78 self.dist = pkg_resources.Distribution(
79 target,
80 pkg_resources.PathMetadata(target, os.path.abspath(ei.egg_info)),
81 project_name=ei.egg_name
82 )
83
84 self.setup_path = self._resolve_setup_path(
85 self.egg_base,
86 self.install_dir,
87 self.egg_path,
88 )
89
90 @staticmethod
91 def _resolve_setup_path(egg_base, install_dir, egg_path):
92 """
93 Generate a path from egg_base back to '.' where the
94 setup script resides and ensure that path points to the
95 setup path from $install_dir/$egg_path.
96 """
97 path_to_setup = egg_base.replace(os.sep, '/').rstrip('/')
98 if path_to_setup != os.curdir:
99 path_to_setup = '../' * (path_to_setup.count('/') + 1)
100 resolved = pkg_resources.normalize_path(
101 os.path.join(install_dir, egg_path, path_to_setup)
102 )
103 if resolved != pkg_resources.normalize_path(os.curdir):
104 raise DistutilsOptionError(
105 "Can't get a consistent path to setup script from"
106 " installation directory", resolved,
107 pkg_resources.normalize_path(os.curdir))
108 return path_to_setup
109
110 def install_for_development(self):
111 if not six.PY2 and getattr(self.distribution, 'use_2to3', False):
112 # If we run 2to3 we can not do this inplace:
113
114 # Ensure metadata is up-to-date
115 self.reinitialize_command('build_py', inplace=0)
116 self.run_command('build_py')
117 bpy_cmd = self.get_finalized_command("build_py")
118 build_path = pkg_resources.normalize_path(bpy_cmd.build_lib)
119
120 # Build extensions
121 self.reinitialize_command('egg_info', egg_base=build_path)
122 self.run_command('egg_info')
123
124 self.reinitialize_command('build_ext', inplace=0)
125 self.run_command('build_ext')
126
127 # Fixup egg-link and easy-install.pth
128 ei_cmd = self.get_finalized_command("egg_info")
129 self.egg_path = build_path
130 self.dist.location = build_path
131 # XXX
132 self.dist._provider = pkg_resources.PathMetadata(
133 build_path, ei_cmd.egg_info)
134 else:
135 # Without 2to3 inplace works fine:
136 self.run_command('egg_info')
137
138 # Build extensions in-place
139 self.reinitialize_command('build_ext', inplace=1)
140 self.run_command('build_ext')
141
142 self.install_site_py() # ensure that target dir is site-safe
143 if setuptools.bootstrap_install_from:
144 self.easy_install(setuptools.bootstrap_install_from)
145 setuptools.bootstrap_install_from = None
146
147 self.install_namespaces()
148
149 # create an .egg-link in the installation dir, pointing to our egg
150 log.info("Creating %s (link to %s)", self.egg_link, self.egg_base)
151 if not self.dry_run:
152 with open(self.egg_link, "w") as f:
153 f.write(self.egg_path + "\n" + self.setup_path)
154 # postprocess the installed distro, fixing up .pth, installing scripts,
155 # and handling requirements
156 self.process_distribution(None, self.dist, not self.no_deps)
157
158 def uninstall_link(self):
159 if os.path.exists(self.egg_link):
160 log.info("Removing %s (link to %s)", self.egg_link, self.egg_base)
161 egg_link_file = open(self.egg_link)
162 contents = [line.rstrip() for line in egg_link_file]
163 egg_link_file.close()
164 if contents not in ([self.egg_path],
165 [self.egg_path, self.setup_path]):
166 log.warn("Link points to %s: uninstall aborted", contents)
167 return
168 if not self.dry_run:
169 os.unlink(self.egg_link)
170 if not self.dry_run:
171 self.update_pth(self.dist) # remove any .pth link to us
172 if self.distribution.scripts:
173 # XXX should also check for entry point scripts!
174 log.warn("Note: you must uninstall or replace scripts manually!")
175
176 def install_egg_scripts(self, dist):
177 if dist is not self.dist:
178 # Installing a dependency, so fall back to normal behavior
179 return easy_install.install_egg_scripts(self, dist)
180
181 # create wrapper scripts in the script dir, pointing to dist.scripts
182
183 # new-style...
184 self.install_wrapper_scripts(dist)
185
186 # ...and old-style
187 for script_name in self.distribution.scripts or []:
188 script_path = os.path.abspath(convert_path(script_name))
189 script_name = os.path.basename(script_path)
190 with io.open(script_path) as strm:
191 script_text = strm.read()
192 self.install_script(dist, script_name, script_text, script_path)
193
194 def install_wrapper_scripts(self, dist):
195 dist = VersionlessRequirement(dist)
196 return easy_install.install_wrapper_scripts(self, dist)
197
198
199 class VersionlessRequirement:
200 """
201 Adapt a pkg_resources.Distribution to simply return the project
202 name as the 'requirement' so that scripts will work across
203 multiple versions.
204
205 >>> from pkg_resources import Distribution
206 >>> dist = Distribution(project_name='foo', version='1.0')
207 >>> str(dist.as_requirement())
208 'foo==1.0'
209 >>> adapted_dist = VersionlessRequirement(dist)
210 >>> str(adapted_dist.as_requirement())
211 'foo'
212 """
213
214 def __init__(self, dist):
215 self.__dist = dist
216
217 def __getattr__(self, name):
218 return getattr(self.__dist, name)
219
220 def as_requirement(self):
221 return self.project_name