Skip to content

Commit

Permalink
Maintain order for CYLC_PYTHONPATH (cylc#5853)
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim authored Nov 30, 2023
1 parent 381e071 commit da365bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
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']

0 comments on commit da365bf

Please sign in to comment.