style.py (6868:c7eb55c68529) style.py (7807:15553b536bd6)
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

--- 18 unchanged lines hidden (view full) ---

27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Authors: Nathan Binkert
30
31import re
32import os
33import sys
34
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

--- 18 unchanged lines hidden (view full) ---

27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Authors: Nathan Binkert
30
31import re
32import os
33import sys
34
35sys.path.insert(0, os.path.dirname(__file__))
36
37from file_types import lang_type
38
35lead = re.compile(r'^([ \t]+)')
36trail = re.compile(r'([ \t]+)$')
37any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
38good_control = re.compile(r'\b(if|while|for) [(]')
39
39lead = re.compile(r'^([ \t]+)')
40trail = re.compile(r'([ \t]+)$')
41any_control = re.compile(r'\b(if|while|for)[ \t]*[(]')
42good_control = re.compile(r'\b(if|while|for) [(]')
43
40lang_types = { 'c' : "C",
41 'h' : "C",
42 'cc' : "C++",
43 'hh' : "C++",
44 'cxx' : "C++",
45 'hxx' : "C++",
46 'cpp' : "C++",
47 'hpp' : "C++",
48 'C' : "C++",
49 'H' : "C++",
50 'i' : "swig",
51 'py' : "python",
52 's' : "asm",
53 'S' : "asm",
54 'isa' : "isa" }
55def file_type(filename):
56 extension = filename.split('.')
57 extension = len(extension) > 1 and extension[-1]
58 return lang_types.get(extension, None)
44whitespace_types = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
45format_types = set(('C', 'C++'))
59
46
60whitespace_types = ('C', 'C++', 'swig', 'python', 'asm', 'isa')
61def whitespace_file(filename):
62 if file_type(filename) in whitespace_types:
63 return True
64
65 if filename.startswith("SCons"):
66 return True
67
68 return False
69
70format_types = ( 'C', 'C++' )
71def format_file(filename):
72 if file_type(filename) in format_types:
73 return True
74
75 return True
76
77def checkwhite_line(line):
78 match = lead.search(line)
79 if match and match.group(1).find('\t') != -1:
80 return False
81
82 match = trail.search(line)
83 if match:
84 return False
85
86 return True
87
88def checkwhite(filename):
47def checkwhite_line(line):
48 match = lead.search(line)
49 if match and match.group(1).find('\t') != -1:
50 return False
51
52 match = trail.search(line)
53 if match:
54 return False
55
56 return True
57
58def checkwhite(filename):
89 if not whitespace_file(filename):
59 if lang_type(filename) not in whitespace_types:
90 return
91
92 try:
93 f = file(filename, 'r+')
94 except OSError, msg:
95 print 'could not open file %s: %s' % (filename, msg)
96 return
97

--- 13 unchanged lines hidden (view full) ---

111 newline += line[i:]
112 break
113
114 line = newline
115
116 return line.rstrip() + '\n'
117
118def fixwhite(filename, tabsize, fixonly=None):
60 return
61
62 try:
63 f = file(filename, 'r+')
64 except OSError, msg:
65 print 'could not open file %s: %s' % (filename, msg)
66 return
67

--- 13 unchanged lines hidden (view full) ---

81 newline += line[i:]
82 break
83
84 line = newline
85
86 return line.rstrip() + '\n'
87
88def fixwhite(filename, tabsize, fixonly=None):
119 if not whitespace_file(filename):
89 if lang_type(filename) not in whitespace_types:
120 return
121
122 try:
123 f = file(filename, 'r+')
124 except OSError, msg:
125 print 'could not open file %s: %s' % (filename, msg)
126 return
127

--- 41 unchanged lines hidden (view full) ---

169''' % (self.toolong, self.toolong80, self.trailwhite, self.leadtabs,
170 self.badcontrol, self.cret)
171
172 def __nonzero__(self):
173 return self.toolong or self.toolong80 or self.leadtabs or \
174 self.trailwhite or self.badcontrol or self.cret
175
176def validate(filename, stats, verbose, exit_code):
90 return
91
92 try:
93 f = file(filename, 'r+')
94 except OSError, msg:
95 print 'could not open file %s: %s' % (filename, msg)
96 return
97

--- 41 unchanged lines hidden (view full) ---

139''' % (self.toolong, self.toolong80, self.trailwhite, self.leadtabs,
140 self.badcontrol, self.cret)
141
142 def __nonzero__(self):
143 return self.toolong or self.toolong80 or self.leadtabs or \
144 self.trailwhite or self.badcontrol or self.cret
145
146def validate(filename, stats, verbose, exit_code):
177 if not format_file(filename):
147 if lang_type(filename) not in format_types:
178 return
179
180 def msg(lineno, line, message):
181 print '%s:%d>' % (filename, lineno + 1), message
182 if verbose > 2:
183 print line
184
185 def bad():
186 if exit_code is not None:
187 sys.exit(exit_code)
188
148 return
149
150 def msg(lineno, line, message):
151 print '%s:%d>' % (filename, lineno + 1), message
152 if verbose > 2:
153 print line
154
155 def bad():
156 if exit_code is not None:
157 sys.exit(exit_code)
158
189 cpp = filename.endswith('.cc') or filename.endswith('.hh')
190 py = filename.endswith('.py')
191
192 if py + cpp != 1:
193 raise AttributeError, \
194 "I don't know how to deal with the file %s" % filename
195
196 try:
197 f = file(filename, 'r')
198 except OSError:
199 if verbose > 0:
200 print 'could not open file %s' % filename
201 bad()
202 return
203

--- 105 unchanged lines hidden (view full) ---

309 except:
310 from mercurial import context
311 wctx = context.workingctx(repo)
312
313 for fname in modified:
314 if skip(fname):
315 continue
316
159 try:
160 f = file(filename, 'r')
161 except OSError:
162 if verbose > 0:
163 print 'could not open file %s' % filename
164 bad()
165 return
166

--- 105 unchanged lines hidden (view full) ---

272 except:
273 from mercurial import context
274 wctx = context.workingctx(repo)
275
276 for fname in modified:
277 if skip(fname):
278 continue
279
317 if not whitespace_file(fname):
280 if lang_type(fname) not in whitespace_types:
318 continue
319
320 fctx = wctx.filectx(fname)
321 pctx = fctx.parents()
322
323 file_data = fctx.data()
324 lines = mdiff.splitnewlines(file_data)
325 if len(pctx) in (1, 2):

--- 17 unchanged lines hidden (view full) ---

343 if ui.verbose:
344 ui.write(">>%s<<\n" % line[:-1])
345 fixonly.add(i)
346
347 if fixonly:
348 if prompt(fname, fixonly):
349 return True
350
281 continue
282
283 fctx = wctx.filectx(fname)
284 pctx = fctx.parents()
285
286 file_data = fctx.data()
287 lines = mdiff.splitnewlines(file_data)
288 if len(pctx) in (1, 2):

--- 17 unchanged lines hidden (view full) ---

306 if ui.verbose:
307 ui.write(">>%s<<\n" % line[:-1])
308 fixonly.add(i)
309
310 if fixonly:
311 if prompt(fname, fixonly):
312 return True
313
351def check_whitespace(ui, repo, hooktype, node, parent1, parent2, **kwargs):
352 if hooktype != 'pretxncommit':
353 raise AttributeError, \
354 "This hook is only meant for pretxncommit, not %s" % hooktype
355
356 args = { 'tabsize' : 8 }
357 return do_check_whitespace(ui, repo, **args)
358
359def check_format(ui, repo, hooktype, node, parent1, parent2, **kwargs):
360 if hooktype != 'pretxncommit':
361 raise AttributeError, \
362 "This hook is only meant for pretxncommit, not %s" % hooktype
363
314def do_check_format(ui, repo, **args):
364 modified, added, removed, deleted, unknown, ignore, clean = repo.status()
365
366 verbose = 0
367 stats = ValidationStats()
368 for f in modified + added:
369 validate(f, stats, verbose, None)
370
371 if stats:

--- 4 unchanged lines hidden (view full) ---

376 pass
377 elif result.startswith('a'):
378 return True
379 else:
380 raise util.Abort(_("Invalid response: '%s'") % result)
381
382 return False
383
315 modified, added, removed, deleted, unknown, ignore, clean = repo.status()
316
317 verbose = 0
318 stats = ValidationStats()
319 for f in modified + added:
320 validate(f, stats, verbose, None)
321
322 if stats:

--- 4 unchanged lines hidden (view full) ---

327 pass
328 elif result.startswith('a'):
329 return True
330 else:
331 raise util.Abort(_("Invalid response: '%s'") % result)
332
333 return False
334
335def check_hook(hooktype):
336 if hooktype not in ('pretxncommit', 'pre-qrefresh'):
337 raise AttributeError, \
338 "This hook is not meant for %s" % hooktype
339
340def check_whitespace(ui, repo, hooktype, **kwargs):
341 check_hook(hooktype)
342 args = { 'tabsize' : 8 }
343 return do_check_whitespace(ui, repo, **args)
344
345def check_format(ui, repo, hooktype, **kwargs):
346 check_hook(hooktype)
347 args = {}
348 return do_check_format(ui, repo, **args)
349
384try:
385 from mercurial.i18n import _
386except ImportError:
387 def _(arg):
388 return arg
389
390cmdtable = {
391 '^m5style' :
392 ( do_check_whitespace,
393 [ ('a', 'auto', False, _("automatically fix whitespace")),
394 ('t', 'tabsize', 8, _("Number of spaces TAB indents")) ],
350try:
351 from mercurial.i18n import _
352except ImportError:
353 def _(arg):
354 return arg
355
356cmdtable = {
357 '^m5style' :
358 ( do_check_whitespace,
359 [ ('a', 'auto', False, _("automatically fix whitespace")),
360 ('t', 'tabsize', 8, _("Number of spaces TAB indents")) ],
395 _('hg m5check [-t <tabsize>] [FILE]...')),
361 _('hg m5style [-a] [-t <tabsize>] [FILE]...')),
362 '^m5format' :
363 ( do_check_format,
364 [ ],
365 _('hg m5format [FILE]...')),
396}
366}
367
397if __name__ == '__main__':
398 import getopt
399
400 progname = sys.argv[0]
401 if len(sys.argv) < 2:
402 sys.exit('usage: %s <command> [<command args>]' % progname)
403
404 fixwhite_usage = '%s fixwhite [-t <tabsize> ] <path> [...] \n' % progname

--- 47 unchanged lines hidden ---
368if __name__ == '__main__':
369 import getopt
370
371 progname = sys.argv[0]
372 if len(sys.argv) < 2:
373 sys.exit('usage: %s <command> [<command args>]' % progname)
374
375 fixwhite_usage = '%s fixwhite [-t <tabsize> ] <path> [...] \n' % progname

--- 47 unchanged lines hidden ---