Skip to content

Commit c036d29

Browse files
committed
sketch for new merge logic
1 parent 029cf01 commit c036d29

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

python/private/pypi/parse_requirements.bzl

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,65 @@ def parse_requirements(
207207
# Return normalized names
208208
ret_requirements = ret.setdefault(normalize_name(whl_name), [])
209209

210+
# First, find if there's a common sdist across all platforms
211+
common_sdist = None
212+
common_sdist_hash = None
213+
all_platforms = []
210214
for r in sorted(reqs.values(), key = lambda r: r.requirement_line):
215+
target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms)
216+
all_platforms.extend(target_platforms)
211217
whls, sdist = _add_dists(
212218
requirement = r,
213219
index_urls = index_urls.get(whl_name),
214220
logger = logger,
215221
)
216-
217-
target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms)
222+
if sdist:
223+
if not common_sdist:
224+
common_sdist = sdist
225+
common_sdist_hash = sdist.sha256
226+
elif sdist.sha256 == common_sdist_hash:
227+
continue
228+
else:
229+
common_sdist = None
230+
break
231+
232+
# If we found a common sdist, add it as a separate entry
233+
if common_sdist:
234+
# Take the first requirement's info as base since version should be same
235+
base_req = sorted(reqs.values(), key = lambda r: r.requirement_line)[0]
218236
ret_requirements.append(
219237
struct(
220-
distribution = r.distribution,
221-
srcs = r.srcs,
222-
target_platforms = sorted(target_platforms),
223-
extra_pip_args = r.extra_pip_args,
224-
whls = whls,
225-
sdist = sdist,
238+
distribution = base_req.distribution,
239+
srcs = base_req.srcs,
240+
target_platforms = sorted(all_platforms),
241+
extra_pip_args = base_req.extra_pip_args,
242+
whls = [],
243+
sdist = common_sdist,
226244
is_exposed = is_exposed,
227245
),
228246
)
229247

248+
# Now add platform-specific entries with wheels
249+
for r in sorted(reqs.values(), key = lambda r: r.requirement_line):
250+
whls, sdist = _add_dists(
251+
requirement = r,
252+
index_urls = index_urls.get(whl_name),
253+
logger = logger,
254+
)
255+
if not common_sdist or len(whls) > 0: # Only add entries that have wheels
256+
target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms)
257+
ret_requirements.append(
258+
struct(
259+
distribution = r.distribution,
260+
srcs = r.srcs,
261+
target_platforms = sorted(target_platforms),
262+
extra_pip_args = r.extra_pip_args,
263+
whls = whls,
264+
sdist = None if common_sdist else sdist, # No sdist in wheel-specific entries
265+
is_exposed = is_exposed,
266+
),
267+
)
268+
230269
if logger:
231270
logger.debug(lambda: "Will configure whl repos: {}".format(ret.keys()))
232271

tests/pypi/parse_requirements/parse_requirements_tests.bzl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,21 @@ def _test_sdist_different_hashes(env):
506506
shas = ["cafebabe", "deadbaaf"],
507507
version = "0.0.1",
508508
),
509+
target_platforms = ["cp315_linux_x86_64", "cp315_windows_x86_64"],
510+
whls = [],
511+
),
512+
struct(
513+
distribution = "foo",
514+
extra_pip_args = [],
515+
is_exposed = True,
516+
sdist = None,
517+
srcs = struct(
518+
marker = "",
519+
requirement = "foo==0.0.1",
520+
requirement_line = "foo==0.0.1 --hash=sha256:deadbaaf --hash=sha256:cafebabe",
521+
shas = ["cafebabe", "deadbaaf"],
522+
version = "0.0.1",
523+
),
509524
target_platforms = ["cp315_linux_x86_64"],
510525
whls = [
511526
struct(
@@ -520,12 +535,7 @@ def _test_sdist_different_hashes(env):
520535
distribution = "foo",
521536
extra_pip_args = [],
522537
is_exposed = True,
523-
sdist = struct(
524-
filename = "foo-0.0.1.tar.gz",
525-
url = "https://pypi.org/foo-0.0.1.tar.gz",
526-
sha256 = "cafebabe",
527-
yanked = False,
528-
),
538+
sdist = None,
529539
srcs = struct(
530540
marker = "",
531541
requirement = "foo==0.0.1",

0 commit comments

Comments
 (0)