From 3059c46b2d14f7f1002de8ab476adcfa520b0b35 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 20 Jun 2024 09:27:03 -0500 Subject: Check for special files that might share names/extensions Check for special files and hidden files that might be inadvertently added to the zip bundle by the operating system in use that might share names and/or extensions with the main bundle files. --- r_qtl/r_qtl2.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index 87491d0..0a96e7c 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -17,11 +17,26 @@ FILE_TYPES = ( "geno", "founder_geno", "pheno", "covar", "phenocovar", "gmap", "pmap", "phenose") + +def __special_file__(filename): + """ + Check whether the file is special in some ways, e.g. MacOSX seems to include + files in a directory `__MACOSX` that share parts of the name, and extensions + with the main files in the bundle. + """ + is_macosx_special_file = filename.startswith("__MACOSX") + is_nix_hidden_file = Path(filename).name.startswith(".") + + return (is_macosx_special_file or is_nix_hidden_file) + + def control_data(zfile: ZipFile) -> dict: """Retrieve the control file from the zip file info.""" files = tuple(filename for filename in zfile.namelist() - if (filename.endswith(".yaml") or filename.endswith(".json"))) + if (not __special_file__(filename) + and (filename.endswith(".yaml") + or filename.endswith(".json")))) num_files = len(files) if num_files == 0: raise InvalidFormat("Expected a json or yaml control file.") -- cgit v1.2.3