-
-
Notifications
You must be signed in to change notification settings - Fork 601
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
Skip inaccessible paths for default data dir resolution. #8812
base: develop
Are you sure you want to change the base?
Conversation
WalkthroughThe changes involve modifications to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant choose_data_dir
participant FileSystem
User->>choose_data_dir: Request to choose data directory
choose_data_dir->>FileSystem: Check read access for files in directory
alt No read access
choose_data_dir->>choose_data_dir: Check next directory
else Read access granted
choose_data_dir->>User: Return valid directory
end
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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 and nitpick comments (1)
sickchill/helper/common.py (1)
443-447
: LGTM! The error handling addresses the permission issue effectively.The added try-except block correctly handles the PermissionError when checking file existence, allowing the function to gracefully skip inaccessible paths and continue searching other locations.
However, consider adding debug logging to help troubleshoot permission issues:
try: if location.joinpath(check).exists(): return location.resolve() except PermissionError: + logger.debug(f"Skipping inaccessible path {location}: Permission denied") continue
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
sickchill/helper/common.py
(1 hunks)
🔇 Additional comments (1)
sickchill/helper/common.py (1)
443-447
: Verify the impact on non-root installations.
Since this change affects the data directory resolution, we should verify it works correctly for non-root installations.
Consider documenting this behavior in the installation guide or README to help users understand the data directory resolution process, especially for non-root installations.
✅ Verification successful
Data directory resolution works correctly for non-root installations
The code changes are safe for non-root installations based on the following evidence:
- The
choose_data_dir
function follows a clear precedence order:- Checks old data directory (program's parent directory)
- Checks user's home directory (
~/sickchill
) - Uses proper XDG config directory (
appdirs.user_config_dir
)
- The debian-ubuntu install script explicitly documents the data directory locations:
/var/lib/sickchill/.config/sickchill/
when run as sickchill user/home/user/.config/sickchill/
when run as regular user- Custom location with
--datadir
flag
- The PermissionError handling ensures graceful fallback if a location is inaccessible
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for related test cases
rg -l "choose_data_dir|data_dir" tests/
# Search for documentation about installation
fd -g "INSTALL*" -g "README*" -g "docs/*.md"
Length of output: 239
Script:
#!/bin/bash
# Search for test files and documentation with case-insensitive patterns
fd -i "readme|install" .
fd "docs" .
# Search for data directory related code and tests
rg -l "data_dir|choose_data_dir|get_data_dir" .
# Look for permission-related error handling
ast-grep --pattern 'except PermissionError: $$$'
Length of output: 401
Script:
#!/bin/bash
# Check the content of README for installation instructions
cat README.md
# Check the data directory resolution logic in the main files
rg -A 5 -B 5 "data_dir|choose_data_dir" sickchill/helper/common.py
rg -A 5 -B 5 "data_dir" SickChill.py
# Check the debian installation script for any data directory handling
cat contrib/debian-ubuntu-install.sh
Length of output: 14788
sickchill/helper/common.py
Outdated
try: | ||
if location.joinpath(check).exists(): | ||
return location.resolve() | ||
except PermissionError: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Based on the search results, I can generate the final response now:
Consider handling OSError for path operations
The codebase consistently handles OSError for similar filesystem operations, as seen in multiple files. For example:
sickchill/tv.py
handles OSError for file operations with detailed error loggingsickchill/oldbeard/browser.py
catches OSError during path operations with appropriate loggingsickchill/oldbeard/helpers.py
has multiple instances of OSError handling for filesystem operations
try:
if location.joinpath(check).exists():
return location.resolve()
-except PermissionError:
+except OSError as error: # Covers PermissionError and other filesystem errors
+ logger.debug(f"Failed to check path {location}: {error}")
continue
🔗 Analysis chain
Verify error handling for other potential filesystem exceptions.
While PermissionError is handled, other filesystem-related exceptions might occur during path resolution.
Consider handling other potential filesystem exceptions:
try:
if location.joinpath(check).exists():
return location.resolve()
-except PermissionError:
+except (PermissionError, OSError) as e:
+ logger.debug(f"Skipping path {location}: {str(e)}")
continue
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for similar error handling patterns in the codebase
rg -A 2 "except (PermissionError|OSError|IOError)"
# Search for filesystem operations that might need similar error handling
ast-grep --pattern 'try {
$$$
.exists()
$$$
} except $_ {
$$$
}'
Length of output: 9119
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should try to avoid the old EAFP pattern in modern python. I don't like to just discard random exceptions. However, there are other ways.
Maybe check if user has access to the path?
found_location = location.joinpath(check).resolve()
if found_location.exists() and os.access(found_location):
return location.resolve()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miigotu, understood. Avoiding the EAFP pattern here makes sense. Using os.access()
to check if the user has access to the path is a better approach. Thank you for the guidance.
✏️ Learnings added
Learnt from: miigotu
PR: SickChill/sickchill#8812
File: sickchill/helper/common.py:443-447
Timestamp: 2024-11-29T05:51:19.578Z
Learning: In the `choose_data_dir` function in `sickchill/helper/common.py`, avoid using try-except blocks to handle `PermissionError` or `OSError` when checking for path existence or access. Instead, check if the user has access to the path using `os.access()` to determine if the user has the necessary permissions, following the preference to avoid the EAFP pattern in modern Python.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miigotu Fair point, though I was primarily motivated by consistency with the rest of the file in my approach. I've committed an alternative in 3c6ca47. Note that exists()
raises, so the check should happen before it, and os.access
takes an argument + already performs the exists
check internally (as well as a readable check, now).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
Fixes
PermissionError: [Errno 13] Permission denied: '/root/.config/sickchill/sickbeard.db'
when running as non-root user, despite passing--data-dir
to a different path.Proposed changes in this pull request:
When resolving the default data-dir, skip any candidate paths which are inaccessible
PR is based on the DEVELOP branch
Don't send big changes all at once. Split up big PRs into multiple smaller PRs that are easier to manage and review
Read contribution guide
Summary by CodeRabbit