-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEOSSafeUtilsOutputAtCERN.py
143 lines (124 loc) · 4.01 KB
/
EOSSafeUtilsOutputAtCERN.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os, sys, getopt
import string
#This file has some general EOS safe utilities for eos file operations
# EOSpathExists(path)
# EOSisfile(path)
# EOSrm(path):
# EOSrmdir(path):
#xrd = 'eos root://cmseos.fnal.gov/'
xrd = 'eos root://eoscms.cern.ch/'
#xrd = 'xrdfs root://cmseos.fnal.gov/' #doesn't work.
def cleanEOSpath(path):
#if path starts with /eos/uscms remove it.
if string.find(path,'/eos/uscms',0,10) == 0:
return path[10:]
return path
def EOSpathExists(path):
#returns a bool true iff the path exists
path = cleanEOSpath(path)
return len(os.popen(xrd+' ls -d '+path).readlines()) == 1
def EOSisfile(path):
#returns a bool saying if this is a file.
path = cleanEOSpath(path)
if not EOSpathExists(path):
return False
lines = os.popen(xrd+' ls -l '+path).readlines()
if len(lines) != 1:
return False
else:
return string.find(lines[0],'-',0,1) == 0 #entry should start with -
def EOSisZeroSizefile(path, month):
#returns a bool saying if this is a file.
path = cleanEOSpath(path)
if not EOSpathExists(path):
return False
lines = os.popen(xrd+' ls -l '+path).readlines()
if len(lines) != 1:
return False
else:
return string.find(lines[0],' 0 '+month+' ',0,len(lines[0])) > 0
def EOSrm(path):
#rm path, but EOS safe.
path = cleanEOSpath(path)
if EOSpathExists(path):
os.system(xrd +" rm "+path)
def EOSrmdir(path):
#rmdir path, but EOS safe.
path = cleanEOSpath(path)
if EOSpathExists(path):
os.system(xrd +" rmdir "+path)
def striplist(alist):
#takes a list of strings, returns a version of the list with
#whitespace stripped from all entries.
ret = []
for item in alist:
ret.append(item.strip())
return ret
def EOSlistdir(Dir):
#ls Dir, only eos safe.
#does not check that this is a directory
#returns a list of the contents of ls
Dir = cleanEOSpath(Dir)
items = os.popen(xrd+' ls '+Dir).readlines() #they have a \n at the end
return striplist(items)
def EOSlist_root_files(Dir):
#ls Dir/*.root, returns a list of the root file names that it finds (without the path)
Dir = cleanEOSpath(Dir)
items = os.popen(xrd+' ls '+Dir).readlines() #they have a \n at the end
items2 = striplist(items)
rootlist = []
for item in items2:
if string.rfind(item,'root',-4) != -1:
rootlist.append(item)
return rootlist
def EOSlist_lhe_files(Dir):
#ls Dir/*.root, returns a list of the root file names that it finds (without the path)
Dir = cleanEOSpath(Dir)
items = os.popen(xrd+' ls '+Dir).readlines() #they have a \n at the end
items2 = striplist(items)
rootlist = []
for item in items2:
if string.rfind(item,'lhe.gz',-6) != -1:
rootlist.append(item)
return rootlist
tempfile = '.eossafetemp'
def killtemp():
#deletes the the temp file
if os.path.exists(tempfile):
os.system("rm "+tempfile)
def EOSopen_via_temp(path,option):
#copies the file to the temp file, then opens that.
#be careful to not open more than one file at a time
#or else the first file will get deleted.
path = cleanEOSpath(path)
killtemp()
#os.system("xrdcp -s root://cmseos.fnal.gov/"+path+" ./"+tempfile)
os.system("xrdcp -s root://eoscms.cern.ch/"+path+" ./"+tempfile)
return open(tempfile,option)
def copytotemp(path):
#copies a file from eos to the local temp file
killtemp()
path = cleanEOSpath(path)
#os.system("xrdcp -s root://cmseos.fnal.gov/"+path+" ./"+tempfile)
os.system("xrdcp -s root://eoscms.cern.ch/"+path+" ./"+tempfile)
#I need to replace thei line:
#folders = [x for x in os.walk(Dir).next()[1] ]
#this lists all the folders in Dir.
def EOSlistSubdirs(Dir):
#returns a list of all the subdirectories of Dir
#built to substitutes for the line
#folders = [x for x in os.walk(Dir).next()[1] ]
Dir = cleanEOSpath(Dir)
ret = []
if not EOSpathExists(Dir):
return ret
for line in os.popen(xrd+' ls -l '+Dir).readlines():
words = line.split()
if len(words) < 9: #if not a full ls -l entry
continue
elif words[0][0]=='d': #if its a directory
ret.append(words[8]) #append the directory name
return ret
#def EOSwalk(Dir):
#safeDir = cleanEOSpath(Dir)
#https://docs.python.org/2/library/os.html