📄 fileutils.py
字号:
#----------------------------------------------------------------------------
# Name: fileutils.py
# Purpose: Active grid miscellaneous utilities
#
# Author: Jeff Norton
#
# Created: 12/10/04
# CVS-ID: $Id: fileutils.py,v 1.5 2006/04/20 06:25:51 RD Exp $
# Copyright: (c) 2004-2005 ActiveGrid, Inc.
# License: wxWindows License
#----------------------------------------------------------------------------
import logging
import copy
import os
import shutil
import sys
import zipfile
import activegrid.util.aglogging as aglogging
import activegrid.util.sysutils as sysutils
import activegrid.util.utillang as utillang
from activegrid.util.lang import *
global fileutilsLogger
fileutilsLogger = logging.getLogger("activegrid.util.fileutils")
# FATAL : No logging
# ERROR : No logging
# WARN : No logging
# INFO : No logging
# DEBUG : debugging
aglogging.setLevelFatal(fileutilsLogger)
#logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
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])
def makeDirsForFile(filename):
d = os.path.dirname(filename)
if (not os.path.exists(d)):
os.makedirs(d)
def createFile(filename, mode='w'):
f = None
if (not os.path.exists(filename)):
makeDirsForFile(filename)
f = file(filename, mode)
return f
def compareFiles(file1, file2, ignore=None):
## result = filecmp.cmp(file1, file2)
## if result:
## return 0
## return -1
file1.seek(0)
file2.seek(0)
while True:
line1 = file1.readline()
line2 = file2.readline()
if (len(line1) == 0):
if (len(line2) == 0):
return 0
else:
return -1
elif (len(line2) == 0):
return -1
elif (line1 != line2):
if (ignore != None):
if (line1.startswith(ignore) or line2.startswith(ignore)):
continue
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 expandKnownAGVars(value):
return expandVars(value, includeEnv=False)
def expandVars(value, includeEnv=True):
"""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_VAR):
varval = runtime.appInfo.getSystemDir()
elif (varname == AG_SYSTEM_STATIC_VAR):
varval = runtime.appInfo.getSystemStaticDir()
elif (varname == AG_APP_VAR):
varval = runtime.appInfo.getAppDir()
elif (varname == AG_APP_STATIC_VAR):
varval = runtime.appInfo.getAppStaticDir()
else:
if (includeEnv):
varval = os.getenv(varname)
else:
varval = None
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:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -