]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/coding_tools/fixshebangs.py
Merge pull request #236 from daira/2725.timezone-test.0
[tahoe-lafs/tahoe-lafs.git] / misc / coding_tools / fixshebangs.py
1 #!/usr/bin/env python
2
3 from allmydata.util import fileutil
4
5 import re, shutil, sys
6
7 R=re.compile("^#! */usr/bin/python *$")
8 for fname in sys.argv[1:]:
9     inf = open(fname, "rU")
10     rntf = fileutil.ReopenableNamedTemporaryFile()
11     outf = open(rntf.name, "w")
12     first = True
13     for l in inf:
14         if first and R.search(l):
15             outf.write("#!/usr/bin/env python\n")
16         else:
17             outf.write(l)
18         first = False
19     outf.close()
20
21     try:
22         shutil.move(rntf.name, fname)
23     except EnvironmentError:
24         # Couldn't atomically overwrite, so just hope that this process doesn't die
25         # and the target file doesn't get recreated in between the following two
26         # operations:
27         shutil.move(fname, fname + ".bak")
28         shutil.move(rntf.name, fname)
29
30         fileutil.remove_if_possible(fname + ".bak")