def main(): with losetup_ctxmgr() as device: out = file("-s", device).stdout.decode("utf8").strip() print(out) with losetup_ctxmgr() as device: out = file("-s", device).stdout.decode("utf8").strip() print(out)
def findBinaryForCore(coreName): """Uses strings on the binary to find potential binary names, then picks the one with the most recent mtime, since we assume these cores are from development.""" f = str(sh.file(coreName)) looking_for = "execfn: '" index = f.find(looking_for) if index != -1: end_index = f.find("',", index + len(looking_for)) if end_index != -1: return f[index + len(looking_for):end_index] strings = sh.strings(coreName).split("\n") strings = [s for s in strings if s] candidate = ("", 0) for s in strings: if op.exists(s): finfo = sh.file(s) if "ELF" in finfo and "executable" in finfo: mtime = op.getmtime(s) if mtime > candidate[1]: candidate = (s, mtime) return candidate[0]
def determine_file_type(file): """Determine if `file' is compressed and if so how, and return file and its associated type. `file' needs to be a file name or a stream containing enough information to determine its type. """ if isinstance(file, str): file_type = sh.file(file, '--brief').stdout else: file_type = sh.file('-', '--brief', _in=file).stdout # tricky: we get a byte sequence here which we have to decode into a string: return file_type.split()[0].lower().decode()
def load(self): log.debug("loading mapable drive {0}".format(self.path)) try: if not re.search(r"block special", str(sh.file(self.path).stdout, 'utf8'), flags=re.IGNORECASE): self.lodev = sh.losetup("-f").split()[0] sh.losetup(self.lodev, self.path) sh.blkid(self.lodev) try: sh.partprobe(self.lodev) except: pass else: sh.blkid(self.path) try: sh.partprobe(self.path) except: pass sh.sync("/dev/") self.process_devicemap() except Exception as e: log.exception(e) return False return True
def is_binary_stripped(elf_path): from sh import file result = file(elf_path) if "not stripped" in result: return False else: return True
def is_video_file(filename): """ Determines whether given filename is video file or not. Uses UNIX utility called ``file`` (no Win support, sorry, Pull Requests welcomed). """ result = sh.file(filename, mime=True) mime_type = result.replace('{}: '.format(filename), '') return 'video/' in mime_type
def __init__(self, file: str, **kwargs): if type(self) == Mountable: if not _p.isfile(file): raise FileNotFoundError(file) if not (cls := self.__resolve_cls(file)): raise Exception('unsupported file type ' + file + ' ' + str(sh.file(file))) self.__class__ = cls self.__init__(file, **kwargs)
def parse_image_type(image): # Trace symbolic link with -L image_format = sh.file('-L', image, _ok_code=range(255)) if 'ASCII cpio archive' in image_format: return 'CPIO' elif 'MBR boot sector' in image_format: return 'MBR' elif re.match(r'.*Linux .* ext[0-9] filesystem.*', str(image_format)): return 'E2FS' return ''
def __resolve_cls(cls, file: str): from .types import MountableType desc = str(sh.file(file, brief=True)).strip() for typ in MountableType: if typ == MountableType.AUTO: continue mountable_cls = typ.value if mountable_cls._ismountable(file, desc): return mountable_cls
def _get_label(self): with self.losetup_context_manager() as device: out = file("-s", device, **SH_OPTS).stdout.decode("utf8").strip() # "label:" is for fat partitions, # "volume name" is for ext partitions # "BTRFS Filesystem label" is for btrfs partitions match = search( '(BTRFS Filesystem label|label:|volume name) "(.*)"', out, ) if match is not None: return match.groups()[1].strip()
def method_GET(self, start_response): input = self.environ['PATH_INFO'] output = join(self.basepath, input[1:]) if not self._check_path(input): return self._respond_error(start_response) mime = file("-b", "--mime-type", output) start_response('200 OK', [ ('Content-Length', getsize(output)), ('Content-Type', mime) ]) return open(output, 'rb')
def extract_elf_binaries_using_file(directory: str): h = list(absoluteFilePaths(directory)) from sh import file file_command_output = file(h) result_list = [] lines = file_command_output.split("\n") for line in lines: line_split = line.split(":") if len(line_split) < 2: continue binary_desc = line_split[1] if "ELF" in binary_desc: result_list.append(line_split[0]) return result_list
def findBinaryForCore(coreName): """Uses strings on the binary to find potential binary names, then picks the one with the most recent mtime, since we assume these cores are from development.""" strings = sh.strings(coreName).split("\n") strings = [s for s in strings if s] candidate = ("", 0) for s in strings: if op.exists(s): finfo = sh.file(s) if "ELF" in finfo and "executable" in finfo: mtime = op.getmtime(s) if mtime > candidate[1]: candidate = (s, mtime) return candidate[0]
def __init__(self, tarball_path): compression_method = conf.tarball_compression_method self.tarfile_method = 'r:' + compression_method ############### # Some checks # ############### compression_method_info\ = {'bz2': ('bzip2', '.bz2'), 'gz': ('gzip', '.gz')} # Are we dealing with a compressed tar file? sys.assert_file_exists(tarball_path) assert tarfile.is_tarfile(tarball_path),\ '%s is not a tarball.' % tarball_path compression_type, compression_ext\ = compression_method_info[compression_method] assert compression_type in sh.file(tarball_path),\ '%s is not %s?' % (tarball_path, compression_method) root, ext = os.path.splitext(tarball_path) assert ext == compression_ext,\ '%s is not %s?' % (tarball_path, compression_method) ################# # Checks passed # ################# self.tarball_path = tarball_path # Get the name of the directory where we will extract the tarball, and # then zap any file or dir by that name and create an empty directory. root, ext = os.path.splitext(self.tarball_path) # knock of the .bz2 root, ext = os.path.splitext(root) # knock of the .tar self.extraction_dir = os.path.basename(root)
def mimetype(filename): return sh.file('-b', '--mime-type', '--', filename).stdout.strip()
def is_file_binary(filename): res = sh.file(filename, L=True, mime=True) if ("charset=binary" in res) and (get_file_size(filename) > 0): return True return False
gitIgnorePath.append(root) path = op.join(root, filename) print path if op.islink(path): # Without this check the stat below will fail # when we have a bad symlink, like the .# kind emacs # creates deliberately to lock files. continue filesize = os.stat(path).st_size relpath = op.relpath(path, args.dir) if filesize > 1024 * 800: # 800KB # PDFs are ok, they maybe big ext = op.splitext(path)[-1] if ext != ".pdf" and ext != ".pack": tooLarge.add(relpath) fileinfo = sh.file(path) if "ELF" in fileinfo and "executable" in fileinfo: executable.add(relpath) if len(gitIgnorePath) < 1: print >> sys.stderr, "Couldn't find git repo." sys.exit(1) elif len(gitIgnorePath) > 1: print >> sys.stderr, "Found more than one .gitignore, using least nested for filtering." gitIgnorePath = [ min(gitIgnorePath, key=lambda x: op.normpath(x).count(os.sep)) ] print ".gitignore path: " + op.join(gitIgnorePath[0], ".gitignore") # Figure out which files git will ignore
def determineFileType(file): fileType = sh.file('--brief', file).stdout.split()[0].lower() return (file, fileType)
def _ismountable(cls, path: str = None, file_cmd_out: str = None) -> bool: if 'gzip compressed data' in file_cmd_out: file_cmd_out = sh.file(path, z=True) return 'ASCII cpio archive' in file_cmd_out
def assert_is_shell_script(path): """Assert path is an executable shell script.""" assert os.path.isfile(path) output = sh.file(path) # run "file" command line utility assert "shell script" in output assert "executable" in output
def get_filetype(filepath): if isinstance(filepath, str): filepath = homedir_replace(filepath) return sh.file(filepath, '-b', '--mime') else: raise TypeError('argument type must be "str"')
def files_info(files): print(sh.file( sh.glob(files )))
def files_info(files): print(sh.file(sh.glob(files)))
gitIgnorePath.append(root) path = op.join(root, filename) print path if op.islink(path): # Without this check the stat below will fail # when we have a bad symlink, like the .# kind emacs # creates deliberately to lock files. continue filesize = os.stat(path).st_size relpath = op.relpath(path, args.dir) if filesize > 1024*800: # 800KB # PDFs are ok, they maybe big ext = op.splitext(path)[-1] if ext != ".pdf" and ext != ".pack": tooLarge.add(relpath) fileinfo = sh.file(path) if "ELF" in fileinfo and "executable" in fileinfo: executable.add(relpath) if len(gitIgnorePath) < 1: print >> sys.stderr, "Couldn't find git repo." sys.exit(1) elif len(gitIgnorePath) > 1: print >> sys.stderr, "Found more than one .gitignore, using least nested for filtering." gitIgnorePath = [min(gitIgnorePath, key=lambda x: op.normpath(x).count(os.sep))] print ".gitignore path: " + op.join(gitIgnorePath[0], ".gitignore") # Figure out which files git will ignore os.chdir(gitIgnorePath[0]) ignored_files = sh.cut(sh.git("ls-files", "--others", "-i", "--exclude-standard"), "-d ", "-f3-").stdout.split("\n")