#!/usr/bin/env python3

import shutil
import os
import time
import signal
import atexit
import sys
import argparse

def cleanup(path):
  time.sleep(2)
  shutil.rmtree(path, ignore_errors=True)

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument('path')
  args = parser.parse_args()

  def handler(signum, frame):
    cleanup(args.path)
    sys.exit(-1)

  signal.signal(signal.SIGPIPE, handler)
  signal.signal(signal.SIGSEGV, handler)
  signal.signal(signal.SIGTERM, handler)
  signal.signal(signal.SIGINT, handler)
  atexit.register(lambda: cleanup(args.path))

  while os.getppid() != 1: time.sleep(1)
  cleanup(args.path)