williamr/convert_to_epl.py
author William Roberts <williamr@symbian.org>
Tue, 09 Feb 2010 12:50:02 +0000
changeset 156 753418a2eb15
child 173 048763260c9e
permissions -rw-r--r--
Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
156
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     1
#!/usr/bin/python
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     2
# Copyright (c) 2009 Symbian Foundation.
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     3
# All rights reserved.
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     4
# This component and the accompanying materials are made available
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     6
# which accompanies this distribution, and is available
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     8
#
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
     9
# Initial Contributors:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    10
# Symbian Foundation - Initial contribution
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    11
# 
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    12
# Description:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    13
# Map the SFL license to the EPL license
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    14
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    15
import os
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    16
import os.path
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    17
import re
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    18
import codecs
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    19
from optparse import OptionParser
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    20
import sys
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    21
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    22
oldtext0 = re.compile('terms of the License "Symbian Foundation License v1.0"(to Symbian Foundation)?')
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    23
oldtext1 = re.compile('the URL "http:..www.symbianfoundation.org/legal/sfl-v10.html"')
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    24
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    25
newtext = [
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    26
  'terms of the License "Eclipse Public License v1.0"',
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    27
  'the URL "http://www.eclipse.org/legal/epl-v10.html"'
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    28
]
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    29
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    30
errorfiles = []
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    31
multinoticefiles = []
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    32
shadowroot = 'shadow_epoc32'
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    33
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    34
def file_type(file) :
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    35
	f = open(file, 'r')
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    36
	data = f.read(256)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    37
	f.close()
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    38
	if len(data) < 2:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    39
		return None # too short to be worth bothering about anyway
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    40
	if data[0] == chr(255) and data[1] == chr(254) :
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    41
		return 'utf_16_le'
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    42
	if data.find(chr(0)) >= 0 : 
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    43
		return None	# zero byte implies binary file
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    44
	return 'text'
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    45
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    46
def map_eula(dir, name, encoded) :
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    47
	global oldtext0
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    48
	global newtext1
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    49
	global newtext
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    50
	file = os.path.join(dir, name)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    51
	if encoded == 'text':
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    52
		f = open(file, 'r')
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    53
	else:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    54
		f = codecs.open(file, 'r', encoding=encoded)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    55
	lines = f.readlines()
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    56
	# print ">> %s encoded as %s" % (file, f.encoding)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    57
	f.close()
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    58
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    59
	updated = 0
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    60
	newlines = []
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    61
	while len(lines) > 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    62
		line = lines.pop(0)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    63
		pos1 = oldtext0.search(line)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    64
		if pos1 != None:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    65
			# be careful - oldtext is a prefix of newtext
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    66
			if pos1.group(1) != None:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    67
				# line already converted - nothing to do
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    68
				newlines.append(line)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    69
				continue
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    70
			midlines = []
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    71
			midlinecount = 1
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    72
			while len(lines) > 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    73
				nextline = lines.pop(0)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    74
				if not re.match('^\s$', nextline):
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    75
					# non-blank line
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    76
					if midlinecount == 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    77
						break
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    78
					midlinecount -= 1
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    79
				midlines.append(nextline)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    80
			urlline = nextline
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    81
			pos2 = oldtext1.search(urlline)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    82
			if pos2 != None:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    83
				# found it - assume that there's only one instance
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    84
				newline = oldtext0.sub(newtext[0], line)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    85
				newurl  = oldtext1.sub(newtext[1], urlline)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    86
				newlines.append(newline)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    87
				newlines.extend(midlines)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    88
				newlines.append(newurl)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    89
				updated += 1
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    90
				continue
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    91
			else:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    92
			  if updated != 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    93
			  	lineno = 1 + len(newlines)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    94
			  	print "Problem in " + file + " at " + lineno + ": incorrectly formatted >"
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    95
			  	print line
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    96
			  	print midlines
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    97
			  	print urlline
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    98
			  	global errorfiles
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
    99
			  	errorfiles.append(file)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   100
			  break
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   101
		newlines.append(line)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   102
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   103
	if updated == 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   104
		# print " = no change to " + file
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   105
		return 0
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   106
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   107
	if updated > 1:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   108
	  global multinoticefiles
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   109
	  multinoticefiles.append(file)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   110
	  print '! found %d SFL notices in %s' % (updated, file)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   111
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   112
	# global shadowroot
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   113
	# shadowdir = os.path.join(shadowroot, dir)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   114
	# if not os.path.exists(shadowdir) :
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   115
	# 	os.makedirs(shadowdir)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   116
	# newfile = os.path.join(shadowroot,file)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   117
	# os.rename(file, newfile)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   118
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   119
	global options
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   120
	if not options.dryrun:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   121
		if encoded == 'text':
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   122
			f = open(file, 'w')
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   123
		else:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   124
			f = codecs.open(file, 'w', encoding=encoded)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   125
		f.writelines(newlines)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   126
		f.close()
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   127
	print "* updated %s (encoding %s)" % (file, encoded)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   128
	return 1
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   129
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   130
parser = OptionParser(version="%prog 0.2", usage="Usage: %prog [options]")
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   131
parser.add_option("-n", "--check", action="store_true", dest="dryrun",
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   132
	help="report the files which would be updated, but don't change anything")
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   133
parser.set_defaults(dryrun=False)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   134
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   135
(options, args) = parser.parse_args()
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   136
if len(args) != 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   137
	parser.error("Unexpected commandline arguments")
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   138
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   139
# process tree
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   140
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   141
update_count = 0
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   142
for root, dirs, files in os.walk('.', topdown=True):
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   143
	if '.hg' in dirs:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   144
			dirs.remove('.hg') # don't recurse into the Mercurial repository storage
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   145
	for name in files:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   146
		encoding = file_type(os.path.join(root, name))
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   147
		if encoding:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   148
			update_count += map_eula(root, name, encoding)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   149
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   150
print '%d problem files' % len(errorfiles)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   151
print errorfiles
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   152
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   153
print '%d files with multiple notices' % len(multinoticefiles)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   154
print multinoticefiles
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   155
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   156
if options.dryrun and update_count > 0:
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   157
	print "%d files need updating" % update_count
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   158
	sys.exit(1)
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   159
	
753418a2eb15 Switch convert_to_epl from Perl to Python, adding unicode file support and a "--check" option
William Roberts <williamr@symbian.org>
parents:
diff changeset
   160