Skip to content

Commit

Permalink
chore: migrate the workspace Python project to uv (SMR-11) (#2994)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaffter authored Feb 5, 2025
1 parent ea9758a commit 67eb412
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 622 deletions.
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

3 changes: 1 addition & 2 deletions dev-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ function workspace-install-nodejs-dependencies {
}

function workspace-install-python-dependencies {
poetry env use $(pyenv which python)
poetry install --with dev
uv sync
workspace-initialize-env
}

Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {

'**/*.py': (filenames) => [
// Format files with Black
`poetry run black ${filenames.join(' ')}`,
`uv run black ${filenames.join(' ')}`,
// Lint the projects affected by the staged files
`nx affected --target=lint --files=${filenames.join(',')}`,
// Type check the projects affected by the staged files
Expand Down
585 changes: 0 additions & 585 deletions poetry.lock

This file was deleted.

2 changes: 0 additions & 2 deletions poetry.toml

This file was deleted.

37 changes: 20 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
[tool.poetry]
name = "sage-monorepo"
version = "1.0.0"
description = ""
authors = ["Thomas Schaffter <[email protected]>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "3.10.14"

[tool.poetry.group.dev.dependencies]
black = "24.8.0"
sqlfluff = "3.2.0"

[tool.black]
line-length = 88

Expand All @@ -22,5 +7,23 @@ dialect = "mysql"
sql_file_exts = ".sql"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[project]
authors = [
{name = "Thomas Schaffter", email = "[email protected]"},
]
requires-python = "==3.10.14"
dependencies = []
name = "sage-monorepo"
version = "1.0.0"
description = ""
readme = "README.md"
package-mode = false

[dependency-groups]
dev = [
"black==24.8.0",
"sqlfluff==3.2.0",
]
19 changes: 10 additions & 9 deletions tools/prepare-python-envs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const { spawn } = require('child_process');
const { getGitDiffFiles } = require('./git-util');
const { getNxProjects, getNxProjectFiles } = require('./nx-util');

// Returns true if the directory specified includes a Poetry lock file.
const hasPoetryLockFile = (projectDir) => {
// Returns true if the directory specified includes a Poetry or uv lock file.
const hasPoetryOrUvLockFile = (projectDir) => {
const filenames = fs.readdirSync(projectDir);
return filenames.includes('poetry.lock');
return filenames.includes('poetry.lock') || filenames.includes('uv.lock');
};

// Returns true if the dir specified includes a Poetry lock file that has changed.
const hasPoetryDefinitionChanged = (directory, changedFiles) => {
if (hasPoetryLockFile(directory)) {
const projectDefinitionPaths = ['poetry.lock'].map((filename) =>
const hasPoetryOrUvDefinitionChanged = (directory, changedFiles) => {
if (hasPoetryOrUvLockFile(directory)) {
const projectDefinitionPaths = ['poetry.lock', 'uv.lock'].map((filename) =>
['.', ''].includes(directory) ? `${filename}` : `${directory}/${filename}`,
);
if (
Expand Down Expand Up @@ -47,7 +47,7 @@ const runCommand = (command, args) => {

const installWorkspacePythonDependencies = async () => {
try {
await runCommand('poetry', ['install', '--with', 'dev']);
await runCommand('uv', ['sync']);
// The bin folder of the virtualenv has already been added to the path in dev-env.sh
} catch (error) {
console.error(`Error: ${error.message}`);
Expand All @@ -65,12 +65,13 @@ const installProjectPythonDependencies = async (projectNames) => {

console.log('✨ Preparing Python dependencies');
getGitDiffFiles().then((changedFiles) => {
if (hasPoetryDefinitionChanged('.', changedFiles)) {
if (hasPoetryOrUvDefinitionChanged('.', changedFiles)) {
installWorkspacePythonDependencies();
}
getNxProjects()
.then((projects) => {
const toUpdate = (project) => hasPoetryDefinitionChanged(project['projectDir'], changedFiles);
const toUpdate = (project) =>
hasPoetryOrUvDefinitionChanged(project['projectDir'], changedFiles);
return projects.filter(toUpdate);
})
.then((projectsToUpdate) => {
Expand Down
10 changes: 5 additions & 5 deletions tools/workspace-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
pnpm install --frozen-lockfile

# Install workspace Python dependencies
# poetry env use $(pyenv which python)
# poetry install --with dev
uv sync

# Prepare projects
pnpm dlx nx run-many --target=create-config
pnpm dlx nx run-many --target=prepare --projects=tag:language:java --parallel=1
# nx run-many --target=prepare --projects=tag:language:python --parallel=1
pnpm dlx nx run-many --target=prepare --projects=tag:language:r
# Projects that can be prepared in parallel
pnpm dlx nx run-many --target=prepare --projects=!tag:language:java,!tag:language:python
# Python and Java projects must be installed one at a time
pnpm dlx nx run-many --target=prepare --projects=tag:language:java,tag:language:python --parallel=1

Loading

0 comments on commit 67eb412

Please sign in to comment.