hgbook

diff it/examples/ch09-check_whitespace.py.lst.it @ 797:26b05b7dd445

Minor changes and translation of code snippets for Ch.10.
author Giulio@puck
date Thu Aug 13 18:57:50 2009 +0200 (2009-08-13)
parents
children 7252e7b7f07d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/it/examples/ch09-check_whitespace.py.lst.it	Thu Aug 13 18:57:50 2009 +0200
     1.3 @@ -0,0 +1,49 @@
     1.4 +<!-- BEGIN ch09/check_whitespace.py.lst -->
     1.5 +<programlisting>#!/usr/bin/env python
     1.6 +#
     1.7 +# salvate il file come .hg/controllo_spazio_bianco.py e rendetelo eseguibile
     1.8 +
     1.9 +import re
    1.10 +
    1.11 +def spazio_bianco_in_coda(righe_di_diff):
    1.12 +    # 
    1.13 +    numriga, intestazione = 0, False
    1.14 +
    1.15 +    for riga in righe_di_diff:
    1.16 +        if intestazione:
    1.17 +            # ricorda il nome del file coinvolto in questo diff
    1.18 +            m = re.match(r'(?:---|\+\+\+) ([^\t]+)', riga)
    1.19 +            if m and m.group(1) != '/dev/null':
    1.20 +                nomefile = m.group(1).split('/', 1)[-1]
    1.21 +            if riga.startswith('+++ '):
    1.22 +                intestazione = False
    1.23 +            continue
    1.24 +        if riga.startswith('diff '):
    1.25 +            intestazione = True
    1.26 +            continue
    1.27 +        # intestazione - salva il numero di riga
    1.28 +        m = re.match(r'@@ -\d+,\d+ \+(\d+),', riga)
    1.29 +        if m:
    1.30 +            numriga = int(m.group(1))
    1.31 +            continue
    1.32 +        # corpo - cerca una riga aggiunta con spazio bianco in coda
    1.33 +        m = re.match(r'\+.*\s$', riga)
    1.34 +        if m:
    1.35 +            yield nomefile, numriga
    1.36 +        if riga and riga[0] in ' +':
    1.37 +            numriga += 1
    1.38 +
    1.39 +if __name__ == '__main__':
    1.40 +    import os, sys
    1.41 +    
    1.42 +    aggiunte = 0
    1.43 +    for nomefile, numriga in spazio_bianco_in_coda(os.popen('hg export tip')):
    1.44 +        print &gt;&gt; sys.stderr, ('%s, riga %d: aggiunto spazio bianco in coda' %
    1.45 +                              (nomefile, numriga))
    1.46 +        aggiunte += 1
    1.47 +    if aggiunte:
    1.48 +        # salva il messaggio di commit in modo da non doverlo digitare di nuovo
    1.49 +        os.system('hg tip --template "{desc}" &gt; .hg/commit.save')
    1.50 +        print &gt;&gt; sys.stderr, 'messaggio di commit salvato nel file .hg/commit.save'
    1.51 +        sys.exit(1)</programlisting>
    1.52 +<!-- END ch09/check_whitespace.py.lst -->