From 24650490ed27bdcfd28b817d1f3567ea08724872 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Tue, 4 Nov 2008 18:01:30 -0700
Subject: [PATCH] misc/fixshebangs.py

---
 misc/fixshebangs.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 misc/fixshebangs.py

diff --git a/misc/fixshebangs.py b/misc/fixshebangs.py
new file mode 100644
index 00000000..02446490
--- /dev/null
+++ b/misc/fixshebangs.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+from allmydata.util import fileutil
+
+import re, shutil, sys
+
+R=re.compile("^#! */usr/bin/python *$")
+for fname in sys.argv[1:]:
+    inf = open(fname, "rU")
+    rntf = fileutil.ReopenableNamedTemporaryFile()
+    outf = open(rntf.name, "w")
+    first = True
+    for l in inf:
+        if first and R.search(l):
+            outf.write("#!/usr/bin/env python\n")
+        else:
+            outf.write(l)
+        first = False
+    outf.close()
+
+    try:
+        shutil.move(rntf.name, fname)
+    except EnvironmentError:
+        # Couldn't atomically overwrite, so just hope that this process doesn't die
+        # and the target file doesn't get recreated in between the following two
+        # operations:
+        shutil.move(fname, fname + ".bak")
+        shutil.move(rntf.name, fname)
+
+        fileutil.remove_if_possible(fname + ".bak")
-- 
2.45.2