sbsv2/raptor/python/plugins/filter_copyfile.py
branchfix
changeset 533 408bfff46ad7
child 534 3b10c85868b1
equal deleted inserted replaced
532:1083c9a3a7cf 533:408bfff46ad7
       
     1 #
       
     2 # Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # Filter class for doing CLEAN, CLEANEXPORT and REALLYCLEAN efficiently.
       
    16 #
       
    17 
       
    18 import os
       
    19 import sys
       
    20 import tempfile
       
    21 import filter_interface
       
    22 import shutil
       
    23 import generic_path
       
    24 import stat
       
    25 
       
    26 class FilterCopyFile(filter_interface.Filter):
       
    27 	
       
    28 	def open(self, params):
       
    29 		"initialise"
       
    30 		
       
    31 		self.ok = True
       
    32 
       
    33 		self.files = {}
       
    34 		
       
    35 		return self.ok
       
    36 	
       
    37 	
       
    38 	def write(self, text):
       
    39 		"process some log text"
       
    40 		
       
    41 		for line in text.splitlines():
       
    42 			if line.startswith("<copy"):
       
    43 				source_start=line.find("source='")
       
    44 				source=line[source_start+8:line.find("'", source_start+8)]
       
    45 				destinations = line[line.find(">",source_start)+1:line.find("</copy>")].split(" ")
       
    46 
       
    47 				if source in self.files:
       
    48 					self.files[source].update(destinations)
       
    49 				else:
       
    50 					self.files[source] = set(destinations)
       
    51 				
       
    52 				
       
    53 		return self.ok
       
    54 	
       
    55 	
       
    56 	def summary(self):
       
    57 		"finish off"
       
    58 		for source in self.files.keys():
       
    59 			print "<debug>self.files %s</debug>" % self.files[source]
       
    60 			for dest in self.files[source]:
       
    61 				self.copyfile(source, dest)
       
    62 		
       
    63 		return self.ok
       
    64 
       
    65 
       
    66 	def close(self):
       
    67 		"nop"
       
    68 		
       
    69 
       
    70 		return self.ok
       
    71 
       
    72 	def copyfile(self, _source, _destination):
       
    73 		"""Copy the source file to the destination file (create a directory
       
    74 		   to copy into if it does not exist). Don't copy if the destination
       
    75 		   file exists and has an equal or newer modification time."""
       
    76 		source = generic_path.Path(str(_source).replace('%20',' '))
       
    77 		destination = generic_path.Path(str(_destination).replace('%20',' '))
       
    78 		dest_str = str(destination)
       
    79 		source_str = str(source)
       
    80 
       
    81 		try:
       
    82 
       
    83 
       
    84 			destDir = destination.Dir()
       
    85 			if not destDir.isDir():
       
    86 				os.makedirs(str(destDir))
       
    87 				shutil.copyfile(source_str, dest_str)
       
    88 				return 
       
    89 
       
    90 			# Destination file exists so we have to think about updating it
       
    91 			sourceMTime = 0
       
    92 			destMTime = 0
       
    93 			sourceStat = 0
       
    94 			try:
       
    95 				sourceStat = os.stat(source_str)
       
    96 				sourceMTime = sourceStat[stat.ST_MTIME]
       
    97 				destMTime = os.stat(dest_str)[stat.ST_MTIME]
       
    98 			except OSError, e:
       
    99 				if sourceMTime == 0:
       
   100 					message = "Source of copyfile does not exist:  " + str(source)
       
   101 					print message
       
   102 
       
   103 			if destMTime == 0 or destMTime < sourceMTime:
       
   104 				if os.path.exists(dest_str):
       
   105 					os.chmod(dest_str,stat.S_IREAD | stat.S_IWRITE)
       
   106 				shutil.copyfile(source_str, dest_str)
       
   107 
       
   108 				# Ensure that the destination file remains executable if the source was also:
       
   109 				os.chmod(dest_str,sourceStat[stat.ST_MODE] | stat.S_IREAD | stat.S_IWRITE | stat.S_IWGRP ) 
       
   110 
       
   111 
       
   112 		except Exception,e:
       
   113 			message = "Could not export " + source_str + " to " + dest_str + " : " + str(e)
       
   114 			print message
       
   115 
       
   116 		return 
       
   117 	
       
   118 # the end				
       
   119