Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
attr (str): This is the name of the attribute which we're assigning
value (unknown): This is the value being assigned.
"""
actual_value = value
if attr == "namePattern":
this_dir, actual_value = os.path.split(value)
if this_dir:
self.localDir = this_dir
elif attr == 'localDir':
if value:
new_value = os.path.abspath(expandfilename(value))
if os.path.exists(new_value):
actual_value = new_value
super(DiracFile, self).__setattr__(attr, actual_value)
if not os.path.exists(name):
if not self.compressed:
raise GangaFileError('Cannot upload file. File "%s" must exist!' % name)
name += '.gz'
if not os.path.exists(name):
raise GangaFileError('File "%s" must exist!' % name)
else:
if self.compressed:
os.system('gzip -c %s > %s.gz' % (name, name))
name += '.gz'
if not os.path.exists(name):
raise GangaFileError('File "%s" must exist!' % name)
lfn = os.path.join(lfn_base, os.path.basename(this_file))
d = DiracFile()
d.namePattern = os.path.basename(name)
d.compressed = self.compressed
d.localDir = sourceDir
stderr = ''
stdout = ''
logger.info('Uploading file \'%s\' to \'%s\' as \'%s\'' % (name, storage_elements[0], lfn))
logger.debug('execute: uploadFile("%s", "%s", %s)' % (lfn, os.path.join(sourceDir, name), str([storage_elements[0]])))
try:
stdout = execute('uploadFile("%s", "%s", %s)' % (lfn, os.path.join(sourceDir, name), str([storage_elements[0]])), cred_req=self.credential_requirements)
except GangaDiracError as err:
logger.warning("Couldn't upload file '%s': \'%s\'" % (os.path.basename(name), err))
failureReason = "Error in uploading file '%s' : '%s'" % (os.path.basename(name), err)
if regex.search(self.namePattern) is not None:
d.failureReason = failureReason
outputFiles.append(d)
continue
guid = tokens[3]
try:
locations = eval(tokens[2])
except Exception as err:
logger.debug("line_process err: %s" % err)
locations = tokens[2]
if pattern == name:
logger.debug("pattern == name")
logger.error("Failed to parse outputfile data for file '%s'" % name)
return True
# This is the case that multiple files were requested
if pattern == dirac_file.namePattern:
logger.debug("pattern == dirac_file.namePattern")
d = DiracFile(namePattern=name, lfn=lfn)
d.compressed = dirac_file.compressed
d.guid = guid
d.locations = locations
d.localDir = localPath
dirac_file.subfiles.append(d)
#dirac_line_processor(line, d)
return False
# This is the case that an individual file was requested
elif name == dirac_file.namePattern:
logger.debug("name == dirac_file.namePattern")
if lfn == '###FAILED###':
dirac_file.failureReason = tokens[2]
logger.error("Failed to upload file '%s' to Dirac: %s" % (name, dirac_file.failureReason))
return True
dirac_file.lfn = lfn
if regex.search(self.namePattern) is not None:
if self.lfn != "":
logger.warning("Cannot specify a single lfn for a wildcard namePattern")
logger.warning("LFN will be generated automatically")
self.lfn = ""
if not self.remoteDir:
try:
job = self.getJobObject()
lfn_folder = os.path.join("GangaJob_%s" % job.getFQID('/'), "OutputFiles")
except AssertionError:
t = datetime.datetime.now()
this_date = t.strftime("%H.%M_%A_%d_%B_%Y")
lfn_folder = os.path.join('GangaFiles_%s' % this_date)
lfn_base = os.path.join(DiracFile.diracLFNBase(self.credential_requirements), lfn_folder)
else:
lfn_base = os.path.join(DiracFile.diracLFNBase(self.credential_requirements), self.remoteDir)
if uploadSE == "":
if self.defaultSE != "":
storage_elements = [self.defaultSE]
else:
if configDirac['allDiracSE']:
storage_elements = [random.choice(configDirac['allDiracSE'])]
else:
raise GangaFileError("Can't upload a file without a valid defaultSE or storageSE, please provide one")
elif isinstance(uploadSE, list):
storage_elements = uploadSE
else:
storage_elements = [uploadSE]
def diracLFNBase(credential_requirements):
"""
Compute a sensible default LFN base name
If ``DiracLFNBase`` has been defined, use that.
Otherwise, construct one from the user name and the user VO
Args:
credential_requirements (DiracProxy): This is the credential which governs how we should format the path
"""
if configDirac['DiracLFNBase']:
return configDirac['DiracLFNBase']
user = DiracProxyInfo(credential_requirements).username
return '/{0}/user/{1}/{2}'.format(configDirac['userVO'], user[0], user)
# add DiracFile objects to the configuration scope (i.e. it will be
# possible to write instatiate DiracFile() objects via config file)
GangaCore.Utility.Config.config_scope['DiracFile'] = DiracFile
exportToGPI('GangaDirac', GangaList, 'Classes')