Beispiel #1
0
def status_damnit(new_file, damnit_path='.damnit'):
    # Check if file exists
    if os.path.exists(new_file):
        print('File is real<br>')
    else:
        raise OSError('File is not real<br>')

    # Convert new file into md5
    md5 = load_md5(new_file)

    # Open and read md5Graph_NewToOld.json file
    original_md5Graph = read_json(damnit_path + "/md5Graph_NewToOld.json")

    # Open and read filenames_dict.json file
    filenames_dict = read_json(damnit_path + "/filenames_dict.json")

    # Check if md5 exists in md5_dict.json file
    if md5 in original_md5Graph and md5 in filenames_dict.values(
    ) and new_file in filenames_dict:
        print('File exists in database<br>')
        print(new_file)
    elif new_file in filenames_dict and md5 not in original_md5Graph:
        print('File needs to be updated.<br>')
        print(new_file)
    elif md5 in original_md5Graph and new_file not in filenames_dict:
        print('File name has changed and needs updated.<br>')
        print(new_file)
    else:
        print('File does not exist in database<br>')
        print(new_file)

    return status_damnit
Beispiel #2
0
def load_patch_dict(indivFile, damnit_path='.damnit'):
    newMD5 = load_md5(indivFile)  # find the md5 of the new file
    md5Graph_NewToOld = read_json(damnit_path + "/md5Graph_NewToOld.json")
    # Open and read filenames_dict.json file
    FileNameDictionary = read_json(damnit_path + "/filenames_dict.json")

    if indivFile in FileNameDictionary:
        oldMD5 = FileNameDictionary[
            indivFile]  # for ease of reading, rename the file
        if newMD5 == oldMD5:  # if the new md5 is the same as the old md5 do nothing.
            print("no change to", indivFile)
        else:  # if the file is the same and the file has changed:
            # Process:
            # 1) find the difference between the files (the path
            # 2) the graph dictionary is the new MD5 as key and the old MD5 as value
            # 3) change the file name dictionary to the new value (the new MD5)
            # 4) copy the file to DamnitPath directory the name of this file is the MD5
            # 5) copy the Patch to the DamnitPath directory, the name is the old MD5
            # !!!!!!!!!!! this will overwright the old file, and all the data in it. !!!!!!!!
            difference = find_diff(indivFile, (damnit_path + '/' + oldMD5),
                                   damnit_path)  # this is the patch
            shutil.copyfile(indivFile, (damnit_path + '/' + newMD5))
            #shutil.copyfile((oldMD5+".patch"), (damnit_path + '/' + oldMD5))
            md5Graph_NewToOld[
                newMD5] = oldMD5  # add in the new md5 as a key, the old md5 is the value
            FileNameDictionary[
                indivFile] = newMD5  # the file name is now linked to the new MD5 (old one lost)
            with open(damnit_path + "/filenames_dict.json",
                      "w") as newFileDict:
                json.dump(FileNameDictionary, newFileDict)
            with open(damnit_path + "/md5Graph_NewToOld.json",
                      "w") as newPatch:
                json.dump(md5Graph_NewToOld,
                          newPatch)  # open the file as write, save new dict
            print(indivFile, "has changed")
    else:  # now the file name is not tracked, and it is not in the dictionary
        # Process:
        # 1) we need to generate the new file MD5, add it to the dictionary
        # 2) save the new filename dictionary
        # 3) add the file to the the graph dictionary.
        # 4) save the graph dictionary
        shutil.copyfile(indivFile, (damnit_path + '/' + newMD5))
        FileNameDictionary[
            indivFile] = newMD5  # add it to the file name dictionary
        with open(damnit_path + "/filenames_dict.json", "w") as newFileDict:
            json.dump(FileNameDictionary, newFileDict)
# old version created the graph file.  since it is now created we don't need it anymore
        md5Graph_NewToOld[
            newMD5] = "none"  # since there are no changes, the first value will be none
        with open(damnit_path + "/md5Graph_NewToOld.json", "w") as newPatch:
            json.dump(md5Graph_NewToOld,
                      newPatch)  # open the file as write, save new dict
            print("new file", indivFile)
Beispiel #3
0
def all_file_name_dict(damnit_path = '.damnit'):
	make_damnit(damnit_path)
	# Find all files in dictionary.
	filenames = glob.glob('*')
	filenames_dict = {}
	md5_dict = {}
	md5Graph = {}
	print('Adding files:')

	# Create filenames file
	fo_filenames = open(damnit_path + "/filenames_dict.json" , "w")
	json.dump(filenames_dict, fo_filenames)
	fo_filenames.close()

	# Create md5 file
	fo_md5 = open(damnit_path + "/md5_dict.json" , "w")
	json.dump(md5_dict, fo_md5)
	fo_md5.close()

	for filename in filenames:
		if os.path.isdir(filename):
			continue
		else:
#			print(filename)
			load_patch_dict(filename, damnit_path)
			md5 = load_md5(filename)
			filenames_dict[filename] = md5
			md5_dict[md5] = filename

	# Create filenames file
	fo_filenames = open(damnit_path + "/filenames_dict.json" , "w")
	json.dump(filenames_dict, fo_filenames)
	fo_filenames.close()

	# Create md5 file
	fo_md5 = open(damnit_path + "/md5_dict.json" , "w")
	json.dump(md5_dict, fo_md5)
	fo_md5.close()

#	fo_Graph = open(damnit_path + "/md5Graph_NewToOld.json" ,"w")
#	json.dump(md5Graph, fo_Graph)
#	fo_Graph.close() 
	
	return all_file_name_dict
Beispiel #4
0
def find_diff(new_file, old_file, damnit_path='.damnit'):
    oldMD5 = load_md5(old_file)
    string = "diff {} {} > {}/{}.patch"
    command = string.format(new_file, old_file, damnit_path, oldMD5)
    subprocess.run(command, shell=True)
Beispiel #5
0
#!/usr/bin/env python3

import sys
from damnit_IO import read_json
import os
from md5_module import load_md5
import json

# Create a script to re create a file

# Define imported file name
new_file = sys.argv[1]

# Convert new file into md5
md5 = load_md5(new_file)

# Open and read md5Graph_NewToOld.json file
original_md5Graph = read_json(".damnit/md5Graph_NewToOld.json")

if md5 in original_md5Graph:
    print(original_md5Graph[md5])
Beispiel #6
0
    print('Thank you for loading a file<br>')
    #    if form['NewFile'].value:
    #        print("did the file load?")
    #        print(form['NewFile'].value)
    #        print("file loaded")

    fileitem = form['NewFile']

    # Test if the file was uploaded
    if fileitem.filename:
        # strip leading path from file name
        # to avoid directory traversal attacks
        fn = '/'.join([DirectoryPath, os.path.basename(fileitem.filename)])

        open(fn, 'wb').write(fileitem.file.read())
        cks = load_md5(fn)
        message = 'The file "' + os.path.basename(
            fileitem.filename) + '" was uploaded successfully ({})'.format(cks)
        print(message)
        print('<br>')
        status_damnit(fn, DirectoryPath)
        #print('<br>Upload Now <input type=file name=upload><br>')
        #print('When ready upload file: <input type=submit value="Upload File", name=uploadbutton>')
        #if uploadbutton in form:
        #load_patch_dict(fn, DirectoryPath)
        print('<br>')

    else:
        message = 'please select and submit another file'
        print(message)
print('</body>')