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-2016  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  from pysys.exceptions import * 
22   
23 -def copyfileobj(fsrc, fdst, length=16*1024):
24 """Internal method to read bytes from a source file descriptor, and write to a destination file descriptor. 25 26 @param fsrc: The source file descriptor 27 @param fdst: The destination file descriptor 28 @param length: The buffer length to read from the src and write to the destination 29 30 """ 31 while 1: 32 buf = fsrc.read(length) 33 if not buf: 34 break 35 fdst.write(buf)
36 37
38 -def filecopy(src, dst):
39 """Copy source file to a destination file. 40 41 @param src: Full path to the source filename 42 @param dst: Full path the destination filename 43 @raises FileNotFoundException: Raised if the source file does not exist 44 45 """ 46 if not os.path.exists(src): 47 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(src)) 48 49 fsrc = None 50 fdst = None 51 try: 52 fsrc = open(src, 'rb') 53 fdst = open(dst, 'wb') 54 copyfileobj(fsrc, fdst) 55 finally: 56 if fdst: 57 fdst.close() 58 if fsrc: 59 fsrc.close()
60 61 62 # entry point for running the script as an executable 63 if __name__ == "__main__": 64 if len(sys.argv) < 2: 65 print "Usage: fileunzip <src> <dst>" 66 sys.exit() 67 else: 68 filecopy(sys.argv[1], sys.argv[2]) 69