def get_config_directory( *, project_name: str, project_version: str) -> str: # pragma: no cover ## Not relevant """ Provides the location of the configuration directory. """ # pylint: disable=too-many-branches env_var_helper = EnvironmentVariableHelper() directory_helper = DirectoryHelper() if env_var_helper.set_name("PYFUNCEBLE_CONFIG_DIR").exists(): config_directory = env_var_helper.get_value() elif env_var_helper.set_name("PYFUNCEBLE_OUTPUT_DIR").exists(): config_directory = env_var_helper.get_value() elif (VersionUtility(project_version).is_cloned() or env_var_helper.set_name("TRAVIS_BUILD_DIR").exists() or env_var_helper.set_name("CI_PROJECT_DIR").exists() and env_var_helper.set_name("GITLAB_CI").exists()): config_directory = directory_helper.get_current(with_end_sep=True) else: if PlatformUtility.is_unix(): config_dir_path = os.path.expanduser(os.path.join("~", ".config")) if directory_helper.set_path(config_dir_path).exists(): config_directory = config_dir_path elif directory_helper.set_path(os.path.expanduser("~")).exists(): config_directory = directory_helper.join_path(".") else: config_directory = directory_helper.get_current( with_end_sep=True) elif PlatformUtility.is_windows(): if env_var_helper.set_name("APPDATA").exists(): config_directory = env_var_helper.get_value() else: config_directory = directory_helper.get_current( with_end_sep=True) else: config_directory = directory_helper.get_current(with_end_sep=True) if not config_directory.endswith(os.sep): config_directory += os.sep config_directory += project_name + os.sep if not directory_helper.set_path(config_directory).exists(): directory_helper.create() if not config_directory.endswith(os.sep): config_directory += os.sep return config_directory
def test_run(self) -> None: """ Tests the method which let us run a command. """ if PlatformUtility.is_unix(): expected = ["Hello, World!"] actual = list(CommandHelper("echo 'Hello, World!'").run()) self.assertEqual(expected, actual[:1])
def test_execute(self) -> None: """ Tests the method which executes a command. """ if PlatformUtility.is_unix(): given = "echo 'Hello, World!'" expected = "Hello, World!\n" actual = self.helper.set_command(given).execute() self.assertEqual(expected, actual)
def test_execute_empty_output(self) -> None: """ Tests the method which executes a command for the case that the given command is empty. """ if PlatformUtility.is_unix(): expected = "" actual = self.helper.set_command("printf ''").execute() self.assertEqual(expected, actual)
def test_is_cygwin_with_version(self): """ Tests the method which let us know if the current platform is the CygWin one for the case that it's given with a certain version. """ with self.system_platform_mock as platform_patch: platform_patch.return_value = "Cygwin-NT-50.0.1" expected = True actual = PlatformUtility.is_cygwin() self.assertEqual(expected, actual)
def test_is_not_mac_os(self): """ Tests the method which let us know if the current platform is the MacOS one for that case that it's the Windows one. """ with self.system_platform_mock as platform_patch: platform_patch.return_value = "Windows" expected = False actual = PlatformUtility.is_mac_os() self.assertEqual(expected, actual)
def test_is_cygwin(self): """ Tests the method which let us know if the current platform is the CygWin one. """ with self.system_platform_mock as platform_patch: platform_patch.return_value = "Cygwin" expected = True actual = PlatformUtility.is_cygwin() self.assertEqual(expected, actual)
def test_run_to_stdout(self) -> None: """ Tests the method which let us run a command an output to stdout. """ if PlatformUtility.is_unix(): expected = "Hello, World!\n" CommandHelper("echo 'Hello, World!'").run_to_stdout() actual = sys.stdout.getvalue() self.assertEqual(expected, actual)
def test_is_not_cygwin(self): """ Tests the method which let us know if the current platform is the CygWin one for the case it's no cygwin version. """ with self.system_platform_mock as platform_patch: platform_patch.return_value = "Windows" expected = False actual = PlatformUtility.is_cygwin() self.assertEqual(expected, actual)
def test_execute_error(self, communicate_patch) -> None: """ Tests the method which executes a command for the case that an error is produced by the command. """ if PlatformUtility.is_unix(): expected = "This is an error." communicate_patch.return_value = (b"", b"This is an error.") given = "echo 'This is an error.' 1>&2" actual = CommandHelper(given).execute() self.assertEqual(expected, actual)
def test_join_path(self) -> None: """ Tests the method which let us join paths. """ given = "/hello/world" if PlatformUtility.is_windows(): expected = "/hello/world\\hello\\world" else: expected = "/hello/world/hello/world" actual = FileHelper(given).join_path("hello", "world") self.assertEqual(expected, actual)
def test_execute_error_exception(self, communicate_patch) -> None: """ Tests the method which executes a command for the case that an error is produced by the command and the end-user want an exception when such cases happen. """ if PlatformUtility.is_unix(): communicate_patch.return_value = (b"", b"This is an error.") given = "echo 'This is an error.' 1>&2" self.helper.set_command(given) self.assertRaises( RuntimeError, lambda: self.helper.execute(raise_on_error=True) )
def test_get_current_with_sep( self, getcwd_path: unittest.mock.MagicMock) -> None: """ Tests the method which let us get the current directory for the case that we want to have the directory separator at the end. """ getcwd_path.return_value = "/hello/world" if PlatformUtility.is_windows(): expected = "/hello/world\\" else: expected = "/hello/world/" actual = self.helper.get_current(with_end_sep=True) self.assertEqual(expected, actual)
def generate_splitted_status_file(self) -> "StatusFileGenerator": """ Generates the splitted status file. """ self.file_printer.destination = os.path.join( self.get_output_basedir(), PyFunceble.cli.storage.OUTPUTS.splitted.directory, self.status.status.upper(), ) if not PlatformUtility.is_unix(): self.file_printer.destination += ".txt" self.file_printer.template_to_use = "all" self.file_printer.dataset = self.status.to_dict() self.file_printer.print_interpolated_line() return self
def get_backup_data(self) -> dict: """ Stores the backup at the current destination. """ data = DictHelper().from_json_file(self.source_file) if PlatformUtility.is_windows(): result = {} for directory, files in data.items(): result[os.path.normpath(directory)] = files PyFunceble.facility.Logger.debug("Backup (read) data:\n%r", result) return result PyFunceble.facility.Logger.debug("Backup (read) data:\n%r", data) return data
# to the producer queue. producer_process_manager.add_to_input_queue( (communication_dataset, test_result)) # We are now done, it's time to send the stop signal. # The stop signal will inform thhe producer thread that it needs to stop # listening to new order (from the time it reads the stop signal). producer_process_manager.send_stop_signal(worker_name="main") # Now we wait until it's done. producer_process_manager.wait() # From here all files were generated we can do whatever we want with them. unix_path = os.path.join(dir_structure_restoration.get_output_basedir(), "domains", "ACTIVE", "list") win_path = f"{unix_path}.txt" path_to_use = unix_path if PlatformUtility.is_unix() else win_path if os.path.isfile(path_to_use): print(f"{colorama.Style.BRIGHT}{colorama.Fore.GREEN}All right, " "files correctly generated!") sys.exit(0) else: print( f"{colorama.Style.BRIGHT}{colorama.Fore.RED}Something went wrong, " "files not correctly generated!") sys.exit(1)
See the License for the specific language governing permissions and limitations under the License. """ import os import sys from typing import List, Optional import colorama from box import Box import PyFunceble.cli.storage_facility from PyFunceble.helpers.merge import Merge from PyFunceble.utils.platform import PlatformUtility if PlatformUtility.is_unix() and sys.stdin.encoding == "utf-8": STD_EPILOG: str = ( f"Crafted with {colorama.Fore.RED}♥{colorama.Fore.RESET} by " f"{colorama.Style.BRIGHT}{colorama.Fore.CYAN}Nissar Chababy (@funilrys)" f"{colorama.Style.RESET_ALL} " f"with the help of\n{colorama.Style.BRIGHT}{colorama.Fore.GREEN}" f"https://git.io/JkUPS{colorama.Style.RESET_ALL} " f"&& {colorama.Style.BRIGHT}{colorama.Fore.GREEN}" f"https://git.io/JkUPF{colorama.Style.RESET_ALL}") ASCII_PYFUNCEBLE = """ ██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗ ██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝ ██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗ ██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝ ██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗