Last active
August 29, 2015 14:16
-
-
Save paul-schwendenman/5e05d98d3feb2a1ccf61 to your computer and use it in GitHub Desktop.
File tracker
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
import os | |
import json | |
import getpass | |
import pprint | |
pathMac = [ | |
'/Applications/', os.path.join('/Users/', getpass.getuser(), 'Documents/'), | |
#os.path.join('/home/', getpass.getuser(), 'Downloads/'), | |
#os.path.join('/home/', getpass.getuser(), 'Documents/'), | |
] | |
################################## | |
# FUNCTIONS | |
################################## | |
def create_dir_index(path): | |
files = [] | |
subdirs = [] | |
for root, dirs, filenames in os.walk(path): | |
for subdir in dirs: | |
subdirs.append(os.path.relpath(os.path.join(root, subdir), path)) | |
for f in filenames: | |
files.append(os.path.relpath(os.path.join(root, f), path)) | |
return dict(files=files, subdirs=subdirs) | |
def create_state(): | |
''' | |
create a state of all indexes in pathMac to json | |
''' | |
state = dict(files=[], subdirs=[]) | |
for path in pathMac: | |
dir_index = create_dir_index(path) | |
state['files'].extend(dir_index['files']) | |
state['subdirs'].extend(dir_index['subdirs']) | |
return state | |
def save_state(): | |
''' | |
create a state of all indexes in pathMac and write to json file | |
''' | |
with open("Manifest.json", "w") as out_file: | |
json.dump(create_state(), out_file) | |
def compare_states(dir_base, dir_cmp): | |
''' | |
return a comparison two manifest json files | |
''' | |
data = {} | |
data['deleted'] = list(set(dir_cmp['files']) - set(dir_base['files'])) | |
data['created'] = list(set(dir_base['files']) - set(dir_cmp['files'])) | |
data['deleted_dirs'] = list( | |
set(dir_cmp['subdirs']) - set(dir_base['subdirs'])) | |
data['created_dirs'] = list( | |
set(dir_base['subdirs']) - set(dir_cmp['subdirs'])) | |
return data | |
################################## | |
# MAIN | |
################################## | |
if __name__ == '__main__': | |
response = raw_input("Would you like to Compare or Create? ") | |
if response == "Create": | |
# CREATE MANIFEST json file | |
save_state() | |
print "Manifest file created." | |
elif response == "Compare": | |
# create the CURRENT state of all indexes in pathMac | |
current = create_state() | |
# Open and Load the contents from the file into dictionaries | |
manifest = json.load(open("Manifest.json", "r")) | |
pprint.pprint(compare_states(current, manifest)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment