Skip to content

Instantly share code, notes, and snippets.

@geant44
Last active January 10, 2020 09:55
Show Gist options
  • Save geant44/9d08c07c5b5d87b0507a7fd86ef99d86 to your computer and use it in GitHub Desktop.
Save geant44/9d08c07c5b5d87b0507a7fd86ef99d86 to your computer and use it in GitHub Desktop.
Geant4Dataset
import threading
import requests
from pathlib import Path
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("pos1", help="Geant4DatasetDefinitions.cmake file", default="cmake/Modules/Geant4DatasetDefinitions.cmake")
parser.add_argument("-o", "--output-dir", help="output dir", dest="opd", default="./data")
args = parser.parse_args()
Geant4Dataset={}
downloaddir=Path(args.opd)
if not downloaddir.exists():
downloaddir.mkdir()
def download(link, filelocation):
#r = requests.get(link, stream=True)
with requests.get(link, stream=True) as r:
if r.status_code == 200 :
with open(filelocation, 'wb') as f:
f.write(r.raw.read())
else:
print('network error')
def createNewDownloadThread(link, filelocation):
download_thread = threading.Thread(target=download, args=(link,filelocation))
download_thread.start()
if Path(args.pos1).exists():
cmakedatafile = Path(args.pos1)
else:
print('{} is not found.'.format(args.pos1))
with open(cmakedatafile,'r',newline='') as cs:
t=cs.readlines()
for i in range(len(t)):
if 'geant4_add_dataset' in t[i]:
Name=t[i+1].split(' ')[2].replace('\n','')
Version=t[i+2].split(' ')[5].replace('\n','')
filename=t[i+3].split(' ')[2].replace('\n','')
Ext=t[i+4].split(' ')[3].replace('\n','')
print("Name is {}, version {}".format(Name,Version),
"filename is {}.{}".format(filename,Ext)
)
Geant4Dataset[Name]=filename+'.'+Version+'.'+Ext
for Name in Geant4Dataset:
file = downloaddir.joinpath(Geant4Dataset[Name])
print("{}".format(file))
if not file.exists():
createNewDownloadThread("http://geant4-data.web.cern.ch/geant4-data/datasets/"+Geant4Dataset[Name], file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment