Skip to content

Commit

Permalink
Expand variables for working directory and adjust Windows default path (
Browse files Browse the repository at this point in the history
#212)

* Expand variables when creating working_dir

* Use os.path.expanduser logic to set default working_dir on Windows

* Add news item

* Fix Linux path

* Use separate test file for working dir test

* Apply suggestions from code review

---------

Co-authored-by: jaimergp <jaimergp@users.noreply.github.com>
  • Loading branch information
marcoesters and jaimergp authored May 21, 2024
1 parent faeb24d commit da8133f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion menuinst/platforms/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _write_desktop_file(self):

working_dir = self.render_key("working_dir")
if working_dir:
Path(working_dir).mkdir(parents=True, exist_ok=True)
Path(os.path.expandvars(working_dir)).mkdir(parents=True, exist_ok=True)
lines.append(f"Path={working_dir}")

for key in menuitem_defaults["platforms"]["linux"]:
Expand Down
2 changes: 1 addition & 1 deletion menuinst/platforms/osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _command(self) -> str:

working_dir = self.render_key("working_dir")
if working_dir:
Path(working_dir).mkdir(parents=True, exist_ok=True)
Path(os.path.expandvars(working_dir)).mkdir(parents=True, exist_ok=True)
lines.append(f'cd "{working_dir}"')

precommand = self.render_key("precommand")
Expand Down
10 changes: 8 additions & 2 deletions menuinst/platforms/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,15 @@ def create(self) -> Tuple[Path, ...]:
target_path, *arguments = self._process_command()
working_dir = self.render_key("working_dir")
if working_dir:
Path(working_dir).mkdir(parents=True, exist_ok=True)
Path(os.path.expandvars(working_dir)).mkdir(parents=True, exist_ok=True)
# There are two possible interpretations of HOME on Windows:
# `%USERPROFILE%` and `%HOMEDRIVE%%HOMEPATH%`.
# Follow os.path.expanduser logic here, but keep the variables
# so that Windows can resolve them at runtime in case the drives change.
elif "USERPROFILE" in os.environ:
working_dir = "%USERPROFILE%"
else:
working_dir = "%HOMEPATH%"
working_dir = "%HOMEDRIVE%%HOMEPATH%"

icon = self.render_key("icon") or ""

Expand Down
19 changes: 19 additions & 0 deletions news/212-improve-working-dir-handling
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Expand variables when creating `working_dir` and use `os.path.expanduser` logic for default `working_dir` on Windows. (#211 via #212)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
35 changes: 35 additions & 0 deletions tests/data/jsons/working-dir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$id": "https://schemas.conda.io/menuinst-1.schema.json",
"menu_name": "Sys.Prefix {{ DISTRIBUTION_NAME }}",
"menu_items": [
{
"name": "Sys.Prefix",
"description": "This will install to Windows and Linux with default options. MacOS has a custom option.",
"icon": null,
"command": [
"{{ PYTHON }}",
"-c",
"import sys; print(sys.prefix)"
],
"platforms": {
"win": {
"command": [
"{{ PYTHON }}",
"-c",
"import sys; f = open(r'__OUTPUT_FILE__', 'w'); f.write(sys.prefix); f.close()"
],
"description": "Note how __OUTPUT_FILE__ is using raw-strings. Otherwise the backslashes are not properly escaped.",
"working_dir": "%TEMP%/working_dir_test"
},
"linux": {
"working_dir": "${TMP}/working_dir_test"
},
"osx": {
"CFBundleName": "Sys Prefix",
"working_dir": "${TMPDIR}/working_dir_test"
}
}
}
]
}
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,21 @@ def test_name_dictionary(target_env_is_base):
assert item_names == expected
finally:
remove(abs_json_path, target_prefix=tmp_target_path, base_prefix=tmp_base_path)


def test_vars_in_working_dir(tmp_path, monkeypatch, delete_files):
if PLATFORM == "win":
expected_directory = Path(os.environ["TEMP"], "working_dir_test")
elif PLATFORM == "osx":
expected_directory = Path(os.environ["TMPDIR"], "working_dir_test")
else:
# Linux often does not have an environment variable for the tmp directory
monkeypatch.setenv("TMP", "/tmp")
expected_directory = Path("/tmp/working_dir_test")
delete_files.append(expected_directory)
datafile = str(DATA / "jsons" / "working-dir.json")
try:
install(datafile, base_prefix=tmp_path, target_prefix=tmp_path)
assert expected_directory.exists()
finally:
remove(datafile, base_prefix=tmp_path, target_prefix=tmp_path)

0 comments on commit da8133f

Please sign in to comment.