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, fobj=None, silent=False): |
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 |
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): |
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 |
235 236 lang = lang_type(filename) 237 assert lang in self.languages 238 239 errors = 0 |
240 for num,line in enumerate(fobj): |
241 if num not in regions: 242 continue 243 line = line.rstrip('\n') 244 if not self.check_line(line, language=lang): |
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]) |
250 errors += 1 |
251 if close: 252 fobj.close() |
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 |
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 |
353 norm_fname = self.normalize_filename(filename) 354 |
355 old = [ l.rstrip('\n') for l in fobj.xreadlines() ] 356 if close: 357 fobj.close() |
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: |
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)) |
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 --- |