Skip to content

Add sudo-safe pycache redirect and ps-based process discovery fallback#14

Open
wwulfric wants to merge 1 commit into
masterfrom
codex/fix-permission-error-during-peeka-update
Open

Add sudo-safe pycache redirect and ps-based process discovery fallback#14
wwulfric wants to merge 1 commit into
masterfrom
codex/fix-permission-error-during-peeka-update

Conversation

@wwulfric

Copy link
Copy Markdown
Collaborator

Motivation

  • Ensure Peeka avoids writing root-owned bytecode caches into user virtualenvs when launched under sudo by redirecting sys.pycache_prefix to a safe temporary prefix.
  • Provide process discovery on platforms without /proc (e.g., macOS) by falling back to ps output parsing.

Description

  • Add import-time policy in peeka/__init__.py with _running_under_sudo, _sudo_pycache_prefix, and _configure_sudo_bytecode_policy to set sys.pycache_prefix when running under sudo, guardable via PEEKA_DISABLE_SUDO_PYCACHE_REDIRECT and configurable via PEEKA_SUDO_PYCACHE_PREFIX.
  • Refactor peeka/core/processes.py to return a ps-based fallback by adding _discover_python_processes_with_ps, _python_process_from_fields, and using subprocess/shlex to parse ps -axo pid=,comm=,command= when /proc is unavailable.
  • Adjust _read_python_process to centralize construction of PythonProcess objects and carry created_at through the new helper.
  • Add new unit tests in tests/test_import_policy.py covering the sudo bytecode cache redirection behavior and extend tests/test_processes.py with tests for the ps fallback and related filtering.

Testing

  • Ran the new import-time policy tests in tests/test_import_policy.py which exercise importlib.reload(peeka) under mocked geteuid and environment variables, and they passed.
  • Executed the updated process discovery tests in tests/test_processes.py including test_discover_python_processes_falls_back_to_ps_without_proc and test_discover_python_processes_ps_fallback_excludes_current_process, and they passed.
  • Overall unit test run for the modified files succeeded.

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant