|
7 | 7 | from json import JSONDecodeError
|
8 | 8 | from pathlib import Path
|
9 | 9 | from tempfile import NamedTemporaryFile
|
10 |
| -from typing import Any, Dict, Iterator, List, Optional |
| 10 | +from typing import Any, Dict, Iterator, List, Optional, Set, Tuple |
11 | 11 |
|
12 | 12 | from pipenv.patched.pip._internal.req.req_install import InstallRequirement
|
13 | 13 | from pipenv.utils.dependencies import (
|
@@ -43,77 +43,96 @@ def merge_markers(entry, markers):
|
43 | 43 |
|
44 | 44 | def format_requirement_for_lockfile(
|
45 | 45 | req: InstallRequirement,
|
46 |
| - markers_lookup, |
47 |
| - index_lookup, |
48 |
| - original_deps, |
49 |
| - pipfile_entries, |
50 |
| - hashes=None, |
51 |
| -): |
52 |
| - if req.specifier: |
53 |
| - version = str(req.specifier) |
54 |
| - else: |
55 |
| - version = None |
| 46 | + markers_lookup: Dict[str, str], |
| 47 | + index_lookup: Dict[str, str], |
| 48 | + original_deps: Dict[str, Any], |
| 49 | + pipfile_entries: Dict[str, Any], |
| 50 | + hashes: Optional[Set[str]] = None, |
| 51 | +) -> Tuple[str, Dict[str, Any]]: |
| 52 | + """Format a requirement for the lockfile with improved VCS handling.""" |
56 | 53 | name = normalize_name(req.name)
|
57 |
| - index = index_lookup.get(name) |
58 |
| - markers = req.markers |
59 |
| - req.index = index |
60 |
| - pipfile_entry = pipfile_entries[name] if name in pipfile_entries else {} |
61 |
| - entry = {} |
| 54 | + entry: Dict[str, Any] = {"name": name} |
| 55 | + pipfile_entry = pipfile_entries.get(name, {}) |
| 56 | + |
| 57 | + # Handle VCS requirements |
62 | 58 | if req.link and req.link.is_vcs:
|
63 | 59 | vcs = req.link.scheme.split("+", 1)[0]
|
64 |
| - entry["ref"] = determine_vcs_revision_hash(req, vcs, pipfile_entry.get("ref")) |
65 | 60 |
|
| 61 | + # Get VCS URL from original deps or normalize the link URL |
66 | 62 | if name in original_deps:
|
67 | 63 | entry[vcs] = original_deps[name]
|
68 | 64 | else:
|
69 | 65 | vcs_url, _ = normalize_vcs_url(req.link.url)
|
70 | 66 | entry[vcs] = vcs_url
|
71 |
| - if pipfile_entry.get("subdirectory"): |
| 67 | + |
| 68 | + # Handle reference information - try multiple sources |
| 69 | + ref = determine_vcs_revision_hash(req, vcs, pipfile_entry.get("ref")) |
| 70 | + if ref: |
| 71 | + entry["ref"] = ref |
| 72 | + |
| 73 | + # Handle subdirectory information |
| 74 | + if isinstance(pipfile_entry, dict) and pipfile_entry.get("subdirectory"): |
72 | 75 | entry["subdirectory"] = pipfile_entry["subdirectory"]
|
73 | 76 | elif req.link.subdirectory_fragment:
|
74 | 77 | entry["subdirectory"] = req.link.subdirectory_fragment
|
75 |
| - if req.req: |
76 |
| - entry["version"] = str(req.specifier) |
77 |
| - elif version: |
78 |
| - entry["version"] = version |
79 |
| - elif req.link and req.link.is_file: |
80 |
| - entry["file"] = req.link.url |
81 |
| - if hashes: |
82 |
| - entry["hashes"] = sorted(set(hashes)) |
83 |
| - entry["name"] = name |
84 |
| - if index: |
85 |
| - entry.update({"index": index}) |
| 78 | + |
| 79 | + # Handle non-VCS requirements |
| 80 | + else: |
| 81 | + if req.req and req.req.specifier: |
| 82 | + entry["version"] = str(req.req.specifier) |
| 83 | + elif req.specifier: |
| 84 | + entry["version"] = str(req.specifier) |
| 85 | + elif req.link and req.link.is_file: |
| 86 | + entry["file"] = req.link.url |
| 87 | + |
| 88 | + # Add index information |
| 89 | + if name in index_lookup: |
| 90 | + entry["index"] = index_lookup[name] |
| 91 | + |
| 92 | + # Handle markers |
| 93 | + markers = req.markers |
86 | 94 | if markers:
|
87 |
| - entry.update({"markers": str(markers)}) |
| 95 | + entry["markers"] = str(markers) |
88 | 96 | if name in markers_lookup:
|
89 | 97 | merge_markers(entry, markers_lookup[name])
|
90 |
| - if isinstance(pipfile_entry, dict) and "markers" in pipfile_entry: |
91 |
| - merge_markers(entry, pipfile_entry["markers"]) |
92 |
| - if isinstance(pipfile_entry, dict) and "os_name" in pipfile_entry: |
93 |
| - merge_markers(entry, f"os_name {pipfile_entry['os_name']}") |
94 |
| - entry = translate_markers(entry) |
| 98 | + if isinstance(pipfile_entry, dict): |
| 99 | + if "markers" in pipfile_entry: |
| 100 | + merge_markers(entry, pipfile_entry["markers"]) |
| 101 | + if "os_name" in pipfile_entry: |
| 102 | + merge_markers(entry, f"os_name {pipfile_entry['os_name']}") |
| 103 | + |
| 104 | + # Handle extras |
95 | 105 | if req.extras:
|
96 | 106 | entry["extras"] = sorted(req.extras)
|
97 |
| - if isinstance(pipfile_entry, dict) and pipfile_entry.get("file"): |
98 |
| - entry["file"] = pipfile_entry["file"] |
99 |
| - if pipfile_entry.get("editable"): |
100 |
| - entry["editable"] = pipfile_entry.get("editable") |
101 |
| - entry.pop("version", None) |
102 |
| - entry.pop("index", None) |
103 |
| - elif isinstance(pipfile_entry, dict) and pipfile_entry.get("path"): |
104 |
| - entry["path"] = pipfile_entry["path"] |
105 |
| - if pipfile_entry.get("editable"): |
106 |
| - entry["editable"] = pipfile_entry.get("editable") |
107 |
| - entry.pop("version", None) |
108 |
| - entry.pop("index", None) |
| 107 | + |
| 108 | + # Handle hashes |
| 109 | + if hashes: |
| 110 | + entry["hashes"] = sorted(set(hashes)) |
| 111 | + |
| 112 | + # Handle file/path entries from Pipfile |
| 113 | + if isinstance(pipfile_entry, dict): |
| 114 | + if pipfile_entry.get("file"): |
| 115 | + entry["file"] = pipfile_entry["file"] |
| 116 | + if pipfile_entry.get("editable"): |
| 117 | + entry["editable"] = pipfile_entry["editable"] |
| 118 | + entry.pop("version", None) |
| 119 | + entry.pop("index", None) |
| 120 | + elif pipfile_entry.get("path"): |
| 121 | + entry["path"] = pipfile_entry["path"] |
| 122 | + if pipfile_entry.get("editable"): |
| 123 | + entry["editable"] = pipfile_entry["editable"] |
| 124 | + entry.pop("version", None) |
| 125 | + entry.pop("index", None) |
| 126 | + |
| 127 | + entry = translate_markers(entry) |
109 | 128 | return name, entry
|
110 | 129 |
|
111 | 130 |
|
112 | 131 | def get_locked_dep(project, dep, pipfile_section, current_entry=None):
|
113 | 132 | # initialize default values
|
114 | 133 | is_top_level = False
|
115 | 134 |
|
116 |
| - # if the dependency has a name, find corresponding entry in pipfile |
| 135 | + # # if the dependency has a name, find corresponding entry in pipfile |
117 | 136 | if isinstance(dep, dict) and dep.get("name"):
|
118 | 137 | dep_name = pep423_name(dep["name"])
|
119 | 138 | for pipfile_key, pipfile_entry in pipfile_section.items():
|
|
0 commit comments