hgbook

view it/examples/ch09-check_whitespace.py.lst.it @ 882:50d09b8f3c4f

Remove /tmp/REV*-hello when clean
author Dongsheng Song <dongsheng.song@gmail.com>
date Wed Oct 21 16:33:28 2009 +0800 (2009-10-21)
parents 26b05b7dd445
children 719b03ea27c8
line source
1 <!-- BEGIN ch09/check_whitespace.py.lst -->
2 <programlisting>#!/usr/bin/env python
3 #
4 # salvate il file come .hg/controllo_spazio_bianco.py e rendetelo eseguibile
6 import re
8 def spazio_bianco_in_coda(righe_di_diff):
9 #
10 numriga, intestazione = 0, False
12 for riga in righe_di_diff:
13 if intestazione:
14 # ricorda il nome del file coinvolto in questo diff
15 m = re.match(r'(?:---|\+\+\+) ([^\t]+)', riga)
16 if m and m.group(1) != '/dev/null':
17 nomefile = m.group(1).split('/', 1)[-1]
18 if riga.startswith('+++ '):
19 intestazione = False
20 continue
21 if riga.startswith('diff '):
22 intestazione = True
23 continue
24 # intestazione - salva il numero di riga
25 m = re.match(r'@@ -\d+,\d+ \+(\d+),', riga)
26 if m:
27 numriga = int(m.group(1))
28 continue
29 # corpo - cerca una riga aggiunta con spazio bianco in coda
30 m = re.match(r'\+.*[ \t]$', riga)
31 if m:
32 yield nomefile, numriga
33 if riga and riga[0] in ' +':
34 numriga += 1
36 if __name__ == '__main__':
37 import os, sys
39 aggiunte = 0
40 for nomefile, numriga in spazio_bianco_in_coda(os.popen('hg export tip')):
41 print &gt;&gt; sys.stderr, ('%s, riga %d: aggiunto spazio bianco in coda' %
42 (nomefile, numriga))
43 aggiunte += 1
44 if aggiunte:
45 # salva il messaggio di commit in modo da non doverlo digitare di nuovo
46 os.system('hg tip --template "{desc}" &gt; .hg/commit.save')
47 print &gt;&gt; sys.stderr, 'messaggio di commit salvato nel file .hg/commit.save'
48 sys.exit(1)</programlisting>
49 <!-- END ch09/check_whitespace.py.lst -->