Skip to content

Commit

Permalink
add --json flag for uenv view that ouputs the env. vars. as json
Browse files Browse the repository at this point in the history
  • Loading branch information
bcumming committed Jun 4, 2024
1 parent cba33bb commit 2dd264e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
42 changes: 41 additions & 1 deletion lib/envvars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
import json
import os
from typing import Optional, List

Expand Down Expand Up @@ -181,6 +182,10 @@ def __init__(self):
def lists(self):
return self._lists

def clear(self):
self._lists = {}
self._scalars = {}

@property
def scalars(self):
return self._scalars
Expand Down Expand Up @@ -272,6 +277,41 @@ def export(self, dirty=False):

return {"pre": pre, "post": post}

# returns a string that represents the environment variable modifications
# in json format
#{
# "list": {
# "PATH": [
# {"op": "set", "value": "/user-environment/bin"},
# {"op": "prepend", "value": "/user-environment/env/default/bin"}
# ],
# "LD_LIBRARY_PATH": [
# {"op": "prepend", "value": "/user-environment/env/default/lib"}
# {"op": "prepend", "value": "/user-environment/env/default/lib64"}
# ]
# },
# "scalar": {
# "CUDA_HOME": "/user-environment/env/default",
# "MPIF90": "/user-environment/env/default/bin/mpif90"
# }
#}
def json(self) -> str:
# create a dictionary with the information formatted for JSON
d = {"list": {}, "scalar": {}}

for name, var in self.lists.items():
ops = []
for u in var.updates:
op = "set" if u.op == EnvVarOp.SET else ("prepend" if u.op==EnvVarOp.PREPEND else "append")
ops.append({"op": op, "value": u.value})

d["list"][name] = ops

for name, var in self.scalars.items():
d["scalar"][name] = var.value

return json.dumps(d, separators=(',', ':'))

def set_post(self, value: bool):
self._generate_post = value

Expand Down Expand Up @@ -305,7 +345,7 @@ def read_activation_script(filename: str, env: Optional[EnvVarSet]=None) -> EnvV
# rhs the value that is assigned to the environment variable
rhs = fields[1]
if name in list_variables:
fields = rhs.split(":")
fields = [f for f in rhs.split(":") if len(f.strip())>0]
lists[name] = fields
# look for $name as one of the fields (only works for append or prepend)

Expand Down
15 changes: 14 additions & 1 deletion uenv-impl
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,20 @@ the available views will be printed.
{colorize("Example", "blue")} - load the file system view {colorize('default','cyan')} provided by the loaded uenv {colorize('prgenv-gnu','cyan')}:
{colorize("uenv view prgenv-gnu:default", "white")}
{colorize("Note", "cyan")} - currently only one file system view can be loaded at a time.
{colorize("Note", "cyan")} - only one file system view can be loaded at a time.
{colorize("Example", "blue")} - output the environment variables that would be
set without loading the view:
{colorize("uenv view --json develop", "white")}
"""
))
views_parser.add_argument("view_name",
nargs='?',
default=None,
help="the view to load")
views_parser.add_argument("--json",
help="Output the environment variable changes as json.",
action="store_true")

return parser

Expand Down Expand Up @@ -959,6 +966,12 @@ def generate_view_command(args, env, env_vars):

env_vars.set_post(False)

if args.json:
terminal.info(f"outputing variables in JSON format")
json_string = env_vars.json()
env_vars.clear()
return [f"echo '{json_string}'"]

qualified_view_name = f"{uenv.name}:{vname}"
return [f"echo 'loading the view {colorize(qualified_view_name, 'cyan')}'"]

Expand Down

0 comments on commit 2dd264e

Please sign in to comment.