Last active
April 28, 2016 02:35
-
-
Save douglashill/fccff0fd68205da11074f9057b944566 to your computer and use it in GitHub Desktop.
Make source code files in a directory all begin with consistent preamble
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
# coding: utf-8 | |
import os | |
import sys | |
# Make source code files in a directory all begin with consistent preamble, which is: | |
# // | |
# // <filename> | |
# | |
# This code is very rough and could be better in lots of ways. | |
# There was a problem with the ends of files not being written correctly. | |
# Make sure the target is in version control so unwanted changes are easy to revert. | |
def main(): | |
root_directory = "<put path to root directory here>" | |
ignore = "Vendor" | |
ignore_file = "InfoPlist.h" | |
extensions = [".m", ".mm", ".c", ".h", ".cpp", ".cc"] | |
good_count = 0 | |
bad_count = 0 | |
other_type_count = 0 | |
short_count = 0 | |
total = 0 | |
for directory, subdirectories, files in os.walk(root_directory, topdown=False): | |
if ignore in directory: | |
continue | |
for name in files: | |
total += 1 | |
if os.path.splitext(name)[1] not in extensions or name == ignore_file: | |
other_type_count += 1 | |
continue | |
with open(os.path.join(directory, name), "r+") as f: | |
lines = f.readlines() | |
if len(lines) > 1: | |
wanted_first_line = "//\n" | |
wanted_second_line = "// " + name + "\n" | |
if lines[0] != wanted_first_line or lines[1] != wanted_second_line: | |
bad_count += 1 | |
print os.path.join(directory, name) | |
lines[0] = wanted_first_line | |
lines[1] = wanted_second_line | |
f.seek(0) | |
f.write("".join(lines)) | |
else: | |
good_count += 1 | |
else: | |
short_count += 1 | |
print "total: " + str(total) + "\nOther: " + str(other_type_count) + "\nShort: " + str(short_count) + "\nGood: " + str(good_count) + "\nbad: " + str(bad_count) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment