Created
July 21, 2014 13:15
-
-
Save euantorano/9cfb8246e3f138910d1d to your computer and use it in GitHub Desktop.
Removes closing PHP tags from files which currently have one withina directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os, sys, re, argparse | |
parser = argparse.ArgumentParser(description='Fix PHP file endings') | |
parser.add_argument('directory', metavar='d', help='The path to the PHP sources') | |
args = parser.parse_args() | |
path = args.directory | |
php_dir = os.listdir(path) | |
regex = re.compile("\?>",re.MULTILINE) | |
for root, dirs, files in os.walk(path): | |
for name in files: | |
file_name = os.path.join(root, name) | |
file_content = "" | |
with open(file_name, "r") as f: | |
file_content = f.read() | |
occurrences = regex.findall(file_content) | |
if len(occurrences) is 1: | |
file_content = file_content.replace("?>", "", 1).rstrip() + "\n" | |
with open(file_name, "w") as f: | |
file_content = f.write(file_content) | |
print("Fixed file: " + file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment