22
33import os
44import shutil
5- import subprocess
65import tarfile
76import urllib .request
87
1312from . import interface
1413
1514PLOUGHSHARE_LINK_FILENAME = "ploughshare_link.txt"
16- GRIDS_PROCESSOR = "process_grids.sh"
1715GRIDS_TMP = "grids"
1816
1917
@@ -28,19 +26,25 @@ def __init__(self, pinecard, theorycard, *args, **kwargs):
2826 self .filename = self .link .rsplit ("/" )[- 1 ]
2927 self .dir_name = self .filename .rsplit ("." , 1 )[0 ]
3028 self .tarball = self .dest / self .filename
31- self .processor = self .source / GRIDS_PROCESSOR
32- self .run ()
33- self .generate_pineappl ()
34- self .timestamp = 0
3529
3630 def run (self ):
3731 """Download and extract the .tgz file."""
3832 print ("Downloading from ploughshare..." )
39- self .download_to_dest ()
33+ try :
34+ self .download_to_dest ()
35+ if self .tarball .exists ():
36+ print (f"Grids successfully downloaded to { self .tarball } " )
37+ else :
38+ raise FileNotFoundError (
39+ f"{ self .tarball } not found but the download didn't seem to fail?"
40+ )
41+ except Exception as e :
42+ raise FileNotFoundError (f"{ self .tarball } could not be downloaded!" ) from e
4043 print (f"Grids successfully downloaded to { self .tarball } " )
4144 print ("Extracting files..." )
4245 self .extract_tarball ()
43- print (f"Grids successfully extracted to { self .dir_name } " )
46+ print (f"Grids successfully extracted to { self .dest } " )
47+ self .cleanup ()
4448
4549 def results (self ):
4650 """Results are collected and compared at the pineappl (script) level."""
@@ -51,30 +55,26 @@ def collect_versions(self):
5155 return {}
5256
5357 def generate_pineappl (self ):
54- """Converts donwloaded grids into pineappl format."""
55- print ("Grid conversion started..." )
56- # the grids are converted and processed here
57- os .environ ["PS_DIR" ] = str (self .grids_dir )
58- # note that filename is also dir_name
59- os .environ ["FILENAME" ] = str (self .dir_name )
60- if os .access (self .processor , os .X_OK ):
61- shutil .copy2 (self .processor , self .dest )
62- subprocess .run ("./process_grids.sh" , cwd = self .dest , check = True )
63- (self .dest / "process_grids.sh" ).unlink ()
64- else :
65- raise ValueError (
66- f"Grid conversion file present but not executable: { self .processor } "
67- )
68- self .grids = []
69- for g in self .dest .glob ("*.pineappl.lz4" ):
70- self .grids .append (g )
58+ """Grids are converted in postrun.sh."""
59+ return
7160
7261 def download_to_dest (self ):
73- """Download the file and move it to the output folder."""
62+ """Download the file to the output folder."""
7463 urllib .request .urlretrieve (self .link , self .dest / self .filename )
7564
7665 def extract_tarball (self ):
7766 """Extract the contents."""
7867 with tarfile .open (self .tarball , "r:*" ) as tf :
7968 tf .extractall (self .dest )
8069 self .grids_dir = self .dest / self .dir_name / GRIDS_TMP
70+ grids_list = sorted (os .listdir (self .grids_dir ))
71+ for i , grid in enumerate (grids_list ):
72+ extension = grid .split ("." , 2 )[2 ]
73+ print (extension )
74+ os .rename (self .grids_dir / grid , self .dest / f"grid_{ i } .{ extension } " )
75+
76+ def cleanup (self ):
77+ """Delete unnecessary files and create tmp.pineappl.lz4 to allow postprocessing."""
78+ shutil .rmtree (self .dest / self .dir_name )
79+ self .tarball .unlink ()
80+ open (self .dest / "tmp.pineappl.lz4" , "x" )
0 commit comments