williamr/convert_to_epl.py
author MattD <mattd@symbian.org>
Sun, 13 Jun 2010 15:29:20 +0100
branchDBRToolsDev
changeset 283 398d483e91bb
parent 173 048763260c9e
permissions -rw-r--r--
Changed help so that summaries come from components. Added smoketest method to try to reduce regressions (when I write some tests). A few minor cleanups.
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
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
    30
modifiedfiles = []
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
    31
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
    32
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
    33
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
    34
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
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
    36
	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
    37
	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
    38
	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
    39
	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
    40
		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
    41
	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
    42
		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
    43
	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
    44
		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
    45
	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
    46
	
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
    47
def map_epl(dir, name, encoded) :
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
    48
	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
    49
	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
    50
	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
    51
	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
    52
	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
    53
		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
    54
	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
    55
		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
    56
	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
    57
	# 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
    58
	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
    59
	
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
	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
    61
	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
    62
	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
    63
		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
    64
		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
    65
		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
    66
			# 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
    67
			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
    68
				# 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
    69
				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
    70
				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
    71
			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
    72
			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
    73
			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
    74
				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
    75
				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
    76
					# 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
    77
					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
    78
						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
    79
					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
    80
				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
    81
			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
    82
			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
    83
			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
    84
				# 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
    85
				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
    86
				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
    87
				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
    88
				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
    89
				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
    90
				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
    91
				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
    92
			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
    93
			  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
    94
			  	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
    95
			  	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
    96
			  	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
    97
			  	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
    98
			  	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
    99
			  	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
   100
			  	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
   101
			  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
   102
		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
   103
	
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
	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
   105
		# 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
   106
		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
   107
	
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
	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
   109
	  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
   110
	  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
   111
	  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
   112
	
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
	# 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
   114
	# 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
   115
	# 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
   116
	# 	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
   117
	# 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
   118
	# 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
   119
	
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
	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
   121
	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
   122
		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
   123
			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
   124
		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
   125
			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
   126
		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
   127
		f.close()
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   128
		modifiedfiles.append(file)
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
   129
	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
   130
	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
   131
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   132
parser = OptionParser(version="%prog 0.3", usage="Usage: %prog [options]")
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
   133
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
   134
	help="report the files which would be updated, but don't change anything")
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   135
parser.add_option("--nozip", action="store_false", dest="zip",
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   136
	help="disable the attempt to generate a zip of the updated files")
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   137
parser.set_defaults(dryrun=False,zip=True)
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
   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
(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
   140
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
   141
	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
   142
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
# 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
   144
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
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
   146
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
   147
	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
   148
			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
   149
	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
   150
		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
   151
		if encoding:
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   152
			update_count += map_epl(root, name, encoding)
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
   153
	
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 '%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
   155
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
   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
   157
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
   158
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
   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
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
   161
	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
   162
	sys.exit(1)
173
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   163
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   164
if options.zip and len(modifiedfiles):
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   165
	zip = os.popen('zip fixed_files.zip -@', 'w')
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   166
	for file in modifiedfiles:
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   167
		print >> zip, file
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   168
	zip.close()
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   169
	print "%d files written to fixed_files.zip" % len(modifiedfiles)
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   170
048763260c9e v0.3 of comvert_to_epl.py - writes a zip file containing the updated files, unless --nozip is specified
William Roberts <williamr@symbian.org>
parents: 156
diff changeset
   171
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
   172
	
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
   173