def get_file_type(actions): from pkg.flavor.smf_manifest import is_smf_manifest for a in actions: lpath = a.attrs[PD_LOCAL_PATH] if os.stat(lpath).st_size == 0: # Some tests rely on this being identified yield "empty file" continue try: with open(lpath, 'rb') as f: magic = f.read(4) except FileNotFoundError: yield UNFOUND continue if magic == b'\x7fELF': yield ELF elif magic[:2] == b'#!': yield EXEC elif lpath.endswith('.xml'): if is_smf_manifest(lpath): yield SMF_MANIFEST else: # Some tests rely on this type being identified yield "XML document" else: yield "unknown"
def get_file_type(actions): t_fd, t_path = tempfile.mkstemp() t_fh = os.fdopen(t_fd, "w") for a in actions: t_fh.write(os.path.join(a.attrs[PD_LOCAL_PATH]) + "\n") t_fh.close() res = subprocess.Popen( ["/usr/bin/file", "-f", t_path], stdout=subprocess.PIPE).communicate()[0].splitlines() remove(t_path) assert (len(actions) == len(res)) for i, file_out in enumerate(res): file_out = file_out.strip() # ensure we can manipulate the string if isinstance(file_out, bytes) and six.PY3: file_out = file_out.decode("utf-8") a = actions[i] proto_file = a.attrs[PD_LOCAL_PATH] colon_cnt = proto_file.count(":") + 1 tmp = file_out.split(":", colon_cnt) res_file_name = ":".join(tmp[0:colon_cnt]) if res_file_name != proto_file: raise RuntimeError("pf:{0} rfn:{1} file_out:{2}".format( proto_file, res_file_name, file_out)) file_type = tmp[colon_cnt].strip().split() joined_ft = " ".join(file_type) if file_type[0] == "ELF": yield ELF elif file_type[0] == "executable": yield EXEC elif joined_ft == "cannot open: No such file or directory": yield UNFOUND elif file_type[0] == "XML": from pkg.flavor.smf_manifest import is_smf_manifest if is_smf_manifest(proto_file): yield SMF_MANIFEST else: yield joined_ft else: yield joined_ft
def get_file_type(actions): t_fd, t_path = tempfile.mkstemp() t_fh = os.fdopen(t_fd, "w") for a in actions: t_fh.write(os.path.join(a.attrs[PD_LOCAL_PATH]) + "\n") t_fh.close() res = subprocess.Popen(["/usr/bin/file", "-f", t_path], stdout=subprocess.PIPE).communicate()[0].splitlines() remove(t_path) assert(len(actions) == len(res)) for i, file_out in enumerate(res): file_out = file_out.strip() a = actions[i] proto_file = a.attrs[PD_LOCAL_PATH] colon_cnt = proto_file.count(":") + 1 tmp = file_out.split(":", colon_cnt) res_file_name = ":".join(tmp[0:colon_cnt]) if res_file_name != proto_file: raise RuntimeError("pf:%s rfn:%s file_out:%s" % (proto_file, res_file_name, file_out)) file_type = tmp[colon_cnt].strip().split() joined_ft = " ".join(file_type) if file_type[0] == "ELF": yield ELF elif file_type[0] == "executable": yield EXEC elif joined_ft == "cannot open: No such file or directory": yield UNFOUND elif file_type[0] == "XML": from pkg.flavor.smf_manifest import is_smf_manifest if is_smf_manifest(proto_file): yield SMF_MANIFEST else: yield joined_ft else: yield joined_ft