Skip to content

Add support for detecting and doing the right thing for Gershwin users#83

Closed
pkgdemon wants to merge 5 commits into
masterfrom
gershwin-user-support
Closed

Add support for detecting and doing the right thing for Gershwin users#83
pkgdemon wants to merge 5 commits into
masterfrom
gershwin-user-support

Conversation

@pkgdemon
Copy link
Copy Markdown
Contributor

@pkgdemon pkgdemon commented Jul 11, 2025

  • If /Users is detected use this for home folder during user creation
  • If /Users is detected set zsh as default shell
  • Otherwise it should use /home and fish as default like normal

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:

  • Detect '/Users' directory and use it as the base for user home folders during user creation
  • Set zsh as the default shell when '/Users' is detected and fall back to fish otherwise

Enhancements:

  • Replace hardcoded home path and shell selection logic with a try/except check against '/Users'
  • Use a computed shell_default index variable instead of a fixed value for setting the active shell option

@pkgdemon pkgdemon self-assigned this Jul 11, 2025
@pkgdemon pkgdemon requested review from a team as code owners July 11, 2025 15:31
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jul 11, 2025

Reviewer's Guide

This PR adds runtime detection of the /Users directory to choose the appropriate home folder path and default shell (zsh when /Users exists, fish otherwise).

Class diagram for updated user creation logic

classDiagram
    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
Loading

Flow diagram for home directory and shell selection logic

flowchart 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]
Loading

File-Level Changes

Change Details Files
Detect and use /Users for home directory when present
  • Wrap open('/Users') in try/except for environment detection
  • Assign hf to '/Users/' on success, else fall back to '/home/'
src/add_users.py
Set default shell to zsh for /Users environments
  • Use try/except around open('/Users') to set self.sh to zsh or fish
  • Introduce shell_default variable for the active ComboBox index
  • Replace hardcoded shell.set_active(3) with dynamic shell_default
src/add_users.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/add_users.py Outdated
Comment thread src/add_users.py Outdated
shell = self.sh
hf = '/home/%s' % self.user.get_text()
try:
open('/Users')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 something

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@pkgdemon I do agree with @sourcery-ai on that one.

Comment thread src/add_users.py Outdated
Comment thread src/add_users.py
Comment on lines +31 to +33
hf = '/Users/%s' % self.user.get_text()
except:
hf = '/home/%s' % self.user.get_text()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (code-quality): We've found these issues:

Suggested change
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()}'

Comment thread src/add_users.py
open('/Users')
self.sh = '/usr/local/bin/zsh'
shell_default = 7
except:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (code-quality): Use except Exception: rather than bare except: (do-not-use-bare-except)

Suggested change
except:
except Exception:

Comment thread src/add_users.py Outdated
shell.append_text('ksh')
shell.append_text('zsh')
shell.set_active(3)
shell.set_active(shell_default)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This note is no longer part of the UI.

@github-project-automation github-project-automation Bot moved this from In Review to In Progress in Development Tracker Jul 11, 2025
Comment thread src/add_users.py Outdated
shell_default = 7
except:
self.sh = '/usr/local/bin/fish'
shell_default = 3
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does not affect the UI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor

@ericbsd ericbsd left a comment

Choose a reason for hiding this comment

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

Use

@pkgdemon
Copy link
Copy Markdown
Contributor Author

@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 pkgdemon closed this Jul 12, 2025
@ericbsd
Copy link
Copy Markdown
Contributor

ericbsd commented Jul 12, 2025

@pkgdemon No problem.

@pkgdemon pkgdemon deleted the gershwin-user-support branch February 22, 2026 17:55
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.

3 participants