1#! /usr/bin/env python 2# Copyright (c) 2006 The Regents of The University of Michigan 3# Copyright (c) 2007 The Hewlett-Packard Development Company 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: redistributions of source code must retain the above copyright --- 22 unchanged lines hidden (view full) --- 31import re 32import os 33import sys 34 35sys.path.insert(0, os.path.dirname(__file__)) 36 37from file_types import lang_type 38 |
39tabsize = 8 |
40lead = re.compile(r'^([ \t]+)') 41trail = re.compile(r'([ \t]+)$') 42any_control = re.compile(r'\b(if|while|for)[ \t]*[(]') 43good_control = re.compile(r'\b(if|while|for) [(]') 44 45whitespace_types = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons')) 46format_types = set(('C', 'C++')) 47 --- 17 unchanged lines hidden (view full) --- 65 except OSError, msg: 66 print 'could not open file %s: %s' % (filename, msg) 67 return 68 69 for num,line in enumerate(f): 70 if not checkwhite_line(line): 71 yield line,num + 1 72 |
73def fixwhite_line(line): |
74 if lead.search(line): 75 newline = '' 76 for i,c in enumerate(line): 77 if c == ' ': 78 newline += ' ' 79 elif c == '\t': 80 newline += ' ' * (tabsize - len(newline) % tabsize) 81 else: 82 newline += line[i:] 83 break 84 85 line = newline 86 87 return line.rstrip() + '\n' 88 |
89def fixwhite(filename, fixonly=None): |
90 if lang_type(filename) not in whitespace_types: 91 return 92 93 try: 94 f = file(filename, 'r+') 95 except OSError, msg: 96 print 'could not open file %s: %s' % (filename, msg) 97 return --- 122 unchanged lines hidden (view full) --- 220 if i < fbeg: 221 modified.add(i) 222 elif i + 1 >= fend: 223 break 224 elif i > max_lines: 225 break 226 return modified 227 |
228def do_check_style(ui, repo, *files, **args): |
229 """check files for proper m5 style guidelines""" 230 from mercurial import mdiff, util 231 232 if files: 233 files = frozenset(files) 234 235 def skip(name): 236 return files and name in files 237 |
238 def prompt(name, func, fixonly=None): |
239 if args.get('auto', False): 240 result = 'f' 241 else: 242 while True: 243 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", default='a') 244 if result in 'aif': 245 break 246 247 if result == 'a': 248 return True 249 elif result == 'f': |
250 func(repo.wjoin(name), fixonly) |
251 252 return False 253 254 modified, added, removed, deleted, unknown, ignore, clean = repo.status() 255 256 for fname in added: 257 if skip(fname): 258 continue 259 260 ok = True 261 for line,num in checkwhite(repo.wjoin(fname)): 262 ui.write("invalid whitespace in %s:%d\n" % (fname, num)) 263 if ui.verbose: 264 ui.write(">>%s<<\n" % line[-1]) 265 ok = False 266 267 if not ok: |
268 if prompt(fname, fixwhite): |
269 return True 270 271 try: 272 wctx = repo.workingctx() 273 except: 274 from mercurial import context 275 wctx = context.workingctx(repo) 276 --- 56 unchanged lines hidden (view full) --- 333 334 return False 335 336def check_hook(hooktype): 337 if hooktype not in ('pretxncommit', 'pre-qrefresh'): 338 raise AttributeError, \ 339 "This hook is not meant for %s" % hooktype 340 |
341def check_style(ui, repo, hooktype, **kwargs): |
342 check_hook(hooktype) |
343 args = {} 344 return do_check_style(ui, repo, **args) |
345 346def check_format(ui, repo, hooktype, **kwargs): 347 check_hook(hooktype) 348 args = {} 349 return do_check_format(ui, repo, **args) 350 351try: 352 from mercurial.i18n import _ 353except ImportError: 354 def _(arg): 355 return arg 356 357cmdtable = { 358 '^m5style' : |
359 ( do_check_style, 360 [ ('a', 'auto', False, _("automatically fix whitespace")) ], 361 _('hg m5style [-a] [FILE]...')), |
362 '^m5format' : 363 ( do_check_format, 364 [ ], 365 _('hg m5format [FILE]...')), 366} 367 368if __name__ == '__main__': 369 import getopt --- 18 unchanged lines hidden (view full) --- 388 usage = chkformat_usage 389 else: 390 sys.exit(fixwhite_usage + chkwhite_usage + chkformat_usage) 391 392 opts, args = getopt.getopt(sys.argv[2:], flags) 393 394 code = 1 395 verbose = 1 |
396 for opt,arg in opts: 397 if opt == '-n': 398 code = None 399 if opt == '-t': 400 tabsize = int(arg) 401 if opt == '-v': 402 verbose += 1 403 --- 18 unchanged lines hidden --- |