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

maintain order for CYLC_PYTHONPATH #5853

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cylc/flow/scripts/cylc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ def pythonpath_manip():
https://github.com/cylc/cylc-flow/issues/5124
"""
if 'CYLC_PYTHONPATH' in os.environ:
for item in os.environ['CYLC_PYTHONPATH'].split(os.pathsep):
abspath = os.path.abspath(item)
sys.path.insert(0, abspath)
paths = [
os.path.abspath(item)
for item in os.environ['CYLC_PYTHONPATH'].split(os.pathsep)
]
paths.extend(sys.path)
sys.path = paths
if 'PYTHONPATH' in os.environ:
for item in os.environ['PYTHONPATH'].split(os.pathsep):
abspath = os.path.abspath(item)
Expand Down
12 changes: 5 additions & 7 deletions tests/unit/scripts/test_cylc.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,16 @@ def test_pythonpath_manip(monkeypatch):

and adds items from CYLC_PYTHONPATH
"""
# If PYTHONPATH is set...
monkeypatch.setenv('PYTHONPATH', '/remove-from-sys.path')
monkeypatch.setattr('sys.path', ['/leave-alone', '/remove-from-sys.path'])
monkeypatch.setenv('PYTHONPATH', '/remove1:/remove2')
monkeypatch.setattr('sys.path', ['/leave-alone', '/remove1', '/remove2'])
pythonpath_manip()
# ... we don't change PYTHONPATH
assert os.environ['PYTHONPATH'] == '/remove-from-sys.path'
assert os.environ['PYTHONPATH'] == '/remove1:/remove2'
# ... but we do remove PYTHONPATH items from sys.path, and don't remove
# items there not in PYTHONPATH
assert sys.path == ['/leave-alone']

# If CYLC_PYTHONPATH is set we retrieve its contents and
# add them to the sys.path:
monkeypatch.setenv('CYLC_PYTHONPATH', '/add-to-sys.path')
monkeypatch.setenv('CYLC_PYTHONPATH', '/add1:/add2')
pythonpath_manip()
assert sys.path == ['/add-to-sys.path', '/leave-alone']
assert sys.path == ['/add1', '/add2', '/leave-alone']