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

add --add-env argument for appending to generic environment variables #77

Merged
merged 1 commit into from
Apr 30, 2024
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
6 changes: 3 additions & 3 deletions cue-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def test_MisspelledConfigurationGetsWarning(self):


class TestSetupForBuild(unittest.TestCase):
args = Namespace(paths=[])
args = Namespace(extra_env_vars=[])
if ci_os == 'windows':
choco_installs = ['make']
if ci_service != 'appveyor':
Expand All @@ -721,7 +721,7 @@ def tearDown(self):

def test_AddPathsOption(self):
os.environ['FOOBAR'] = 'BAR'
args = Namespace(paths=['/my/{FOOBAR}/dir', '/my/foobar'])
args = Namespace(extra_env_vars=['PATH=/my/{FOOBAR}/dir', 'PATH=/my/foobar'])
cue.setup_for_build(args)
self.assertTrue(re.search('/my/BAR/dir', os.environ['PATH']), 'Expanded path not in PATH')
self.assertTrue(re.search('/foobar', os.environ['PATH']), 'Plain path not in PATH')
Expand Down Expand Up @@ -887,7 +887,7 @@ def test_archive7z(self):

@unittest.skipIf(ci_os != 'linux', 'CrossCompatibilityHandling tests only apply to linux')
class TestCrossCompatibilityHandling(unittest.TestCase):
args = Namespace(paths=[])
args = Namespace(extra_env_vars=[])

def setUp(self):
cue.clear_lists()
Expand Down
32 changes: 24 additions & 8 deletions cue.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,17 +766,33 @@ def setup_for_build(args):

# apparently %CD% is handled automagically, so use getcwd() instead
os.environ['TOP'] = os.getcwd()
os.environ['MAKE'] = 'make'
os.environ['EPICS_BASE'] = places['EPICS_BASE']

addpaths = []
for path in args.paths:
changed_vars = set()

for extra_env_var in args.extra_env_vars:
try:
addpaths.append(path.format(**os.environ))
key_value = extra_env_var.split('=')
key = key_value[0]
value = key_value[1]
expanded_value = value.format(**os.environ)

# Update the environment right now so later variables have access
if key in os.environ:
old_value = [os.environ[key]]
else:
old_value = []

os.environ[key] = os.pathsep.join(old_value + [expanded_value])
changed_vars.add(key)
except KeyError:
print('Environment')
[print(' ', K, '=', repr(V)) for K, V in os.environ.items()]
raise

os.environ['PATH'] = os.pathsep.join([os.environ['PATH']] + addpaths)
for key in changed_vars:
print("{0}{2} = {3}{1}".format(ANSI_CYAN, ANSI_RESET, key, os.environ[key]))

# os.environ completely updated at this point

Expand Down Expand Up @@ -1354,8 +1370,6 @@ def test_results(args):
def doExec(args):
'exec user command with vcvars'
setup_for_build(args)
os.environ['MAKE'] = 'make'
os.environ['EPICS_BASE'] = places['EPICS_BASE']
fold_start('exec.command', 'Execute command {}'.format(args.cmd))
sp.check_call(' '.join(args.cmd), shell=True)
fold_end('exec.command', 'Execute command {}'.format(args.cmd))
Expand Down Expand Up @@ -1425,8 +1439,10 @@ def timespec(s):
p = ArgumentParser()
p.add_argument('--no-vcvars', dest='vcvars', default=True, action='store_false',
help='Assume vcvarsall.bat has already been run')
p.add_argument('--add-path', dest='paths', default=[], action='append',
help='Append directory to $PATH or %%PATH%%. Expands {ENVVAR}')
p.add_argument('--add-path', dest='extra_env_vars', type=lambda x: "PATH={}".format(x), default=[], action='append',
help='Append directory to $PATH or %%PATH%%. Expands {ENVVAR}. Equivalent to: "--add-env PATH=<PATHS>"')
p.add_argument('--add-env', dest='extra_env_vars', default=[], action='append',
help='Append directory to the specified $ENVVAR or %%ENVVAR%%. Expands {OTHER_ENVVAR}. Example: "--add-env \'LD_LIBRARY_PATH={EPICS_BASE}/lib/{EPICS_HOST_ARCH}\'"')
minijackson marked this conversation as resolved.
Show resolved Hide resolved
p.add_argument('-T', '--timeout', type=timespec, metavar='DLY',
help='Terminate make after delay. DLY interpreted as second, or may be qualified with "S", "M", or "H". (default no timeout)')
subp = p.add_subparsers()
Expand Down
Loading