Last active
July 23, 2018 12:21
-
-
Save allhailwesttexas/b437602acb99236a5a131d4d40f4afd7 to your computer and use it in GitHub Desktop.
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/env python | |
import subprocess | |
from datetime import datetime | |
from dateutil import parser | |
import os.path as op | |
import pytz | |
FORMATTER_ADOPTION_DATE = pytz.UTC.localize(datetime(2018, 7, 1)) | |
EXECUTABLES = ( | |
("black", [],), | |
) | |
def git(args): | |
args = ["git"] + args | |
git_cmd = subprocess.Popen(args, stdout=subprocess.PIPE) | |
details = git_cmd.stdout.read() | |
details = details.decode("utf-8").strip() | |
return details | |
def is_python(filename): | |
return op.splitext(filename)[1] == ".py" | |
def get_modified_files(): | |
files = git(["diff-index", "-z", "--cached", "HEAD", "--name-only"]) | |
for f in files.rstrip("\0").split("\0"): | |
yield f | |
def get_creation_date_for(filename): | |
datestr = git( | |
["log", "--diff-filter=A", "--follow", "--format=%aD", "-1", "--", filename] | |
) | |
return parser.parse(datestr).replace(tzinfo=pytz.UTC) | |
def process(f): | |
for binary, args in EXECUTABLES: | |
subprocess.call([binary, f, *args]) | |
def should_process(filename): | |
dt = get_creation_date_for(f) | |
return dt > FORMATTER_ADOPTION_DATE and is_python(filename) | |
def main(): | |
for f in get_modified_files(): | |
if should_process(f): | |
process(f) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment