21"""Copy SFTs between directories. The destination directory is organised
22following the convention detailed in the SFT spec (T040164)."""
29from contextlib
import contextmanager
30from concurrent.futures
import ProcessPoolExecutor, as_completed
33from lal
import LALERRORBIT, LALWARNINGBIT, LALINFOBIT, LALTRACEBIT
34from lal
import GetDebugLevel, ClobberDebugLevel
36from lalpulsar
import git_version
37from lalpulsar
import ValidateSFTFile, SFTErrorMessage
41__author__ =
"Karl Wette <karl.wette@ligo.org>"
42__version__ = git_version.id
43__date__ = git_version.date
48 saveDebugLevel = GetDebugLevel()
49 silentDebugLevel = saveDebugLevel & ~(
50 LALERRORBIT | LALWARNINGBIT | LALINFOBIT | LALTRACEBIT
52 ClobberDebugLevel(silentDebugLevel)
56 ClobberDebugLevel(saveDebugLevel)
61 parser = argparse.ArgumentParser(description=__doc__)
63 "-p",
"--processes", type=int, default=1, help=
"number of copying processes"
70 help=
"do not validate destination SFTs",
76 help=
"write README.md in the destination directory",
78 parser.add_argument(
"source_directory", type=str, help=
"SFT source directory")
79 parser.add_argument(
"dest_directory", type=str, help=
"SFT destination directory")
80 args = parser.parse_args()
83 if args.processes <= 0:
84 parser.error(
"--processes must be strictly positive")
85 if not os.path.isdir(args.source_directory):
86 parser.error(
"source_directory is not a directory")
87 if not os.path.isdir(args.dest_directory):
88 parser.error(
"dest_directory is not a directory")
101 print_progress_step = 100
102 print_progress_max = 1000
103 for src_root, _, src_files
in os.walk(source_directory):
104 for src_file
in src_files:
105 if src_file.endswith(
".sft"):
106 src_path = os.path.join(src_root, src_file)
107 _, src_name = os.path.split(src_path)
111 dest_path = os.path.join(dest_dir, src_name)
114 dest_dirs.add(dest_dir)
115 src_dest_paths.append((src_path, dest_path))
119 if num_SFTs % print_progress == 0:
120 dt = time.time() - t0
122 f
"{__file__}: found {num_SFTs} SFTs in {dt:0.1f} seconds",
125 print_progress += print_progress_step
126 if print_progress == print_progress_max:
127 print_progress_step *= 10
128 print_progress_max *= 10
130 print(f
"{__file__}: found {num_SFTs} SFTs\n", flush=
True)
132 return dest_dirs, src_dest_paths
137 print(f
"{__file__}: making {len(dest_dirs)} directories ...", flush=
True)
138 for dest_dir
in dest_dirs:
139 if not os.path.isdir(dest_dir):
140 os.makedirs(dest_dir)
141 print(f
"{__file__}: making {len(dest_dirs)} directories ... done\n", flush=
True)
146 tmp_dest_path = dest_path +
"_TO_BE_VALIDATED"
147 shutil.copyfile(src_path, tmp_dest_path)
153 if validate_errorcode != 0:
155 return (tmp_dest_path, validate_errorstr)
158 os.rename(tmp_dest_path, dest_path)
167 print(f
"{__file__}: copying {len(src_dest_paths)} SFTs ...", flush=
True)
168 with ProcessPoolExecutor(max_workers=args.processes)
as executor:
171 executor.submit(copy_SFT_file, src_path, dest_path, validate)
172 for src_path, dest_path
in src_dest_paths
176 for task
in tqdm(as_completed(pool), total=len(pool)):
177 validate_error = task.result()
178 if validate_error
is not None:
179 validate_errors.append(validate_error)
186 f
"{__file__}: failed to validate {len(validate_errors)} SFTs after copying:",
189 for tmp_dest_path, validate_errorstr
in validate_errors:
190 print(f
" {tmp_dest_path}\n {validate_errorstr}", flush=
True)
193 print(f
"{__file__}: copying {len(src_dest_paths)} SFTs ... done\n", flush=
True)
198 with open(os.path.join(dest_directory,
"README.md"),
"w")
as f:
202if __name__ ==
"__main__":
206 args.source_directory, args.dest_directory
216 print(f
"{__file__}: DONE", flush=
True)
const char * SFTErrorMessage(int errorcode)
int ValidateSFTFile(const char *fname)
Verify that the contents of a SFT file are valid.
def public_sft_directory_readme_md()
def public_sft_directory(filename)
def copy_all_SFT_files(src_dest_paths, validate, processes)
def silence_xlal_error_messages()
def copy_SFT_file(src_path, dest_path, validate)
def find_SFT_files(source_directory, dest_directory)
def write_readme_md(dest_directory)
def make_dest_dirs(dest_dirs)