Skip to content

simplify venv setup #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2025
Merged

simplify venv setup #246

merged 3 commits into from
Aug 5, 2025

Conversation

Jason2866
Copy link

@Jason2866 Jason2866 commented Aug 5, 2025

by using fixed path and addsitedir

Description:

Related issue (if applicable): fixes #

Checklist:

  • The pull request is done against the latest develop branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR, more changes are allowed when changing boards.json
  • I accept the CLA

Summary by CodeRabbit

  • Refactor
    • Simplified and unified handling of Python virtual environment and executable paths, resulting in more reliable environment setup.
    • Improved clarity and maintainability by removing redundant code and streamlining environment path management.

by using fixed path and addsitedir
Copy link

coderabbitai bot commented Aug 5, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This change refactors the management of Python virtual environment and executable paths in builder/main.py. Multiple specialized helper functions are consolidated into a single get_executable_path() function, simplifying how executables are located within the virtual environment. Path setup for Python modules is streamlined using site.addsitedir(), and subprocess calls now consistently use the updated executable paths.

Changes

Cohort / File(s) Change Summary
Virtual Environment Path Handling Refactor
builder/main.py
Consolidated multiple helper functions into get_executable_path(), replaced manual path logic, simplified Python path setup with site.addsitedir(), updated subprocess calls to use unified executable paths, and removed redundant code.

Sequence Diagram(s)

sequenceDiagram
    participant MainScript
    participant VirtualEnv
    participant Subprocess

    MainScript->>VirtualEnv: get_executable_path("python" or "pip" or "uv")
    VirtualEnv-->>MainScript: Return full path to executable
    MainScript->>VirtualEnv: setup_pipenv_in_package()
    MainScript->>MainScript: site.addsitedir() for penv and site-packages
    MainScript->>Subprocess: Call executable (pip/uv) using path from get_executable_path()
    Subprocess-->>MainScript: Execution result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15–25 minutes

Possibly related PRs

Poem

🐇
In the warren of code, paths tangled and tight,
Now one bunny hop brings executables to light.
No more winding tunnels, just one simple lane—
With get_executable_path(), clarity we gain!
The virtual burrow’s tidy, the subprocesses run,
This streamlined refactor—reviewing’s more fun!
🌱

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch venv_simplify

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Jason2866
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Aug 5, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
builder/main.py (1)

284-292: Update docstring to reflect function changes.

The docstring states "Returns: str: Path to esptool executable", but the function now returns None. This should be updated to reflect the actual behavior.

 def install_esptool():
     """
     Install esptool from package folder "tool-esptoolpy" using uv package manager.
     
-    Returns:
-        str: Path to esptool executable
-
     Raises:
         SystemExit: If esptool installation fails
     """
🧹 Nitpick comments (2)
builder/main.py (2)

185-186: Misleading comments about environment usage.

The comments state "Use modified environment with venv Python", but os.environ is the original process environment. The virtual environment Python is used because PYTHON_EXE and tool paths have been updated to point to venv executables, not because the environment is modified.

Consider updating the comments to be more accurate:

env=os.environ  # Pass current environment (venv paths are used via executable paths)

Also applies to: 219-220, 262-263


309-309: Remove trailing whitespace.

Line 309 contains trailing whitespace that should be removed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 020fd73 and 757bd99.

📒 Files selected for processing (1)
  • builder/main.py (7 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
builder/main.py (1)
builder/frameworks/espidf.py (1)
  • install_python_deps (1488-1552)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: build (macos-15, examples/espidf-peripherals-usb)
  • GitHub Check: build (windows-latest, examples/espidf-exceptions)
  • GitHub Check: build (macos-15, examples/arduino-zigbee-light)
  • GitHub Check: build (macos-15, examples/arduino-wifiscan)
  • GitHub Check: build (windows-latest, examples/espidf-http-request)
  • GitHub Check: build (windows-latest, examples/arduino-wifiscan)
  • GitHub Check: build (windows-latest, examples/arduino-zigbee-switch)
  • GitHub Check: build (windows-latest, examples/arduino-zigbee-light)
  • GitHub Check: build (windows-latest, examples/arduino-NimBLE-ext_client)
  • GitHub Check: build (ubuntu-latest, examples/espidf-ulp-lp)
  • GitHub Check: build (ubuntu-latest, examples/espidf-blink)
  • GitHub Check: build (ubuntu-latest, examples/espidf-arduino-C6-ULP-blink)
  • GitHub Check: build (ubuntu-latest, examples/espidf-arduino-matter-light)
  • GitHub Check: build (ubuntu-latest, examples/espidf-coap-server)
  • GitHub Check: build (ubuntu-latest, examples/espidf-hello-world)
  • GitHub Check: build (ubuntu-latest, examples/arduino-NimBLE-ext_client)
  • GitHub Check: build (ubuntu-latest, examples/espidf-arduino-littlefs)
  • GitHub Check: build (ubuntu-latest, examples/arduino-rmt-blink)
  • GitHub Check: build (ubuntu-latest, examples/tasmota)
  • GitHub Check: build (ubuntu-latest, examples/arduino-blink)
🔇 Additional comments (8)
builder/main.py (8)

19-19: LGTM!

The site module import is correctly added and necessary for the new path management approach using site.addsitedir().


67-81: Well-designed path helper function.

The new get_executable_path() function effectively consolidates the logic from the removed helper functions and correctly handles platform-specific differences for executable paths.


95-95: Correct usage of the new helper function.

The assertion correctly uses get_executable_path("pip") to verify the virtual environment was created properly.


99-105: Proper virtual environment activation.

The code correctly sets up the virtual environment and updates both the SCons environment and global Python executable paths to use the venv Python.


133-137: Correct tool path initialization.

The code properly initializes the Python paths and sets up the executable paths for esptool and uv using the new helper function.


324-327: Clear installation sequence.

The comments helpfully clarify the installation order, ensuring dependencies are installed before esptool.


710-714: Good handling of paths with spaces.

The code correctly wraps the esptool path in quotes when it contains spaces, preventing command-line execution issues.


1001-1001: Modern f-string usage.

Good use of f-string for better readability.

@Jason2866
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Aug 5, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
builder/main.py (1)

278-284: Update docstring to reflect function behavior.

The docstring states "Returns: str: Path to esptool executable" but the function no longer returns anything.

 def install_esptool():
     """
     Install esptool from package folder "tool-esptoolpy" using uv package manager.
     
-    Returns:
-        str: Path to esptool executable
-
     Raises:
         SystemExit: If esptool installation fails
     """
♻️ Duplicate comments (1)
builder/main.py (1)

111-125: Excellent improvement using site.addsitedir().

The refactored path setup properly addresses the previous concern about multiple Python versions by using sys.version_info to construct the directory name. The use of site.addsitedir() is a more robust approach than manual path manipulation as it handles .pth files and other site-specific configurations correctly.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 020fd73 and 0b4f5dd.

📒 Files selected for processing (1)
  • builder/main.py (7 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Jason2866
PR: pioarduino/platform-espressif32#225
File: builder/main.py:99-108
Timestamp: 2025-07-13T20:03:29.695Z
Learning: In the ESP32 platform builder, the subprocess call to get site-packages directory in setup_python_paths() function is intentionally left without error handling because if this basic Python operation fails, it indicates a fundamental system issue that would cause the entire build process to fail anyway.
📚 Learning: in the esp32 platform builder, the subprocess call to get site-packages directory in setup_python_pa...
Learnt from: Jason2866
PR: pioarduino/platform-espressif32#225
File: builder/main.py:99-108
Timestamp: 2025-07-13T20:03:29.695Z
Learning: In the ESP32 platform builder, the subprocess call to get site-packages directory in setup_python_paths() function is intentionally left without error handling because if this basic Python operation fails, it indicates a fundamental system issue that would cause the entire build process to fail anyway.

Applied to files:

  • builder/main.py
🧬 Code Graph Analysis (1)
builder/main.py (1)
builder/frameworks/espidf.py (1)
  • install_python_deps (1488-1552)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: build (windows-latest, examples/arduino-rmt-blink)
  • GitHub Check: build (macos-15, examples/espidf-hello-world)
  • GitHub Check: build (macos-15, examples/espidf-arduino-C6-ULP-blink)
  • GitHub Check: build (windows-latest, examples/arduino-matter-light)
  • GitHub Check: build (windows-latest, examples/espidf-coap-server)
  • GitHub Check: build (windows-latest, examples/espidf-hello-world)
  • GitHub Check: build (windows-latest, examples/espidf-storage-sdcard)
  • GitHub Check: build (windows-latest, examples/arduino-zigbee-light)
  • GitHub Check: build (macos-15, examples/arduino-usb-keyboard)
  • GitHub Check: build (macos-15, examples/espidf-arduino-matter-light)
  • GitHub Check: build (macos-15, examples/arduino-wifiscan)
  • GitHub Check: build (windows-latest, examples/espidf-http-request)
  • GitHub Check: build (windows-latest, examples/arduino-zigbee-switch)
  • GitHub Check: build (windows-latest, examples/arduino-wifiscan)
  • GitHub Check: build (windows-latest, examples/espidf-exceptions)
  • GitHub Check: build (windows-latest, examples/espidf-arduino-blink)
  • GitHub Check: build (windows-latest, examples/arduino-blink)
  • GitHub Check: build (ubuntu-latest, examples/espidf-peripherals-usb)
  • GitHub Check: build (ubuntu-latest, examples/espidf-arduino-matter-light)
  • GitHub Check: build (ubuntu-latest, examples/arduino-blink)
🔇 Additional comments (7)
builder/main.py (7)

19-19: LGTM!

The addition of the site module import is appropriate for the new path setup approach using site.addsitedir().


67-81: Clean consolidation of executable path logic.

The new get_executable_path() function effectively consolidates the previously scattered path construction logic into a single, well-documented function. The platform-specific handling is correct.


83-97: Good use of the new helper function.

The refactoring to use get_executable_path("pip") simplifies the code and maintains consistency across the codebase.


99-108: LGTM!

The virtual environment setup and Python executable replacement logic is clear and properly sequenced.


127-132: Consistent executable path setup.

Good use of the centralized get_executable_path() function for all tool executables.


318-322: LGTM!

The setup order is logical with helpful comments explaining the sequence.


704-708: Good handling of paths with spaces.

The code properly quotes the esptool path when it contains spaces, which is important for cross-platform compatibility.

@Jason2866 Jason2866 merged commit ede42a8 into develop Aug 5, 2025
68 checks passed
@Jason2866 Jason2866 deleted the venv_simplify branch August 6, 2025 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant