hgbook

diff es/examples/data/check_whitespace.py @ 379:dbc78b312fc0

finished translation of tour-basic
author Javier Rojas <jerojasro@devnull.li>
date Mon Oct 27 23:29:24 2008 -0500 (2008-10-27)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/es/examples/data/check_whitespace.py	Mon Oct 27 23:29:24 2008 -0500
     1.3 @@ -0,0 +1,44 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +import re
     1.7 +
     1.8 +def trailing_whitespace(difflines):
     1.9 +    added, linenum, header = [], 0, False
    1.10 +
    1.11 +    for line in difflines:
    1.12 +        if header:
    1.13 +            # remember the name of the file that this diff affects
    1.14 +            m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
    1.15 +            if m and m.group(1) != '/dev/null':
    1.16 +                filename = m.group(1).split('/', 1)[-1]
    1.17 +            if line.startswith('+++ '):
    1.18 +                header = False
    1.19 +            continue
    1.20 +        if line.startswith('diff '):
    1.21 +            header = True
    1.22 +            continue
    1.23 +        # hunk header - save the line number
    1.24 +        m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
    1.25 +        if m:
    1.26 +            linenum = int(m.group(1))
    1.27 +            continue
    1.28 +        # hunk body - check for an added line with trailing whitespace
    1.29 +        m = re.match(r'\+.*\s$', line)
    1.30 +        if m:
    1.31 +            added.append((filename, linenum))
    1.32 +        if line and line[0] in ' +':
    1.33 +            linenum += 1
    1.34 +    return added
    1.35 +
    1.36 +if __name__ == '__main__':
    1.37 +    import os, sys
    1.38 +    
    1.39 +    added = trailing_whitespace(os.popen('hg export tip'))
    1.40 +    if added:
    1.41 +        for filename, linenum in added:
    1.42 +            print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
    1.43 +                                  (filename, linenum))
    1.44 +        # save the commit message so we don't need to retype it
    1.45 +        os.system('hg tip --template "{desc}" > .hg/commit.save')
    1.46 +        print >> sys.stderr, 'commit message saved to .hg/commit.save'
    1.47 +        sys.exit(1)