Package pysys :: Package utils :: Module filecopy
[hide private]
[frames] | no frames]

Source Code for Module pysys.utils.filecopy

 1  #!/usr/bin/env python 
 2  # PySys System Test Framework, Copyright (C) 2006-2013  M.B.Grieve 
 3   
 4  # This library is free software; you can redistribute it and/or 
 5  # modify it under the terms of the GNU Lesser General Public 
 6  # License as published by the Free Software Foundation; either 
 7  # version 2.1 of the License, or (at your option) any later version. 
 8   
 9  # This library is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12  # Lesser General Public License for more details. 
13   
14  # You should have received a copy of the GNU Lesser General Public 
15  # License along with this library; if not, write to the Free Software 
16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
17   
18  # Contact: moraygrieve@users.sourceforge.net 
19   
20  import sys, os 
21   
22 -def copyfileobj(fsrc, fdst, length=16*1024):
23 """Internal method to read bytes from a source file descriptor, and write to a destination file descriptor. 24 25 @param fsrc: The source file descriptor 26 @param fdst: The destination file descriptor 27 @param length: The buffer length to read from the src and write to the destination 28 29 """ 30 while 1: 31 buf = fsrc.read(length) 32 if not buf: 33 break 34 fdst.write(buf)
35 36
37 -def filecopy(src, dst):
38 """Copy source file to a destination file. 39 40 @param src: Full path to the source filename 41 @param dst: Full path the destination filename 42 @raises FileNotFoundException: Raised if the source file does not exist 43 44 """ 45 if not os.path.exists(src): 46 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(src)) 47 48 fsrc = None 49 fdst = None 50 try: 51 fsrc = open(src, 'rb') 52 fdst = open(dst, 'wb') 53 copyfileobj(fsrc, fdst) 54 finally: 55 if fdst: 56 fdst.close() 57 if fsrc: 58 fsrc.close()
59 60 61 # entry point for running the script as an executable 62 if __name__ == "__main__": 63 if len(sys.argv) < 2: 64 print "Usage: fileunzip <src> <dst>" 65 sys.exit() 66 else: 67 filecopy(sys.argv[1], sys.argv[2]) 68