def test_get_all(self): """ Test Hash.get() for the case that we want all. """ expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) File(self.file).write("\n".join(self.data_to_write)) expected = True actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) expected = self.expected_hashed actual = Hash(self.file, algorithm="all").get() self.assertEqual(expected, actual) actual = Hash(data="\n".join(self.data_to_write), algorithm="all").get() self.assertEqual(expected, actual) File(self.file).delete() expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual)
def test_get_specific_algo(self): """ Test Hash.get() for the case that we want a specifig algorithm. """ expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) File(self.file).write("\n".join(self.data_to_write)) expected = True actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) expected = self.expected_hashed["sha512"] actual = Hash(self.file, algorithm="sha512", only_hash=True).get() self.assertEqual(expected, actual) expected = self.expected_hashed["sha512"] actual = Hash( data="\n".join(self.data_to_write), algorithm="sha512", only_hash=True ).get() self.assertEqual(expected, actual) File(self.file).delete() expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual)
def test_hash_data(self): """ Tests the method wich let us hash a given data. """ given = "\n".join(self.data_to_write) for algo, expected in self.expected_hashed.items(): self.assertEqual(expected, Hash(algo=algo).data(given)) self.assertEqual(expected, Hash(algo=algo).data(given.encode()))
def restore(self): """ Restore the 'output/' directory structure based on the `dir_structure.json` file. """ structure = self._get_structure() list_of_key = list(structure.keys()) structure = structure[list_of_key[0]] parent_path = list_of_key[0] + directory_separator for directory in structure: base = self.base + parent_path + directory + directory_separator self._create_directory(base) for file in structure[directory]: file_path = base + file content_to_write = structure[directory][file]["content"] online_sha = structure[directory][file]["sha512"] content_to_write = Regex(content_to_write, "@@@", escape=True, replace_with="\\n").replace() git_to_keep = file_path.replace("gitignore", "keep") keep_to_git = file_path.replace("keep", "gitignore") if self._restore_replace(): if path.isfile(file_path) and Hash( file_path, "sha512", True).get() == online_sha: rename(file_path, git_to_keep) write = False else: File(file_path).delete() file_path = git_to_keep write = True else: if path.isfile(keep_to_git) and Hash( file_path, "sha512", True).get() == online_sha: rename(file_path, keep_to_git) write = False else: File(keep_to_git).delete() file_path = keep_to_git write = True if write: File(file_path).write(content_to_write + "\n", True)
def testhash_file(self): """ Test Hash._hash_file(). """ expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) File(self.file).write("\n".join(self.data_to_write)) expected = True actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) for algo, result in self.expected_hashed.items(): self.assertEqual( result, Hash(self.file)._hash_file(algo), msg="%s did not passed the test" % repr(algo), ) File(self.file).delete() expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual)
def test_hash_file(self): """ Tests the method which let us the content of a given file. """ file_instance = File(self.file) expected = False actual = file_instance.exists() self.assertEqual(expected, actual) file_instance.write("\n".join(self.data_to_write)) expected = True actual = file_instance.exists() self.assertEqual(expected, actual) for algo, expected in self.expected_hashed.items(): self.assertEqual( expected, Hash(algo=algo).file(self.file), ) file_instance.delete() expected = False actual = file_instance.exists() self.assertEqual(expected, actual)
def backup(cls): """ Backup the developer state of `output/` in order to make it restorable and portable for user. """ output_path = PyFunceble.CURRENT_DIRECTORY + PyFunceble.OUTPUTS[ "parent_directory"] result = {PyFunceble.OUTPUTS["parent_directory"]: {}} for root, _, files in walk(output_path): directories = root.split(output_path)[1] local_result = result[PyFunceble.OUTPUTS["parent_directory"]] for file in files: file_path = root + directory_separator + file file_hash = Hash(file_path, "sha512", True).get() lines_in_list = [line.rstrip("\n") for line in open(file_path)] formated_content = "@@@".join(lines_in_list) local_result = local_result.setdefault( directories, {file: { "sha512": file_hash, "content": formated_content }}, ) Dict(result).to_json(PyFunceble.CURRENT_DIRECTORY + "dir_structure_production.json")
def test_hash_unknown_algo(self): """ Tests the hash class for the case that we give an unknown algo. """ given = "\n".join(self.data_to_write) self.assertRaises(ValueError, lambda: Hash(algo="Hello, World!").data(given))
def test_hash_data_not_string_nor_bytes(self): """ Tests the method which let us hash a given data for the case that we given a non string or bytes input. """ given = [1, 2, 3, 4] self.assertRaises(ValueError, lambda: Hash().data(given))
def testhash_data(self): """ Test Hash._hash_data(). """ to_test = "\n".join(self.data_to_write) for algo, result in self.expected_hashed.items(): self.assertEqual( result, Hash(data=to_test)._hash_data(algo), msg="%s did not passed the test" % repr(algo), )
def test_get_path_not_exist(self): """ Test Hash.get() for the case that the given file does not exist. """ expected = False actual = PyFunceble.path.isfile(self.file) self.assertEqual(expected, actual) expected = None actual = Hash(self.file).get() self.assertEqual(expected, actual)
def backup(self): """ Backup the developer state of `output/` in order to make it restorable and portable for user. """ # We set the current output directory path. output_path = self.base + PyFunceble.OUTPUTS["parent_directory"] # We initiate the structure base. result = {PyFunceble.OUTPUTS["parent_directory"]: {}} for root, _, files in PyFunceble.walk(output_path): # We loop through the current output directory structure. # We get the currently read directory name. directories = Directory(root.split(output_path)[1]).fix_path() # We initiate a local variable which will get the structure of the subdirectory. local_result = result[PyFunceble.OUTPUTS["parent_directory"]] for file in files: # We loop through the list of files. # We construct the file path. file_path = root + PyFunceble.directory_separator + file # We get the hash of the file. file_hash = Hash(file_path, "sha512", True).get() # We convert the file content to a list. lines_in_list = [line.rstrip("\n") for line in open(file_path)] # We convert the file content into a more flat format. # We use `@@@` as glue and implicitly replacement for `\n`. formatted_content = "@@@".join(lines_in_list) # We update the local result (and implicitly the global result) # with the files and directory informations/structure. local_result = local_result.setdefault( directories, { file: { "sha512": file_hash, "content": formatted_content } }, ) # We finally save the directory structure into the production file. Dict(result).to_json(self.base + "dir_structure_production.json")
def test_hash_file_not_exists(self): """ Tests the method which let us the content of a given file. """ file_instance = File(self.file) file_instance.delete() expected = False actual = file_instance.exists() self.assertEqual(expected, actual) expected = None for algo in self.expected_hashed: self.assertEqual( expected, Hash(algo=algo).file(self.file), )
def restore(self): """ Restore the 'output/' directory structure based on the `dir_structure.json` file. """ # We get the structure we have to create/apply. structure = self._get_structure() # We get the list of key which is implicitly the list of directory to recreate. list_of_key = list(structure.keys()) # We move to the content of the parent as we know that we are creating only one directory. # Note: if one day we will have to create multiple directory, we will have to change # the following. structure = structure[list_of_key[0]] # We also set the parent directory as we are going to construct its childen. parent_path = list_of_key[0] if not parent_path.endswith(PyFunceble.directory_separator): parent_path += PyFunceble.directory_separator # We get if we have to replace `.gitignore` to `.keep` and versa. replacement_status = self._restore_replace() for directory in structure: # We loop through the list of directory to create. # We construct the full path. base = self.base + parent_path + directory if not base.endswith(PyFunceble.directory_separator): base += PyFunceble.directory_separator # We create the constructed path if it does not exist. self._create_directory(base) for file in structure[directory]: # We loop through the list of files in the currently read directory. # We construct the full file path.s file_path = base + file # We get the file content. content_to_write = structure[directory][file]["content"] # And its sha512 checksum. online_sha = structure[directory][file]["sha512"] # We update the content to write by replacing our glue with `\n`. content_to_write = Regex(content_to_write, "@@@", escape=True, replace_with="\\n").replace() # We get the file path as .keep. git_to_keep = file_path.replace("gitignore", "keep") # We get the file path as .gitignore. keep_to_git = file_path.replace("keep", "gitignore") if replacement_status: # We have to replace every .gitignore to .keep. if (PyFunceble.path.isfile(file_path) and Hash( file_path, "sha512", True).get() == online_sha): # * The currently read file exist. # and # * Its sha512sum is equal to the one we have in our structure. # We rename the file. PyFunceble.rename(file_path, git_to_keep) # And we disallow the file writing. write = False else: # * The currently read file does not exist. # or # * Its sha512sum is not equal to the one we have in our structure. # We delere the file if it does exist. File(file_path).delete() # We update the file path. file_path = git_to_keep # And we allow the file writing. write = True else: # We have to replace every .keep to .gitignore. if (PyFunceble.path.isfile(keep_to_git) and Hash( file_path, "sha512", True).get() == online_sha): # * The .keep file exist. # and # * Its sha512sum is equal to the one we have in our structure. # We rename the file. PyFunceble.rename(file_path, keep_to_git) # And we disallow the file writing. write = False else: # * The .keep file does not exist. # or # * Its sha512sum is not equal to the one we have in our structure. # We delete the file if it exist. File(keep_to_git).delete() # We update the file path file_path = keep_to_git # And we allow the file writing. write = True if write: # The file writing is allowed. # We write our file content into the file path. File(file_path).write(content_to_write + "\n", True) self.delete_uneeded()