Skip to content

Commit 7ca079d

Browse files
committed
Update to PEP517 style builds.
1 parent 299f611 commit 7ca079d

File tree

1 file changed

+106
-117
lines changed

1 file changed

+106
-117
lines changed

Lib/pip.py

Lines changed: 106 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sysconfig
66
import warnings
77
import platform
8+
import rebuildpython
89

910
import __np__
1011

@@ -21,18 +22,6 @@
2122
del sys.path[0]
2223
sys.modules["pip"] = _pip
2324

24-
try:
25-
import pip._internal.pyproject
26-
27-
def load_pyproject_toml(use_pep517, pyproject_toml, setup_py, req_name):
28-
return None
29-
30-
pip._internal.pyproject.load_pyproject_toml = load_pyproject_toml
31-
except:
32-
pass
33-
34-
import pip._internal.req.req_install
35-
3625

3726
def urlretrieve(url, output_filename):
3827
local_filename = __np__.download_file(url, os.path.dirname(output_filename))
@@ -261,127 +250,99 @@ def install_dependency(name):
261250
f.write(package_index["version"])
262251

263252

264-
_InstallRequirement = pip._internal.req.req_install.InstallRequirement
265-
253+
from pip._internal.req.req_install import InstallRequirement
254+
from typing import Iterable, List, Optional, Tuple
255+
import pip._internal.wheel_builder
256+
_orig_build_one_inside_env = pip._internal.wheel_builder._build_one_inside_env
257+
258+
def _build_one_inside_env(
259+
req: InstallRequirement,
260+
output_dir: str,
261+
build_options: List[str],
262+
global_options: List[str],
263+
editable: bool,
264+
) -> Optional[str]:
265+
fallback = False
266+
if req.name == "setuptools":
267+
raise NotImplementedError("setuptools not yet supported")
268+
try:
269+
package_index = getPackageJson("packages", req.name)
270+
except __np__.NoSuchURL:
271+
fallback = True
272+
except:
273+
if req.name == "certifi":
274+
fallback = True
275+
else:
276+
throw
266277

267-
class InstallRequirement(_InstallRequirement):
268-
def install(
269-
self,
270-
install_options,
271-
global_options=None,
272-
root=None,
273-
home=None,
274-
prefix=None,
275-
warn_script_location=True,
276-
use_user_site=False,
277-
pycompile=True,
278-
):
279-
fallback = False
278+
if fallback or not req.source_dir:
279+
__np__.my_print("FALLBACK to standard install for %s" % req.name)
280280

281-
try:
282-
package_index = getPackageJson("packages", self.name)
283-
except __np__.NoSuchURL:
284-
fallback = True
285-
except:
286-
if self.name == "certifi":
287-
fallback = True
288-
else:
289-
throw
290-
291-
if fallback or not self.source_dir:
292-
__np__.my_print("FALLBACK to standard install for %s" % self.name)
293-
294-
return _InstallRequirement.install(
295-
self,
296-
install_options,
297-
global_options,
298-
root,
299-
home,
300-
prefix,
301-
warn_script_location,
302-
use_user_site,
303-
pycompile,
304-
)
281+
return _orig_build_one_inside_env(req, output_dir, build_options, global_options, editable)
305282

306-
matched_source = None
307-
for source in package_index["scripts"]:
308-
for key, value_glob in source["metadata"].items():
309-
if not fnmatch.fnmatch(self.metadata[key], value_glob):
310-
matched_metadata = False
311-
break
312-
else:
313-
matched_metadata = True
314-
315-
if matched_metadata:
316-
matched_source = source
283+
matched_source = None
284+
for source in package_index["scripts"]:
285+
for key, value_glob in source["metadata"].items():
286+
if not fnmatch.fnmatch(req.metadata[key], value_glob):
287+
matched_metadata = False
317288
break
289+
else:
290+
matched_metadata = True
318291

319-
install_temp_dir = os.path.dirname(self.source_dir)
292+
if matched_metadata:
293+
matched_source = source
294+
break
320295

321-
if "build_tools" in matched_source:
322-
for dep in matched_source["build_tools"]:
323-
install_build_tool(dep)
296+
install_temp_dir = os.path.dirname(req.source_dir)
324297

325-
if "dependencies" in matched_source:
326-
for dep in matched_source["dependencies"]:
327-
install_dependency(dep)
298+
if "build_tools" in matched_source:
299+
for dep in matched_source["build_tools"]:
300+
install_build_tool(dep)
328301

329-
for file in matched_source["files"]:
330-
package_dir_url = getPackageUrl("packages", self.name)
331-
urlretrieve(
332-
"{package_dir_url}/{file}".format(**locals()),
333-
os.path.join(install_temp_dir, file),
334-
)
335-
336-
build_script_module_name = getBuildScriptName(install_temp_dir, self.name)
302+
if "dependencies" in matched_source:
303+
for dep in matched_source["dependencies"]:
304+
install_dependency(dep)
337305

338-
initcwd = os.getcwd()
339-
initenviron = dict(os.environ)
340-
build_script_module = importFileAsModule(
341-
build_script_module_name,
342-
os.path.join(install_temp_dir, matched_source["build_script"]),
306+
for file in matched_source["files"]:
307+
package_dir_url = getPackageUrl("packages", req.name)
308+
urlretrieve(
309+
"{package_dir_url}/{file}".format(**locals()),
310+
os.path.join(install_temp_dir, file),
343311
)
344312

345-
static_pattern = matched_source.get("static_pattern")
346-
if static_pattern is None and os.name == "nt":
347-
static_pattern = "*"
313+
build_script_module_name = getBuildScriptName(install_temp_dir, req.name)
348314

349-
# Put to empty, to avoid need to remove it and for easier manual usage.
350-
os.environ["NUITKA_PYTHON_STATIC_PATTERN"] = static_pattern or ""
315+
initcwd = os.getcwd()
316+
initenviron = dict(os.environ)
317+
build_script_module = importFileAsModule(
318+
build_script_module_name,
319+
os.path.join(install_temp_dir, matched_source["build_script"]),
320+
)
351321

352-
try:
353-
result = build_script_module.run(install_temp_dir, self.source_dir)
354-
finally:
355-
if build_script_module_name in sys.modules:
356-
del sys.modules[build_script_module_name]
357-
try:
358-
del build_script_module
359-
except NameError:
360-
pass
361-
os.chdir(initcwd)
362-
os.environ.clear()
363-
os.environ.update(initenviron)
322+
static_pattern = matched_source.get("static_pattern")
323+
if static_pattern is None and os.name == "nt":
324+
static_pattern = "*"
364325

365-
if result:
366-
_InstallRequirement.install(
367-
self,
368-
install_options,
369-
global_options,
370-
root,
371-
home,
372-
prefix,
373-
warn_script_location,
374-
use_user_site,
375-
pycompile,
376-
)
326+
# Put to empty, to avoid need to remove it and for easier manual usage.
327+
os.environ["NUITKA_PYTHON_STATIC_PATTERN"] = static_pattern or ""
377328

378-
if not int(os.environ.get("NUITKA_PYTHON_MANUAL_REBUILD", "0")):
379-
import rebuildpython
329+
try:
330+
result = build_script_module.run(install_temp_dir, req.source_dir)
331+
finally:
332+
if build_script_module_name in sys.modules:
333+
del sys.modules[build_script_module_name]
334+
try:
335+
del build_script_module
336+
except NameError:
337+
pass
338+
os.chdir(initcwd)
339+
os.environ.clear()
340+
os.environ.update(initenviron)
380341

381-
rebuildpython.run_rebuild()
342+
return result
382343

383344

384-
pip._internal.req.req_install.InstallRequirement = InstallRequirement
345+
pip._internal.wheel_builder._build_one_inside_env = _build_one_inside_env
385346

386347

387348
import pip._internal.index.package_finder
@@ -391,7 +352,7 @@ def install(
391352

392353
_PackageFinder = pip._internal.index.package_finder.PackageFinder
393354

394-
355+
"""
395356
def build_stub(
396357
requirements, # type: Iterable[InstallRequirement]
397358
wheel_cache, # type: WheelCache
@@ -403,8 +364,8 @@ def build_stub(
403364
r.use_pep517 = False
404365
return [], requirements
405366
406-
407367
wheel_builder.build = build_stub
368+
"""
408369

409370

410371
class PackageFinder(_PackageFinder):
@@ -430,6 +391,34 @@ def find_all_candidates(self, project_name):
430391

431392
pip._internal.index.package_finder.PackageFinder = PackageFinder
432393

394+
my_path = os.path.abspath(__file__)
395+
396+
def get_runnable_pip() -> str:
397+
return my_path
398+
399+
import pip._internal.build_env
400+
pip._internal.build_env.get_runnable_pip = get_runnable_pip
401+
402+
403+
import pip._internal.req.req_install
404+
orig_install = pip._internal.req.req_install.InstallRequirement.install
405+
406+
def install(
407+
self,
408+
global_options = None,
409+
root = None,
410+
home = None,
411+
prefix = None,
412+
warn_script_location = True,
413+
use_user_site = False,
414+
pycompile = True,
415+
):
416+
orig_install(self, global_options, root, home, prefix, warn_script_location, use_user_site, pycompile)
417+
418+
rebuildpython.run_rebuild()
419+
420+
pip._internal.req.req_install.InstallRequirement.install = install
421+
433422

434423
def main():
435424
# Work around the error reported in #9540, pending a proper fix.

0 commit comments

Comments
 (0)