blob: affb73fc83a5a9477568fce9eab8281b5718f90e [file] [log] [blame]
Dusan Klinec3880d232014-11-10 19:01:13 +01001#!/usr/bin/env python2
2"""
3Adds ObjectiveC prefixes to the Protocol Buffers entities.
Dusan Klinecee7d8982014-11-10 19:01:48 +01004Used with Protoc objective C compiler: https://github.com/alexeyxo/protobuf-objc
Dusan Klinec3880d232014-11-10 19:01:13 +01005
6@author Ph4r05
7"""
8
9import sys
10import re
11import plyproto.parser
12import plyproto.model as m
13import argparse
14import traceback
15import os.path
16
17class MyVisitor(m.Visitor):
18 content=""
19 offset=0
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +010020 doNameSanitization=False
Dusan Klinec3880d232014-11-10 19:01:13 +010021 statementsChanged=0
22 prefix=""
23
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +010024 reserved = ['auto','else','long','switch','break','enum','register','typedef','case','extern','return',
25 'union','char','float','short','unsigned','const','for','signed','void','continue','goto',
26 'sizeof','volatile','default','if','static','while','do','int','struct','_Packed','double',
27 'protocol','interface','implementation','NSObject','NSInteger','NSNumber','CGFloat','property',
28 'nonatomic', 'retain','strong', 'weak', 'unsafe_unretained', 'readwrite' 'readonly',
29 'hash', 'description', 'id']
30
Dusan Klinec3880d232014-11-10 19:01:13 +010031 def prefixize(self, lu, oldId):
32 '''
33 Simple frefixization - with constant prefix to all identifiers (flat).
34 :param lu:
35 :return:
36 '''
37 return self.replace(lu, self.prefix + str(oldId))
38
39 def replace(self, lu, newCode):
40 '''
41 Replaces given LU string occurrence with the new one. Modifies local state.
42 :param lu:
43 :param newCode:
44 :return:
45 '''
46 if not hasattr(lu, "lexspan"):
47 raise Exception("LU does not implement lexspan, %s" % lu)
48 if lu.lexspan == None:
49 raise Exception("LU has None lexspan, %s" % lu)
50
51 # Computing code positions/lengths.
52 constCodePart=""
53 oldCodeLen=lu.lexspan[1] - (lu.lexspan[0]-len(constCodePart))
54 codeStart=self.offset+lu.lexspan[0]-1-len(constCodePart)
55 codeEnd=self.offset+lu.lexspan[1]-1
56
57 # Change the content, replace with the new version.
58 newCodeLen = len(newCode)
59 newContent = self.content[:codeStart] + newCode + self.content[codeEnd:]
60 self.content = newContent
61 self.offset += newCodeLen - oldCodeLen
62 self.statementsChanged+=1
63
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +010064 def isNameInvalid(self, name):
65 '''
66 Returns true if name conflicts with objectiveC. It cannot be from the list of a reserved words
67 or starts with init or new.
68 :param name:
69 :return:
70 '''
71 return name in self.reserved or name.startswith('init') or name.startswith('new')
72
73 def sanitizeName(self, obj):
74 '''
75 Replaces entity name if it is considered conflicting.
76 :param obj:
77 :return:
78 '''
79 if not self.doNameSanitization:
80 return
81
82 if isinstance(obj, m.Name):
83 n = str(obj.value)
84 if self.isNameInvalid(n):
85 if self.verbose>1:
86 print "!!Invalid name: %s, %s" % (n, obj)
87 self.replace(obj.value, 'x'+n)
88
89 elif isinstance(obj, m.LU):
90 return
91
92 else:
93 return
94
Dusan Klinec3880d232014-11-10 19:01:13 +010095 def __init__(self):
96 super(MyVisitor, self).__init__()
97
98 self.first_field = True
99 self.first_method = True
100
101 def visit_PackageStatement(self, obj):
102 '''Ignore'''
103 return True
104
105 def visit_ImportStatement(self, obj):
106 '''Ignore'''
107 return True
108
109 def visit_OptionStatement(self, obj):
110 '''Ignore'''
111 return True
112
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100113 def visit_LU(self, obj):
114 return True
115
116 def visit_default(self, obj):
117 return True
118
Dusan Klinec3880d232014-11-10 19:01:13 +0100119 def visit_FieldDirective(self, obj):
120 '''Ignore, Field directive, e.g., default value.'''
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100121 n = str(obj.name)
122 if n == 'default':
123 self.sanitizeName(obj.value)
Dusan Klinec3880d232014-11-10 19:01:13 +0100124 return True
125
126 def visit_FieldType(self, obj):
127 '''Field type, if type is name, then it may need refactoring consistent with refactoring rules according to the table'''
128 return True
129
130 def visit_FieldDefinition(self, obj):
131 '''New field defined in a message, check type, if is name, prefixize.'''
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100132 if self.verbose > 4:
133 print "\tField: name=%s, lex=%s parent=%s" % (obj.name, obj.lexspan, obj.parent!=None)
Dusan Klinec3880d232014-11-10 19:01:13 +0100134
135 if isinstance(obj.ftype, m.Name):
136 self.prefixize(obj.ftype, obj.ftype.value)
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100137 self.sanitizeName(obj.ftype)
Dusan Klinec3880d232014-11-10 19:01:13 +0100138
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100139 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100140 return True
141
142 def visit_EnumFieldDefinition(self, obj):
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100143 if self.verbose > 4:
Dusan Klinec3880d232014-11-10 19:01:13 +0100144 print "\tEnumField: name=%s, %s" % (obj.name, obj)
145
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100146 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100147 return True
148
149 def visit_EnumDefinition(self, obj):
150 '''New enum definition, refactor name'''
151 if self.verbose > 3:
152 print "Enum, [%s] body=%s\n\n" % (obj.name, obj.body)
153
154 self.prefixize(obj.name, obj.name.value)
155 return True
156
157 def visit_MessageDefinition(self, obj):
158 '''New message, refactor name, w.r.t. path'''
159 if self.verbose > 3:
160 print "Message, [%s] lex=%s body=|%s|\n" % (obj.name, obj.lexspan, obj.body)
161
162 self.prefixize(obj.name, str(obj.name.value))
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100163 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100164 return True
165
166 def visit_MessageExtension(self, obj):
167 '''New message extension, refactor'''
168 if self.verbose > 3:
169 print "MessageEXT, [%s] body=%s\n\n" % (obj.name, obj.body)
170
171 self.prefixize(obj.name, obj.name.value)
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100172 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100173 return True
174
175 def visit_MethodDefinition(self, obj):
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100176 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100177 return True
178
179 def visit_ServiceDefinition(self, obj):
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100180 self.sanitizeName(obj.name)
Dusan Klinec3880d232014-11-10 19:01:13 +0100181 return True
182
183 def visit_ExtensionsDirective(self, obj):
184 return True
185
186 def visit_Literal(self, obj):
187 return True
188
189 def visit_Name(self, obj):
190 return True
191
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100192 def visit_DotName(self, obj):
193 return True
194
Dusan Klinec3880d232014-11-10 19:01:13 +0100195 def visit_Proto(self, obj):
196 return True
197
198# Main executable code
199if __name__ == '__main__':
200 parser = argparse.ArgumentParser(description='Log statements formating string converter.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
201 parser.add_argument('-i','--in-place', help='Overwrites provided file with the new content', required=False, default=False, dest='inplace')
202 parser.add_argument('-p','--prefix', help='Constant prefix to be prepended to the entities found', required=False, default="", dest='prefix')
203 parser.add_argument('-o','--outdir', help='Output directory', required=False, default="", dest='outdir')
204 parser.add_argument('-e','--echo', help='Writes output to the standard output', required=False, default=False)
205 parser.add_argument('-v','--verbose', help='Writes output to the standard output', required=False, default=0, type=int)
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100206 parser.add_argument('-s','--sanitize', help='If set, performs entity name sanitization - renames conflicting names', required=False, default=0, type=int)
Dusan Klinec3880d232014-11-10 19:01:13 +0100207 parser.add_argument('file')
208 args = parser.parse_args()
209
210 # Load the file and instantiate the visitor object.
211 p = plyproto.parser.ProtobufAnalyzer()
212 if args.verbose>0:
213 print " [-] Processing file: %s" % (args.file)
214
215 # Start the parsing.
216 try:
217 v = MyVisitor()
218 v.offset = 0
219 v.prefix = args.prefix
Dusan Klinecd0fdd3a2014-11-10 21:55:51 +0100220 v.verbose = args.verbose
221 v.doNameSanitization = args.sanitize > 0
Dusan Klinec3880d232014-11-10 19:01:13 +0100222 with open(args.file, 'r') as content_file:
223 v.content = content_file.read()
224
225 tree = p.parse_file(args.file)
226 tree.accept(v)
227
228 # If here, probably no exception occurred.
229 if args.echo:
230 print v.content
231 if args.outdir != None and len(args.outdir)>0 and v.statementsChanged>0:
232 outfile = args.outdir + '/' + v.prefix + os.path.basename(args.file).capitalize()
233 with open(outfile, 'w') as f:
234 f.write(v.content)
235 if args.inplace and v.statementsChanged>0:
236 with open(args.file, 'w') as f:
237 f.write(v.content)
238
239 if args.verbose>0:
240 print " [-] Processing finished, changed=%d" % v.statementsChanged
241 except Exception as e:
242 print " Error occurred! file[%s]" % (args.file), e
243 if args.verbose>1:
244 print '-'*60
245 traceback.print_exc(file=sys.stdout)
246 print '-'*60
247 sys.exit(1)
248