Skip to content

Commit

Permalink
Fix menuinst._legacy.cwp (#168)
Browse files Browse the repository at this point in the history
* More tests

* Remove conda#11882 patch

* Update test_backwards_compatibility.py

* cwd -> cwp

* Fix imports

* Update cwp.py

* Create 168-fix-legacy-cwp
  • Loading branch information
kenodegard authored Dec 10, 2023
1 parent 97d5788 commit a83d5ea
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ jobs:
--file tests/requirements.txt \
python=${{ matrix.python-version }}
- name: Patch conda with PR#11882 (TEMPORARY)
shell: bash -el {0}
run: |
pip install -U --no-deps https://github.com/conda/conda/archive/cep-menuinst.tar.gz
- shell: bash -el {0}
name: Conda info
run: |
Expand Down
83 changes: 45 additions & 38 deletions menuinst/_legacy/cwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,48 @@
import sys
from os.path import join, pathsep

from menuinst._legacy.knownfolders import FOLDERID, get_folder_path

# call as: python cwp.py [--no-console] PREFIX ARGs...
parser = argparse.ArgumentParser()
parser.add_argument(
"--no-console", action="store_true", help="Create subprocess with CREATE_NO_WINDOW flag."
)
parser.add_argument("prefix", help="Prefix to be 'activated' before calling `args`.")
parser.add_argument("args", nargs="*", help="Command (and arguments) to be executed.")
parsed_args = parser.parse_args()

no_console = parsed_args.no_console
prefix = parsed_args.prefix
args = parsed_args.args

new_paths = pathsep.join(
[
prefix,
join(prefix, "Library", "mingw-w64", "bin"),
join(prefix, "Library", "usr", "bin"),
join(prefix, "Library", "bin"),
join(prefix, "Scripts"),
]
)
env = os.environ.copy()
env["PATH"] = new_paths + pathsep + env["PATH"]
env["CONDA_PREFIX"] = prefix

documents_folder, exception = get_folder_path(FOLDERID.Documents)
if exception:
documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
if not exception:
os.chdir(documents_folder)

creationflags = {}
if no_console:
creationflags["creationflags"] = getattr(subprocess, "CREATE_NO_WINDOW", 0x08000000)
sys.exit(subprocess.call(args, env=env, **creationflags))
# this must be an absolute import since the cwp.py script is copied to $PREFIX
from menuinst.knownfolders import FOLDERID, get_folder_path


def main():
# call as: python cwp.py [--no-console] PREFIX ARGs...
parser = argparse.ArgumentParser()
parser.add_argument(
"--no-console", action="store_true", help="Create subprocess with CREATE_NO_WINDOW flag."
)
parser.add_argument("prefix", help="Prefix to be 'activated' before calling `args`.")
parser.add_argument("args", nargs="*", help="Command (and arguments) to be executed.")
parsed_args = parser.parse_args()

no_console = parsed_args.no_console
prefix = parsed_args.prefix
args = parsed_args.args

new_paths = pathsep.join(
[
prefix,
join(prefix, "Library", "mingw-w64", "bin"),
join(prefix, "Library", "usr", "bin"),
join(prefix, "Library", "bin"),
join(prefix, "Scripts"),
]
)
env = os.environ.copy()
env["PATH"] = new_paths + pathsep + env["PATH"]
env["CONDA_PREFIX"] = prefix

documents_folder, exception = get_folder_path(FOLDERID.Documents)
if exception:
documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
if not exception:
os.chdir(documents_folder)

creationflags = {}
if no_console:
creationflags["creationflags"] = getattr(subprocess, "CREATE_NO_WINDOW", 0x08000000)
sys.exit(subprocess.call(args, env=env, **creationflags))


if __name__ == "__main__":
main()
7 changes: 3 additions & 4 deletions menuinst/_legacy/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
from os.path import join

import menuinst._legacy as menuinst

from .. import __version__, install
from ..utils import DEFAULT_PREFIX


Expand All @@ -20,11 +19,11 @@ def main():
opts, args = p.parse_args()

if opts.version:
sys.stdout.write("menuinst: %s\n" % menuinst.__version__)
sys.stdout.write("menuinst: %s\n" % __version__)
return

for arg in args:
menuinst.install(join(opts.prefix, arg), opts.remove, opts.prefix)
install(join(opts.prefix, arg), opts.remove, opts.prefix)


if __name__ == '__main__':
Expand Down
19 changes: 19 additions & 0 deletions news/168-fix-legacy-cwp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fix invalid import in `menuinst v1`'s legacy `cwp.py`. (#168)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
15 changes: 10 additions & 5 deletions tests/test_backwards_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

@pytest.mark.skipif(os.name != "nt", reason="Windows only")
def test_import_paths():
"These import paths are/were used by conda <=23.7.2. Ensure they still work."
from menuinst import install # noqa
from menuinst.knownfolders import FOLDERID, get_folder_path # noqa
from menuinst.win32 import dirs_src # noqa
from menuinst.winshortcut import create_shortcut # noqa
"""Imports used by conda <=23.7.2. Ensure they still work."""
import menuinst._legacy.cwp # noqa: F401
import menuinst._legacy.main # noqa: F401
import menuinst._legacy.utils # noqa: F401
import menuinst._legacy.win32 # noqa: F401
from menuinst import install # noqa: F401
from menuinst.knownfolders import FOLDERID, get_folder_path # noqa: F401
from menuinst.win32 import dirs_src # noqa: F401
from menuinst.win_elevate import isUserAdmin, runAsAdmin # noqa: F401
from menuinst.winshortcut import create_shortcut # noqa: F401

0 comments on commit a83d5ea

Please sign in to comment.