This repository has been archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathheader.py
199 lines (167 loc) · 7.08 KB
/
header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from shutil import copyfile, rmtree
from string import Template
import os.path
import re
import sys
import autogen
class ForwardHeaderGenerator():
def __init__( self, copy, path, includepath, srcpath, project, subprojects, prefix,
prefixed, cleanIncludeDir = True, additionalHeaders = {} ):
self.copy = copy
self.path = path
self.includepath = includepath
self.srcpath = srcpath
self.project = project
self.subprojects = subprojects
self.prefix = prefix
self.prefixed = prefixed
self.cleanIncludeDir = cleanIncludeDir
self.additionalHeaders = additionalHeaders
self.__projectFile = ""
def run( self ):
self.createProject()
def _isValidHeaderFile( self, filename ):
if ( filename.endswith( ".h" ) ):
if filename.startswith( "moc_" ):
return False
if filename.startswith( "ui_" ):
return False
if filename.startswith( "qrc_" ):
return False;
if filename.endswith( "_p.h" ):
return False
return True
else:
return False
def _suggestedHeaderNames( self, project, header ):
regex = re.compile( "(?:class\s+[{0}|{1}][_0-9A-Z]*_EXPORT|MAKEINCLUDES_EXPORT)\s+([a-zA-Z_][A-Za-z0-9_]*)"
.format( project.upper(), self.project.upper() ) )
regex2 = re.compile( "(?:class\s+MAKEINCLUDES_EXPORT)\s+([a-zA-Z_][A-Za-z0-9_]*)" )
regex3 = re.compile( "(?:\\\\file)\s+([a-zA-Z_][A-Za-z0-9_]*)" )
f = open( header, "r" )
classNames = set()
for line in f.readlines():
line = line.strip()
className = None
noPrefix = False
if ( regex.match( line ) ):
className = regex.match( line ).groups()[0]
noPrefix = False
else:
if regex2.match( line ):
className = regex2.match( line ).groups()[0]
noPrefix = False
else:
if ( None != regex3.search( line ) ):
className = regex3.search( line ).groups()[0]
noPrefix = True
if not className:
continue
if self.prefixed and not noPrefix:
className = project + className
classNames.add( className )
f.close()
return classNames
def _addForwardHeader( self, targetPath, fileName, projectFile ):
INCLUDE_STR = "#include \"{0}\""
newHeader = open( targetPath, "wb" )
newHeader.write( INCLUDE_STR.format( fileName ) + os.linesep )
newHeader.close()
basename = os.path.basename( targetPath )
projectFile.write( basename + " \\" + os.linesep )
def _createForwardHeader( self, header, projectFile, project ):
path = os.path.dirname( header )
basename = os.path.basename( header )
classNames = self._suggestedHeaderNames( project, header )
if len( classNames ) > 0:
for classname in classNames:
fHeaderName = os.path.abspath( path + "/" + classname )
self._addForwardHeader( fHeaderName, basename, projectFile )
projectFile.write( basename + " \\" + os.linesep )
elif not basename in self.additionalHeaders.values(): # only create "foo" for "foo.h" if additionalHeaders doesn't overrides it
sanitizedBasename = basename.replace( ".h", "" )
#fHeaderName = os.path.abspath( self.includepath + "/" + sanitizedBasename )
#self._addForwardHeader( fHeaderName, basename, self.__projectFile )
fHeaderNameProjectDir = os.path.dirname( os.path.abspath( header ) ) + "/" + sanitizedBasename;
self._addForwardHeader( fHeaderNameProjectDir, "{0}".format( basename ), projectFile )
projectFile.write( basename + " \\" + os.linesep )
def createProject( self ):
if ( not os.path.exists( self.path ) ):
errStr = Template( "Error, the directory $DIR does not exist!" )
errStr = errStr.substitute( DIR = self.path )
raise BaseException( errStr )
if self.cleanIncludeDir and os.path.exists( self.includepath ):
rmtree( self.includepath )
if not os.path.exists( self.includepath ):
os.mkdir( self.includepath )
if autogen.policyVersion() >= 2:
includeProjectName = os.path.basename( self.includepath.rstrip( "/" ) )
else:
includeProjectName = self.project
profilename = os.path.abspath( self.includepath ) + "/" + includeProjectName + ".pro"
projectFile = open( profilename, "wb" )
self.__projectFile = projectFile
lines = []
lines.append( "TEMPLATE = subdirs" + os.linesep )
lines.append( "SUBDIRS = " )
for subProject in self.subprojects:
line = subProject
if ( subProject != self.subprojects[ -1 ] ):
line += " \\"
lines.append( line + os.linesep )
projectFile.writelines( lines )
projectFile.write( os.linesep )
projectFile.write( "INSTALL_HEADERS.files = " )
for subProject in self.subprojects:
self._createSubproject( subProject )
for fileName, includePath in self.additionalHeaders.items():
targetPath = os.path.join( self.includepath, fileName )
self._addForwardHeader( targetPath , includePath, projectFile )
self._copyHeaders( self.srcpath, self.includepath, projectFile, self.project, self.prefixed )
installPath = "{0}/include".format( self.prefix )
self._projectFile_finalize( projectFile, installPath )
projectFile.close()
def _createSubproject( self, project ):
inclPath = os.path.abspath( self.includepath + "/" + project )
srcPath = os.path.abspath( self.srcpath + "/" + project )
os.mkdir( inclPath )
profilename = os.path.abspath( self.includepath ) + "/" + project + "/" + project + ".pro"
projectFile = open( profilename, "wb" )
projectFile.write( "TEMPLATE = subdirs" + os.linesep )
projectFile.write( "INSTALL_HEADERS.files = " )
self._copyHeaders( srcPath, inclPath, projectFile, project, self.prefixed )
installPath = "{0}/include/{1}".format( self.prefix, project )
self._projectFile_finalize( projectFile, installPath )
projectFile.close()
def _projectFile_finalize( self, projectFile, installPath ):
projectFile.write( os.linesep )
#projectFile.write( "message( $$INSTALL_HEADERS.path )" + os.linesep )
projectFile.write( "INSTALL_HEADERS.path = {0}".format( installPath ) + os.linesep )
#projectFile.write( "message( $$INSTALL_HEADERS.path )" + os.linesep )
projectFile.write( "INSTALLS += INSTALL_HEADERS" + os.linesep )
def _copyHeaders( self, srcDir, destDir, projectFile, project, prefixed = False ):
rootDir = srcDir == self.srcpath
dir = os.listdir( srcDir )
headersForCatchAll = []
for filename in dir:
if ( rootDir ):
if ( filename in self.subprojects ):
continue
file = os.path.abspath( srcDir + "/" + filename )
if os.path.isdir( file ):
self._copyHeaders( file, destDir, projectFile, project, prefixed )
else:
if self._isValidHeaderFile( filename ):
destfile = os.path.abspath( destDir + "/" + filename )
srcfile = os.path.abspath( srcDir + "/" + filename )
copyfile( srcfile, destfile )
self._createForwardHeader( destfile, projectFile, project )
headersForCatchAll.append( filename )
# Create "catch all" convenience header including all headers:
if len( headersForCatchAll ) > 0:
catchAllFileName = os.path.abspath( destDir + "/" + os.path.basename( destDir ) )
catchAllContent = ["#include \"%s\"%s" % (header, os.linesep) for header in headersForCatchAll]
catchAllFile = open( catchAllFileName, "wb" )
catchAllFile.writelines( catchAllContent )
catchAllFile.close()
projectFile.write( os.path.basename( catchAllFileName ) + " \\" + os.linesep )