if not excludes: excludes
= []if runas: euid
= os.geteuid() egid
= os.getegid() uinfo
= __salt__['user.info'](runas)if not uinfo:raise SaltInvocationError("User '{0}' does not exist".format(runas) )
zip_file, dest
= _render_filenames(zip_file, dest, None, template)
if runas and (euid != uinfo['uid'] or egid != uinfo['gid']):# Change the egid first, as changing it after the euid will fail # if the runas user is non-privileged.
os.setegid(uinfo['gid'])
os.seteuid(uinfo['uid'])
try:
exc = None
# Define cleaned_files here so that an exception will not prevent this
# variable from being defined and cause a NameError in the return
# statement at the end of the function.
cleaned_files = []
with contextlib.closing(zipfile.ZipFile(zip_file, "r")) as zfile:
# 旧方法修改文件的修改时间
if True:
files = zfile.namelist()
cleaned_files.extend([x for x in files if x not in excludes])
for file in zfile.infolist():
d = file.date_time#修改时间元信息
gettime = "%s/%s/%s %s:%s" % (d[0], d[1], d[2], d[3], d[4])
zfile.extract(file, dest, password)#解压
filep = os.path.join(dest, file.filename)
print "recover:%s modify time" % filep
timearry = time.mktime(time.strptime(gettime, '%Y/%m/%d %H:%M'))
os.utime(filep, (timearry, timearry))#替换时间
else:
files = zfile.namelist()
if isinstance(excludes, string_types):
excludes = [x.strip() for x in excludes.split(',')]
elif isinstance(excludes, (float, integer_types)):
excludes = [str(excludes)]
cleaned_files.extend([x for x in files if x not in excludes])
for target in cleaned_files:
if target not in excludes:
if salt.utils.is_windows() is False:
info = zfile.getinfo(target)
# Check if zipped file is a symbolic link
if stat.S_ISLNK(info.external_attr >> 16):
source = zfile.read(target)
os.symlink(source, os.path.join(dest, target))
continue
zfile.extract(target, dest, password)
if extract_perms:
perm = zfile.getinfo(target).external_attr >> 16
if perm == 0:
umask_ = os.umask(0)
os.umask(umask_)
if target.endswith('/'):
perm = 0o777 & ~umask_
else:
perm = 0o666 & ~umask_
os.chmod(os.path.join(dest, target), perm)
except Exception as exc:
pass
finally:
# Restore the euid/egid
if runas:
os.seteuid(euid)
os.setegid(egid)
if exc is not None:
# Wait to raise the exception until euid/egid are restored to avoid
# permission errors in writing to minion log.
raise CommandExecutionError(
'Exception encountered unpacking zipfile: {0}'.format(exc)
)