Created
November 3, 2019 05:01
-
-
Save antlauzon/64ef242cc0937361822f8c13d25a4f64 to your computer and use it in GitHub Desktop.
hash and save files that change on your filesystem in realtime
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
#!/usr/bin/env python3 | |
import argparse | |
import datetime | |
import re | |
import os | |
import hashlib | |
import logging | |
import sys | |
import subprocess | |
import shutil | |
import shlex | |
import signal | |
import sys | |
import time | |
import traceback | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--savedir', required=True) | |
parser.add_argument('--logdir', required=True) | |
parser.add_argument('--watchdir', required=True) | |
parser.add_argument('--interval', type=int, default=1000) | |
args, _ = parser.parse_known_args() | |
INTERVAL = args.interval | |
SAVE_DIR = args.savedir | |
LOG_DIR = args.logdir | |
WATCH_DIR = args.watchdir | |
NOTIFY_CMD = 'notifywait' | |
PATH_RE = re.compile(b'^Change\s*\d+\s*in\s+(\/.*)$') | |
LOG_FILENAME = 'gsdepot.log' | |
os.makedirs(SAVE_DIR, exist_ok=True) | |
os.makedirs(LOG_DIR, exist_ok=True) | |
def setup_logging(): | |
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s") | |
rootLogger = logging.getLogger() | |
fileHandler = logging.FileHandler("{}/{}".format(LOG_DIR, LOG_FILENAME)) | |
fileHandler.setFormatter(logFormatter) | |
rootLogger.addHandler(fileHandler) | |
consoleHandler = logging.StreamHandler() | |
consoleHandler.setFormatter(logFormatter) | |
rootLogger.addHandler(consoleHandler) | |
rootLogger.setLevel(logging.DEBUG) | |
setup_logging() | |
def signal_handler(sig, frame): | |
print('GANGSTALKER SALISBURY DRIP DRY') | |
sys.exit(0) | |
signal.signal(signal.SIGINT, signal_handler) | |
while True: | |
try: | |
p = subprocess.Popen([NOTIFY_CMD, WATCH_DIR], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
so, se = p.communicate() | |
for line in so.split(b'\n'): | |
print(line) | |
path_line = line.split(b',')[0].strip() | |
m = PATH_RE.match(path_line.strip()) | |
if m: | |
path = m.group(1) | |
logging.info("SAVING_PATH: {}".format(path)) | |
filename = os.path.basename(path) | |
m = hashlib.sha256() | |
m.update(open(path, 'rb').read()) | |
m.hexdigest() | |
iso_time = datetime.datetime.now().strftime("%Y-%m-%d") | |
os.makedirs("{}/{}".format(SAVE_DIR, iso_time), exist_ok=True) | |
shutil.copy(path, | |
"{}/{}/{}-{}".format(SAVE_DIR, | |
iso_time, | |
m.hexdigest(), | |
filename.decode('utf-8'))) | |
os.chmod(path, 0000) | |
except: | |
traceback.print_exc() | |
time.sleep(float(INTERVAL)/1000) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment