sbsv2/raptor/python/plugins/filter_clean.py
changeset 0 044383f39525
child 3 e1eecf4d390d
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     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 
       
    23 class FilterClean(filter_interface.Filter):
       
    24 	
       
    25 	def open(self, params):
       
    26 		"initialise"
       
    27 		
       
    28 		targets = [x.lower() for x in params.targets]
       
    29 		
       
    30 		self.removeExports = ("cleanexport" in targets or "reallyclean" in targets)
       
    31 		self.removeTargets = ("clean" in targets or "reallyclean" in targets)
       
    32 		
       
    33 		self.ok = True
       
    34 		
       
    35 		# create a temporary file to record all the exports and directories
       
    36 		# in. We can only remove those after "make" has finished running all
       
    37 		# the CLEAN targets.
       
    38 		try:
       
    39 			self.tmp = tempfile.TemporaryFile()
       
    40 		except:
       
    41 			sys.stderr.write("sbs: could not create temporary file for FilterClean\n")
       
    42 			self.ok = False
       
    43 		
       
    44 		return self.ok
       
    45 	
       
    46 	
       
    47 	def write(self, text):
       
    48 		"process some log text"
       
    49 		
       
    50 		for line in text.splitlines():
       
    51 		
       
    52 			if self.removeTargets:
       
    53 				if line.startswith("<file>"):
       
    54 					self.doFile(line)
       
    55 				elif line.startswith("<dir>"):
       
    56 					self.doDirectory(line)
       
    57 						
       
    58 			if self.removeExports:
       
    59 				if line.startswith("<export "):
       
    60 					self.doExport(line)
       
    61 				elif line.startswith("<member>"):
       
    62 					self.doMember(line)
       
    63 				elif line.startswith("<zipmarker>"):
       
    64 					self.doZipMarker(line)
       
    65 				
       
    66 		return self.ok
       
    67 	
       
    68 	
       
    69 	def summary(self):
       
    70 		"finish off"
       
    71 		
       
    72 		# remove files, remembering directories
       
    73 		dirs = set()
       
    74 		
       
    75 		try:
       
    76 			self.tmp.flush()	# write what is left in the buffer
       
    77 			self.tmp.seek(0)	# rewind to the beginning
       
    78 			
       
    79 			for line in self.tmp.readlines():
       
    80 				path = line.strip()
       
    81 				
       
    82 				if os.path.isfile(path):
       
    83 					self.removeFile(path)
       
    84 					
       
    85 				elif os.path.isdir(path):
       
    86 					dirs.add(path)
       
    87 					
       
    88 			self.tmp.close()	# this also deletes the temporary file
       
    89 		except:
       
    90 			sys.stderr.write("sbs: could not access temporary file for FilterClean\n")
       
    91 			self.ok = False
       
    92 		
       
    93 		# finally remove (empty) directories
       
    94 		for dir in dirs:
       
    95 			try:
       
    96 				os.removedirs(dir)	# may fail if the directory has files in
       
    97 			except:
       
    98 				pass				# silently ignore all errors
       
    99 				
       
   100 		return self.ok
       
   101 
       
   102 
       
   103 	def close(self):
       
   104 		"nop"
       
   105 		
       
   106 		return self.ok
       
   107 	
       
   108 	
       
   109 	def removeFile(self, path):
       
   110 		try:
       
   111 			os.unlink(path)
       
   112 		except Exception, e:
       
   113 			sys.stderr.write("sbs: could not remove " + path + "\n")
       
   114 			sys.stderr.write(str(e) + "\n")
       
   115 		
       
   116 				
       
   117 	def saveItem(self, path):
       
   118 		"put path into a temporary file."
       
   119 		try:
       
   120 			self.tmp.write(path + "\n")
       
   121 		except:
       
   122 			sys.stderr.write("sbs: could not write temporary file in FilterClean\n")
       
   123 			self.ok = False
       
   124 	
       
   125 			
       
   126 	def doFile(self, line):
       
   127 		"remove filenames in <file> tags immediately (not .d or .dep)."
       
   128 		filename = line[6:-7]                # line is "<file>filename</file>
       
   129 		filename = filename.strip("\"\'")    # some names are quoted
       
   130 		
       
   131 		# dependency files must be deleted at the end,
       
   132 		# everything else can be deleted straight away.
       
   133 		if filename.endswith(".d") or filename.endswith(".dep"):
       
   134 			self.saveItem(filename)
       
   135 		else:
       
   136 			if os.path.isfile(filename):
       
   137 				self.removeFile(filename)
       
   138 
       
   139 
       
   140 	def doDirectory(self, line):
       
   141 		"save directories in <dir> tags for the end."
       
   142 		# assuming <dir>X</dir>
       
   143 		dirname = line[5:-6]
       
   144 		self.saveItem(dirname.strip("\"\'"))
       
   145 		
       
   146 		
       
   147 	def doExport(self, line):
       
   148 		"save exported files in <export> tags for the end."
       
   149 		# assuming <export destination='X' source='Y' />
       
   150 		filename = line[21:line.find("'", 21)]
       
   151 		self.saveItem(filename)
       
   152 		
       
   153 		
       
   154 	def doMember(self, line):
       
   155 		"save zip exports in <member> tags for the end."
       
   156 		# assuming <member>X</member>
       
   157 		filename = line[8:-9]
       
   158 		self.saveItem(filename)
       
   159 		
       
   160 		
       
   161 	def doZipMarker(self, line):
       
   162 		"Remove file in <zipmarker> tags"
       
   163 		# assuming <zipmarker>X</zipmarker>
       
   164 		filename = line[11:-12]
       
   165 		if os.path.isfile(filename):
       
   166 			self.removeFile(filename)
       
   167 
       
   168 
       
   169 # the end				
       
   170