1
|
1 |
# #################################################################
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
#
|
|
5 |
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
6 |
#
|
|
7 |
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
8 |
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9 |
# * Neither the name of Nokia Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10 |
#
|
|
11 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
12 |
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
|
|
13 |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
14 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
15 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
|
|
16 |
#
|
|
17 |
# #################################################################
|
|
18 |
|
|
19 |
import os
|
|
20 |
import getopt
|
|
21 |
import string
|
|
22 |
import sys
|
|
23 |
import re
|
|
24 |
|
|
25 |
def usage(code, msg=""):
|
|
26 |
print msg
|
|
27 |
print "Usage: Buildparser.py -h -s <script dir> -m <main file> -o <output dir/file> -v <version text>"
|
|
28 |
print
|
|
29 |
print "options:"
|
|
30 |
print " -h command help"
|
|
31 |
print " -s script dir is the directory containing python subscripts"
|
|
32 |
print " -m main file is the file which contains main() function"
|
|
33 |
print " -o output dir/file is the directory (and file) for output result"
|
|
34 |
print " -v version text indicates within scripts which fractions to copy"
|
|
35 |
print " into resultant script"
|
|
36 |
print " -l localised filename"
|
|
37 |
print
|
|
38 |
sys.exit(code)
|
|
39 |
|
|
40 |
class CFileScanner:
|
|
41 |
|
|
42 |
# Main scan file
|
|
43 |
def startscan(self):
|
|
44 |
# Default output file name is Resultant.py
|
|
45 |
if os.path.isdir(output):
|
|
46 |
resultantfile = open(output+"\Resultant.py", "w")
|
|
47 |
else:
|
|
48 |
resultantfile = open(output, "w")
|
|
49 |
|
|
50 |
# Parse main file from begin to '#!PARSE' tag (copy appropriate lines to resultant file)
|
|
51 |
# and store/parse remaining lines (lines after '#!Parse') into an array
|
|
52 |
# scanfile is in use to scan script files. Here is in use to scan main file! (exception)
|
|
53 |
storelines=self.scanfile(mainfile,resultantfile)
|
|
54 |
# Parse other script files(copy appropriate lines to resultant file)
|
|
55 |
self.traverse(scriptdir,resultantfile)
|
|
56 |
# Flush remaining lines from main file (array) to resultant file
|
|
57 |
self.FlushMainFile(resultantfile,storelines)
|
|
58 |
|
|
59 |
resultantfile.close()
|
|
60 |
# Flush
|
|
61 |
def FlushMainFile(self,resultantfile,storelines):
|
|
62 |
for line in storelines:
|
|
63 |
resultantfile.write(line)
|
|
64 |
|
|
65 |
def InsertFile( self, aResultantfile, aFilename ):
|
|
66 |
# inserts a file without preparsing - for example localised text
|
|
67 |
fileToInsert = open( aFilename, "r" )
|
|
68 |
fileToInsertContent = fileToInsert.readlines()
|
|
69 |
|
|
70 |
for fileToInsertLine in fileToInsertContent:
|
|
71 |
aResultantfile.write( fileToInsertLine )
|
|
72 |
|
|
73 |
fileToInsert.close()
|
|
74 |
|
|
75 |
|
|
76 |
def scanfile(self,fullfilename,resultantfile):
|
|
77 |
storelines=""
|
|
78 |
ParseDelimiter=False
|
|
79 |
# Copy by default (All lines will be copied which are not within tags)
|
|
80 |
CopyIndicator=True
|
|
81 |
sourcefile = open(fullfilename,"r")
|
|
82 |
filelines = sourcefile.readlines()
|
|
83 |
for line in filelines:
|
|
84 |
delimiter=line.find("#!PARSE")
|
|
85 |
# Parse to delimiter (#!PARSE)
|
|
86 |
if (delimiter<>-1):
|
|
87 |
ParseDelimiter=True
|
|
88 |
|
|
89 |
if( -1 <> line.find( "#!LOCALISEHERE" ) ):
|
|
90 |
self.InsertFile( resultantfile, localiseFilename )
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
# Find begining tag
|
|
95 |
delimiter=line.find("#<<")
|
|
96 |
if (delimiter<>-1):
|
|
97 |
# Version (tag) text not found. Don't copy additional lines
|
|
98 |
if (line.find(version,delimiter+3,delimiter+3+len(version))==-1):
|
|
99 |
CopyIndicator=False
|
|
100 |
else:
|
|
101 |
# Found given version, copy lines
|
|
102 |
CopyIndicator=True
|
|
103 |
|
|
104 |
# Find ending tag
|
|
105 |
reMethod = re.compile("#.*>>")
|
|
106 |
delimiter = reMethod.search(line)
|
|
107 |
if (delimiter):
|
|
108 |
# End of previous version block declaration
|
|
109 |
if (not CopyIndicator):
|
|
110 |
# If previous version block was not copied, skip '# >>' (don't write this tag to resultant file)
|
|
111 |
CopyIndicator=True
|
|
112 |
continue
|
|
113 |
else:
|
|
114 |
CopyIndicator=True
|
|
115 |
|
|
116 |
# Haven't reached '#!Parse' delimiter so copy current line to resultant file
|
|
117 |
if (not ParseDelimiter):
|
|
118 |
# Copy indicator is still working
|
|
119 |
if (CopyIndicator):
|
|
120 |
resultantfile.write(line)
|
|
121 |
else:
|
|
122 |
if (CopyIndicator):
|
|
123 |
# Store lines after '#!PARSE'
|
|
124 |
storelines=storelines+line
|
|
125 |
|
|
126 |
resultantfile.write("\n")
|
|
127 |
sourcefile.close()
|
|
128 |
return storelines
|
|
129 |
|
|
130 |
# Searching subdirectories and files
|
|
131 |
def traverse(self, currentdir,resultantfile):
|
|
132 |
contents = os.listdir(currentdir)
|
|
133 |
for entry in contents:
|
|
134 |
fullfilename = os.path.normpath(os.path.join(currentdir, entry))
|
|
135 |
if os.path.isdir(fullfilename):
|
|
136 |
self.traverse(fullfilename,resultantfile)
|
|
137 |
else:
|
|
138 |
# Scan file if it is not the main file and it is python file
|
|
139 |
if (fullfilename.upper().find((mainfile[mainfile.rfind("\\")+1:]).upper())==-1 and fullfilename.upper()[-3:]==".PY"):
|
|
140 |
self.scanfile(fullfilename,resultantfile)
|
|
141 |
|
|
142 |
#
|
|
143 |
# main
|
|
144 |
#
|
|
145 |
opts, args = getopt.getopt(sys.argv[1:], "hs:m:o:v:l:", ["help", "scriptdir=", "mainfile=", "output=","version=","localisefile="])
|
|
146 |
|
|
147 |
storelines=""
|
|
148 |
scriptdir=""
|
|
149 |
mainfile=""
|
|
150 |
output=""
|
|
151 |
version=0
|
|
152 |
localiseFilename=""
|
|
153 |
|
|
154 |
for o, a in opts:
|
|
155 |
if o in ("-h", "--help"):
|
|
156 |
usage(0)
|
|
157 |
if o in ("-s", "--scriptdir"):
|
|
158 |
scriptdir = a
|
|
159 |
if o in ("-m", "--mainfile"):
|
|
160 |
mainfile = a
|
|
161 |
if o in ("-o", "--output"):
|
|
162 |
output = a
|
|
163 |
if o in ("-v", "--version"):
|
|
164 |
version = a
|
|
165 |
if o in ("-l", "--localisefile"):
|
|
166 |
localiseFilename = a
|
|
167 |
|
|
168 |
if (scriptdir=="" or mainfile=="" or output=="" or version==0 or localiseFilename=="" ):
|
|
169 |
usage(1)
|
|
170 |
|
|
171 |
scanner = CFileScanner()
|
|
172 |
scanner.startscan() |