williamr/convert_to_eula.py
changeset 137 b9165bfa078f
equal deleted inserted replaced
136:2010b0f5c1f3 137:b9165bfa078f
       
     1 #!/usr/bin/python
       
     2 # Copyright (c) 2009 Symbian Foundation.
       
     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 # Symbian Foundation - Initial contribution
       
    11 # 
       
    12 # Description:
       
    13 # Map the SFL license to the EULA license, keeping a copy of the original file
       
    14 # in a parallel tree for creation of a "repair" kit to reinstate the SFL
       
    15 
       
    16 import os
       
    17 import os.path
       
    18 import re
       
    19 import codecs
       
    20 
       
    21 oldtext0 = re.compile('terms of the License "Symbian Foundation License v1.0"(to Symbian Foundation)?')
       
    22 oldtext1 = re.compile('the URL "http:..www.symbianfoundation.org/legal/sfl-v10.html"')
       
    23 
       
    24 newtext = [
       
    25   'terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members',
       
    26   'the URL "http://www.symbianfoundation.org/legal/licencesv10.html"'
       
    27 ]
       
    28 
       
    29 errorfiles = []
       
    30 multinoticefiles = []
       
    31 shadowroot = 'shadow_epoc32'
       
    32 
       
    33 def file_type(file) :
       
    34 	f = open(file, 'r')
       
    35 	data = f.read(256)
       
    36 	f.close()
       
    37 	if len(data) < 2:
       
    38 		return None # too short to be worth bothering about anyway
       
    39 	if data[0] == chr(255) and data[1] == chr(254) :
       
    40 		return 'utf_16_le'
       
    41 	if data.find(chr(0)) >= 0 : 
       
    42 		return None	# zero byte implies binary file
       
    43 	return 'text'
       
    44 	
       
    45 def map_eula(dir, name, encoded) :
       
    46 	global oldtext0
       
    47 	global newtext1
       
    48 	global newtext
       
    49 	file = os.path.join(dir, name)
       
    50 	if encoded == 'text':
       
    51 		f = open(file, 'r')
       
    52 	else:
       
    53 		f = codecs.open(file, 'r', encoding=encoded)
       
    54 	lines = f.readlines()
       
    55 	# print ">> %s encoded as %s" % (file, f.encoding)
       
    56 	f.close()
       
    57 	
       
    58 	updated = 0
       
    59 	newlines = []
       
    60 	while len(lines) > 0:
       
    61 		line = lines.pop(0)
       
    62 		pos1 = oldtext0.search(line)
       
    63 		if pos1 != None:
       
    64 			# be careful - oldtext is a prefix of newtext
       
    65 			if pos1.group(1) != None:
       
    66 				# line already converted - nothing to do
       
    67 				newlines.append(line)
       
    68 				continue
       
    69 			midlines = []
       
    70 			midlinecount = 1
       
    71 			while len(lines) > 0:
       
    72 				nextline = lines.pop(0)
       
    73 				if not re.match('^\s$', nextline):
       
    74 					# non-blank line
       
    75 					if midlinecount == 0:
       
    76 						break
       
    77 					midlinecount -= 1
       
    78 				midlines.append(nextline)
       
    79 			urlline = nextline
       
    80 			pos2 = oldtext1.search(urlline)
       
    81 			if pos2 != None:
       
    82 				# found it - assume that there's only one instance
       
    83 				newline = oldtext0.sub(newtext[0], line)
       
    84 				newurl  = oldtext1.sub(newtext[1], urlline)
       
    85 				newlines.append(newline)
       
    86 				newlines.extend(midlines)
       
    87 				newlines.append(newurl)
       
    88 				updated += 1
       
    89 				continue
       
    90 			else:
       
    91 			  if updated != 0:
       
    92 			  	lineno = 1 + len(newlines)
       
    93 			  	print "Problem in " + file + " at " + lineno + ": incorrectly formatted >"
       
    94 			  	print line
       
    95 			  	print midlines
       
    96 			  	print urlline
       
    97 			  	global errorfiles
       
    98 			  	errorfiles.append(file)
       
    99 			  break
       
   100 		newlines.append(line)
       
   101 	
       
   102 	if updated == 0:
       
   103 		print " = no change to " + file
       
   104 		return
       
   105 	
       
   106 	if updated > 1:
       
   107 	  global multinoticefiles
       
   108 	  multinoticefiles.append(file)
       
   109 	  print '! found %d SFL notices in %s' % (updated, file)
       
   110 	
       
   111 	global shadowroot
       
   112 	shadowdir = os.path.join(shadowroot, dir)
       
   113 	if not os.path.exists(shadowdir) :
       
   114 		os.makedirs(shadowdir)
       
   115 	newfile = os.path.join(shadowroot,file)
       
   116 	os.rename(file, newfile)
       
   117 	if encoded == 'text':
       
   118 		f = open(file, 'w')
       
   119 	else:
       
   120 		f = codecs.open(file, 'w', encoding=encoded)
       
   121 	f.writelines(newlines)
       
   122 	f.close()
       
   123 	print "* updated %s (encoding %s)" % (file, f.encoding)
       
   124 
       
   125 # process tree
       
   126 
       
   127 for root, dirs, files in os.walk('epoc32', topdown=True):
       
   128 	if re.compile('epoc32$').match(root) >= 0:
       
   129 		if 'build' in dirs:
       
   130 			dirs.remove('build') # don't recurse into the epoc32/build subtree
       
   131 	for name in files:
       
   132 		encoding = file_type(os.path.join(root, name))
       
   133 		if encoding:
       
   134 			map_eula(root, name, encoding)
       
   135 	
       
   136 print '%d problem files' % len(errorfiles)
       
   137 print errorfiles
       
   138 
       
   139 print '%d files with multiple notices' % len(multinoticefiles)
       
   140 print multinoticefiles
       
   141 
       
   142