Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_se_path(grid_mode):
se_name = get_se_name(grid_mode)
rtc, out, err = execute('lcg-info', ['--list-se', '--vo', 'snoplus.snolab.ca', '--attrs',
'VOInfoPath', '--query', 'SE=%s' % (se_name)])
if rtc!=0:
raise JobToolsException('Cannot get se-path for %s' % (se_name))
try:
se_path = out[-2].split()[-1]
if se_path[0]=='/':
se_path = se_path[1:]
return se_path
except:
raise JobToolsException('Cannot get se-path from %s' % (out))
def getsize(url):
'''Get the size (must be an srm)
'''
rtc, out, err = lcg_ls('-l', url)
if rtc!=0:
raise JobToolsException('File does not exist %s' % url)
else:
try:
bits = out[0].split()
size = int(bits[4])
return size
except:
raise JobToolsException('Cannot get size of %s' % url)
def checksum(url):
'''Get the checksum of a file (adler32).
Must provide an SRM.
'''
rtc,out,err = execute('lcg-get-checksum', [url])
if rtc!=0:
raise JobToolsException('Checksum error: %s'%url)
else:
try:
cks = out[0].split()[0].strip()
return cks
except IndexError as e:
raise JobToolsException('Checksum error: %s'%url)
def exists(url):
'''Just checks a file exists
'''
command = 'lcg-ls'
rtc, out, err = execute(command, [url])
if rtc!=0:
raise JobToolsException('File does not exist %s: %s' % (url, err))
def listreps(url):
'''Get the locations of a file in the lfc.
Returns a list.
'''
rtc,out,err = execute('lcg-lr', [url])
if rtc!=0:
raise JobToolsException('lcg-lr error: %s'%url)
else:
return out
def get_se_name(grid_mode):
if grid_mode == 'srm':
# Assume this means westgrid
se_name = "sehn02.atlas.ualberta.ca"
elif grid_mode == 'lcg':
se_name = os.environ['VO_SNOPLUS_SNOLAB_CA_DEFAULT_SE']
else:
raise JobToolsException('Unknown grid mode %s; cannot get se name' % grid_mode)
return se_name
import os
import sys
import re
import subprocess
###########################################
# Exceptions
###########################################
class JobToolsException(Exception):
def __init__(self, error):
Exception.__init__(self, error)
class CommandException(JobToolsException):
def __init__(self, command, out, err):
'''Pass in the command failed and outputs/errors
'''
error = '\nCommand: %s' % command
error += '\nOutput: %s' % out
error += '\nError: %s' % err
JobToolsException.__init__(self, error)
###########################################
# Other classes
###########################################
class JobHelper(object):
'''A singleton class to persist information relating to job setup.
Useful for jobs where multiple functions need to be called that all
def add_environment(self, path):
'''Append an environment file that should be used.
These must be added in the correct order.'''
if not os.path.exists(path):
raise JobToolsException("JobHelper: no such file %s" % path)
self._envs.append(path)
def getguid(url):
'''Get the guid of a file
'''
rtc,out,err = execute('lcg-lg', [url])
if rtc!=0:
raise JobToolsException('Cannot get url of %s'%url)
else:
guid = out[0]
return guid
def __init__(self, command, out, err):
'''Pass in the command failed and outputs/errors
'''
error = '\nCommand: %s' % command
error += '\nOutput: %s' % out
error += '\nError: %s' % err
JobToolsException.__init__(self, error)