-
Notifications
You must be signed in to change notification settings - Fork 47
Add support for menuinst.toml #477
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
base: main
Are you sure you want to change the base?
Changes from all commits
0ad397a
cb1360d
48eb399
04e17e3
606c8bc
a2fbeab
a291190
d5324fb
7aeb8e6
df00f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,9 @@ | |
| DEFAULT_PREFIX, | ||
| _UserOrSystem, | ||
| elevate_as_needed, | ||
| read_menuinst_toml, | ||
| user_is_admin, | ||
| write_menuinst_toml, | ||
| ) | ||
|
|
||
| log = getLogger(__name__) | ||
|
|
@@ -38,6 +40,55 @@ def _maybe_try_user(base_prefix: str, target_prefix: str) -> bool: | |
| return Path(base_prefix, ".nonadmin").is_file() | ||
|
|
||
|
|
||
| def record_shortcuts( | ||
| prefix: Path, | ||
| base_prefix: Path, | ||
| source: str, | ||
| paths: list[os.PathLike], | ||
| distribution_name: str | None = None, | ||
| ) -> None: | ||
| """Record created shortcuts to menuinst.toml.""" | ||
| if not paths: | ||
| return | ||
|
|
||
| data = read_menuinst_toml(prefix) | ||
|
|
||
| # Write distribution_name only to base prefix, and only if not already set | ||
| if prefix.samefile(base_prefix) and distribution_name: | ||
| data.setdefault("distribution_name", distribution_name) | ||
|
|
||
| # Append shortcuts | ||
| shortcuts = data.setdefault("shortcuts", []) | ||
| for path in paths: | ||
| shortcuts.append({"source": source, "path": str(path)}) | ||
|
|
||
| write_menuinst_toml(prefix, data) | ||
|
|
||
|
|
||
| def remove_shortcut_records(prefix: Path, source: str) -> None: | ||
| """Remove shortcut entries matching source from menuinst.toml. | ||
|
|
||
| TODO: Use the recorded paths as the source of truth for shortcut removal, | ||
| instead of recomputing paths from menu JSON metadata. This would handle | ||
| cases where shortcuts were moved or the menu JSON changed. | ||
|
Comment on lines
+71
to
+73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be missing some of the complexity here, but I think this is well in scope of this PR. All the pieces are already there.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this TODO to help the reviewers and keep it simple. Merging this wouldn't break any existing behavior. I'm happy to do it in this PR but perhaps even opening a separate PR targeting this branch would be more feasible, let me know what you prefer.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A separate PR targeting this branch would work for me. As of this PR, all we do with those recorded paths is remove them from the record on uninstallation. So, the records currently don't find much use.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcoesters please see this separate PR lrandersson#1 targeting this functionality. |
||
| """ | ||
| data = read_menuinst_toml(prefix) | ||
| if not data: | ||
| return | ||
|
|
||
| shortcuts = data.get("shortcuts", []) | ||
| if not shortcuts: | ||
| return | ||
|
|
||
| # Filter out entries matching this source | ||
| filtered = [s for s in shortcuts if s.get("source") != source] | ||
| if len(filtered) == len(shortcuts): | ||
| return # Nothing was removed | ||
|
|
||
| data["shortcuts"] = filtered | ||
| write_menuinst_toml(prefix, data) | ||
|
|
||
|
|
||
| def _load( | ||
| metadata_or_path: os.PathLike | dict, | ||
| target_prefix: str | None = None, | ||
|
|
@@ -77,6 +128,19 @@ def install( | |
| for menu_item in menu_items: | ||
| paths += menu_item.create() | ||
|
|
||
| # Record shortcuts to menuinst.toml | ||
| if isinstance(metadata_or_path, (str, Path)): | ||
| source = Path(metadata_or_path).name | ||
| else: | ||
| source = f"{menu.name}.json" | ||
| record_shortcuts( | ||
| Path(target_prefix), | ||
| Path(base_prefix), | ||
| source, | ||
| paths, | ||
| distribution_name=menu.placeholders.get("DISTRIBUTION_NAME"), | ||
| ) | ||
|
|
||
| return paths | ||
|
|
||
|
|
||
|
|
@@ -108,6 +172,13 @@ def remove( | |
| paths += menu_item.remove() | ||
| paths += menu.remove() | ||
|
|
||
| # Remove shortcut records from menuinst.toml | ||
| if isinstance(metadata_or_path, (str, Path)): | ||
| source = Path(metadata_or_path).name | ||
| else: | ||
| source = f"{menu.name}.json" | ||
| remove_shortcut_records(Path(target_prefix), source) | ||
|
|
||
| return paths | ||
|
|
||
|
|
||
|
|
@@ -198,7 +269,8 @@ def _install_adapter(path: str, remove: bool = False, prefix: str = DEFAULT_PREF | |
| kwargs.setdefault("base_prefix", kwargs.pop("root_prefix", DEFAULT_BASE_PREFIX)) | ||
| if kwargs["base_prefix"] is None: | ||
| kwargs["base_prefix"] = DEFAULT_BASE_PREFIX | ||
| # Pass path so install/remove records the actual filename in menuinst.toml | ||
| if remove: | ||
| _api_remove(metadata, target_prefix=prefix, **kwargs) | ||
| _api_remove(json_path, target_prefix=prefix, **kwargs) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change was done to properly register the file name the shortcuts originate from in the toml file. There are other ways to achieve the same behavior but this approach required the least intrusive code changes. |
||
| else: | ||
| install(metadata, target_prefix=prefix, **kwargs) | ||
| install(json_path, target_prefix=prefix, **kwargs) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,8 @@ requirements: | |
| - setuptools_scm >=6.2 | ||
| run: | ||
| - python | ||
| - tomli # [py<311] | ||
| - tomli-w | ||
|
|
||
| test: | ||
| requires: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.