#! python: #w#VIC files.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC files.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #s#system modules import string import os import os.path import stat #s#def gulpFile ### ### Read the content of the given filename ### def gulpFile(path): content = "" try: f = open(path,"r") content = f.read() f.close() except: content = "" return content #s#def isPlainFilename ### ### Does the given filename include a path component? ### ../foo ..\bar c:foo c:\bar /path/to/file c:\path\to\file ### def isPlainFilename(filename): if (filename == None): return 0 elif (string.count(filename,os.sep)): return 0 elif (string.count(filename,":") > 0 and os.name != "posix"): ### dos, os/2, windows, amiga-os return 0 return 1 #s#def sameDir ### ### Are the two given paths the same ? ### (not sure if this will actually work on *every* OS, best test it) ### def sameDir(dir1,dir2): p1 = os.path.abspath(dir1) p2 = os.path.abspath(dir2) return p1 == p2 #s#def isDir def isDir(path): return (os.path.isdir(path)) #s#def isFile def isFile(path): return (os.path.isfile(path)) #s#def makeDir ### ### Create a new directory ### def makeDir(path): result = 0 path_copy = path ### I have a suspicion that certain operating systems CD into the new directory ### So we're going to save it now, and then restore it later cwd = os.getcwd() ### Amiga Doesn't like to mkdir() where the path ends in a seperator if (path_copy[-1] == os.sep): path_copy = path_copy[0:len(path_copy)-1] try: os.makedirs(path_copy) result = 1 except: pass ### CD back to before os.chdir(cwd) return result #s#def delFile ### ### Delete a file, returns 1 on success, 0 otherwise ### def delFile(path): result = 0 try: if (isFile(path)): os.unlink(path) result = 1 except: pass return result #s#def fileReadable ### ### Return 1 if the given filename is readable by this process ### 0 otherwise ### def fileReadable(filename): try: f = open(filename,'r') f.close() return 1 except: return 0 #s#def fileName ### ### Given a full path /foo/bar/something/filename.txt ### return just the filename bit -> filename.txt ### NB: just does string manipulation, does not check existance ### def fileName(path_file): if (path_file == None): return None elif (string.count(path_file,os.sep) > 0): return string.split(path_file,os.sep)[-1] else: return path_file #s#def dirName ### ### Given a full path /foo/bar/something/filename.txt ### Return the directory component of the path -> /foo/bar/something ### NB: just does string manipulation, does not check existance ### def dirName(path_file): if (string.count(path_file,os.sep) > 0): parts = string.split(path_file,os.sep) path = "" for i in range(len(parts)-1): if (i > 0 or path_file[0] == os.sep): path = path + os.sep path = path + parts[i] return path else: return path_file #s#def fileVersion ### ### Perform VIC pk-file versioning ### def fileVersion(filename): if (not isFile(filename)): return filename lut = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" try: last_dot = string.rfind(filename,'.') if (last_dot != -1): name = filename[:last_dot] ext = filename[last_dot+1:] else: name = filename ext = "" except: name = filename ext = "" for char in lut: test_name = name+".pk"+char if (not isFile(test_name)): return test_name return "" ### Failed #s#def path2IsInPath1 ### ### Return 1 if path 2 is a subdir (or the same as) path2 ### def path2IsInPath1(path1,path2): p1 = os.path.abspath(path1) p2 = os.path.abspath(path2) result = 0 if (len(p2) >= len(p1)): if (p2 == p1): result = 1 else: if (p2[0:len(p1)] == p1): result = 1 return result #s#def safeRecursiveDelete ### ### Recurively delete the given path, but only if it lives under Home ### def safeRecursiveDelete(home,path): if (isDir(path) and isDir(home)): if (path2IsInPath1(home,path)): for filename in os.listdir(path): filename = os.path.join(path,filename) if (isDir(filename)): safeRecursiveDelete(home,filename) try: os.rmdir(filename) except: pass else: #(isFile(filename)): try: os.remove(filename) except: pass try: os.rmdir(path) except: pass #s#def chdirOK ### ### chdir that returns 1 if successful, 0 otherwsie ### def chdirOK(path): result = 1 try: os.chdir(path) except: result = 0 return result #s#def fileAllDigits ### ### Checks to see if the present filename is all numbers ### def fileAllDigits(path): if (path == '' or path == None): return 0 for i in range(len(path)): if (not path[i] in '0123456789'): return 0 return 1