Skip to content

Commit 38cba0c

Browse files
committed
Update plough.py
1 parent 1c1e0a5 commit 38cba0c

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/pinefarm/external/plough.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
'''
2+
Download grids + convert them to pineappl format
3+
'''
4+
15
from . import interface
26
from .. import table
37
import requests
8+
import urllib.request
49
import shutil
510
import tarfile
611
import subprocess
712
import os
813
import pineappl
914

10-
'''
11-
Download grids + convert them to pineappl format
12-
'''
15+
PLOUGHSHARE_LINK_FILENAME = "ploughshare_link.txt"
16+
GRIDS_PROCESSOR = "process_grids.sh"
17+
GRIDS_TMP = "grids"
18+
1319

1420
class Plough(interface.External):
1521

1622
def __init__(self, pinecard, theorycard, *args, **kwargs):
1723
super().__init__(pinecard, theorycard, *args, **kwargs)
18-
self.ps_link = self.source/"ploughshare_link.txt"
19-
self.link = self.ps_link.read_text()
24+
self.ps_link = self.source/PLOUGHSHARE_LINK_FILENAME
25+
with open(self.ps_link) as ps_link:
26+
self.link = ps_link.readline()
2027

2128
self.filename = self.link.rsplit('/')[-1]
22-
self.foldername = self.filename.rsplit('.', 1)[0]
29+
self.dir_name = self.filename.rsplit('.', 1)[0]
2330
self.tarball = self.dest/self.filename
24-
self.processor = self.source/"process_grids.sh"
31+
self.processor = self.source/GRIDS_PROCESSOR
2532
self.run()
2633
self.generate_pineappl()
2734
self.timestamp = 0
@@ -35,7 +42,7 @@ def run(self):
3542
print(f"Grids successfully downloaded to {self.tarball}")
3643
print("Extracting files...")
3744
self.extract_tarball()
38-
print(f"Grids successfully extracted to {self.foldername}")
45+
print(f"Grids successfully extracted to {self.dir_name}")
3946

4047
def results(self):
4148
pass
@@ -46,9 +53,9 @@ def collect_versions(self):
4653
def generate_pineappl(self):
4754
print("Grid conversion started...")
4855
# the grids are converted and processed here
49-
os.environ["PS_DIR"] = str(self.gridsfolder)
50-
# note that filename is also foldername
51-
os.environ["FILENAME"] = str(self.foldername)
56+
os.environ["PS_DIR"] = str(self.grids_dir)
57+
# note that filename is also dir_name
58+
os.environ["FILENAME"] = str(self.dir_name)
5259
if os.access(self.processor, os.X_OK):
5360
shutil.copy2(self.processor, self.dest)
5461
subprocess.run("./process_grids.sh", cwd=self.dest, check=True)
@@ -63,17 +70,12 @@ def download_to_dest(self):
6370
'''
6471
Download the file and move it to the output folder
6572
'''
66-
with requests.get(self.link, stream=True) as r:
67-
r.raise_for_status()
68-
with (self.dest/self.filename).open("wb") as f:
69-
for chunk in r.iter_content(chunk_size=1024*1024):
70-
if chunk:
71-
f.write(chunk)
73+
urllib.request.urlretrieve(self.link, self.dest/self.filename)
7274

7375
def extract_tarball(self):
7476
'''
7577
extract the contents
7678
'''
7779
with tarfile.open(self.tarball, "r:*") as tf:
7880
tf.extractall(self.dest)
79-
self.gridsfolder = self.dest/self.foldername/"grids"
81+
self.grids_dir = self.dest/self.dir_name/GRIDS_TMP

0 commit comments

Comments
 (0)