+def addRef(varname):
+ return "${%s}" % varname
+
+AG_SYSTEM_VAR_NAMES = [] # all AG System vars, with ${} syntax
+
+AG_SYSTEM_VAR = "AG_SYSTEM"
+AG_SYSTEM_VAR_REF = addRef(AG_SYSTEM_VAR)
+AG_SYSTEM_VAR_NAMES.append(AG_SYSTEM_VAR_REF)
+
+AG_SYSTEM_STATIC_VAR = "AG_SYSTEM_STATIC"
+AG_SYSTEM_STATIC_VAR_REF = addRef(AG_SYSTEM_STATIC_VAR)
+AG_SYSTEM_VAR_NAMES.append(AG_SYSTEM_STATIC_VAR_REF)
+
+AG_APP_VAR = "AG_APP"
+AG_APP_STATIC_VAR = "AG_APP_STATIC"
+
+# _initAGSystemVars needs to be called to initialize the following two
+# containers:
+EXPANDED_AG_SYSTEM_VARS = {} # ${varname} -> value (path)
+# ${varname}, ordered from longest to shortest path value
+AG_SYSTEM_VARS_LENGTH_ORDER = []
+
+def _initAGSystemVars():
+ if (len(EXPANDED_AG_SYSTEM_VARS) > 0):
+ return
+
+ for v in AG_SYSTEM_VAR_NAMES:
+ EXPANDED_AG_SYSTEM_VARS[v] = os.path.abspath(expandVars(v))
+ AG_SYSTEM_VARS_LENGTH_ORDER.append(v)
+
+ AG_SYSTEM_VARS_LENGTH_ORDER.sort(_sortByValLength)
+
+
+def parameterizePathWithAGSystemVar(inpath):
+ """Returns parameterized path if path starts with a known AG directory. Otherwise returns path as it was passed in."""
+ _initAGSystemVars()
+ path = inpath
+ if not sysutils.isWindows():
+ # ensure we have forward slashes
+ path = path.replace("\\", "/")
+
+ path = os.path.abspath(path)
+
+ for varname in AG_SYSTEM_VARS_LENGTH_ORDER:
+ varval = EXPANDED_AG_SYSTEM_VARS[varname]
+ if path.startswith(varval):
+ return path.replace(varval, varname)
+
+ return inpath
+
+def startsWithAgSystemVar(path):
+ """Returns True if path starts with a known AG system env var, False otherwise."""
+ for varname in AG_SYSTEM_VAR_NAMES:
+ if path.startswith(varname):
+ return True
+ return False
+
+def _sortByValLength(v1, v2):
+ return len(EXPANDED_AG_SYSTEM_VARS[v2]) - len(EXPANDED_AG_SYSTEM_VARS[v1])