# $Id: FileTest.py,v 1.1 2002/05/13 14:38:15 graham Exp $ # # RDF FileTest class for performing various tests on files. This module # has been written to isolate some system dependencies when porting code # between regular Python and Jython. # #--------+---------+---------+---------+---------+---------+---------+---------+ # # Copyright (c) 2002, G. KLYNE # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # #--------+---------+---------+---------+---------+---------+---------+---------+ # $Source: /file/cvsdev/PythonN3/FileTest.py,v $ # $Author: graham $ # $Revision: 1.1 $ $Date: 2002/05/13 14:38:15 $ #--------+---------+---------+---------+---------+---------+---------+---------+ # 1 2 3 4 5 6 7 8 import os #import re import sys import string from stat import * #--------+---------+---------+---------+---------+---------+---------+---------+ # FileTest - class for testing filenames # class FileTest: """ Perform various file-related tests. This module has been written to isolate some system dependencies when porting code between regular Python and Jython. """ # End of FileTest def exists( DirPath, FileName ): """ Test if named file in specified directory exists, either as a file or as a directory. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A fully qualified filename string if the file exists, otherwise None. """ if not DirPath: DirPath = os.getcwd() FullName = os.path.normpath( os.path.join( DirPath, FileName ) ) if os.path.exists( FullName ): return FullName return None def isFile( DirPath, FileName ): """ Test if named file in specified directory exists as a file. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A fully qualified filename string if the file exists, otherwise None. """ FullName = exists( DirPath, FileName ) if FullName: if os.path.isfile( FullName ): return FullName return None def isDirectory( DirPath, FileName ): """ Test if named file in specified directory exists as a directory. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A fully qualified filename string if the file exists, otherwise None. """ FullName = exists( DirPath, FileName ) if FullName: if os.path.isdir( FullName ): return FullName return None def canRead( DirPath, FileName ): """ Test if named file in specified directory can be read. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A true value if the indicated file can be read by the calling program, otherwise a false value. """ FullName = exists( DirPath, FileName ) if FullName: ### if checkMode( FullName, 0x124 ): return FullName if os.access( FullName, os.R_OK ): return FullName return None def canWrite( DirPath, FileName ): """ Test if named file in specified directory can be written or created. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A true value if the indicated file can be written or created by the calling program, otherwise a false value. """ FullName = exists( DirPath, FileName ) if FullName: ### if checkMode( FullName, 0x092 ): return FullName if os.access( FullName, os.W_OK ): return FullName else: return canCreate( DirPath, FileName ) return None def canCreate( DirPath, FileName ): """ Test if named file in specified directory can be created. Parameters: DirPath path of directory where file resides, or None FileName name of file to be tested Returns: A true value if the indicated file can be created by the calling program, otherwise a false value if the file already exists or the directory is not writeable. """ if not FileName: return None FullName = exists( DirPath, FileName ) if FullName: return None FullName = os.path.normpath( os.path.join( DirPath, FileName ) ) DirName = os.path.dirname( FullName ) if not DirName: DirName = os.getcwd() #### if checkMode( DirName, 0x092 ): return FullName if os.access( DirName, os.W_OK ): return FullName return None def checkMode( FullName, Mode ): """ Local helper function checks file access mode against supplied value: returns true value if any of the required mode bits are set, otherwise false. """ Stat = os.stat( FullName ) FileMode = S_IMODE( Stat[ST_MODE] ) sys.stdout.write( " Mode for "+FullName+" is "+hex(FileMode)+"\n" ) # For now: # base writeability assessent on union of owner, group, world permissions return FileMode & Mode #--------+---------+---------+---------+---------+---------+---------+---------+ # # Stand-alone test code follows: # if __name__ == '__main__': sys.stdout.write( "FileTest tests\n" ) ### Change this to match the running environment ### CurrentDir = "D:\Dev\PythonN3" CurrentDir = os.getcwd() PathSeparator = os.sep sys.stdout.write( "os.getcwd(): "+os.getcwd()+"\n" ) sys.stdout.write( "os.sep: "+repr(os.sep)+"\n" ) sys.stdout.write( "os.altsep: "+repr(os.altsep)+"\n" ) sys.stdout.write( "os.pathsep: "+repr(os.pathsep)+"\n" ) sys.stdout.write( "os.name: "+repr(os.name)+"\n" ) sys.stdout.write( "exists() tests:\n" ) e1 = exists( ".", "FileTest.py" ) if not e1: sys.stdout.write( "exists( '.', 'FileTest.py' ) returned false.\n" ) elif e1 != "FileTest.py": sys.stdout.write( "exists( '.', 'FileTest.py' ) returned '"+e1+"'\n" ) e2 = exists( None, "FileTest.py" ) if not e2: sys.stdout.write( "exists( None, 'FileTest.py' ) returned false.\n" ) elif e2 != CurrentDir + PathSeparator + "FileTest.py": sys.stdout.write( "exists( None, 'FileTest.py' ) returned '"+e2+"'\n" ) e3 = exists( ".", "FileTest.none" ) if e3: sys.stdout.write( "exists( '.', 'FileTest.None' ) returned '"+e3+"'\n" ) n4 = os.path.dirname( e2 ) e4 = exists( None, n4 ) if not e4: sys.stdout.write( "exists( None, "+n4+" ) returned false.\n" ) elif e4 != CurrentDir: sys.stdout.write( "exists( None, "+n4+" ) returned '"+e4+"'\n" ) sys.stdout.write( "isFile() tests:\n" ) f1 = isFile( ".", "FileTest.py" ) if not f1: sys.stdout.write( "isFile( '.', 'FileTest.py' ) returned false.\n" ) elif f1 != "FileTest.py": sys.stdout.write( "isFile( '.', 'FileTest.py' ) returned '"+f1+"'\n" ) f2 = isFile( None, "FileTest.py" ) if not f2: sys.stdout.write( "isFile( None, 'FileTest.py' ) returned false.\n" ) elif f2 != CurrentDir + PathSeparator + "FileTest.py": sys.stdout.write( "isFile( None, 'FileTest.py' ) returned '"+f2+"'\n" ) f3 = isFile( ".", "FileTest.none" ) if f3: sys.stdout.write( "isFile( '.', 'FileTest.None' ) returned '"+f3+"'\n" ) n4 = os.path.dirname( e2 ) f4 = isFile( None, n4 ) if f4: sys.stdout.write( "isFile( None, "+n4+" ) returned '"+f4+"'\n" ) sys.stdout.write( "isDirectory() tests:\n" ) d1 = isDirectory( ".", "FileTest.py" ) if d1: sys.stdout.write( "isDirectory( '.', 'FileTest.py' ) returned '"+d1+"'\n" ) d2 = isDirectory( None, "FileTest.py" ) if d2: sys.stdout.write( "isDirectory( None, 'FileTest.py' ) returned '"+d2+"'\n" ) d3 = isDirectory( ".", "FileTest.none" ) if d3: sys.stdout.write( "isDirectory( '.', 'FileTest.None' ) returned '"+d3+"'\n" ) n4 = os.path.dirname( e2 ) d4 = isDirectory( None, n4 ) if not d4: sys.stdout.write( "isDirectory( None, "+n4+" ) returned false.\n" ) elif d4 != CurrentDir: sys.stdout.write( "isDirectory( None, "+n4+" ) returned '"+d4+"'\n" ) sys.stdout.write( "canRead() tests:\n" ) r1 = canRead( ".", "FileTest.py" ) if not r1: sys.stdout.write( "canRead( '.', 'FileTest.py' ) returned false.\n" ) elif r1 != "FileTest.py": sys.stdout.write( "canRead( '.', 'FileTest.py' ) returned '"+r1+"'\n" ) r2 = canRead( None, "FileTest.py" ) if not r2: sys.stdout.write( "canRead( None, 'FileTest.py' ) returned false.\n" ) elif r2 != CurrentDir + PathSeparator + "FileTest.py": sys.stdout.write( "canRead( None, 'FileTest.py' ) returned '"+r2+"'\n" ) r3 = canRead( ".", "FileTest.none" ) if r3: sys.stdout.write( "canRead( '.', 'FileTest.None' ) returned '"+r3+"'\n" ) sys.stdout.write( "canWrite() tests\n" ) w1 = canWrite( ".", "FileTest.py" ) if not w1: sys.stdout.write( "canWrite( '.', 'FileTest.py' ) returned false.\n" ) elif w1 != "FileTest.py": sys.stdout.write( "canWrite( '.', 'FileTest.py' ) returned '"+w1+"'\n" ) w2 = canWrite( None, "FileTest.py" ) if not w2: sys.stdout.write( "canWrite( None, 'FileTest.py' ) returned false.\n" ) elif w2 != CurrentDir + PathSeparator + "FileTest.py": sys.stdout.write( "canWrite( None, 'FileTest.py' ) returned '"+w2+"'\n" ) w3 = canWrite( ".", "FileTest.new" ) if not w3: sys.stdout.write( "canWrite( '.', 'FileTest.new' ) returned false.\n" ) elif w3 != "FileTest.new": sys.stdout.write( "canWrite( '.', 'FileTest.new' ) returned '"+w3+"'\n" ) sys.stdout.write( "canCreate() tests\n" ) c1 = canCreate( ".", "FileTest.py" ) if c1: sys.stdout.write( "canCreate( '.', 'FileTest.py' ) returned '"+c1+"'\n" ) c2 = canCreate( None, "FileTest.py" ) if c2: sys.stdout.write( "canCreate( None, 'FileTest.py' ) returned '"+c2+"'\n" ) c3 = canCreate( ".", "FileTest.new" ) if not c3: sys.stdout.write( "canCreate( '.', 'FileTest.new' ) returned false.\n" ) elif c3 != "FileTest.new": sys.stdout.write( "canCreate( '.', 'FileTest.new' ) returned '"+c3+"'\n" ) sys.stdout.write( "Exiting\n" ) #--------+---------+---------+---------+---------+---------+---------+---------+ # # $Log: FileTest.py,v $ # Revision 1.1 2002/05/13 14:38:15 graham # Add Jython capability to test for file readability/writability # #