source: trunk/misc/coding_tools/fixshebangs.py

Last change on this file was 1fc6be2, checked in by Zooko O'Whielacronx <zooko@…>, at 2010-06-07T05:16:18Z

setup: organize misc/ scripts and tools and remove obsolete ones
This is for ticket #1068.

  • Property mode set to 100644
File size: 858 bytes
Line 
1#!/usr/bin/env python
2
3from allmydata.util import fileutil
4
5import re, shutil, sys
6
7R=re.compile("^#! */usr/bin/python *$")
8for 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")
Note: See TracBrowser for help on using the repository browser.