utils/check-style.py
author Tom Henderson <tomh@tomh.org>
Fri, 20 Aug 2010 12:17:19 -0700
changeset 6589 9c325569fb01
parent 5936 66ca055e81c0
permissions -rwxr-xr-x
Help waf to guess release versions of nsc and pybindgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5936
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
#!/usr/bin/env python
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
import os
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     4
import subprocess
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
import tempfile
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
import sys
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
import filecmp
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
import optparse
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
import shutil
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
import difflib
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
import re
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
def hg_modified_files():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
    files = os.popen ('hg st -nma')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
    return [filename.strip() for filename in files]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
def copy_file(filename):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
    [tmp,pathname] = tempfile.mkstemp()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
    src = open(filename, 'r')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
    dst = open(pathname, 'w')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
    for line in src:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
        dst.write(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
    dst.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
    src.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    25
    return pathname
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
# generate a temporary configuration file
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
def uncrustify_config_file(level):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    29
    level2 = """
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    30
nl_collapse_empty_body=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    31
nl_if_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    32
nl_brace_else=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
nl_elseif_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
nl_else_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
nl_while_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
nl_do_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
nl_for_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
nl_brace_while=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
nl_switch_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    40
nl_after_case=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    41
nl_namespace_brace=Remove
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    42
nl_after_brace_open=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    43
nl_class_leave_one_liners=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    44
nl_enum_leave_one_liners=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    45
nl_func_leave_one_liners=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
nl_if_leave_one_liners=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    47
nl_class_colon=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    48
nl_after_access_spec=1
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    49
nl_after_semicolon=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
pos_class_colon=Lead
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
pos_class_comma=Trail
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
pos_bool=Lead
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    53
nl_class_init_args=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
nl_template_class=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    55
nl_class_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    56
# does not work very well
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
nl_func_type_name=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
nl_func_scope_name=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
nl_func_type_name_class=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
nl_func_proto_type_name=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    61
# function\\n(
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    62
nl_func_paren=Remove
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
nl_fdef_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
nl_struct_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
nl_enum_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    66
nl_union_brace=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
mod_full_brace_do=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
mod_full_brace_for=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
mod_full_brace_if=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
mod_full_brace_while=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
mod_full_brace_for=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
mod_remove_extra_semicolon=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
# max code width
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
#code_width=128
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
#ls_for_split_full=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    76
#ls_func_split_full=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    77
"""
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
    level1 = """
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    79
# extra spaces here and there
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
sp_func_proto_paren=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    81
sp_func_def_paren=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    82
sp_func_call_paren=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    83
sp_brace_typedef=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    84
sp_enum_assign=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    85
sp_before_sparen=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    86
sp_after_semi_for=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    87
sp_arith=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    88
sp_assign=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    89
sp_compare=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    90
sp_cmt_cpp_start=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    91
sp_func_class_paren=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    92
sp_after_type=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    93
sp_type_func=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    94
sp_angle_paren=Add
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
"""
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    96
    level0 = """
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    97
sp_after_semi_for=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    98
sp_before_sparen=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    99
sp_type_func=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   100
sp_after_type=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   101
nl_class_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   102
nl_enum_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   103
nl_func_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   104
nl_assign_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   105
#nl_collapse_empty_body=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   106
nl_getset_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   107
nl_if_leave_one_liners=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   108
nl_fdef_brace=Ignore
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   109
# finally, indentation configuration
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   110
indent_with_tabs=0
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   111
indent_namespace=false
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   112
indent_columns=2
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   113
indent_brace=2
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   114
indent_case_brace=2
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   115
indent_class=true
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   116
indent_class_colon=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   117
# alignment
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   118
indent_align_assign=False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   119
align_left_shift=True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   120
# comment reformating disabled
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   121
cmt_reflow_mode=1 # do not touch comments at all
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   122
cmt_indent_multi=False # really, do not touch them
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   123
"""
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   124
    [tmp,pathname] = tempfile.mkstemp()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   125
    dst = open(pathname, 'w')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   126
    dst.write(level0)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   127
    if level >= 1:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   128
        dst.write(level1)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   129
    if level >= 2:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   130
        dst.write(level2)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   131
    dst.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   132
    return pathname
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   133
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   134
class PatchChunkLine:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   135
    SRC = 1
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   136
    DST = 2
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   137
    BOTH = 3
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   138
    def __init__(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   139
        self.__type = 0
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   140
        self.__line = ''
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   141
    def set_src(self,line):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   142
        self.__type = self.SRC
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   143
        self.__line = line
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   144
    def set_dst(self,line):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   145
        self.__type = self.DST
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   146
        self.__line = line
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   147
    def set_both(self,line):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   148
        self.__type = self.BOTH
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   149
        self.__line = line
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   150
    def append_to_line(self, s):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   151
        self.__line = self.__line + s
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   152
    def line(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   153
        return self.__line
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   154
    def is_src(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   155
        return self.__type == self.SRC or self.__type == self.BOTH
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   156
    def is_dst(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   157
        return self.__type == self.DST or self.__type == self.BOTH
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   158
    def write(self, f):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   159
        if self.__type == self.SRC:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   160
            f.write('-%s\n' % self.__line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   161
        elif self.__type == self.DST:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   162
            f.write('+%s\n' % self.__line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   163
        elif self.__type == self.BOTH:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   164
            f.write(' %s\n' % self.__line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   165
        else:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   166
            raise Exception('invalid patch')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   167
    
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   168
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   169
class PatchChunk:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   170
    def __init__(self, src_pos, dst_pos):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   171
        self.__lines = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   172
        self.__src_pos = int(src_pos)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   173
        self.__dst_pos = int(dst_pos)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   174
    def src_start(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   175
        return self.__src_pos
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   176
    def add_line(self,line):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   177
        self.__lines.append(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   178
    def src(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   179
        src = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   180
        for line in self.__lines:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   181
            if line.is_src():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   182
                src.append(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   183
        return src
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   184
    def dst(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   185
        dst = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   186
        for line in self.__lines:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   187
            if line.is_dst():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   188
                dst.append(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   189
        return dst
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   190
    def src_len(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   191
        return len(self.src())
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   192
    def dst_len(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   193
        return len(self.dst())
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   194
    def write(self,f):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   195
        f.write('@@ -%d,%d +%d,%d @@\n' % (self.__src_pos, self.src_len(),
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   196
                                           self.__dst_pos, self.dst_len()))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   197
        for line in self.__lines:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   198
            line.write(f)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   199
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   200
class Patch:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   201
    def __init__(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   202
        self.__src = ''
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   203
        self.__dst = ''
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   204
        self.__chunks = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   205
    def add_chunk(self, chunk):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   206
        self.__chunks.append(chunk)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   207
    def chunks(self):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   208
        return self.__chunks
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   209
    def set_src(self,src):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   210
        self.__src = src
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   211
    def set_dst(self,dst):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   212
        self.__dst = dst
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   213
    def apply(self,filename):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   214
        # XXX: not implemented
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   215
        return
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   216
    def write(self,f):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   217
        f.write('--- %s\n' % self.__src )
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   218
        f.write('+++ %s\n' % self.__dst )
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   219
        for chunk in self.__chunks:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   220
            chunk.write(f)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   221
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   222
def parse_patchset(generator):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   223
    src_file = re.compile('^--- (.*)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   224
    dst_file = re.compile('^\+\+\+ (.*)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   225
    chunk_start = re.compile('^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   226
    src = re.compile('^-(.*)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   227
    dst = re.compile('^\+(.*)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   228
    both = re.compile('^ (.*)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   229
    patchset = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   230
    current_patch = None
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   231
    for line in generator:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   232
        m = src_file.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   233
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   234
            current_patch = Patch()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   235
            patchset.append(current_patch)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   236
            current_patch.set_src(m.group(1))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   237
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   238
        m = dst_file.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   239
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   240
            current_patch.set_dst(m.group(1))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   241
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   242
        m = chunk_start.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   243
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   244
            current_chunk = PatchChunk(m.group(1), m.group(3))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   245
            current_patch.add_chunk(current_chunk)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   246
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   247
        m = src.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   248
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   249
            l = PatchChunkLine()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   250
            l.set_src(m.group(1))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   251
            current_chunk.add_line(l)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   252
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   253
        m = dst.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   254
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   255
            l = PatchChunkLine()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   256
            l.set_dst(m.group(1))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   257
            current_chunk.add_line(l)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   258
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   259
        m = both.search(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   260
        if m is not None:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   261
            l = PatchChunkLine()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   262
            l.set_both(m.group(1))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   263
            current_chunk.add_line(l)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   264
            continue
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   265
        raise Exception()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   266
    return patchset
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   267
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   268
def remove_trailing_whitespace_changes(patch_generator):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   269
    whitespace = re.compile('^(.*)([ \t]+)$')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   270
    patchset = parse_patchset(patch_generator)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   271
    for patch in patchset:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   272
        for chunk in patch.chunks():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   273
            src = chunk.src()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   274
            dst = chunk.dst()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   275
            try:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   276
                for i in range(0,len(src)):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   277
                    s = src[i]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   278
                    d = dst[i]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   279
                    m = whitespace.search(s.line())
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   280
                    if m is not None and m.group(1) == d.line():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   281
                        d.append_to_line(m.group(2))
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   282
            except:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   283
                return patchset
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   284
    return patchset
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   285
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   286
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   287
def indent(source, debug, level):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   288
    output = tempfile.mkstemp()[1]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   289
    # apply uncrustify
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   290
    cfg = uncrustify_config_file(level)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   291
    if debug:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   292
        sys.stderr.write('original file=' + source + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   293
        sys.stderr.write('uncrustify config file=' + cfg + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   294
        sys.stderr.write('temporary file=' + output + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   295
    try:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   296
        uncrust = subprocess.Popen(['uncrustify', '-c', cfg, '-f', source, '-o', output],
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   297
                                   stdin = subprocess.PIPE,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   298
                                   stdout = subprocess.PIPE,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   299
                                   stderr = subprocess.PIPE)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   300
        (out, err) = uncrust.communicate('')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   301
        if debug:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   302
            sys.stderr.write(out)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   303
            sys.stderr.write(err)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   304
    except OSError:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   305
        raise Exception ('uncrustify not installed')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   306
    # generate a diff file
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   307
    src = open(source, 'r')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   308
    dst = open(output, 'r')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   309
    diff = difflib.unified_diff(src.readlines(), dst.readlines(), 
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   310
                                fromfile=source, tofile=output)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   311
    src.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   312
    dst.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   313
    if debug:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   314
        initial_diff = tempfile.mkstemp()[1]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   315
        sys.stderr.write('initial diff file=' + initial_diff + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   316
        tmp = open(initial_diff, 'w')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   317
        tmp.writelines(diff)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   318
        tmp.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   319
    final_diff = tempfile.mkstemp()[1]
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   320
    if level < 3:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   321
        patchset = remove_trailing_whitespace_changes(diff);
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   322
        dst = open(final_diff, 'w')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   323
        if len(patchset) != 0:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   324
            patchset[0].write(dst)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   325
        dst.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   326
    else:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   327
        dst = open(final_diff, 'w')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   328
        dst.writelines(diff)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   329
        dst.close()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   330
            
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   331
            
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   332
    # apply diff file
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   333
    if debug:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   334
        sys.stderr.write('final diff file=' + final_diff + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   335
    shutil.copyfile(source,output)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   336
    patch = subprocess.Popen(['patch', '-p1', '-i', final_diff, output],
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   337
                             stdin = subprocess.PIPE,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   338
                             stdout = subprocess.PIPE,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   339
                             stderr = subprocess.PIPE)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   340
    (out, err) = patch.communicate('')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   341
    if debug:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   342
        sys.stderr.write(out)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   343
        sys.stderr.write(err)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   344
    return output
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   345
 
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   346
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   347
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   348
def indent_files(files, diff=False, debug=False, level=0, inplace=False):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   349
    output = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   350
    for f in files:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   351
        dst = indent(f, debug=debug, level=level)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   352
        output.append([f,dst])
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   353
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   354
    # First, copy to inplace
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   355
    if inplace:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   356
        for src,dst in output:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   357
            shutil.copyfile(dst,src)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   358
        return True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   359
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   360
    # now, compare
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   361
    failed = []
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   362
    for src,dst in output:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   363
        if filecmp.cmp(src,dst) == 0:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   364
            failed.append([src, dst])
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   365
    if len(failed) > 0:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   366
        if not diff:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   367
            print 'Found %u badly indented files:' % len(failed)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   368
            for src,dst in failed:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   369
                print '  ' + src
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   370
        else:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   371
            for src,dst in failed:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   372
                s = open(src, 'r').readlines()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   373
                d = open(dst, 'r').readlines()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   374
                for line in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   375
                    sys.stdout.write(line)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   376
        return False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   377
    return True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   378
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   379
def run_as_hg_hook(ui, repo, **kwargs):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   380
    # hack to work around mercurial < 1.3 bug
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   381
    from mercurial import lock, error
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   382
    lock.LockError = error.LockError
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   383
    # actually do the work
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   384
    files = hg_modified_files()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   385
    if not indent_files(files, inplace=False):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   386
        return True
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   387
    return False
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   388
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   389
def run_as_main():
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   390
    parser = optparse.OptionParser()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   391
    parser.add_option('--debug', action='store_true', dest='debug', default=False,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   392
                      help='Output some debugging information')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   393
    parser.add_option('-l', '--level', type='int', dest='level', default=0,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   394
                      help="Level of style conformance: higher levels include all lower levels. "
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   395
                      "level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and "
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   396
                      "extra braces around single-line statements. level=3: remove all trailing spaces")
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   397
    parser.add_option('--check-hg-hook', action='store_true', dest='hg_hook', default=False, 
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   398
                      help='Get the list of files to check from mercurial\'s list of modified '
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   399
                      'and added files and assume that the script runs as a pretxncommit mercurial hook')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   400
    parser.add_option('--check-hg', action='store_true', dest='hg', default=False,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   401
                      help="Get the list of files to check from mercurial\'s list of modified and added files")
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   402
    parser.add_option('-f', '--check-file', action='store', dest='file', default='',
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   403
                      help="Check a single file")
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   404
    parser.add_option('--diff', action='store_true', dest='diff', default=False,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   405
                      help="Generate a diff on stdout of the indented files")
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   406
    parser.add_option('-i', '--in-place', action='store_true', dest='in_place', default=False,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   407
                      help="Indent the input files in-place")
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   408
    (options,args) = parser.parse_args()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   409
    debug = options.debug
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   410
    if options.hg_hook:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   411
        files = hg_modified_files()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   412
        if not indent_files(files, debug=options.debug,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   413
                            level=options.level,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   414
                            inplace=False):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   415
            sys.exit(1)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   416
    elif options.hg:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   417
        files = hg_modified_files()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   418
        indent_files(files, diff=options.diff, 
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   419
                     debug=options.debug,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   420
                     level=options.level,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   421
                     inplace=options.in_place)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   422
    elif options.file != '':
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   423
        file = options.file
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   424
        if not os.path.exists(file) or \
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   425
                not os.path.isfile(file):
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   426
            print 'file %s does not exist' % file
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   427
            sys.exit(1)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   428
        indent_files([file], diff=options.diff, 
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   429
                     debug=options.debug,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   430
                     level=options.level,
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   431
                     inplace=options.in_place)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   432
    sys.exit(0)
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   433
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   434
if __name__ == '__main__':
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   435
#    try:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   436
        run_as_main()
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   437
#    except Exception, e:
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   438
#        sys.stderr.write(str(e) + '\n')
66ca055e81c0 enforce that style conforms to our coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   439
#        sys.exit(1)