forked from mflechl/ProductionFromNano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runUtils.py
107 lines (83 loc) · 3.14 KB
/
runUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import glob
import subprocess as sp
import shutil
import shlex
import json
import os
def main():
checkTokens()
# checkProxy()
prepareTokens()
useToken("hephy")
# getHeplxPublicFolder()
def checkTokens():
prepareTokens()
return True
def prepareTokens():
on_system = getSystem()
p = sp.Popen( shlex.split("klist"), stdout=sp.PIPE, stderr=sp.PIPE )
(out,err) = p.communicate()
for line in out.splitlines():
if "Ticket cache" in line:
token = line.split("FILE:")[1]
if not os.path.exists("kerberos"):
os.mkdir("kerberos")
shutil.copyfile(token, "kerberos/krb5_token_"+on_system )
if on_system == "cern.ch":
if os.path.exists("kerberos/username"):
with open("kerberos/username","r") as FSO:
user = FSO.read()
else:
user = raw_input("Give username for cell hephy.at: ")
save = raw_input("You want to save '{0}' as your username for later? (Y/n)".format(user))
if save != "n":
with open("kerberos/username","w") as FSO:
FSO.write(user)
p = sp.Popen( shlex.split("kinit {0}@HEPHY.AT -c kerberos/krb5_token_hephy.at".format(user)) )
p.communicate()
os.environ["KRB5CCNAME"] = "kerberos/krb5_token_hephy.at"
os.system("aklog -d hephy.at")
os.environ["KRB5CCNAME"] = "kerberos/krb5_token_cern.ch"
def useToken(cell):
if cell == "hephy":
os.environ["KRB5CCNAME"] = "kerberos/krb5_token_hephy.at"
# os.system("aklog -d hephy.at")
if cell == "cern":
os.environ["KRB5CCNAME"] = "kerberos/krb5_token_cern.ch"
# os.system("aklog -d cern.ch")
def checkProxy():
p = sp.Popen( shlex.split("voms-proxy-info"), stdout=sp.PIPE, stderr=sp.PIPE )
(out,err) = p.communicate()
if err:
print err
return False
info = {"time":-1,"path":""}
for line in out.splitlines():
if "timeleft" in line: info['time'] = int( line.replace(" ","").split("timeleft:")[1].replace(":","") )
if "path" in line: info["path"] = line.split(":")[1].replace(" ","")
if info['time'] > 0:
if not os.path.exists("proxy"):
os.mkdir("proxy")
shutil.copyfile(info["path"], "proxy/x509_proxy")
return True
else:
print "Proxy expired! Get a new one with 'voms-proxy-init --voms cms'"
return False
def getHeplxPublicFolder():
user = os.environ["USER"]
first = user[0]
if user == 'jaandrej' and getSystem()=="hephy.at":
second = 'jandrejkovic'
else :
second = user[:8]
return glob.glob( "/afs/{0}//user/{1}/{2}*/public".format(getSystem(),first,second) )[0]
def getSystem(inverse = False):
host = os.environ["HOSTNAME"]
if "heplx" in host:
if inverse: return "cern.ch"
else: return "hephy.at"
elif "lxplus" in host:
if inverse: return "hephy.at"
else: return "cern.ch"
if __name__ == '__main__':
main()