refactor stat

make format function 'format only'
added platform dependant info, when it is available
avoid rechecking same info
added comments to each info gathering section

(cherry picked from commit a79acf73d7eb79b76d808ff8a1d6c505dfd9ec82)
pull/18777/head
Brian Coca 8 years ago committed by Matt Clay
parent e2fce828f6
commit fccaf883da

@ -331,8 +331,7 @@ from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_bytes from ansible.module_utils._text import to_bytes
def format_output(module, path, st, follow, get_md5, get_checksum, def format_output(module, path, st):
checksum_algorithm, mimetype=None, charset=None):
mode = st.st_mode mode = st.st_mode
# back to ansible # back to ansible
@ -367,37 +366,30 @@ def format_output(module, path, st, follow, get_md5, get_checksum,
xoth=bool(mode & stat.S_IXOTH), xoth=bool(mode & stat.S_IXOTH),
isuid=bool(mode & stat.S_ISUID), isuid=bool(mode & stat.S_ISUID),
isgid=bool(mode & stat.S_ISGID), isgid=bool(mode & stat.S_ISGID),
readable=os.access(path, os.R_OK),
writeable=os.access(path, os.W_OK),
executable=os.access(path, os.X_OK),
) )
if stat.S_ISLNK(mode): # Platform dependant flags:
output['lnk_source'] = os.path.realpath(path) for other in [
# Some Linux
if stat.S_ISREG(mode) and get_md5 and os.access(path, os.R_OK): ('st_blocks','blocks'),
# Will fail on FIPS-140 compliant systems ('st_blksize', 'block_size'),
try: ('st_rdev','device_type'),
output['md5'] = module.md5(path) ('st_flags', 'flags'),
except ValueError: # Some Berkley based
output['md5'] = None ('st_gen', 'generation'),
('st_birthtime', 'birthtime'),
if stat.S_ISREG(mode) and get_checksum and os.access(path, os.R_OK): # RISCOS
output['checksum'] = module.digest_from_file(path, checksum_algorithm) ('st_ftype', 'file_type'),
('st_attrs', 'attributes'),
try: ('st_obtype', 'object_type'),
pw = pwd.getpwuid(st.st_uid) # OS X
('st_rsize', 'real_size'),
('st_creator', 'creator'),
('st_type', 'file_type'),
]:
if hasattr(st, other[0]):
output[other[1]] = getattr(st, other[0])
output['pw_name'] = pw.pw_name
grp_info = grp.getgrgid(st.st_gid)
output['gr_name'] = grp_info.gr_name
except:
pass
if not (mimetype is None and charset is None):
output['mime_type'] = mimetype
output['charset'] = charset
return output return output
@ -425,6 +417,7 @@ def main():
get_checksum = module.params.get('get_checksum') get_checksum = module.params.get('get_checksum')
checksum_algorithm = module.params.get('checksum_algorithm') checksum_algorithm = module.params.get('checksum_algorithm')
# main stat data
try: try:
if follow: if follow:
st = os.stat(b_path) st = os.stat(b_path)
@ -438,26 +431,54 @@ def main():
module.fail_json(msg=e.strerror) module.fail_json(msg=e.strerror)
mimetype = None # process base results
charset = None output = format_output(module, path, st)
if get_mime:
mimetype = 'unknown'
charset = 'unknown'
# resolved permissions
for perm in [('readable', os.R_OK), ('writeable', os.W_OK), ('executable', os.X_OK)]:
output[perm[0]] = os.access(path, perm[1])
# symlink info
if output.get('islnk'):
output['lnk_source'] = os.path.realpath(path)
try: # user data
pw = pwd.getpwuid(st.st_uid)
output['pw_name'] = pw.pw_name
except:
pass
try: # group data
grp_info = grp.getgrgid(st.st_gid)
output['gr_name'] = grp_info.gr_name
except:
pass
# checksums
if output.get('isreg') and output.get('readable'):
if get_md5:
# Will fail on FIPS-140 compliant systems
try:
output['md5'] = module.md5(path)
except ValueError:
output['md5'] = None
if get_checksum:
output['checksum'] = module.digest_from_file(path, checksum_algorithm)
# try to get mime data if requested
if get_mime:
output['mimetype'] = output['charset'] = 'unknown'
filecmd = [module.get_bin_path('file', True), '-i', path] filecmd = [module.get_bin_path('file', True), '-i', path]
try: try:
rc, out, err = module.run_command(filecmd) rc, out, err = module.run_command(filecmd)
if rc == 0: if rc == 0:
mimetype, charset = out.split(':')[1].split(';') mimetype, charset = out.split(':')[1].split(';')
mimetype = mimetype.strip() output['mimetype'] = mimetype.strip()
charset = charset.split('=')[1].strip() output['charset'] = charset.split('=')[1].strip()
except: except:
pass pass
output = format_output(module, path, st, follow, get_md5, get_checksum,
checksum_algorithm, mimetype=mimetype,
charset=charset)
module.exit_json(changed=False, stat=output) module.exit_json(changed=False, stat=output)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save