style.py (10237:b2850bdcec07) | style.py (10293:62c95c428a3d) |
---|---|
1#! /usr/bin/env python | 1#! /usr/bin/env python |
2# Copyright (c) 2014 ARM Limited 3# All rights reserved 4# 5# The license below extends only to copyright in the software and shall 6# not be construed as granting a license to any other intellectual 7# property including but not limited to intellectual property relating 8# to a hardware implementation of the functionality of the software 9# licensed hereunder. You may use the software subject to the license 10# terms below provided that you ensure that this notice is replicated 11# unmodified and in its entirety in all distributions of the software, 12# modified or unmodified, in source code or in binary form. 13# |
|
2# Copyright (c) 2006 The Regents of The University of Michigan 3# Copyright (c) 2007,2011 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 9# notice, this list of conditions and the following disclaimer; --- 20 unchanged lines hidden (view full) --- 30 31import heapq 32import os 33import re 34import sys 35 36from os.path import dirname, join as joinpath 37from itertools import count | 14# Copyright (c) 2006 The Regents of The University of Michigan 15# Copyright (c) 2007,2011 The Hewlett-Packard Development Company 16# All rights reserved. 17# 18# Redistribution and use in source and binary forms, with or without 19# modification, are permitted provided that the following conditions are 20# met: redistributions of source code must retain the above copyright 21# notice, this list of conditions and the following disclaimer; --- 20 unchanged lines hidden (view full) --- 42 43import heapq 44import os 45import re 46import sys 47 48from os.path import dirname, join as joinpath 49from itertools import count |
38from mercurial import bdiff, mdiff | 50from mercurial import bdiff, mdiff, commands |
39 40current_dir = dirname(__file__) 41sys.path.insert(0, current_dir) 42sys.path.insert(1, joinpath(dirname(current_dir), 'src', 'python')) 43 44from m5.util import neg_inf, pos_inf, Region, Regions 45import sort_includes 46from file_types import lang_type --- 326 unchanged lines hidden (view full) --- 373 if cpp: 374 match = any_control.search(line) 375 if match and not good_control.search(line): 376 stats.badcontrol += 1 377 if verbose > 1: 378 msg(i, line, 'improper spacing after %s' % match.group(1)) 379 bad() 380 | 51 52current_dir = dirname(__file__) 53sys.path.insert(0, current_dir) 54sys.path.insert(1, joinpath(dirname(current_dir), 'src', 'python')) 55 56from m5.util import neg_inf, pos_inf, Region, Regions 57import sort_includes 58from file_types import lang_type --- 326 unchanged lines hidden (view full) --- 385 if cpp: 386 match = any_control.search(line) 387 if match and not good_control.search(line): 388 stats.badcontrol += 1 389 if verbose > 1: 390 msg(i, line, 'improper spacing after %s' % match.group(1)) 391 bad() 392 |
381def do_check_style(hgui, repo, *files, **args): 382 """check files for proper m5 style guidelines""" 383 from mercurial import mdiff, util | |
384 | 393 |
385 auto = args.get('auto', False) 386 if auto: 387 auto = 'f' 388 ui = MercurialUI(hgui, hgui.verbose, auto) | 394def do_check_style(hgui, repo, *pats, **opts): 395 """check files for proper m5 style guidelines |
389 | 396 |
390 if files: 391 files = frozenset(files) | 397 Without an argument, checks all modified and added files for gem5 398 coding style violations. A list of files can be specified to limit 399 the checker to a subset of the repository. The style rules are 400 normally applied on a diff of the repository state (i.e., added 401 files are checked in their entirety while only modifications of 402 modified files are checked). |
392 | 403 |
393 def skip(name): 394 # We never want to handle symlinks, so always skip them: If the location 395 # pointed to is a directory, skip it. If the location is a file inside 396 # the gem5 directory, it will be checked as a file, so symlink can be 397 # skipped. If the location is a file outside gem5, we don't want to 398 # check it anyway. 399 if os.path.islink(name): 400 return True 401 return files and name in files | 404 The --all option can be specified to include clean files and check 405 modified files in their entirety. 406 """ 407 from mercurial import mdiff, util |
402 | 408 |
409 opt_fix_white = opts.get('fix_white', False) 410 opt_all = opts.get('all', False) 411 ui = MercurialUI(hgui, hgui.verbose, opt_fix_white) 412 |
|
403 def prompt(name, func, regions=all_regions): 404 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a') 405 if result == 'a': 406 return True 407 elif result == 'f': 408 func(repo.wjoin(name), regions) 409 410 return False 411 | 413 def prompt(name, func, regions=all_regions): 414 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", 'aif', 'a') 415 if result == 'a': 416 return True 417 elif result == 'f': 418 func(repo.wjoin(name), regions) 419 420 return False 421 |
412 modified, added, removed, deleted, unknown, ignore, clean = repo.status() | |
413 | 422 |
414 whitespace = Whitespace(ui) 415 sorted_includes = SortedIncludes(ui) 416 for fname in added: 417 if skip(fname): 418 continue 419 420 fpath = joinpath(repo.root, fname) 421 422 if whitespace.apply(fpath, prompt): 423 return True 424 425 if sorted_includes.apply(fpath, prompt): 426 return True 427 | 423 # Import the match (repository file name matching helper) 424 # function. Different versions of Mercurial keep it in different 425 # modules and implement them differently. |
428 try: | 426 try: |
429 wctx = repo.workingctx() 430 except: 431 from mercurial import context 432 wctx = context.workingctx(repo) | 427 from mercurial import scmutil 428 m = scmutil.match(repo[None], pats, opts) 429 except ImportError: 430 from mercurial import cmdutil 431 m = cmdutil.match(repo, pats, opts) |
433 | 432 |
434 for fname in modified: 435 if skip(fname): 436 continue | 433 modified, added, removed, deleted, unknown, ignore, clean = \ 434 repo.status(match=m, clean=opt_all) 435 if not opt_all: 436 try: 437 wctx = repo.workingctx() 438 except: 439 from mercurial import context 440 wctx = context.workingctx(repo) |
437 | 441 |
442 files = [ (fn, all_regions) for fn in added ] + \ 443 [ (fn, modregions(wctx, fn)) for fn in modified ] 444 else: 445 files = [ (fn, all_regions) for fn in added + modified + clean ] 446 447 whitespace = Whitespace(ui) 448 sorted_includes = SortedIncludes(ui) 449 for fname, mod_regions in files: |
|
438 fpath = joinpath(repo.root, fname) | 450 fpath = joinpath(repo.root, fname) |
439 regions = modregions(wctx, fname) | |
440 | 451 |
441 if whitespace.apply(fpath, prompt, regions): | 452 if whitespace.apply(fpath, prompt, mod_regions): |
442 return True 443 | 453 return True 454 |
444 if sorted_includes.apply(fpath, prompt, regions): | 455 if sorted_includes.apply(fpath, prompt, mod_regions): |
445 return True 446 447 return False 448 449def do_check_format(hgui, repo, **args): 450 ui = MercurialUI(hgui, hgui.verbose, auto) 451 452 modified, added, removed, deleted, unknown, ignore, clean = repo.status() --- 41 unchanged lines hidden (view full) --- 494 495try: 496 from mercurial.i18n import _ 497except ImportError: 498 def _(arg): 499 return arg 500 501cmdtable = { | 456 return True 457 458 return False 459 460def do_check_format(hgui, repo, **args): 461 ui = MercurialUI(hgui, hgui.verbose, auto) 462 463 modified, added, removed, deleted, unknown, ignore, clean = repo.status() --- 41 unchanged lines hidden (view full) --- 505 506try: 507 from mercurial.i18n import _ 508except ImportError: 509 def _(arg): 510 return arg 511 512cmdtable = { |
502 '^m5style' : 503 ( do_check_style, 504 [ ('a', 'auto', False, _("automatically fix whitespace")) ], 505 _('hg m5style [-a] [FILE]...')), | 513 '^m5style' : ( 514 do_check_style, [ 515 ('w', 'fix-white', False, _("automatically fix whitespace")), 516 ('a', 'all', False, 517 _("include clean files and unmodified parts of modified files")), 518 ] + commands.walkopts, 519 _('hg m5style [-a] [FILE]...')), |
506 '^m5format' : 507 ( do_check_format, 508 [ ], 509 _('hg m5format [FILE]...')), 510} 511 512if __name__ == '__main__': 513 import getopt --- 52 unchanged lines hidden --- | 520 '^m5format' : 521 ( do_check_format, 522 [ ], 523 _('hg m5format [FILE]...')), 524} 525 526if __name__ == '__main__': 527 import getopt --- 52 unchanged lines hidden --- |