Created
September 6, 2019 15:10
-
-
Save ndevenish/cb9ed83742d3c5102cc1aef9de1fb24a to your computer and use it in GitHub Desktop.
Removes header lines from all python files in the current directory
This file contains hidden or 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
import os | |
from pathlib import Path | |
# Presence of these means we should keep | |
EXCLUSIONS = {"LIBTBX", "FIXME", "DIALS", "pytest"} | |
EXCLUDE_FILES = { | |
"conftest.py", | |
"util/image_viewer/slip_viewer/frame.py", | |
"util/installer.py", | |
} | |
def should_remove_line(filename, line): | |
if any(x in str(filename) for x in EXCLUDE_FILES): | |
return False | |
if not line.strip(): | |
return True | |
if line.startswith("#!") and ( | |
"command_line" in str(filename) | |
or "setup.py" in str(filename) | |
or "show_version.py" in str(filename) | |
): | |
return False | |
if "coding:" in line: | |
return False | |
if line.startswith("#") and any(x in line for x in EXCLUSIONS): | |
return False | |
if line.startswith("#"): | |
return True | |
def filter_lines(filename, lines): | |
lout = [] | |
line_pos = 0 | |
while lines and (not lines[line_pos].strip() or lines[line_pos].startswith("#")): | |
# if "coding" in lines[line_pos]: | |
# breakpoint() | |
if not should_remove_line(filename, lines[line_pos]): | |
if "coding" in lines[line_pos]: | |
lout.append("# coding: utf-8\n") | |
else: | |
lout.append(lines[line_pos]) | |
line_pos += 1 | |
# If a block ended with newline, we might still have contents, so keep newline | |
# Black will remove if was only file | |
if line_pos > 0 and not lines[line_pos - 1].strip(): | |
lout.append(lines[line_pos - 1]) | |
lout.extend(lines[line_pos:]) | |
return lout | |
headers = [] | |
for (path, dirs, files) in os.walk("."): | |
for fname in files: | |
if not fname.endswith(".py"): | |
continue | |
fullpath = Path(path) / fname | |
lines = [] | |
with open(fullpath) as f: | |
lines = f.readlines() | |
lines_out = filter_lines(fullpath, lines) | |
if not lines_out == lines: | |
with open(fullpath, "w") as f: | |
f.write("".join(lines_out)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment