Skip to content

Instantly share code, notes, and snippets.

@ShawnMilo
Created October 5, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnMilo/7d1261f22ff5f90fad3e02b9bcb7cf30 to your computer and use it in GitHub Desktop.
Save ShawnMilo/7d1261f22ff5f90fad3e02b9bcb7cf30 to your computer and use it in GitHub Desktop.
audit.py
#!/usr/bin/env python3
"""
Find files to audit so that all files are looked
at regularly.
"""
from os.path import abspath, dirname, join
from os import walk
import re
date_pattern = re.compile(r'\b\d{4}-\d{2}-\d{2}\b')
def second_element(x):
return x[1]
def main():
"""
Do the thing.
"""
stamps = []
longest = 0
loc = dirname(abspath(__file__))
for path, dirs, files in walk(loc):
for file in files:
if not file.endswith('.go'):
continue
if '/vendor/' in path:
continue
f = join(path, file)
if len(f) > longest:
longest = len(f)
with open(f) as raw:
lines = raw.readlines()
lines.reverse()
for line in lines:
if line.strip() == "":
continue
last = line
break
if not 'audit' in last:
stamps.append((f, ""))
continue
match = date_pattern.search(last)
if match is None:
stamps.append((f, ""))
continue
stamps.append((f, match.group()))
stamps.sort(key=second_element, reverse=True)
for file, stamp in stamps:
print("{0} {1}".format(file.ljust(longest, " "), stamp))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment