verifiers.py (11592:92509f1b24f7) | verifiers.py (11716:95a34c2188f2) |
---|---|
1#!/usr/bin/env python 2# 3# Copyright (c) 2014, 2016 ARM Limited 4# All rights reserved 5# 6# The license below extends only to copyright in the software and shall 7# not be construed as granting a license to any other intellectual 8# property including but not limited to intellectual property relating --- 178 unchanged lines hidden (view full) --- 187 if result == 'f': 188 self.fix(filename, regions) 189 elif result == 'a': 190 return True # abort 191 192 return False 193 194 @abstractmethod | 1#!/usr/bin/env python 2# 3# Copyright (c) 2014, 2016 ARM Limited 4# All rights reserved 5# 6# The license below extends only to copyright in the software and shall 7# not be construed as granting a license to any other intellectual 8# property including but not limited to intellectual property relating --- 178 unchanged lines hidden (view full) --- 187 if result == 'f': 188 self.fix(filename, regions) 189 elif result == 'a': 190 return True # abort 191 192 return False 193 194 @abstractmethod |
195 def check(self, filename, regions=all_regions): | 195 def check(self, filename, regions=all_regions, fobj=None, silent=False): |
196 """Check specified regions of file 'filename'. 197 | 196 """Check specified regions of file 'filename'. 197 |
198 Given that it is possible that the current contents of the file 199 differ from the file as 'staged to commit', for those cases, and 200 maybe others, the argument fobj should be a file object open and reset 201 with the contents matching what the file would look like after the 202 commit. This is needed keep the messages using 'filename' meaningful. 203 204 The argument silent is useful to prevent output when we run check in 205 the staged file vs the actual file to detect if the user forgot 206 staging fixes to the commit. This way, we prevent reporting errors 207 twice in stderr. 208 |
|
198 Line-by-line checks can simply provide a check_line() method 199 that returns True if the line is OK and False if it has an 200 error. Verifiers that need a multi-line view (like 201 SortedIncludes) must override this entire function. 202 203 Returns a count of errors (0 if none), though actual non-zero 204 count value is not currently used anywhere. 205 """ --- 5 unchanged lines hidden (view full) --- 211 212 Line-by-line fixes can simply provide a fix_line() method that 213 returns the fixed line. Verifiers that need a multi-line view 214 (like SortedIncludes) must override this entire function. 215 """ 216 pass 217 218class LineVerifier(Verifier): | 209 Line-by-line checks can simply provide a check_line() method 210 that returns True if the line is OK and False if it has an 211 error. Verifiers that need a multi-line view (like 212 SortedIncludes) must override this entire function. 213 214 Returns a count of errors (0 if none), though actual non-zero 215 count value is not currently used anywhere. 216 """ --- 5 unchanged lines hidden (view full) --- 222 223 Line-by-line fixes can simply provide a fix_line() method that 224 returns the fixed line. Verifiers that need a multi-line view 225 (like SortedIncludes) must override this entire function. 226 """ 227 pass 228 229class LineVerifier(Verifier): |
219 def check(self, filename, regions=all_regions): 220 f = self.open(filename, 'r') | 230 def check(self, filename, regions=all_regions, fobj=None, silent=False): 231 close = False 232 if fobj is None: 233 fobj = self.open(filename, 'r') 234 close = True |
221 222 lang = lang_type(filename) 223 assert lang in self.languages 224 225 errors = 0 | 235 236 lang = lang_type(filename) 237 assert lang in self.languages 238 239 errors = 0 |
226 for num,line in enumerate(f): | 240 for num,line in enumerate(fobj): |
227 if num not in regions: 228 continue 229 line = line.rstrip('\n') 230 if not self.check_line(line, language=lang): | 241 if num not in regions: 242 continue 243 line = line.rstrip('\n') 244 if not self.check_line(line, language=lang): |
231 self.ui.write("invalid %s in %s:%d\n" % \ 232 (self.test_name, filename, num + 1)) 233 if self.ui.verbose: 234 self.ui.write(">>%s<<\n" % line[:-1]) | 245 if not silent: 246 self.ui.write("invalid %s in %s:%d\n" % \ 247 (self.test_name, filename, num + 1)) 248 if self.ui.verbose: 249 self.ui.write(">>%s<<\n" % line[:-1]) |
235 errors += 1 | 250 errors += 1 |
236 f.close() | 251 if close: 252 fobj.close() |
237 return errors 238 239 @safefix 240 def fix(self, filename, regions=all_regions): 241 f = self.open(filename, 'r+') 242 243 lang = lang_type(filename) 244 assert lang in self.languages --- 79 unchanged lines hidden (view full) --- 324 languages = sort_includes.default_languages 325 test_name = 'include file order' 326 opt_name = 'include' 327 328 def __init__(self, *args, **kwargs): 329 super(SortedIncludes, self).__init__(*args, **kwargs) 330 self.sort_includes = sort_includes.SortIncludes() 331 | 253 return errors 254 255 @safefix 256 def fix(self, filename, regions=all_regions): 257 f = self.open(filename, 'r+') 258 259 lang = lang_type(filename) 260 assert lang in self.languages --- 79 unchanged lines hidden (view full) --- 340 languages = sort_includes.default_languages 341 test_name = 'include file order' 342 opt_name = 'include' 343 344 def __init__(self, *args, **kwargs): 345 super(SortedIncludes, self).__init__(*args, **kwargs) 346 self.sort_includes = sort_includes.SortIncludes() 347 |
332 def check(self, filename, regions=all_regions): 333 f = self.open(filename, 'r') | 348 def check(self, filename, regions=all_regions, fobj=None, silent=False): 349 close = False 350 if fobj is None: 351 fobj = self.open(filename, 'r') 352 close = True |
334 norm_fname = self.normalize_filename(filename) 335 | 353 norm_fname = self.normalize_filename(filename) 354 |
336 old = [ l.rstrip('\n') for l in f.xreadlines() ] 337 f.close() | 355 old = [ l.rstrip('\n') for l in fobj.xreadlines() ] 356 if close: 357 fobj.close() |
338 339 if len(old) == 0: 340 return 0 341 342 language = lang_type(filename, old[0]) 343 new = list(self.sort_includes(old, norm_fname, language)) 344 345 modified = _modified_regions(old, new) & regions 346 347 if modified: | 358 359 if len(old) == 0: 360 return 0 361 362 language = lang_type(filename, old[0]) 363 new = list(self.sort_includes(old, norm_fname, language)) 364 365 modified = _modified_regions(old, new) & regions 366 367 if modified: |
348 self.ui.write("invalid sorting of includes in %s\n" % (filename)) 349 if self.ui.verbose: 350 for start, end in modified.regions: 351 self.ui.write("bad region [%d, %d)\n" % (start, end)) | 368 if not silent: 369 self.ui.write("invalid sorting of includes in %s\n" 370 % (filename)) 371 if self.ui.verbose: 372 for start, end in modified.regions: 373 self.ui.write("bad region [%d, %d)\n" % (start, end)) |
352 return 1 353 354 return 0 355 356 @safefix 357 def fix(self, filename, regions=all_regions): 358 f = self.open(filename, 'r+') 359 --- 90 unchanged lines hidden --- | 374 return 1 375 376 return 0 377 378 @safefix 379 def fix(self, filename, regions=all_regions): 380 f = self.open(filename, 'r+') 381 --- 90 unchanged lines hidden --- |