forked from cms-tau-pog/TauFW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageSystem.py
205 lines (183 loc) · 7.47 KB
/
StorageSystem.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Author: Izaak Neutelings (May 2020)
# Description: Superclass of a generic storage system with common operations like
# ls, cp, rm, mkdir, etc. to allow for easy implementation of storage system plug-ins.
import os
from fnmatch import fnmatch # for glob pattern
from TauFW.common.tools.utils import execute, ensurelist
from TauFW.common.tools.file import ensuredir, rmfile
from TauFW.PicoProducer.storage.utils import LOG
import getpass, platform
class StorageSystem(object):
def __init__(self,path,verb=0,ensure=False):
self.path = path
self.lscmd = 'ls'
self.lsurl = ''
self.cdcmd = 'cd'
self.cdurl = ''
self.cpcmd = 'cp'
self.cpurl = ''
self.rmcmd = 'rm -rf'
self.rmurl = ''
self.mkdrcmd = 'mkdir -p'
self.mkdrurl = ''
self.chmdprm = '777'
self.chmdcmd = 'chmod'
self.chmdurl = ''
self.haddcmd = 'hadd -f'
self.tmpdir = '/tmp/$USER/' # $TMPDIR # mounted temporary directory
self.fileurl = ""
self.verbosity = verb
if path.startswith('/'):
self.parent = '/'.join(path.split('/')[:3])
else:
self.parent = '/'+'/'.join(path.split('/')[:2])
self.mounted = os.path.exists(self.parent)
def __str__(self):
return self.path
def __repr__(self):
return '<%s("%s") at %s>'%(self.__class__.__name__,self.path,hex(id(self)))
def execute(self,cmd,**kwargs):
kwargs.setdefault('verb',self.verbosity)
return execute(cmd,**kwargs)
def expandpath(self,*paths,**kwargs):
"""Help function to replace variables in given path, or set default to own path."""
#verb = kwargs.get('verb',self.verbosity)
here = kwargs.get('here',False)
url = kwargs.get('url', "")
paths = [p for p in paths if p]
if paths:
path = os.path.join(*paths)
else:
path = self.path
if here and path[0] not in ['/','$']:
path = os.path.join(self.path,path)
if url and ('$PATH' in path or path.startswith(self.parent)):
path = url+path
path = path.replace('$PATH',self.path)
return path
def file(self,*paths,**kwargs):
"""Ensure that a given file exists, and append a file URL if needed."""
ensure = kwargs.get('ensure',False)
path = self.expandpath(*paths,here=True)
if path.startswith(self.parent):
path = self.fileurl + path
if ensure:
if not self.exists(path):
LOG.throw(IOError,"Did not find %s."%(path))
return path
def exists(self,*paths,**kwargs):
"""Ensure that a given path exists."""
verb = kwargs.get('verb',self.verbosity)
path = self.expandpath(*paths,here=True)
cmd = "if `%s %s%s >/dev/null 2>&1`; then echo 1; else echo 0; fi"%(self.lscmd,self.lsurl,path)
out = self.execute(cmd,verb=verb).strip()
return out=='1'
def ensuredir(self,*paths,**kwargs):
"""Ensure path exists."""
verb = kwargs.get('verb',self.verbosity)
path = self.expandpath(*paths)
if not self.exists(path,verb=verb):
self.mkdir(path,verb=verb)
return True
def cd(self,*paths,**kwargs):
"""Change directory if mounted."""
if self.mounted:
#verb = kwargs.get('verb',self.verbosity)
path = self.expandpath(*paths)
ret = os.chdir(path)
#ret = self.execute("%s %s%s"%(self.cdcmd,self.cdurl,path)).split('\n')
return ret
def ls(self,*paths,**kwargs):
"""List contents of given directory."""
verb = kwargs.get('verb',self.verbosity)
dryrun = kwargs.get('dry', False)
filters = ensurelist(kwargs.get('filter',[ ])) # inclusive filters with glob pattern, like '*' or '[0-9]' wildcards
path = self.expandpath(*paths)
retlist = self.execute("%s %s%s"%(self.lscmd,self.lsurl,path),fatal=False,dry=dryrun,verb=verb).split('\n')
if retlist and 'No such file or directory' in retlist[0]:
LOG.warning(retlist[0])
retlist = [ ]
elif filters:
for file in retlist[:]:
if not any(fnmatch(file,f) for f in filters):
retlist.remove(file)
return retlist
def getfiles(self,*paths,**kwargs):
"""Get list of files in a given path.
Return list of files with full path name, and if needed, a file URL.
Use the 'filter' option to filter the list of file names with some pattern."""
verb = kwargs.get('verb',self.verbosity)
fileurl = kwargs.get('url', self.fileurl)
filters = ensurelist(kwargs.get('filter',[ ])) # inclusive filters with glob pattern, like '*' or '[0-9]' wildcards
path = self.expandpath(*paths)
filelist = self.ls(path,**kwargs)
if fileurl and path.startswith(self.parent):
if not isinstance(fileurl,basestring):
fileurl = self.fileurl
else:
fileurl = ""
for i, file in enumerate(filelist):
if filters and not any(fnmatch(file,f) for f in filters): continue
filelist[i] = fileurl+os.path.join(path,file)
return filelist
def cp(self,source,target=None,**kwargs):
"""Copy files."""
dryrun = kwargs.get('dry', False)
verb = kwargs.get('verb',self.verbosity)
source = self.expandpath(source,url=self.cpurl)
target = self.expandpath(target,url=self.cpurl)
return self.execute("%s %s %s"%(self.cpcmd,source,target),dry=dryrun,verb=verb)
def hadd(self,sources,target,**kwargs):
"""Hadd files. Create intermediate target file if needed."""
target = self.expandpath(target,here=True)
dryrun = kwargs.get('dry', False)
verb = kwargs.get('verb', self.verbosity)
fileurl = kwargs.get('url', self.fileurl)
tmpdir = kwargs.get('tmpdir', target.startswith(self.parent) and self.cpurl!='')
htarget = target
if tmpdir:
if not isinstance(tmpdir,str):
tmpdir = self.tmpdir
tmpdir = ensuredir(tmpdir,verb=verb)
htarget = os.path.join(tmpdir,os.path.basename(target))
if isinstance(sources,basestring):
sources = [ sources ]
source = ""
for i, file in enumerate(sources,1):
fname = os.path.basename(file)
if '$PATH' in file and fileurl and isglob(fname): # expand glob pattern
parent = os.path.dirname(file)
files = self.getfiles(parent,filter=fname,url=fileurl)
source += ' '.join(files)+' '
else:
source += self.expandpath(file,url=fileurl)+' '
source = source.strip()
if verb>=2:
print ">>> %-10s = %r"%('sources',sources)
print ">>> %-10s = %r"%('source',source)
print ">>> %-10s = %r"%('target',target)
print ">>> %-10s = %r"%('htarget',htarget)
out = self.execute("%s %s %s"%(self.haddcmd,htarget,source),dry=dryrun,verb=verb)
if tmpdir:
cpout = self.cp(htarget,target,dry=dryrun,verb=verb)
if not dryrun:
rmfile(htarget)
return out
def rm(self,*paths,**kwargs):
"""Remove given file or director."""
path = self.expandpath(*paths,here=True)
verb = kwargs.get('verb',self.verbosity)
return self.execute("%s %s%s"%(self.rmcmd,self.rmurl,path),verb=verb)
def mkdir(self,dirname='$PATH',**kwargs):
verb = kwargs.get('verb',self.verbosity)
dirname = self.expandpath(dirname,here=True)
return self.execute("%s %s%s"%(self.mkdrcmd,self.mkdrurl,dirname),verb=verb)
def chmod(self,file,perm=None,**kwargs):
verb = kwargs.get('verb',self.verbosity)
if not perm: perm = self.chmdprm
return self.execute("%s %s %s%s"%(self.chmdcmd,perm,self.chmdurl,file),verb=verb)
class Local(StorageSystem):
def __init__(self,path,verb=0,ensure=False):
super(Local,self).__init__(path,verb=verb,ensure=ensure)
if ensure:
self.ensuredir(self.path,verb=verb)