# pyright: reportWildcardImportFromLibrary=false from behave import * # noqa: F403 import os from pathlib import Path from typing import OrderedDict import stat try: from steps.common import ThumbValidator except ModuleNotFoundError: from common import ThumbValidator try: from steps.common import Case except ModuleNotFoundError: from common import Case @given(u'a file {file} with the content') # noqa: F405 def step_given_file(context, file: str): file = file.strip(" `'\"") case: Case = context.case relative_file_path = Path(file) absolute_file_path = Path(case.working_dir).joinpath(relative_file_path) relative_dir_path = relative_file_path.parent absolute_dir_path = Path(case.working_dir).joinpath(relative_dir_path) os.makedirs(absolute_dir_path, exist_ok=True) assert not absolute_file_path.exists() var_dict = { "asset-dir": case.asset_dir } with open(absolute_file_path, "w") as f: f.write(context.text.format(**var_dict)) @then(u'the file {file} exists') # noqa: F405 def step_file_exists(context, file): file = file.strip(" `'\"") case: Case = context.case file_path = Path(case.working_dir).joinpath(file) assert file_path.exists(), "File {} should exist but it does not.".format(file_path) assert file_path.is_file() @then(u'the file {file} does not exist') # noqa: F405 def step_file_does_not_exist(context, file): file = file.strip(" `'\"") case: Case = context.case file_path = Path(case.working_dir).joinpath(file) assert not file_path.exists(), "File {} should not exist but it does.".format(file_path) def _assert_permissions_of_nodes( root_dir: str, check_directories: bool, expected_permission ): """ For a given root directory check _either_ all directories _or_ all files beneath (recursively) to have a certain permission set. """ base = Path(root_dir) assert os.path.exists(base) deviations = OrderedDict() for child_name in base.glob('**/*'): child_path = os.path.join(base, child_name) if os.path.isdir(child_path) == check_directories: actual_permisson = stat.S_IMODE(os.stat(child_path).st_mode) if actual_permisson != expected_permission: deviations[child_path] = actual_permisson assert len(deviations) == 0, \ "Not all {} have the expected permission {}. Deviations:\n{}".format( "directories" if check_directories else "files", oct(expected_permission), "\n".join([" {}: {}".format(k, oct(v)) for k, v in deviations.items()]) ) @then(u'the cache directory and all sub-directories have permissions 700') # noqa: F405 def step_directories_with_permission_700_exist(context): case: Case = context.case _assert_permissions_of_nodes( root_dir = case.cache_dir, check_directories = True, expected_permission = 0o700 ) @then(u'all the files within the thumb-directories have permissions 600') # noqa: F405 def step_files_have_permission_600(context): case: Case = context.case _assert_permissions_of_nodes( root_dir = case.cache_dir, check_directories = False, expected_permission = 0o600 )