O2-DQ User Interface 1.0.0
Loading...
Searching...
No Matches
DownloadLibs.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# PYTHON_ARGCOMPLETE_OK
3# -*- coding: utf-8 -*-
4
5# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
6# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
7# All rights not expressly granted are reserved.
8#
9# This software is distributed under the terms of the GNU General Public
10# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
11#
12# In applying this license CERN does not waive the privileges and immunities
13# granted to it by virtue of its status as an Intergovernmental Organization
14# or submit itself to any jurisdiction.
15# \Author: ionut.cristian.arsene@cern.ch
16# \Interface: cevat.batuhan.tolon@cern.ch
17
18import urllib.request
19from urllib.request import Request, urlopen
20import os
21import sys
22import argparse
23import ssl
24import logging
25import logging.config
26import shutil
27"""
28argcomplete - Bash tab completion for argparse
29Documentation https://kislyuk.github.io/argcomplete/
30Instalation Steps
31pip install argcomplete
32sudo activate-global-python-argcomplete
33Only Works On Local not in O2
34Activate libraries in below and activate #argcomplete.autocomplete(parser) line
35"""
36import argcomplete
37
38# This script provides download to DQ libraries from O2Physics-DQ Manually with/without Production tag or get DQ libraries from alice-software in local machine
39
40# header for github download
41headers = {
42 "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36"
43 }
44
45parser = argparse.ArgumentParser(description = "Arguments to pass")
46parser.add_argument(
47 "--version", help = "Online: Your Production tag for O2Physics example: for nightly-20220619, just enter as 20220619", action = "store",
48 type = str.lower,
49 )
50parser.add_argument(
51 "--debug", help = "Online and Local: execute with debug options", action = "store",
52 choices = ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default = "DEBUG", type = str.upper,
53 )
54parser.add_argument(
55 "--local",
56 help = "Local: Use Local Paths for getting DQ Libraries instead of online github download. If you are working LXPLUS, It will not working so don't configure with option",
57 action = "store_true",
58 )
59parser.add_argument(
60 "--localPath",
61 help = "Local: Configure your alice software folder name in your local home path (prefix: home/<user>). Default is home/<user>/alice. Example different configuration is --localpath alice-software --local --> home/<user>/alice-software",
62 action = "store", type = str
63 )
64
65argcomplete.autocomplete(parser)
66extrargs = parser.parse_args()
67
68MY_PATH = os.path.abspath(os.getcwd())
69HOME_PATH = os.environ["HOME"]
70
71ALICE_SOFTWARE_PATH = os.environ["HOME"] + "/alice"
72
73localPathCutsLibrary = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/CutsLibrary.h"
74localPathMCSignalsLibrary = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/MCSignalLibrary.h"
75localPathEventMixing = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/MixingLibrary.h"
76
77URL_CUTS_LIBRARY = ("https://github.com/AliceO2Group/O2Physics/blob/master/PWGDQ/Core/CutsLibrary.h?raw=true")
78URL_MCSIGNALS_LIBRARY = ("https://github.com/AliceO2Group/O2Physics/blob/master/PWGDQ/Core/MCSignalLibrary.h?raw=true")
79URL_MIXING_LIBRARY = ("https://github.com/AliceO2Group/O2Physics/blob/master/PWGDQ/Core/MixingLibrary.h?raw=true")
80
81isLibsExist = True
82
83if extrargs.version is not None:
84 prefix_version = "nightly-"
85 extrargs.version = prefix_version + extrargs.version
86
87if extrargs.debug:
88 DEBUG_SELECTION = extrargs.debug
89 numeric_level = getattr(logging, DEBUG_SELECTION.upper(), None)
90 if not isinstance(numeric_level, int):
91 raise ValueError("Invalid log level: %s" % DEBUG_SELECTION)
92 logging.basicConfig(format = "[%(levelname)s] %(message)s", level = DEBUG_SELECTION)
93
94if extrargs.version and extrargs.local is False:
95 logging.info("DQ libs will downloaded from github. Your Version For Downloading DQ Libs From Github : %s", extrargs.version,)
96
97 URL_CUTS_LIBRARY = ("https://github.com/AliceO2Group/O2Physics/blob/" + extrargs.version + "/PWGDQ/Core/CutsLibrary.h?raw=true")
98 URL_MCSIGNALS_LIBRARY = (
99 "https://github.com/AliceO2Group/O2Physics/blob/" + extrargs.version + "/PWGDQ/Core/MCSignalLibrary.h?raw=true"
100 )
101 URL_MIXING_LIBRARY = ("https://github.com/AliceO2Group/O2Physics/blob/" + extrargs.version + "/PWGDQ/Core/MixingLibrary.h?raw=true")
102
103if extrargs.local and extrargs.version:
104 logging.warning(
105 "Your provided configuration for getting DQ libs in locally. You don't need to configure your github nightly version. It's for Online Downloading"
106 )
107 logging.warning("%s nightly version will not used in interface. Local working on going", extrargs.version)
108
109if extrargs.localPath and extrargs.local:
110 ALICE_SOFTWARE_PATH = os.environ["HOME"] + "/" + extrargs.localPath
111 logging.info("Alice software Local Path is Changed. New Local Path is %s", ALICE_SOFTWARE_PATH)
112 if os.path.isdir(ALICE_SOFTWARE_PATH) is True:
113 logging.info("Alice software found at %s local Path change is true", ALICE_SOFTWARE_PATH)
114 else:
115 logging.error("Alice software not found in path!!! Fatal Error. Check your Alice software path configuration")
116 sys.exit()
117
118if extrargs.localPath and extrargs.local is False:
119 logging.error(
120 "Misconfiguration. You forget to add --local option interface for working localy. You need add this parameter to workflow"
121 )
122 logging.info("Example usage: python3 DownloadLibs.py --local --localPath alice")
123 sys.exit()
124
125if extrargs.local:
126 if extrargs.localPath is None:
127 logging.info("Default Path will used for Alice Software. Default Path : %s", ALICE_SOFTWARE_PATH)
128 if os.path.isdir(ALICE_SOFTWARE_PATH) is True:
129 logging.info("Default Path: %s is Valid", ALICE_SOFTWARE_PATH)
130 elif os.path.isdir(ALICE_SOFTWARE_PATH) is False:
131 logging.error("Default Path: %s is invalid!! Fatal Error. Check your Alice software path configuration", ALICE_SOFTWARE_PATH,)
132 sys.exit()
133
134 logging.info("DQ libs will be getting from local folders. You alice software path : %s", ALICE_SOFTWARE_PATH,)
135
136 localPathCutsLibrary = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/CutsLibrary.h"
137 localPathMCSignalsLibrary = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/MCSignalLibrary.h"
138 localPathEventMixing = ALICE_SOFTWARE_PATH + "/O2Physics/PWGDQ/Core/MixingLibrary.h"
139
140 logging.info("Local CutsLibrary.h Path: %s ", localPathCutsLibrary)
141 logging.info("Local MCSignalsLibrary.h Path: %s ", localPathMCSignalsLibrary)
142 logging.info("Local MixingLibrary.h Path: %s ", localPathEventMixing)
143 try:
144 with open("tempCutsLibrary.h", "wb") as f:
145 shutil.copyfile(localPathCutsLibrary, MY_PATH + "/tempCutsLibrary.h")
146 if os.path.isfile("tempCutsLibrary.h") is True:
147 logging.info("tempCutsLibrary.h created at %s", MY_PATH)
148 else:
149 logging.error("tempCutsLibrary.h not created at %s Fatal Error", MY_PATH)
150 sys.exit()
151 except FileNotFoundError:
152 logging.error("%s not found in your provided alice-software path!!! Check your alice software path", localPathCutsLibrary,)
153 sys.exit()
154
155 try:
156 with open("tempMCSignalsLibrary.h", "wb") as f:
157 shutil.copyfile(localPathMCSignalsLibrary, MY_PATH + "/tempMCSignalsLibrary.h")
158 if os.path.isfile("tempMCSignalsLibrary.h") is True:
159 logging.info("tempMCSignalsLibrary.h created at %s", MY_PATH)
160 else:
161 logging.error("tempMCSignalsLibrary.h not created at %s Fatal Error", MY_PATH)
162 sys.exit()
163 except FileNotFoundError:
164 logging.error("%s not found in your provided alice-software path!!! Check your alice software path", localPathMCSignalsLibrary,)
165 sys.exit()
166
167 try:
168 with open("tempMixingLibrary.h", "wb") as f:
169 shutil.copyfile(localPathEventMixing, MY_PATH + "/tempMixingLibrary.h")
170 if os.path.isfile("tempMixingLibrary.h") is True:
171 logging.info("tempMixingLibrary.h created at %s", MY_PATH)
172 else:
173 logging.error("tempMixingLibrary.h not created at %s Fatal Error", MY_PATH)
174 sys.exit()
175 except FileNotFoundError:
176 logging.error("%s not found in your provided alice-software path!!! Check your alice software path", localPathEventMixing,)
177 sys.exit()
178
179 logging.info("DQ Libraries pulled from local alice software successfully!")
180 sys.exit()
181
182if extrargs.local is False:
183 if (
184 (os.path.isfile("tempCutsLibrary.h") is False) or (os.path.isfile("tempMCSignalsLibrary.h") is False) or
185 (os.path.isfile("tempMixingLibrary.h")) is False
186 ):
187 logging.info("Some Libs are Missing. All DQ libs will download")
188 logging.info("Github CutsLibrary.h Path: %s ", URL_CUTS_LIBRARY)
189 logging.info("Github MCSignalsLibrary.h Path: %s ", URL_MCSIGNALS_LIBRARY)
190 logging.info("Github MixingLibrary.h Path: %s ", URL_MIXING_LIBRARY)
191 isLibsExist = False
192 if extrargs.debug:
193 try:
194 context = ssl._create_unverified_context() # prevent ssl problems
195 request = urllib.request.urlopen(URL_CUTS_LIBRARY, context = context)
196 request = urllib.request.urlopen(URL_MCSIGNALS_LIBRARY, context = context)
197 request = urllib.request.urlopen(URL_MIXING_LIBRARY, context = context)
198 except urllib.error.HTTPError as error:
199 logging.error(error)
200 else:
201 # Dummy SSL Adder
202 context = ssl._create_unverified_context() # prevent ssl problems
203 request = urllib.request.urlopen(URL_CUTS_LIBRARY, context = context)
204
205 # HTTP Request
206 requestCutsLibrary = Request(URL_CUTS_LIBRARY, headers = headers)
207 requestMCSignalsLibrary = Request(URL_MCSIGNALS_LIBRARY, headers = headers)
208 requestMixingLibrary = Request(URL_MIXING_LIBRARY, headers = headers)
209
210 # Get Files With Http Requests
211 htmlCutsLibrary = urlopen(requestCutsLibrary, context = context).read()
212 htmlMCSignalsLibrary = urlopen(requestMCSignalsLibrary, context = context).read()
213 htmlMixingLibrary = urlopen(requestMixingLibrary, context = context).read()
214
215 with open("tempCutsLibrary.h", "wb") as f:
216 f.write(htmlCutsLibrary)
217 logging.info("tempCutsLibrary.h downloaded successfully from github")
218 with open("tempMCSignalsLibrary.h", "wb") as f:
219 f.write(htmlMCSignalsLibrary)
220 logging.info("tempMCSignalsLibrary.h downloaded successfully from github")
221 with open("tempMixingLibrary.h", "wb") as f:
222 f.write(htmlMixingLibrary)
223 logging.info("tempMixingLibrary.h downloaded successfully from github")
224
225 if isLibsExist:
226 logging.info("DQ Libraries have been downloaded before. If you want to update, delete they manually and run this script again.")
227 sys.exit()
228 else:
229 logging.info("DQ Libraries downloaded from github successfully!")
230sys.exit()