Skip to content

Commit

Permalink
Migrate to uv (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored Feb 3, 2025
1 parent 1197bb8 commit bfea018
Show file tree
Hide file tree
Showing 14 changed files with 3,771 additions and 204 deletions.
55 changes: 26 additions & 29 deletions .github/workflows/lint-and-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ jobs:
ruff:
runs-on: ubuntu-22.04
steps:
- name: Checkout ${{ github.repository }}/${{ github.ref }}
uses: actions/checkout@v4
- name: Get Ruff version
id: ruff_version
run: |
$Env:RUFF_VERSION=Select-String -path scripts/requirements-dev.txt -pattern 'ruff ?([=<>~]?= ?[\d\.]+)' | %{ $_.Matches[0].Groups[1].Value }
echo $Env:RUFF_VERSION
echo "RUFF_VERSION=$Env:RUFF_VERSION" >> $Env:GITHUB_OUTPUT
shell: pwsh
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
version: ${{ steps.ruff_version.outputs.RUFF_VERSION }}
version-file: "pyproject.toml"
- run: ruff format --check
Pyright:
runs-on: ${{ matrix.os }}
Expand All @@ -60,27 +52,20 @@ jobs:
os: [windows-latest, ubuntu-22.04]
python-version: ["3.11", "3.12", "3.13"]
steps:
- name: Checkout ${{ github.repository }}/${{ github.ref }}
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
- uses: actions/checkout@v4
- name: Set up uv for Python ${{ matrix.python-version }}
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: "uv.lock"
python-version: ${{ matrix.python-version }}
cache: "pip"
cache-dependency-path: "scripts/requirements*.txt"
- run: scripts/install.ps1
shell: pwsh
- name: Get pyright version
id: pyright_version
run: |
PYRIGHT_VERSION=$(grep '$pyrightVersion = ' 'scripts/lint.ps1' | cut -d "#" -f 1 | cut -d = -f 2 | tr -d " '")
echo pyright version: "${PYRIGHT_VERSION}"
echo PYRIGHT_VERSION="${PYRIGHT_VERSION}" >> "${GITHUB_OUTPUT}"
shell: bash
- run: echo "$PWD/.venv/bin" >> $GITHUB_PATH
- name: Analysing the code with Pyright
uses: jakebailey/pyright-action@v2
with:
version: ${{ steps.pyright_version.outputs.PYRIGHT_VERSION }}
version: PATH
working-directory: src/
python-version: ${{ matrix.python-version }}
Build:
Expand All @@ -98,14 +83,26 @@ jobs:
- os: ubuntu-22.04
python-version: "3.12"
steps:
- name: Checkout ${{ github.repository }}/${{ github.ref }}
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
- uses: actions/checkout@v4
- name: Set up uv for Python ${{ matrix.python-version }}
if: ${{ matrix.os == 'windows-latest' }}
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: "uv.lock"
python-version: ${{ matrix.python-version }}
# https://github.com/pyinstaller/pyinstaller/issues/9012
- name: Set up uv for Python ${{ matrix.python-version }}
if: ${{ matrix.os == 'ubuntu-22.04' }}
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Set up Python for PyInstaller tk issue
if: ${{ matrix.os == 'ubuntu-22.04' }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
cache-dependency-path: "scripts/requirements.txt"
- run: scripts/install.ps1
shell: pwsh
- run: scripts/build.ps1
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ Not a developer? You can still help through the following methods:
- <https://bugreports.qt.io/browse/QTBUG-114635>
- <https://bugreports.qt.io/browse/PYSIDE-2541>
- <https://bugreports.qt.io/browse/PYSIDE-2542>
- <https://github.com/pypa/pip/issues/9948>
- <https://github.com/pypa/pip/pull/10837>
- <https://github.com/pyinstaller/pyinstaller/issues/9012>
- <https://github.com/astral-sh/uv/issues/1495>
- <https://github.com/opencv/opencv/issues?q=is%3Aissue+is%3Aopen+involves%3AAvasam+sort%3Areactions-%2B1-asc+>
- <https://github.com/opencv/opencv/issues/18305>
- <https://github.com/opencv/opencv/issues/23906>
Expand Down
24 changes: 12 additions & 12 deletions docs/build instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
- You need to be part of the `input` and `tty` groups, as well as have permissions on a few files and folders.
If you are missing from either groups, the install script will take care of it on its first run, but you'll need to restart your session.

### WSL

If using WSL to test on Windows, you might need to tell uv to point to a different environment than `.venv`. You can point to your system environment by running:

```shell
export UV_PROJECT_ENVIRONMENT=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('prefix'))")
```

Read more: <https://docs.astral.sh/uv/concepts/projects/config/#project-environment-path>

### All platforms

- [Python](https://www.python.org/downloads/) 3.11+.
- [Node](https://nodejs.org) is optional, but required for complete linting.
- Alternatively you can install the [pyright python wrapper](https://pypi.org/project/pyright/) which has a bit of an overhead delay.
- [uv](https://docs.astral.sh/uv/getting-started/installation/)
- [PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell) is used to run all the scripts.
- This is needed even for Windows, as the bundled PowerShell 5.1 is too old.
- [VSCode](https://code.visualstudio.com/Download) is not required, but highly recommended.
Expand All @@ -24,15 +32,7 @@

## Install and Build steps

- Create and activate a virtual environment:
- Windows / PowerShell:
- `python -m venv .venv`
- `& ./.venv/Scripts/Activate.ps1`
- Unix / Bash:
- `python3 -m venv .venv`
- `source .venv/bin/activate`
- Run `./scripts/install.ps1` to install all dependencies.
- If you're having issues with the PySide generated code, you might want to first run `pip uninstall -y shiboken6 PySide6 PySide6-Essentials`
- Run `./scripts/install.ps1` to create/update a virtual environment and install all dependencies.
- Run the app directly with `./scripts/start.ps1 [--auto-controlled]`.
- Or debug by pressing `F5` in VSCode.
- The `--auto-controlled` flag is passed when AutoSplit is started by LiveSplit.
Expand Down
77 changes: 77 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
[project]
name = "AutoSplit"
dynamic = ["version"]
requires-python = " >=3.11"
dependencies = [
# Dependencies:
"Levenshtein >=0.25",
"PyAutoGUI >=0.9.52",
"PyWinCtl >=0.0.42", # py.typed
"keyboard",
"numpy >=2.1", # Python 3.13 support
"opencv-python-headless >=4.10", # NumPy 2 support
"packaging >=20.0", # py.typed
"psutil >=6.0.0", # Python 3.13 support
# When needed, dev builds can be found at https://download.qt.io/snapshots/ci/pyside/dev?C=M;O=D
"PySide6-Essentials <6.8.1", # Has typing issue with QMessageBox.warning https://bugreports.qt.io/browse/PYSIDE-2939
# "PySide6-Essentials >=6.8.2", # Fixed typing issue with QMessageBox.warning
"scipy >=1.14.1", # Python 3.13 support
"tomli-w >=1.1.0", # Typing fixes
"typing-extensions >=4.4.0", # @override decorator support

#
# Build and compile resources
"pyinstaller >=6.10.0", # Python 3.13 support

#
# https://peps.python.org/pep-0508/#environment-markers
#
# Windows-only dependencies:
"pygrabber >=0.2; sys_platform == 'win32'", # Completed types
"pywin32 >=307; sys_platform == 'win32'", # Python 3.13 support
"typed-D3DShot[numpy] >=1.0.1; sys_platform == 'win32'",
"winrt-Windows.Foundation >=2.2.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics >=2.2.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics.Capture >=2.3.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics.Capture.Interop >=2.3.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics.DirectX >=2.3.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics.DirectX.Direct3D11 >=2.3.0; sys_platform == 'win32'", # Python 3.13 support
"winrt-Windows.Graphics.DirectX.Direct3D11.Interop >=2.3.0; sys_platform == 'win32'",
"winrt-Windows.Graphics.Imaging >=2.3.0; sys_platform == 'win32'", # Python 3.13 support
#
# Linux-only dependencies
"PyScreeze >=1.0.0; sys_platform == 'linux'",
"pillow >=11.0; sys_platform == 'linux'", # Python 3.13 support # Necessary for PyScreeze/ImageGrab.
"python-xlib >=0.33; sys_platform == 'linux'",
]
[dependency-groups]
dev = [
#
# Visual Designer
"qt6-applications >=6.5.0",
#
# Linters & Formatters
"mypy >=1.14",
"pyright[nodejs] >=1.1",
"ruff >=0.8.5",
#
# Types (pins based on implementation version)
"scipy-stubs >=1.14.1.1",
"types-PyAutoGUI >=0.9.3",
"types-PyScreeze >=1.0.0; sys_platform == 'linux'",
"types-keyboard >=0.13.2",
"types-psutil >=6.0.0",
"types-pyinstaller >=6.10.0",
"types-python-xlib >=0.33; sys_platform == 'linux'",
"types-pywin32 >=307; sys_platform == 'win32'",
]
[tool.uv]
dependency-metadata = [
# PyAutoGUI installs extra libraries we don't want. We only use it for hotkeys
# PyScreeze -> pyscreenshot -> mss deps calls SetProcessDpiAwareness on Windows
{ name = "PyAutoGUI", requires-dist = [] },
{ name = "types-PyAutoGUI", requires-dist = [] },
]
[tool.uv.sources]
keyboard = { git = "https://github.com/boppreh/keyboard.git" } # Fix install on macos and linux-ci https://github.com/boppreh/keyboard/pull/568

# https://github.com/microsoft/pyright/blob/main/docs/configuration.md#sample-pyprojecttoml-file
[tool.pyright]
typeCheckingMode = "strict"
Expand Down
22 changes: 3 additions & 19 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,9 @@ $arguments = @(
'--windowed',
'--additional-hooks-dir=Pyinstaller/hooks',
'--icon=res/icon.ico',
'--splash=res/splash.png',
'--exclude=pkg_resources',
'--exclude=setuptools',
# The install script should ensure that these are not installed
# But we'll still include unused dependencies that would be picked up by PyInstaller
# if requirements.txt was used directly to help ensure consistency when building locally.
#
# Installed by PyAutoGUI
'--exclude=pygetwindow',
'--exclude=pymsgbox',
'--exclude=pytweening',
'--exclude=mouseinfo')
'--splash=res/splash.png')
if ($IsWindows) {
# These are used on Linux
$arguments += @(
# Installed by PyAutoGUI
'--exclude=pyscreeze',
# Sometimes installed by other automation/image libraries.
# Keep this exclusion even if nothing currently installs it, to stay future-proof.
'--exclude=PIL',
# Hidden import by winrt.windows.graphics.imaging.SoftwareBitmap.create_copy_from_surface_async
'--hidden-import=winrt.windows.foundation')
}
Expand All @@ -35,7 +18,8 @@ if ($IsLinux) {
'--hidden-import pynput.keyboard._xorg',
'--hidden-import pynput.mouse._xorg')
}
Start-Process -Wait -NoNewWindow pyinstaller -ArgumentList $arguments

Start-Process -Wait -NoNewWindow uv -ArgumentList $(@("run", "pyinstaller")+$arguments)

If ($IsLinux) {
Move-Item -Force $PSScriptRoot/../dist/AutoSplit $PSScriptRoot/../dist/AutoSplit.elf
Expand Down
10 changes: 5 additions & 5 deletions scripts/compile_resources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Set-Location "$PSScriptRoot/.."

New-Item ./src/gen -ItemType directory -Force | Out-Null
New-Item ./src/gen/__init__.py -ItemType File -Force | Out-Null
pyside6-uic './res/about.ui' -o './src/gen/about.py'
pyside6-uic './res/design.ui' -o './src/gen/design.py'
pyside6-uic './res/settings.ui' -o './src/gen/settings.py'
pyside6-uic './res/update_checker.ui' -o './src/gen/update_checker.py'
pyside6-rcc './res/resources.qrc' -o './src/gen/resources_rc.py'
uv run pyside6-uic './res/about.ui' -o './src/gen/about.py'
uv run pyside6-uic './res/design.ui' -o './src/gen/design.py'
uv run pyside6-uic './res/settings.ui' -o './src/gen/settings.py'
uv run pyside6-uic './res/update_checker.ui' -o './src/gen/update_checker.py'
uv run pyside6-rcc './res/resources.qrc' -o './src/gen/resources_rc.py'
$files = Get-ChildItem ./src/gen/ *.py
foreach ($file in $files) {
(Get-Content $file.PSPath) |
Expand Down
10 changes: 1 addition & 9 deletions scripts/designer.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
$python = $IsWindows ? 'python' : 'python3'
$qt6_applications_import = 'import qt6_applications; print(qt6_applications.__path__[0])'
$qt6_applications_path = uv run python -c 'import qt6_applications; print(qt6_applications.__path__[0])'

$qt6_applications_path = &"$python" -c $qt6_applications_import
if ($null -eq $qt6_applications_path) {
Write-Host 'Designer not found, installing qt6_applications'
&"$python" -m pip install qt6_applications
}

$qt6_applications_path = &"$python" -c $qt6_applications_import
& "$qt6_applications_path/Qt/bin/designer" `
"$PSScriptRoot/../res/design.ui" `
"$PSScriptRoot/../res/about.ui" `
Expand Down
28 changes: 6 additions & 22 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
$python = $IsWindows ? 'python' : 'python3'

# Validating user groups on Linux
If ($IsLinux) {
$groups = groups
Expand Down Expand Up @@ -30,34 +28,20 @@ If ($IsLinux) {
}

# Installing Python dependencies
$dev = If ($Env:GITHUB_JOB -eq 'Build') { '' } Else { '-dev' }
If ($IsLinux) {
If (-not $Env:GITHUB_JOB -or $Env:GITHUB_JOB -eq 'Build') {
sudo apt-get update
# python3-tk for splash screen, libxcb-cursor-dev for QT_QPA_PLATFORM=xcb, the rest for PySide6
sudo apt-get install -y python3-pip python3-tk libxcb-cursor-dev libegl1 libxkbcommon0
# having issues with npm for pyright, maybe let users take care of it themselves? (pyright from pip)
sudo apt-get install -y python3-tk libxcb-cursor-dev libegl1 libxkbcommon0
}
}
# Ensures installation tools are up to date. This also aliases pip to pip3 on MacOS.
&"$python" -m pip install wheel pip setuptools --upgrade
# Upgrading QT to 6.6.2 w/o first uninstalling shiboken6 can lead to issues
# https://bugreports.qt.io/browse/PYSIDE-2616?focusedId=777285&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-777285
&"$python" -m pip uninstall shiboken6 -y
&"$python" -m pip install -r "$PSScriptRoot/requirements$dev.txt" --upgrade
# These libraries install extra requirements we don't want
# Open suggestion for support in requirements files: https://github.com/pypa/pip/issues/9948 & https://github.com/pypa/pip/pull/10837
# PyAutoGUI: We only use it for hotkeys
&"$python" -m pip install PyAutoGUI --no-deps --upgrade

# Uninstall optional dependencies if PyAutoGUI was installed outside this script
# PyScreeze -> pyscreenshot -> mss deps call SetProcessDpiAwareness, used to be installed on Windows
# pygetwindow, pymsgbox, pytweening, MouseInfo are picked up by PyInstaller
# (also --exclude from build script, but more consistent with unfrozen run)
&"$python" -m pip uninstall pyscreenshot mss pygetwindow pymsgbox pytweening MouseInfo -y
If ($IsWindows) { &"$python" -m pip uninstall PyScreeze -y }
$prod = If ($Env:GITHUB_JOB -eq 'Build') { '--no-dev' } Else { }
$lock = If ($Env:GITHUB_JOB) { '--locked' } Else { '--upgrade' }
Write-Output "Installing Python dependencies with: uv sync $prod $lock"
uv sync $prod $lock

# Don't compile resources on the Build CI job as it'll do so in build script
If ($dev) {
If (-not $prod) {
& "$PSScriptRoot/compile_resources.ps1"
}
11 changes: 4 additions & 7 deletions scripts/lint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Set-Location "$PSScriptRoot/.."
$exitCodes = 0

Write-Host "`nRunning Ruff check ..."
ruff check --fix
uv run ruff check --fix
$exitCodes += $LastExitCode
if ($LastExitCode -gt 0) {
Write-Host "`Ruff failed ($LastExitCode)" -ForegroundColor Red
Expand All @@ -13,18 +13,15 @@ else {
}

Write-Host "`nRunning Ruff format ..."
ruff format
uv run ruff format

$pyrightVersion = 'latest' # Change this if latest has issues
$pyrightVersion = $(uv run pyright --version).replace("pyright ", "")
Write-Host "`nRunning Pyright $pyrightVersion ..."
$Env:PYRIGHT_PYTHON_FORCE_VERSION = $pyrightVersion
npx -y pyright@$pyrightVersion src/
uv run pyright src/
$exitCodes += $LastExitCode
if ($LastExitCode -gt 0) {
Write-Host "`Pyright failed ($LastExitCode)" -ForegroundColor Red
if ($pyrightVersion -eq 'latest') {
npx pyright@latest --version
}
}
else {
Write-Host "`Pyright passed" -ForegroundColor Green
Expand Down
28 changes: 0 additions & 28 deletions scripts/python_build_from_source_linux.bash

This file was deleted.

Loading

0 comments on commit bfea018

Please sign in to comment.