def test_delete(self) -> None: """ Tests the method which let us delete a file. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() self.assertEqual(expected, actual) with open(file_helper.path, "w", encoding="utf-8") as file_stream: file_stream.write("") expected = True actual = file_helper.exists() self.assertEqual(expected, actual) file_helper.delete() expected = False actual = file_helper.exists() self.assertEqual(expected, actual)
def test_is_empty(self) -> None: """ Tests the method which let us check if a file is empty. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() self.assertEqual(expected, actual) with open(file_helper.path, "w", encoding="utf-8") as file_stream: file_stream.write("") expected = True actual = file_helper.is_empty() self.assertEqual(expected, actual) with open(file_helper.path, "w", encoding="utf-8") as file_stream: file_stream.write("Hello, World!") expected = False actual = file_helper.is_empty() self.assertEqual(expected, actual) os.remove(file_helper.path)
def get_backup_data(self) -> dict: """ Provides the data which acts as a backup. """ result = {} base_dir = self.get_output_basedir() file_helper = FileHelper() hash_helper = HashHelper() for file in DirectoryHelper(base_dir).list_all_files(): file_helper.set_path(file) reduced_path = self.get_path_without_base_dir(file) directory, filename = reduced_path.rsplit(os.sep, 1) file_hash = hash_helper.hash_file(file_helper.path) dataset = { filename: { "content": file_helper.read(), "hash": file_hash } } if directory not in result: result[directory] = dataset else: result[directory].update(dataset) PyFunceble.facility.Logger.debug("Dir Structure to backup:\n%r", result) return result
def start(self) -> "OurInfrastructureUpdater": file_helper = FileHelper() dir_helper = DirectoryHelper() for file in self.FILES_TO_REMOVE: file_helper.set_path( os.path.join(self.info_manager.WORKSPACE_DIR, file)) if file_helper.exists(): logging.info("Starting deletion of %r", file_helper.path) file_helper.delete() logging.info("Finished deletion of %r", file_helper.path) for directory in self.DIRS_TO_REMOVE: dir_helper.set_path( os.path.join(self.info_manager.WORKSPACE_DIR, directory)) if dir_helper.exists(): logging.info("Starting deletion of %r", dir_helper.path) dir_helper.delete() logging.info("Finished deletion of %r", dir_helper.path) return self
def test_open(self) -> None: """ Tests the method which let us open the given file as we want. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() self.assertEqual(expected, actual) with file_helper.open("w") as file_stream: file_stream.write("Hello, World!") expected = True actual = file_helper.exists() self.assertEqual(expected, actual) expected = "Hello, World!" actual = file_helper.read() self.assertEqual(expected, actual)
def test_move(self) -> None: """ Tests of the method which let us move a file to another location. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) destination_file_helper = FileHelper(tempfile.gettempdir()) destination_file_helper.set_path( destination_file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() actual_destination = destination_file_helper.exists() self.assertEqual(expected, actual) self.assertEqual(expected, actual_destination) file_helper.write("Hello, World!") expected = True actual = file_helper.exists() self.assertEqual(expected, actual) expected = False actual_destination = destination_file_helper.exists() self.assertEqual(expected, actual_destination) file_helper.move(destination_file_helper.path) expected = True actual_destination = destination_file_helper.exists() self.assertEqual(expected, actual_destination) expected = "Hello, World!" actual = destination_file_helper.read() self.assertEqual(expected, actual) expected = False actual = file_helper.exists() self.assertEqual(expected, actual) expected = True actual_destination = destination_file_helper.exists() self.assertEqual(expected, actual_destination)
def test_copy(self) -> None: """ Tests the method which let us copy a file to another place. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) copy_file_helper = FileHelper(tempfile.gettempdir()) copy_file_helper.set_path( copy_file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() actual_copy = copy_file_helper.exists() self.assertEqual(expected, actual) self.assertEqual(expected, actual_copy) file_helper.write("Hello, World!") expected = True actual = file_helper.exists() self.assertEqual(expected, actual) expected = False actual_copy = copy_file_helper.exists() self.assertEqual(expected, actual_copy) file_helper.copy(copy_file_helper.path) expected = True actual_copy = copy_file_helper.exists() self.assertEqual(expected, actual_copy) expected = "Hello, World!" actual = copy_file_helper.read() self.assertEqual(expected, actual) expected = True actual = file_helper.exists() actual_copy = copy_file_helper.exists() self.assertEqual(expected, actual) self.assertEqual(expected, actual_copy)
def is_cloned( ) -> bool: # pragma: no cover ## Only used by 1 thing for dev. """ Checks if the local version is a cloned (from git) one. """ file_helper = FileHelper() directory_helper = DirectoryHelper() if not directory_helper.set_path(".git").exists(): return False list_of_files = [ ".coveragerc", ".gitignore", "CODE_OF_CONDUCT.rst", "CONTRIBUTING.rst", "MANIFEST.in", "README.rst", "requirements.txt", "setup.py", "version.yaml", ] list_of_dirs = ["docs", "PyFunceble", "tests", ".github"] if not all(file_helper.set_path(x).exists() for x in list_of_files): return False if not all( directory_helper.set_path(x).exists() for x in list_of_dirs): return False return True
def remove_removed(self) -> None: """ Removed the removed entries from all files to clean. """ file_helper = FileHelper() for file in self.files_to_clean: if not file_helper.set_path(file).exists(): continue logging.info( "Started to cleanup: %r", file, ) whitelist_core_tool( output_file=file, secondary_whitelist=self.whitelist_list.name, use_official=False, processes=os.cpu_count(), ).filter(file=file, already_formatted=True, standard_sort=True) logging.info( "Finished to cleanup: %r", file, )
def test_read_bytes_file_does_not_exists(self) -> None: """ Tests the method which let us read (bytes) a file for the case that the given file does not exists. """ file_helper = FileHelper(tempfile.gettempdir()) file_helper.set_path(file_helper.join_path(secrets.token_hex(8))) expected = False actual = file_helper.exists() self.assertEqual(expected, actual) expected = None actual = file_helper.read_bytes() self.assertEqual(expected, actual)
def test_set_path(self) -> None: """ Tests the method which let us set the path to work with. """ given = tempfile.NamedTemporaryFile() expected = given.name file_helper = FileHelper() file_helper.set_path(given.name) actual = file_helper.path self.assertEqual(expected, actual) file_helper = FileHelper(given.name) actual = file_helper.path self.assertEqual(expected, actual)
def authorized(self) -> bool: if not DirectoryHelper( self.info_manager.PYFUNCEBLE_CONFIG_DIR).exists(): return True file_helper = FileHelper() for file in self.FILES_TO_MOVE: if file_helper.set_path( os.path.join(self.info_manager.PYFUNCEBLE_CONFIG_DIR, file)).exists(): return True if all( file_helper.set_path( os.path.join(self.info_manager.PYFUNCEBLE_CONFIG_DIR, x)).exists() for x in self.INACTIVE_FILES_TO_DELETE): return True return False
def test_set_path_not_str(self) -> None: """ Tests the method which let us set the path to work with for the case that it's not a string. """ given = ["Hello", "World"] file_helper = FileHelper() self.assertRaises(TypeError, lambda: file_helper.set_path(given))
def test_set_path_return(self) -> None: """ Tests the response from the method which let us set the path to work with. """ given = tempfile.NamedTemporaryFile() file_helper = FileHelper() actual = file_helper.set_path(given.name) self.assertIsInstance(actual, FileHelper)
def get_dot_env_file(self) -> str: """ Provides the dotenv file to work with. """ file_helper = FileHelper() for file in self.dotenv_locations: if file_helper.set_path(file).exists(): return file_helper.path return self.dotenv_locations[-1]
for root, _, files in os.walk(dir_helper.path): unformatted_root = root root = root.replace(dir_helper.path, "") if root.startswith("/"): root = root[1:] local_dir_helper = DirectoryHelper(os.path.join(clone_destination, root)) if not local_dir_helper.exists(): local_dir_helper.create() for file in files: file_helper.set_path(os.path.join(dir_helper.path, root, file)).copy( os.path.join(local_dir_helper.path, file) ) CommandHelper(f"cd {clone_destination}").execute(raise_on_error=True) commit_count = int( CommandHelper("git rev-list --all --count").execute().split()[0] ) if commit_count > 1000: DirectoryHelper(os.path.join(clone_destination, ".git")).delete() CommandHelper("git init").execute() CommandHelper("git add --all && git commit -m {COMMIT_MESSAGE!r}")
def restore_from_backup(self) -> "DirectoryStructureRestoration": """ Restores or reconstruct the output directory. """ # pylint: disable=too-many-locals PyFunceble.facility.Logger.info( "Started restoration of the directory structure") backup = self.get_backup_data() base_dir = self.get_output_basedir() dir_helper = DirectoryHelper() file_helper = FileHelper() if dir_helper.set_path(base_dir).exists(): for root, _, files in os.walk(dir_helper.path): reduced_path = self.get_path_without_base_dir(root) if reduced_path not in backup and root != reduced_path: dir_helper.set_path(root).delete() PyFunceble.facility.Logger.debug( "Added %r into the list of directories to delete. " "Reason: not found in own dataset.", root, ) continue for directory, files in backup.items(): dir_helper.set_path(os.path.join(base_dir, directory)).create() for file, dataset in files.items(): file_full_path = os.path.join(dir_helper.path, file) if (file == ".gitignore" and PyFunceble.cli.storage.STD_PARENT_DIRNAME not in file_full_path): to_delete = file_full_path file_helper.set_path(to_delete).delete() PyFunceble.facility.Logger.debug( "(If exists) Deleted: %r. Reason: We are going to " "replace it with .gitkeep", to_delete, ) file_full_path = file_full_path.replace( ".gitignore", ".gitkeep") file_helper.set_path(file_full_path) if not file_helper.exists(): file_helper.write(dataset["content"], overwrite=True) PyFunceble.facility.Logger.info( "Finished restoration of the directory structure") return self
pyfunceble_webworker.storage.CONFIG_DIRECTORY = env_var_helper.get_value() PyFunceble.storage.CONFIG_DIRECTORY = os.path.join( pyfunceble_webworker.storage.CONFIG_DIRECTORY, secrets.token_hex(8), ) DirectoryHelper(PyFunceble.storage.CONFIG_DIRECTORY).create() DirectoryHelper(pyfunceble_webworker.storage.CONFIG_DIRECTORY).create() file_helper = FileHelper() pyfunceble_config_loader = ConfigLoader() if file_helper.set_path( os.path.join( pyfunceble_webworker.storage.CONFIG_DIRECTORY, assets_defaults.OVERWRITE_CONFIG_FILE, )).exists(): local = DictHelper().from_yaml_file(file_helper.path) if local: pyfunceble_config_loader.custom_config = local else: pyfunceble_config_loader.custom_config = dict() else: file_helper.write("") pyfunceble_config_loader.custom_config = Merge( pyfunceble_defaults.PERSISTENT_CONFIG).into( pyfunceble_config_loader.custom_config) pyfunceble_config_loader.start()