Add support for detecting and doing the right thing for Gershwin users#83
Add support for detecting and doing the right thing for Gershwin users#83pkgdemon wants to merge 5 commits into
Conversation
Reviewer's GuideThis PR adds runtime detection of the Class diagram for updated user creation logicclassDiagram
class UserCreator {
+save_selection()
+__init__(button3)
-sh: str
}
UserCreator : +save_selection() uses /Users or /home for home folder
UserCreator : +__init__() sets default shell to zsh or fish based on /Users
Flow diagram for home directory and shell selection logicflowchart TD
A[Start user creation] --> B{Does /Users exist?}
B -- Yes --> C[Set home folder to /Users/<username>]
C --> D[Set default shell to zsh]
B -- No --> E[Set home folder to /home/<username>]
E --> F[Set default shell to fish]
D --> G[Continue user creation]
F --> G[Continue user creation]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @pkgdemon - I've reviewed your changes - here's some feedback:
- Instead of using open('/Users') to detect the directory, use os.path.isdir or os.path.exists to avoid leaking file descriptors.
- Avoid bare except clauses—catch the specific exception (like FileNotFoundError) to prevent masking other errors.
- Extract the "/Users" vs "/home" detection logic into a helper function to reduce duplication and improve readability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of using open('/Users') to detect the directory, use os.path.isdir or os.path.exists to avoid leaking file descriptors.
- Avoid bare except clauses—catch the specific exception (like FileNotFoundError) to prevent masking other errors.
- Extract the "/Users" vs "/home" detection logic into a helper function to reduce duplication and improve readability.
## Individual Comments
### Comment 1
<location> `src/add_users.py:29` </location>
<code_context>
up = self.password.get_text()
shell = self.sh
- hf = '/home/%s' % self.user.get_text()
+ try:
+ open('/Users')
+ hf = '/Users/%s' % self.user.get_text()
+ except:
+ hf = '/home/%s' % self.user.get_text()
</code_context>
<issue_to_address>
Catching all exceptions with a bare except may mask unexpected errors.
Catch only the specific exceptions you expect, such as FileNotFoundError, to prevent hiding unrelated errors.
</issue_to_address>
### Comment 2
<location> `src/add_users.py:30` </location>
<code_context>
shell = self.sh
- hf = '/home/%s' % self.user.get_text()
+ try:
+ open('/Users')
+ hf = '/Users/%s' % self.user.get_text()
+ except:
</code_context>
<issue_to_address>
Using open() to check for directory existence is not robust.
open() is for files, not directories, so it will fail even if '/Users' exists as a directory. Use os.path.isdir('/Users') or os.path.exists('/Users') for a reliable check.
Suggested implementation:
```python
import os
name = self.name.get_text()
up = self.password.get_text()
shell = self.sh
if os.path.isdir('/Users'):
hf = '/Users/%s' % self.user.get_text()
else:
hf = '/home/%s' % self.user.get_text()
hst = self.host.get_text()
ul = [uname, name, up, shell, hf]
```
```python
self.label5 = Gtk.Label("Shell")
shell = Gtk.ComboBoxText()
# If you need to check for '/Users' here as well, use os.path.isdir('/Users')
# Example:
# if os.path.isdir('/Users'):
# # do something
```
</issue_to_address>
### Comment 3
<location> `src/add_users.py:96` </location>
<code_context>
+ try:
+ open('/Users')
+ self.sh = '/usr/local/bin/zsh'
+ shell_default = 7
+ except:
+ self.sh = '/usr/local/bin/fish'
</code_context>
<issue_to_address>
Hardcoded index for shell_default may become fragile if shell list changes.
Determine the index dynamically by shell name instead of hardcoding it to ensure future changes to the shell list don't break the selection.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| shell = self.sh | ||
| hf = '/home/%s' % self.user.get_text() | ||
| try: | ||
| open('/Users') |
There was a problem hiding this comment.
suggestion (bug_risk): Using open() to check for directory existence is not robust.
open() is for files, not directories, so it will fail even if '/Users' exists as a directory. Use os.path.isdir('/Users') or os.path.exists('/Users') for a reliable check.
Suggested implementation:
import os
name = self.name.get_text()
up = self.password.get_text()
shell = self.sh
if os.path.isdir('/Users'):
hf = '/Users/%s' % self.user.get_text()
else:
hf = '/home/%s' % self.user.get_text()
hst = self.host.get_text()
ul = [uname, name, up, shell, hf] self.label5 = Gtk.Label("Shell")
shell = Gtk.ComboBoxText()
# If you need to check for '/Users' here as well, use os.path.isdir('/Users')
# Example:
# if os.path.isdir('/Users'):
# # do somethingThere was a problem hiding this comment.
@pkgdemon I do agree with @sourcery-ai on that one.
| hf = '/Users/%s' % self.user.get_text() | ||
| except: | ||
| hf = '/home/%s' % self.user.get_text() |
There was a problem hiding this comment.
suggestion (code-quality): We've found these issues:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring) - Use
except Exception:rather than bareexcept:(do-not-use-bare-except)
| hf = '/Users/%s' % self.user.get_text() | |
| except: | |
| hf = '/home/%s' % self.user.get_text() | |
| hf = f'/Users/{self.user.get_text()}' | |
| except Exception: | |
| hf = f'/home/{self.user.get_text()}' |
| open('/Users') | ||
| self.sh = '/usr/local/bin/zsh' | ||
| shell_default = 7 | ||
| except: |
There was a problem hiding this comment.
suggestion (code-quality): Use except Exception: rather than bare except: (do-not-use-bare-except)
| except: | |
| except Exception: |
| shell.append_text('ksh') | ||
| shell.append_text('zsh') | ||
| shell.set_active(3) | ||
| shell.set_active(shell_default) |
There was a problem hiding this comment.
This note is no longer part of the UI.
| shell_default = 7 | ||
| except: | ||
| self.sh = '/usr/local/bin/fish' | ||
| shell_default = 3 |
There was a problem hiding this comment.
This does not affect the UI.
There was a problem hiding this comment.
This does not affect the UI.
Does this mean GBI does not set the user shell to fish behind the scenes either? Am I looking at the right file to make the user shell zsh for gershwin for new users created by the installer?
There was a problem hiding this comment.
self.sh = '/usr/local/bin/fish' and self.sh = '/usr/local/bin/zsh' does work but shell_default = 3 and shell_default = 7 does noting.
There was a problem hiding this comment.
I think maybe I understand now. Are you saying I should just remove shell_default = 3 and shell_default = 7 and then the code would be good?
|
@ericbsd I am closing this one and making a new PR since I was touching the wrong file. After following the code path of gbi, gbiWindow I determiend it's add_admin.py that needed to be modified. |
|
@pkgdemon No problem. |
Summary by Sourcery
Add detection for mac-like '/Users' directory to support Gershwin users, using it for home paths and selecting zsh as the default shell when present, while retaining '/home' and fish shell as fallbacks.
New Features:
Enhancements: