Skip to content

Commit

Permalink
Support python uv as pip-compile compatible replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
avilaton committed Jul 14, 2024
1 parent 51bcebe commit 6f339a4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions python/helpers/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plette==2.1.0
poetry==1.8.3
# TODO: Replace 3p package `toml` with 3.11's new stdlib `tomllib` once we drop support for Python 3.10.
toml==0.10.2
uv==0.2.24

# Some dependencies will only install if Cython is present
Cython==3.0.10
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ def compile_new_requirement_files
def compile_file(filename)
# Shell out to pip-compile, generate a new set of requirements.
# This is slow, as pip-compile needs to do installs.
options = pip_compile_options(filename)

options, command = pip_compile_options(filename)
options_fingerprint = pip_compile_options_fingerprint(options)

name_part = "pyenv exec pip-compile " \
name_part = "#{command} " \
"#{options} -P " \
"#{dependency.name}"
fingerprint_name_part = "pyenv exec pip-compile " \
fingerprint_name_part = "#{command} " \
"#{options_fingerprint} -P " \
"<dependency_name>"

Expand Down Expand Up @@ -456,7 +457,13 @@ def pip_compile_options(filename)
options += pip_compile_options_from_compiled_file(requirements_file)
end

options.join(" ")
command = "pyenv exec pip-compile"
if (requirements_file = compiled_file_for_filename(filename)) &&
requirements_file.content.include?("autogenerated by uv")
command = "pyenv exec uv pip compile"
end

[options.join(" "), command]
end

def pip_compile_options_from_compiled_file(requirements_file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def fetch_latest_resolvable_version_string(requirement:)
def compile_file(filename)
# Shell out to pip-compile.
# This is slow, as pip-compile needs to do installs.
options = pip_compile_options(filename)
options, command = pip_compile_options(filename)
options_fingerprint = pip_compile_options_fingerprint(options)

run_pip_compile_command(
"pyenv exec pip-compile -v #{options} -P #{dependency.name} #{filename}",
fingerprint: "pyenv exec pip-compile -v #{options_fingerprint} -P <dependency_name> <filename>"
"#{command} -v #{options} -P #{dependency.name} #{filename}",
fingerprint: "#{command} -v #{options_fingerprint} -P <dependency_name> <filename>"
)

return true if dependency.top_level?
Expand All @@ -110,8 +110,8 @@ def compile_file(filename)
# update_not_possible.
write_original_manifest_files
run_pip_compile_command(
"pyenv exec pip-compile #{options} #{filename}",
fingerprint: "pyenv exec pip-compile #{options_fingerprint} <filename>"
"#{command} #{options} #{filename}",
fingerprint: "#{command} #{options_fingerprint} <filename>"
)

true
Expand Down Expand Up @@ -201,12 +201,12 @@ def check_original_requirements_resolvable
write_temporary_dependency_files(update_requirement: false)

filenames_to_compile.each do |filename|
options = pip_compile_options(filename)
options, command = pip_compile_options(filename)
options_fingerprint = pip_compile_options_fingerprint(options)

run_pip_compile_command(
"pyenv exec pip-compile #{options} #{filename}",
fingerprint: "pyenv exec pip-compile #{options_fingerprint} <filename>"
"#{command} #{options} #{filename}",
fingerprint: "#{command} #{options_fingerprint} <filename>"
)
end

Expand Down Expand Up @@ -251,7 +251,13 @@ def pip_compile_options(filename)
options << "--output-file=#{requirements_file.name}"
end

options.join(" ")
command = "pyenv exec pip-compile"
if (requirements_file = compiled_file_for_filename(filename)) &&
requirements_file.content.include?("autogenerated by uv")
command = "pyenv exec uv pip compile"
end

[options.join(" "), command]
end

def pip_compile_index_options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@
end
end

context "with a uv header" do
let(:manifest_fixture_name) { "unpinned_uv.in" }
let(:generated_fixture_name) { "pip_compile_uv_header.txt" }

it "upgrades attrs to latest" do
expect(updated_files.count).to eq(1)
expect(updated_files.first.content).to include("attrs==18.1.0")
expect(updated_files.first.content).to include("This file was autogenerated by uv")
end
end

context "with a no-binary flag" do
let(:manifest_fixture_name) { "no_binary.in" }
let(:generated_fixture_name) { "pip_compile_no_binary.txt" }
Expand Down
5 changes: 5 additions & 0 deletions python/spec/fixtures/pip_compile_files/unpinned_uv.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flaky
pytest
pytest-xdist
mock
Attrs
22 changes: 22 additions & 0 deletions python/spec/fixtures/requirements/pip_compile_uv_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file was autogenerated by uv via the following command:
# uv pip compile --output-file pip_compile_uv_header.txt unpinned_uv.in -P attrs==18.1.0
attrs==18.1.0
# via -r unpinned_uv.in
execnet==2.1.1
# via pytest-xdist
flaky==3.8.1
# via -r unpinned_uv.in
iniconfig==2.0.0
# via pytest
mock==5.1.0
# via -r unpinned_uv.in
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
pytest==8.2.2
# via
# -r unpinned_uv.in
# pytest-xdist
pytest-xdist==3.6.1
# via -r unpinned_uv.in

0 comments on commit 6f339a4

Please sign in to comment.