Skip to content
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

MAINT: IPython history file per host + configurable #381

Merged
merged 12 commits into from
Apr 19, 2024

Conversation

ZLLentz
Copy link
Member

@ZLLentz ZLLentz commented Apr 16, 2024

Description

Main changes:

  • Add a --hist-file cli argument
  • Default the history file to /u1/${USER}/hutch-python/history.sqlite (local disk)
    • Previously: normal IPython history per user home directory on NFS
  • Add a unit test

Minor changes:

  • Make the cli args type-hinted
  • Fix some minor styling inconsistencies

Motivation and Context

https://jira.slac.stanford.edu/browse/ECS-5096

  • On NFS for some of our endstations, there are some file locking issues related to the ipython history file.
  • Bringing it off of NFS should theoretically alleviate the issues, at the cost of fracturing history per-host.
  • Use CLI over cfg file because this would hypothetically need to be configured per-session, whereas the cfg file is shared between all sessions for one hutch.

How Has This Been Tested?

  • Interactively as me
  • Unit tests
  • Tested as xcsopr and it works as expected: each of xcs-daq and xcs-control has its own separate history file

Where Has This Been Documented?

Only here, I need to write pre-release docs

Pre-merge checklist

  • Code works interactively (a real hutch config file can be loaded)
  • Code contains descriptive docstrings, including context and API
  • New/changed functions and methods are covered in the test suite where possible
  • Test suite passes locally
  • Test suite passes on GitHub Actions
  • Ran docs/pre-release-notes.sh and created a pre-release documentation page
  • Pre-release docs include context, functional descriptions, and contributors as appropriate

@ZLLentz ZLLentz marked this pull request as ready for review April 16, 2024 22:42
@ZLLentz ZLLentz requested a review from tangkong April 16, 2024 22:45
Copy link
Contributor

@tangkong tangkong left a comment

Choose a reason for hiding this comment

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

I guess I should start doing this dataclass-namespace-for-cli-args thing too.

If I understand this right, the way to return to the previous behavior (storing in the ~/.ipython directory) is to invoke:

hutch-python --hist-file=:memory"

?

@@ -109,6 +130,20 @@ def configure_ipython_session():
]
ipy_config.InteractiveShellApp.exec_files = files

# Set up history from local disk (not NFS)
if args.hist_file is None:
hist_file = f"/u1/{os.environ['USER']}/hutch-python/history.sqlite"
Copy link
Contributor

Choose a reason for hiding this comment

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

So the default is now to have each user write to their own history file? What exactly are the conditions for having a /u1/<user> directory on a given host? I don't have one on psbuild so this defaults to the standard location (~/.ipython/profile_default/...)

Copy link
Member Author

@ZLLentz ZLLentz Apr 16, 2024

Choose a reason for hiding this comment

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

As written this only affects e.g. xppopr running on xpp-daq and similar operator accounts.
If you run as zlentz or on psbuild for example, you'll use the normal per-user file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it be prudent for normal users to have a fallback to some per-host NFS location in the ipython config directory?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so, in the interest of keeping original ipython functionality available. I'd personally have left that as the default, and made the flag generate the file on a local disk.

Is it possible to do:

  • no-flag: normal NFS location
  • flag, no filename: default hist_file name in /u1/...
  • flag, filename: history at the provided filename?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll look into the flexibility of argparse tomorrow, but I agree that I should have made the default behavior no change from normal.

I'd like it to "just work" without too much bash-fu in e.g. the xcs3 launcher script (e.g. if xppopr do this, if not do that... is very annoying).

Copy link
Contributor

Choose a reason for hiding this comment

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

I imagine adding just switching the xpppython script to read:

hutch-python --cfg "${HERE}/conf.yml" --hist-file

isn't too invasive, and we can switch it on as we get complaints about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think nargs='?' might work here:

parser = argparse.ArgumentParser()
parser.add_argument('--hist-file', dest='hist_file', nargs='?', const='/u1/...')
parser.parse_args([])  # Namespace(hist_file=None)
parser.parse_args(['--hist-file'])  # Namespace(hist_file='/u1/...')
parser.parse_args(['--hist-file', 'my_file/path'])  # Namespace(hist_file='my_file/path')

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll implement it exactly as you suggest, it's the cleanest overall and is opt-in

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented as suggested, edited a bit so I could include another test, expanded to allow any environment vars, not just user, because why not

@ZLLentz
Copy link
Member Author

ZLLentz commented Apr 16, 2024

If I understand this right, the way to return to the previous behavior

You've made me realize that the opr actually has no simple way to return the old previous behavior, as written. The only supported solution is to not make the special folder.

:memory: is actually an ipython built-in magic value for not using a file at all and storing the session history in the process memory.

@ZLLentz
Copy link
Member Author

ZLLentz commented Apr 16, 2024

dataclass-namespace-for-cli-args thing too

I'm a fan of this because it helps vscode keep track of which arg is supposed to be which type

ZLLentz added 2 commits April 18, 2024 16:18
- Default behavior is now unchanged from before PR
- Providing the new cli arg with empty option uses the correct default
- Switch to using template strings for easy environment variable
  inclusions
- Added an assert that runs through the const/default case
Copy link
Contributor

@tangkong tangkong left a comment

Choose a reason for hiding this comment

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

LGTM, nice little extra mile with the env vars but I'd be immensely surprised if anyone used it.

@ZLLentz
Copy link
Member Author

ZLLentz commented Apr 18, 2024

tbh it just makes it so I can add $HOSTNAME to the default later if we decide to do something like that

@ZLLentz ZLLentz merged commit 7a4df63 into pcdshub:master Apr 19, 2024
9 checks passed
@ZLLentz ZLLentz deleted the maint_ipython_history branch April 19, 2024 00: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.

2 participants