ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file util.py
1 from fs.sshfs import SSHFS
2 import os
3 import contextlib
4 import sys
5 import gitlab as gitlab_api
6 import smtplib, ssl
7 
8 try:
9  from halo import Halo
10 except:
11  Halo = None
12 
13 
14 def get_lxplus_fs(args):
15  assert args.user is not None, "Need username for lxplus"
16  assert args.pwd is not None, "Neew password for lxplus"
17  with Spinner(text="Connecting to lxplus", persist=False):
18  return SSHFS(
19  host="lxplus.cern.ch",
20  user=args.user,
21  passwd=args.pwd,
22  allow_agent=False,
23  look_for_keys=False,
24  )
25 
26 
27 def def_arguments(p, acc=False, gl=False):
28  if acc:
29  p.add_argument("--user", default="atsjenkins")
30  p.add_argument("--pwd", default=os.getenv("ATSJENKINS_PASSWORD", None))
31  if gl:
32  p.add_argument(
33  "--gitlab-access-token",
34  "-t",
35  default=os.getenv("ATSJENKINS_ACCESS_TOKEN", None),
36  help="GitLab access token to authenticate with the GitLab API",
37  )
38  return p
39 
40 
41 def gitlab(args):
42  assert args.gitlab_access_token is not None, "Need gitlab access token"
43  gl = gitlab_api.Gitlab(
44  "https://gitlab.cern.ch", private_token=args.gitlab_access_token
45  )
46  gl.auth()
47  return gl
48 
49 
50 @contextlib.contextmanager
51 def smtp(args):
52  server = "smtp.cern.ch"
53  port = 587 # For SSL
54  username = "atsjenkins@cern.ch"
55  try:
56  context = ssl.create_default_context()
57  server = smtplib.SMTP(server, port)
58  server.ehlo() # Can be omitted
59  server.starttls(context=context) # Secure the connection
60  server.ehlo() # Can be omitted
61  server.login(args.user, args.pwd)
62 
63  yield server
64  finally:
65  server.quit()
66 
67 
68 @contextlib.contextmanager
69 def Spinner(text, persist=True, *args, **kwargs):
70  stream = kwargs.get("stream", sys.stdout)
71  if stream.isatty() and Halo is not None:
72  spinner = Halo(text, *args, **kwargs)
73  spinner.start()
74  try:
75  yield
76  if persist:
77  spinner.succeed()
78  except:
79  if persist:
80  spinner.fail()
81  raise
82  finally:
83  if not persist:
84  spinner.stop()
85  else:
86  stream.write(text + "\n")
87  yield