# -*- coding: utf-8 -*-
# @Filename : take_snapshot.py
# @Date : 2019-07-15-13-44
# @Project: ITC-sniff-for-changes-in-directory
# @Author: Piotr Wołoszyn
# @Website: http://itcave.eu
# @Email: contact@itcave.eu
# @License: MIT
# @Copyright (C) 2019 ITGO Piotr Wołoszyn


# Generic imports
import os
import pickle
import re
import argparse
from datetime import datetime


def clear_path_string(s):
    """
    Simple function that removes chars that are not allowed in file names
    :param s: path_string
    :return: cleaned_path_string
    """
    return (re.sub('[^a-zA-Z]+', '#', s)).lower()


def sniff(sniff_path):
    """
    Walks the path and stores information about directory content
    :param sniff_path: relative or absolute path
    :return: void
    """

    sniff_path = str(sniff_path).lower()

    # Variable in which information will be stored
    dir_store = {}

    # Recursive loop that walks through all of the subdirectories
    for subdir, dirs, files in os.walk(sniff_path):

        if subdir not in dir_store:
            dir_store[subdir] = {}

        dir_store[subdir]['subdirs'] = dirs
        dir_store[subdir]['files'] = files
        dir_store[subdir]['file_details'] = {}

        for file in files:

            f_path = os.path.join(subdir, file)

            # The information that will be store for each of the files - in this case last file modification date
            # Important: it's cross-platform relevant!
            modified_date = os.path.getmtime(f_path)
            dir_store[subdir]['file_details'][file] = (modified_date,)

    # Name of a file in which data will be stored
    dump_name = clear_path_string(sniff_path) + '_' + datetime.now().strftime('%Y%m%d%H%M%S')

    # Save pickled data
    with open(dump_name + '.pkl', 'wb') as output:
        pickle.dump(dir_store, output, pickle.HIGHEST_PROTOCOL)

    print("Directory Snapshot taken:", dump_name)


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Directory Sniffer')
    parser.add_argument('path', help='Path to the directory that you want to take a snapshot of')
    args = parser.parse_args()
    sniff(args.path)

[((42, 31, 42, 50), 'os.walk', 'os.walk', ({(42, 39, 42, 49): 'sniff_path'}, {}), '(sniff_path)', False, 'import os\n'), ((72, 13, 72, 69), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((65, 8, 65, 63), 'pickle.dump', 'pickle.dump', ({(65, 20, 65, 29): 'dir_store', (65, 31, 65, 37): 'output', (65, 39, 65, 62): 'pickle.HIGHEST_PROTOCOL'}, {}), '(dir_store, output, pickle.HIGHEST_PROTOCOL)', False, 'import pickle\n'), ((26, 12, 26, 40), 're.sub', 're.sub', ({(26, 19, 26, 31): '"""[^a-zA-Z]+"""', (26, 33, 26, 36): '"""#"""', (26, 38, 26, 39): 's'}, {}), "('[^a-zA-Z]+', '#', s)", False, 'import re\n'), ((53, 21, 53, 47), 'os.path.join', 'os.path.join', ({(53, 34, 53, 40): 'subdir', (53, 42, 53, 46): 'file'}, {}), '(subdir, file)', False, 'import os\n'), ((57, 28, 57, 52), 'os.path.getmtime', 'os.path.getmtime', ({(57, 45, 57, 51): 'f_path'}, {}), '(f_path)', False, 'import os\n'), ((61, 54, 61, 68), 'datetime.datetime.now', 'datetime.now', ({}, {}), '()', False, 'from datetime import datetime\n')]