解析以下代码:“def copyfiles(run, mode, prof, copybase=True, alarm=True):
"""Copy datas from profile into `pwd` (if mode=='DATA')
or results from `pwd` into destination given by profile (if mode=='RESU').
Aster bases are copied only if copybase is True.
Raise only <E> if an error occurs run CheckOK() after.
"""
if mode == 'DATA':
l_dico = prof.data
icomm = 0
ncomm = len(prof.Get('D', 'comm'))
for df in l_dico:
icomm = copyfileD(run, df, icomm, ncomm)
else:
l_dico = prof.resu
for df in l_dico:
copyfileR(run, df, copybase, alarm)
def copyfileD(run, df, icomm, ncomm):
"""Copy datas from `df` into current directory.
Raise only <E> if an error occurs run CheckOK() after.
"""
dest = None
# 1. ----- if logical unit is set : fort.*
if df['ul'] != 0 or df['type'] in ('nom',):
dest = 'fort.%d' % df['ul']
if df['type'] == 'nom':
dest = osp.basename(df['path'])
# exception for multiple command files (adding _N)
if df['ul'] == 1:
icomm += 1
format = '%%0%dd' % (int(log10(max(1, ncomm))) + 1)
dest = dest + '.' + format % icomm
# warning if file already exists
if run.Exists(dest):
run.Mess(ufmt(_("'%s' overwrites '%s'"), df['path'], dest), '<A>_COPY_DATA')
if df['compr']:
dest = dest + '.gz'
# 2. ----- bases and directories (ul=0)
else:
# base
if df['type'] in ('base', 'bhdf'):
dest = osp.basename(df['path'])
# ensi
elif df['type'] == 'ensi':
dest = 'DONNEES_ENSIGHT'
# repe
elif df['type'] == 'repe':
dest = 'REPE_IN'
if dest is not None:
# 3. --- copy
kret = run.Copy(dest, df['path'], niverr='<E>_COPY_ERROR', verbose=True)
# 4. --- decompression
if kret == 0 and df['compr']:
kret, dest = run.Gunzip(dest, niverr='<E>_DECOMPRESSION', verbose=True)
# 5. --- move the bases in main directory
if df['type'] in ('base', 'bhdf'):
for f in glob(osp.join(dest, '*')):
run.Rename(f, osp.basename(f))
# force the file to be writable
make_writable(dest)
# clean text files if necessary
if df['ul'] != 0 and run.IsTextFileWithCR(dest):
file_cleanCR(dest)
print(ufmt(' ` ' + _('line terminators have been removed from %s'), dest))
return icomm
def copyfileR(run, df, copybase=True, alarm=True):
"""Copy results from current directory into destination given by `df`.
Aster bases are copied only if copybase is True.
Raise only <E> if an error occurs run CheckOK() after.
"""
# list of files
lf = []
isdir = False
# 1. ----- files
if df['ul'] != 0 or df['type'] in ('nom', ):
# if logical unit is set : fort.*
if df['ul'] != 0:
lf.append('fort.%d' % df['ul'])
elif df['type'] == 'nom':
lf.append(osp.basename(df['path']))
# 2. ----- bases and directories (ul=0)
else:
isdir = True
# base
if df['type'] == 'base' and copybase:
lf.extend(glob('glob.*'))
lf.extend(glob('pick.*'))
# bhdf
elif df['type'] == 'bhdf' and copybase:
lbas = glob('bhdf.*')
if len(lbas) == 0:
if alarm:
run.Mess(_("No 'bhdf' found, saving 'glob' instead"), '<A>_COPY_BASE')
lbas = glob('glob.*')
lf.extend(lbas)
lf.extend(glob('pick.*'))
# repe
elif df['type'] == 'repe':
rep = 'REPE_OUT'
if os.name=='nt':
lfrep = [rep]
else:
lfrep = glob(osp.join(rep, '*'))
if len(lfrep) == 0 and alarm:
run.Mess(ufmt(_("%s directory is empty !"), rep), '<A>_COPY_REPE')
lf.extend(lfrep)
# 3. ----- compression
kret = 0
if df['compr']:
lfnam = lf[:]
lf = []
for fnam in lfnam:
kret, f = run.Gzip(fnam, niverr='<E>_COMPRES_ERROR', verbose=True)
if kret == 0:
lf.append(f)
else:
lf.append(fnam)
run.Mess(_("Warning: The uncompressed file will be returned "
"without changing the target filename\n(eventually "
"ending with '.gz' even if it is not compressed; "
"you may have to rename it before use)."),
'<A>_COPYFILE')
# 4. ----- copy
if len(lf) > 0:
# create destination
if isdir:
kret = run.MkDir(df['path'], '<E>_MKDIR_ERROR')
else:
if len(lf) > 1:
run.Mess(ufmt(_("""Only the first one of [%s] is copied."""), ', '.join(lf)),
'<A>_COPYFILE')
lf = [lf[0],]
kret = run.MkDir(osp.dirname(df['path']), '<E>_MKDIR_ERROR')
# copy
if kret == 0:
lfc = lf[:]
for fname in lfc:
if not osp.exists(fname):
if alarm:
run.Mess(ufmt("no such file or directory: %s", fname), '<A>_COPYFILE')
lf.remove(fname)
if len(lf) > 0:
kret = run.Copy(df['path'], niverr='<E>_COPY_ERROR', verbose=True, *lf)
# save base if failure
if kret != 0:
rescue = get_tmpname(run, basename='save_results')
run.Mess(ufmt(_("Saving results in a temporary directory (%s)."), rescue),
'<A>_COPY_RESULTS', store=True)
kret = run.MkDir(rescue, chmod=0o700)
kret = run.Copy(rescue, niverr='<E>_COPY_ERROR',
verbose=True, *lf)
”