|
| 1 | +#!/usr/bin/env python |
| 2 | +import pandas as pd |
| 3 | +import typer |
| 4 | +from pathlib import Path |
| 5 | +from typer import colors |
| 6 | +from typing import Annotated |
| 7 | + |
| 8 | + |
| 9 | +RED = colors.RED |
| 10 | +GREEN = colors.GREEN |
| 11 | + |
| 12 | +app = typer.Typer() |
| 13 | + |
| 14 | + |
| 15 | +def load_se_definition(se_def_path): |
| 16 | + return pd.read_csv(se_def_path, names=["seName", "basePath"], delimiter=";", index_col="seName") |
| 17 | + |
| 18 | + |
| 19 | +def load_dfc_dump(dfc_dump_path, version): |
| 20 | + fc_dump = pd.read_csv(dfc_dump_path, names=["seName", "lfn", "cks", "size"], delimiter="|") |
| 21 | + fc_dump["version"] = version |
| 22 | + return fc_dump |
| 23 | + |
| 24 | + |
| 25 | +def load_se_dump(se_dump_path): |
| 26 | + se_dump = pd.read_csv(se_dump_path, names=["pfn"], delimiter=";", index_col="pfn") |
| 27 | + se_dump["version"] = "se_dump" |
| 28 | + return se_dump |
| 29 | + |
| 30 | + |
| 31 | +@app.command() |
| 32 | +def possibly_lost_data( |
| 33 | + fc_dump_file: Annotated[Path, typer.Option(help="DFC dump AFTER the SE dump")], |
| 34 | + se_def_file: Annotated[Path, typer.Option(help="Definition of the SE path")], |
| 35 | + se_dump_file: Annotated[Path, typer.Option(help="Dump of the SE")], |
| 36 | + lost_file_output: Annotated[Path, typer.Option(help="Output file in which to dump lost")] = "lost.csv", |
| 37 | +): |
| 38 | + """ |
| 39 | + DANGER: make a partial comparison of an SE dump and an FC dump to find lost data |
| 40 | + Be careful because you can't trust the result: |
| 41 | + * if the FC dump is more recent than the SE dump, you may get files that were added on the SE after the dump |
| 42 | + * if the FC dump is older than the SE dump, the file may have been purposedly removed |
| 43 | + """ |
| 44 | + se_dump = load_se_dump(se_dump_file) |
| 45 | + se_def = load_se_definition(se_def_file) |
| 46 | + |
| 47 | + # Compute the PFN for each LFN in the DFC dump |
| 48 | + |
| 49 | + fc_dump = load_dfc_dump(fc_dump_file, "fc") |
| 50 | + fc_dump = pd.merge(fc_dump, se_def, on="seName") |
| 51 | + fc_dump["pfn"] = fc_dump["basePath"] + fc_dump["lfn"] |
| 52 | + fc_dump.set_index("pfn", inplace=True) |
| 53 | + |
| 54 | + # Lost files: in both FC dump but not in the SE |
| 55 | + |
| 56 | + lostData = fc_dump.index.difference(se_dump.index) |
| 57 | + if len(lostData): |
| 58 | + typer.secho(f"Found {len(lostData)} lost files, dumping them in {lost_file_output}", err=True, fg=RED) |
| 59 | + lastDataDetail = fc_dump[fc_dump.index.isin(lostData)] |
| 60 | + lastDataDetail.to_csv(lost_file_output) |
| 61 | + else: |
| 62 | + typer.secho("No dark data found", fg=GREEN) |
| 63 | + |
| 64 | + |
| 65 | +@app.command() |
| 66 | +def possibly_dark_data( |
| 67 | + fc_dump_file: Annotated[Path, typer.Option(help="DFC dump")], |
| 68 | + se_def_file: Annotated[Path, typer.Option(help="Definition of the SE path")], |
| 69 | + se_dump_file: Annotated[Path, typer.Option(help="Dump of the SE")], |
| 70 | + dark_file_output: Annotated[Path, typer.Option(help="Output file in which to dump dark data")] = "dark.csv", |
| 71 | +): |
| 72 | + """ |
| 73 | + DANGER: make a partial comparison of an SE dump and an FC dump to find dark data. |
| 74 | + Be careful because you can't trust the result: |
| 75 | + * if the FC dump is more recent than the SE dump, you may get files that were already removed |
| 76 | + * if the FC dump is older than the SE dump, you may find files that were added properly after the dump (DANGER) |
| 77 | + """ |
| 78 | + se_dump = load_se_dump(se_dump_file) |
| 79 | + se_def = load_se_definition(se_def_file) |
| 80 | + |
| 81 | + # Compute the PFN for each LFN in the DFC dump |
| 82 | + |
| 83 | + fc_dump = load_dfc_dump(fc_dump_file, "fc") |
| 84 | + fc_dump = pd.merge(fc_dump, se_def, on="seName") |
| 85 | + fc_dump["pfn"] = fc_dump["basePath"] + fc_dump["lfn"] |
| 86 | + fc_dump.set_index("pfn", inplace=True) |
| 87 | + |
| 88 | + # Dark data: in the SE dump but not in any of the FC dump |
| 89 | + |
| 90 | + typer.echo(f"Computing dark data") |
| 91 | + # Find the dark data |
| 92 | + darkData = se_dump.index.difference(fc_dump.index) |
| 93 | + |
| 94 | + if len(darkData): |
| 95 | + typer.secho(f"Found {len(darkData)} dark data, dumping them in {dark_file_output}", err=True, fg=RED) |
| 96 | + pd.DataFrame(index=darkData).to_csv(dark_file_output) |
| 97 | + else: |
| 98 | + typer.secho("No dark data found", fg=GREEN) |
| 99 | + |
| 100 | + |
| 101 | +@app.command() |
| 102 | +def threeway( |
| 103 | + old_fc_dump_file: Annotated[Path, typer.Option(help="DFC dump BEFORE the SE dump")], |
| 104 | + new_fc_dump_file: Annotated[Path, typer.Option(help="DFC dump AFTER the SE dump")], |
| 105 | + se_def_file: Annotated[Path, typer.Option(help="Definition of the SE path")], |
| 106 | + se_dump_file: Annotated[Path, typer.Option(help="Dump of the SE")], |
| 107 | + lost_file_output: Annotated[Path, typer.Option(help="Output file in which to dump lost files")] = "lost.csv", |
| 108 | + dark_file_output: Annotated[Path, typer.Option(help="Output file in which to dump dark data")] = "dark.csv", |
| 109 | +): |
| 110 | + """ |
| 111 | + Make a full comparison of two FC dump and one SE dump |
| 112 | + """ |
| 113 | + se_dump = load_se_dump(se_dump_file) |
| 114 | + se_def = load_se_definition(se_def_file) |
| 115 | + |
| 116 | + # Compute the PFN for each LFN in the DFC dump |
| 117 | + old_fc_dump = load_dfc_dump(old_fc_dump_file, "old_fc") |
| 118 | + old_fc_dump = pd.merge(old_fc_dump, se_def, on="seName") |
| 119 | + old_fc_dump["pfn"] = old_fc_dump["basePath"] + old_fc_dump["lfn"] |
| 120 | + old_fc_dump.set_index("pfn", inplace=True) |
| 121 | + |
| 122 | + new_fc_dump = load_dfc_dump(new_fc_dump_file, "new_fc") |
| 123 | + new_fc_dump = pd.merge(new_fc_dump, se_def, on="seName") |
| 124 | + new_fc_dump["pfn"] = new_fc_dump["basePath"] + new_fc_dump["lfn"] |
| 125 | + new_fc_dump.set_index("pfn", inplace=True) |
| 126 | + |
| 127 | + # Dark data: in the SE dump but not in any of the FC dump |
| 128 | + |
| 129 | + typer.echo(f"Computing dark data") |
| 130 | + # Find the dark data |
| 131 | + darkData = se_dump.index.difference(old_fc_dump.index.union(new_fc_dump.index)) |
| 132 | + |
| 133 | + if len(darkData): |
| 134 | + typer.secho(f"Found {len(darkData)} dark data, dumping them in {dark_file_output}", err=True, fg=RED) |
| 135 | + pd.DataFrame(index=darkData).to_csv(dark_file_output) |
| 136 | + else: |
| 137 | + typer.secho("No dark data found", fg=GREEN) |
| 138 | + |
| 139 | + # Lost files: in both FC dump but not in the SE |
| 140 | + |
| 141 | + lostData = (old_fc_dump.index.intersection(new_fc_dump.index)).difference(se_dump.index) |
| 142 | + if len(lostData): |
| 143 | + typer.secho(f"Found {len(lostData)} lost files, dumping them in {lost_file_output}", err=True, fg=RED) |
| 144 | + lastDataDetail = new_fc_dump[new_fc_dump.index.isin(lostData)] |
| 145 | + lastDataDetail.to_csv(lost_file_output) |
| 146 | + else: |
| 147 | + typer.secho("No dark data found", fg=GREEN) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + app() |
0 commit comments