LEFT | RIGHT |
(no file at all) | |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os | 3 import os |
4 import subprocess | 4 import subprocess |
5 import tempfile | 5 import tempfile |
6 import sys | 6 import sys |
7 import filecmp | 7 import filecmp |
8 import optparse | 8 import optparse |
9 import shutil | 9 import shutil |
10 import difflib | 10 import difflib |
11 import re | 11 import re |
| 12 import fnmatch |
| 13 |
| 14 def directory_files(path): |
| 15 files = fnmatch.filter(os.listdir(path), '*.cc') + fnmatch.filter(os.listdir
(path), '*.h') |
| 16 |
| 17 return [path + '/' + filename for filename in files] |
12 | 18 |
13 def hg_modified_files(): | 19 def hg_modified_files(): |
14 files = os.popen ('hg st -nma') | 20 files = os.popen ('hg st -nma') |
15 return [filename.strip() for filename in files] | 21 return [filename.strip() for filename in files] |
16 | 22 |
17 def copy_file(filename): | 23 def copy_file(filename): |
18 [tmp,pathname] = tempfile.mkstemp() | 24 [tmp,pathname] = tempfile.mkstemp() |
19 src = open(filename, 'r') | 25 src = open(filename, 'r') |
20 dst = open(pathname, 'w') | 26 dst = open(pathname, 'w') |
21 for line in src: | 27 for line in src: |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 help="Level of style conformance: higher levels include al
l lower levels. " | 537 help="Level of style conformance: higher levels include al
l lower levels. " |
532 "level=0: re-indent only. level=1: add extra spaces. level
=2: insert extra newlines and " | 538 "level=0: re-indent only. level=1: add extra spaces. level
=2: insert extra newlines and " |
533 "extra braces around single-line statements. level=3: remo
ve all trailing spaces") | 539 "extra braces around single-line statements. level=3: remo
ve all trailing spaces") |
534 parser.add_option('--check-hg-hook', action='store_true', dest='hg_hook', de
fault=False,· | 540 parser.add_option('--check-hg-hook', action='store_true', dest='hg_hook', de
fault=False,· |
535 help='Get the list of files to check from mercurial\'s lis
t of modified ' | 541 help='Get the list of files to check from mercurial\'s lis
t of modified ' |
536 'and added files and assume that the script runs as a pret
xncommit mercurial hook') | 542 'and added files and assume that the script runs as a pret
xncommit mercurial hook') |
537 parser.add_option('--check-hg', action='store_true', dest='hg', default=Fals
e, | 543 parser.add_option('--check-hg', action='store_true', dest='hg', default=Fals
e, |
538 help="Get the list of files to check from mercurial\'s lis
t of modified and added files") | 544 help="Get the list of files to check from mercurial\'s lis
t of modified and added files") |
539 parser.add_option('-f', '--check-file', action='store', dest='file', default
='', | 545 parser.add_option('-f', '--check-file', action='store', dest='file', default
='', |
540 help="Check a single file") | 546 help="Check a single file") |
| 547 parser.add_option('-F', '--check-dir', action='store', dest='dir', default='
', |
| 548 help="Check all files in a directory") |
541 parser.add_option('--diff', action='store_true', dest='diff', default=False, | 549 parser.add_option('--diff', action='store_true', dest='diff', default=False, |
542 help="Generate a diff on stdout of the indented files") | 550 help="Generate a diff on stdout of the indented files") |
543 parser.add_option('-i', '--in-place', action='store_true', dest='in_place',
default=False, | 551 parser.add_option('-i', '--in-place', action='store_true', dest='in_place',
default=False, |
544 help="Indent the input files in-place") | 552 help="Indent the input files in-place") |
545 (options,args) = parser.parse_args() | 553 (options,args) = parser.parse_args() |
546 debug = options.debug | 554 debug = options.debug |
547 if options.hg_hook: | 555 if options.hg_hook: |
548 files = hg_modified_files() | 556 files = hg_modified_files() |
549 if not indent_files(files, debug=options.debug, | 557 if not indent_files(files, debug=options.debug, |
550 level=options.level, | 558 level=options.level, |
551 inplace=False): | 559 inplace=False): |
552 sys.exit(1) | 560 sys.exit(1) |
553 elif options.hg: | 561 elif options.hg: |
554 files = hg_modified_files() | 562 files = hg_modified_files() |
555 indent_files(files, diff=options.diff,· | 563 indent_files(files, diff=options.diff,· |
556 debug=options.debug, | 564 debug=options.debug, |
557 level=options.level, | 565 level=options.level, |
558 inplace=options.in_place) | 566 inplace=options.in_place) |
559 elif options.file != '': | 567 elif options.file != '': |
560 file = options.file | 568 file = options.file |
561 if not os.path.exists(file) or \ | 569 if not os.path.exists(file) or \ |
562 not os.path.isfile(file): | 570 not os.path.isfile(file): |
563 print('file %s does not exist' % file) | 571 print('file %s does not exist' % file) |
564 sys.exit(1) | 572 sys.exit(1) |
565 indent_files([file], diff=options.diff,· | 573 indent_files([file], diff=options.diff,· |
566 debug=options.debug, | 574 debug=options.debug, |
567 level=options.level, | 575 level=options.level, |
568 inplace=options.in_place) | 576 inplace=options.in_place) |
| 577 elif options.dir != '': |
| 578 path = options.dir |
| 579 if not os.path.exists(path): |
| 580 print('directory %s does not exist' % path) |
| 581 sys.exit(1) |
| 582 files = directory_files(path) |
| 583 indent_files(files, diff=options.diff,· |
| 584 debug=options.debug, |
| 585 level=options.level, |
| 586 inplace=options.in_place) |
569 sys.exit(0) | 587 sys.exit(0) |
570 | 588 |
571 if __name__ == '__main__': | 589 if __name__ == '__main__': |
572 # try: | 590 # try: |
573 run_as_main() | 591 run_as_main() |
574 # except Exception, e: | 592 # except Exception, e: |
575 # sys.stderr.write(str(e) + '\n') | 593 # sys.stderr.write(str(e) + '\n') |
576 # sys.exit(1) | 594 # sys.exit(1) |
LEFT | RIGHT |