Skip to content

Commit a46f03f

Browse files
committed
improvements suggested by @scarlehoff
1 parent 75ce0da commit a46f03f

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/pinefarm/external/interface.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def __init__(
4242
runcards_path=None,
4343
output_folder=None,
4444
print_comparison=True,
45-
run_without_grids=False,
45+
postrun_without_grids=False,
4646
):
4747
self.name = name
4848
self.theory = theory
4949
self.pdf = pdf
5050
self.timestamp = timestamp
5151
self._print_comparison = print_comparison
52-
self._run_without_grids = run_without_grids
52+
self._postrun_without_grids = postrun_without_grids
5353
if runcards_path is None:
5454
self._runcards_path = configs.configs["paths"]["runcards"]
5555
else:
@@ -189,7 +189,7 @@ def postprocess(self):
189189
else:
190190
grids = list(self.dest.glob("*.pineappl*"))
191191

192-
if not grids and not self._run_without_grids:
192+
if not grids and not self._postrun_without_grids:
193193
raise ValueError("Tried to run postprocessing in a folder with no grids?")
194194

195195
os.environ["PINECARD"] = self.source.as_posix()

src/pinefarm/external/plough.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,34 @@
1616

1717

1818
class Plough(interface.External):
19-
"""Interface provider."""
19+
"""Interface to download grids directly from ploughshare."""
2020

2121
def __init__(self, pinecard, theorycard, *args, **kwargs):
22-
super().__init__(pinecard, theorycard, *args, **kwargs)
22+
super().__init__(
23+
pinecard,
24+
theorycard,
25+
*args,
26+
print_comparison=False,
27+
postrun_without_grids=True,
28+
**kwargs,
29+
)
2330
self.ps_link = self.source / PLOUGHSHARE_LINK_FILENAME
2431
self.link = self.ps_link.read_text()
2532

2633
self.filename = self.link.rsplit("/")[-1]
2734
self.dir_name = self.filename.rsplit(".", 1)[0]
2835
self.tarball = self.dest / self.filename
29-
self._print_comparison = False
30-
self._run_without_grids = True
3136

3237
def run(self):
3338
"""Download and extract the .tgz file."""
3439
print("Downloading from ploughshare...")
35-
try:
36-
self.download_to_dest()
37-
if self.tarball.exists():
38-
print(f"Grids successfully downloaded to {self.tarball}")
39-
else:
40-
raise FileNotFoundError(
41-
f"{self.tarball} not found but the download didn't seem to fail?"
42-
)
43-
except Exception as e:
44-
raise FileNotFoundError(f"{self.tarball} could not be downloaded!") from e
40+
self.download_to_dest()
4541
print("Extracting files...")
4642
self.extract_tarball()
4743
print(f"Grids successfully extracted to {self.dest}")
4844

4945
def results(self):
50-
"""Results are collected and compared at the pineappl (script) level."""
46+
"""Do nothing."""
5147
pass
5248

5349
def collect_versions(self):
@@ -60,16 +56,26 @@ def generate_pineappl(self):
6056

6157
def download_to_dest(self):
6258
"""Download the file to the output folder."""
63-
urllib.request.urlretrieve(self.link, self.dest / self.filename)
59+
try:
60+
urllib.request.urlretrieve(self.link, self.dest / self.filename)
61+
if self.tarball.exists():
62+
print(f"Grids successfully downloaded to {self.tarball}")
63+
else:
64+
raise FileNotFoundError(
65+
f"{self.tarball} not found but the download didn't seem to fail?"
66+
)
67+
except Exception as e:
68+
raise FileNotFoundError(f"{self.tarball} could not be downloaded!") from e
6469

6570
def extract_tarball(self):
6671
"""Extract the contents."""
6772
with tarfile.open(self.tarball, "r:*") as tf:
6873
tf.extractall(self.dest)
6974
self.grids_dir = self.dest / self.dir_name / GRIDS_TMP
7075
grids_list = sorted(os.listdir(self.grids_dir))
71-
for i, grid in enumerate(grids_list):
72-
extension = grid.split(".", 2)[2]
73-
os.rename(self.grids_dir / grid, self.dest / f"grid_{i}.{extension}")
76+
for grid in grids_list:
77+
grid_num, extension = grid.split(".", 2)[1:]
78+
grid_num = grid_num[-3:]
79+
os.rename(self.grids_dir / grid, self.dest / f"grid_{grid_num}.{extension}")
7480
shutil.rmtree(self.dest / self.dir_name)
7581
self.tarball.unlink()

0 commit comments

Comments
 (0)