+ line1 = line1.replace(" ", "")
+ line2 = line2.replace(" ", "")
+ if (line1 != line2):
+ len1 = len(line1)
+ len2 = len(line2)
+ if ((abs(len1 - len2) == 1) and (len1 > 0) and (len2 > 0)
+ and (line1[-1] == "\n") and (line2[-1] == "\n")):
+ if (len1 > len2):
+ longer = line1
+ shorter = line2
+ else:
+ shorter = line1
+ longer = line2
+ if ((longer[-2] == "\r") and (longer[:-2] == shorter[:-1])):
+ continue
+ if ((longer[-2:] == shorter[-2:]) and (longer[-3] == "\r") and (longer[:-3] == shorter[:-2])):
+ continue
+ return -1
+
+def expandVars(value):
+ """Syntax: ${myvar,default="default value"}"""
+ import activegrid.runtime as runtime
+ sx = value.find("${")
+ if (sx >= 0):
+ result = asString(value[:sx])
+ endx = value.find("}")
+ if (endx > 1):
+ defaultValue = None
+ defsx = value.find(",default=\"")
+ if ((defsx > sx) and (defsx < endx)):
+ varname = value[sx+2:defsx]
+ if (value[endx-1] == '"'):
+ defaultValue = value[defsx+10:endx-1]
+ if (defaultValue == None):
+ varname = value[sx+2:endx]
+ if (varname == "AG_SYSTEM"):
+ varval = runtime.appInfo.getSystemDir()
+ elif (varname == "AG_SYSTEM_STATIC"):
+ varval = runtime.appInfo.getSystemStaticDir()
+ elif (varname == "AG_APP"):
+ varval = runtime.appInfo.getAppDir()
+ elif (varname == "AG_APP_STATIC"):
+ varval = runtime.appInfo.getAppStaticDir()
+ else:
+ varval = os.getenv(varname)
+ if ((varval == None) and (defaultValue != None)):
+ varval = defaultValue
+ if (varval == None):
+ result += value[sx:endx+1]
+ else:
+ result += varval
+ return result + expandVars(value[endx+1:])
+ return value
+
+def toPHPpath(path, otherdir=None):
+ return convertSourcePath(path, "php", otherdir=otherdir)
+
+def toPythonpath(path, otherdir=None):
+ return convertSourcePath(path, "python", otherdir=otherdir)
+
+def toUnixPath(path):
+ if (path != None and os.sep != '/'):
+ path = path.replace(os.sep, '/')
+ return path
+
+def convertSourcePath(path, to, otherdir=None):
+ fromname = "python"
+ if (to == "python"):
+ fromname = "php"
+ pythonNode = os.sep + fromname + os.sep
+ ix = path.find(pythonNode)
+ if (ix < 0):
+ ix = path.find(fromname) - 1
+ if ((ix < 0) or (len(path) <= ix+7)
+ or (path[ix] not in ("\\", "/")) or (path[ix+7] not in ("\\", "/"))):
+ raise Exception("Not in a %s source tree. Cannot create file name for %s." % (fromname, path))
+ if (otherdir == None):
+ return path[:ix+1] + to + path[ix+7:]
+ else:
+ return otherdir + path[ix+7:]
+ if (otherdir == None):
+ return path.replace(pythonNode, os.sep + to + os.sep)
+ else:
+ return otherdir + path[ix+7:]
+
+
+def visit(directory, files, extension):
+ testdirs = os.listdir(directory)
+ for thing in testdirs:
+ fullpath = os.path.join(directory, thing)
+ if (os.path.isdir(fullpath)):
+ visit(fullpath, files, extension)
+ elif thing.endswith(extension):
+ fullname = os.path.normpath(os.path.join(directory, thing))
+ if not fullname in files:
+ files.append(fullname)
+
+def listFilesByExtensionInPath(path=[], extension='.lyt'):
+ #Collect input and output arguments into one bunch
+ retval = []
+ for directory in path:
+ visit(directory, retval, extension)
+ return retval
+
+def getFileLastModificationTime(fileName):
+ return os.path.getmtime(fileName)
+
+def findFileLocation(location, fileName):
+ i = fileName.rfind(os.sep)
+ if i > 0:
+ fileName = fileName[:i]
+ while location[0:2] == '..' and location[2:3] == os.sep:
+ location = location[3:]
+ i = fileName.rfind(os.sep)
+ fileName = fileName[:i]
+ absPath = fileName + os.sep + location
+ return absPath
+
+def getAllExistingFiles(files, basepath=None, forceForwardSlashes=False):
+ """For each file in files, if it exists, adds its absolute path to the rtn list. If file is a dir, calls this function recursively on all child files in the dir.
+ If basepath is set, and if the file being processed is relative to basedir, adds that relative path to rtn list instead of the abs path.
+ Is this is Windows, and forceForwardSlashes is True, make sure returned paths only have forward slashes."""
+ if isinstance(files, basestring):
+ files = [files]
+ rtn = []
+ for file in files:
+ if os.path.exists(file):
+ if os.path.isfile(file):
+ if basepath and hasAncestorDir(file, basepath):
+ rtn.append(getRelativePath(file, basepath))
+ else:
+ rtn.append(os.path.abspath(str(file)))
+ elif os.path.isdir(file):
+ dircontent = [os.path.join(file, f) for f in os.listdir(file)]
+ rtn.extend(getAllExistingFiles(dircontent, basepath))
+
+ if forceForwardSlashes and sysutils.isWindows():
+ newRtn = []
+ for f in rtn:
+ newRtn.append(f.replace("\\", "/"))
+ rtn = newRtn
+
+ return rtn
+
+def hasAncestorDir(file, parent):
+ """Returns true if file has the dir 'parent' as some parent in its path."""
+ return getRelativePath(file, parent) != None
+
+def getRelativePath(file, basedir):
+ """Returns relative path from 'basedir' to 'file', assuming 'file' lives beneath 'basedir'. If it doesn't, returns None."""
+ file = os.path.abspath(file)
+ parent = os.path.abspath(basedir)