当前位置: 首页>>代码示例>>Python>>正文


Python magic.open方法代码示例

本文整理汇总了Python中magic.open方法的典型用法代码示例。如果您正苦于以下问题:Python magic.open方法的具体用法?Python magic.open怎么用?Python magic.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在magic的用法示例。


在下文中一共展示了magic.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: magic

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def magic(indata, mime=False):
        """
        Performs file magic while maintaining compatibility with different
        libraries.
        """

        try:
            if mime:
                mymagic = magic.open(magic.MAGIC_MIME_TYPE)
            else:
                mymagic = magic.open(magic.MAGIC_NONE)
            mymagic.load()
        except AttributeError:
            mymagic = magic.Magic(mime)
            mymagic.file = mymagic.from_file
        return mymagic.file(indata) 
开发者ID:kyechou,项目名称:firmanal,代码行数:18,代码来源:extractor.py

示例2: computehasharchive

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def computehasharchive((filedir, checksums, version, filename)):
	resolved_path = os.path.join(filedir, filename)
	try:
		os.stat(resolved_path)
	except:
		print >>sys.stderr, "Can't find %s" % filename
		return None
	if checksums.has_key(filename):
		filehash = checksums[filename]
	else:
		scanfile = open(resolved_path, 'r')
		h = hashlib.new('sha256')
		h.update(scanfile.read())
		scanfile.close()
		filehash = h.hexdigest()
	return (filename, version, filehash)

## per package:
## 1. unpack archive
## 2. scan all files and compute hashes
## 3. lookup what is available in the database from the same package and origin
## 4. copy unique and unscanned files to new archive dir 
## 5. create text file with metadata
## 6. pack archive
## 7. pack archive + metadata into BAT archive 
开发者ID:armijnhemel,项目名称:binaryanalysis,代码行数:27,代码来源:createbatarchive.py

示例3: computehash

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def computehash((filedir, filename, extrahashes, sha256sum)):
	resolved_path = os.path.join(filedir, filename)
	filehashes = {}
	filehashes['sha256'] = sha256sum
	scanfile = open(resolved_path, 'r')
	data = scanfile.read()
	scanfile.close()
	for i in extrahashes:
		if i == 'crc32':
			filehashes[i] = zlib.crc32(data) & 0xffffffff
		elif i == 'tlsh':
			if os.stat(resolved_path).st_size >= 256:
				tlshhash = tlsh.hash(data)
				filehashes[i] = tlshhash
			else:
				filehashes[i] = None
		else:
			h = hashlib.new(i)
			h.update(data)
			filehashes[i] = h.hexdigest()
	filehashes['sha256'] = sha256sum
	return (filedir, filename, filehashes) 
开发者ID:armijnhemel,项目名称:binaryanalysis,代码行数:24,代码来源:createmanifests.py

示例4: checkalreadyscanned

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def checkalreadyscanned((filedir, filename, checksum)):
	resolved_path = os.path.join(filedir, filename)
	try:
		os.stat(resolved_path)
	except:
		print >>sys.stderr, "Can't find %s" % filename
		return None
	if checksum != None:
		filehash = checksum
	else:
		scanfile = open(resolved_path, 'r')
		h = hashlib.new('sha256')
		h.update(scanfile.read())
		scanfile.close()
		filehash = h.hexdigest()
	return (filename, filehash) 
开发者ID:armijnhemel,项目名称:binaryanalysis,代码行数:18,代码来源:createmanifests.py

示例5: readrewritelist

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def readrewritelist(rewritelist):
	## rewrite is a hash. Key is sha256 of the file.
	rewrite = {}
	try:
		rewritefile = open(rewritelist, 'r')
		rewritelines = rewritefile.readlines()
		rewritefile.close()
		for r in rewritelines:
			rs = r.strip().split()
			## format error, bail out
			if len(rs) != 7:
				return {}
			(package, version, filename, origin, sha256, newp, newv) = rs
			## dupe, skip
			if sha256 in rewrite:
				continue
			rewrite[sha256] = {'package': package, 'version': version, 'filename': filename, 'origin': origin, 'newpackage': newp, 'newversion': newv}
	except:
		return {}
	return rewrite

## split on the special characters, plus remove special control characters that are
## at the beginning and end of the string in escaped form.
## Return a list of strings. 
开发者ID:armijnhemel,项目名称:binaryanalysis,代码行数:26,代码来源:createdb.py

示例6: smart_open

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def smart_open(filename):
    '''
    Returns an open file object if `filename` is plain text, else assumes
    it is a bzip2 compressed file and returns a file-like object to
    handle it.
    '''
    if isplaintext(filename):
        f = open(filename, 'rt')
    else:
        file_type = mimetype(filename)
        if file_type.find('gzip') > -1:
            f = gzip.GzipFile(filename, 'rt')
        elif file_type.find('bzip2') > -1:
            f = bz2file.open(filename, 'rt')
        else:
            pass  # Not supported format
    return f 
开发者ID:rucio,项目名称:rucio,代码行数:19,代码来源:__init__.py

示例7: gfal_download_to_file

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def gfal_download_to_file(url, file_):
    '''
    Download the file in `url` storing it in the `file_` file-like
    object.
    '''
    logger = logging.getLogger('dumper.__init__')
    ctx = gfal2.creat_context()  # pylint: disable=no-member
    infile = ctx.open(url, 'r')

    try:
        chunk = infile.read(CHUNK_SIZE)
    except GError as e:
        if e.code == 70:
            logger.debug('GError(70) raised, using GRIDFTP PLUGIN:STAT_ON_OPEN=False workarround to download %s', url)
            ctx.set_opt_boolean('GRIDFTP PLUGIN', 'STAT_ON_OPEN', False)
            infile = ctx.open(url, 'r')
            chunk = infile.read(CHUNK_SIZE)
        else:
            raise

    while chunk:
        file_.write(chunk)
        chunk = infile.read(CHUNK_SIZE) 
开发者ID:rucio,项目名称:rucio,代码行数:25,代码来源:__init__.py

示例8: get_filetype

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def get_filetype(data):
    """There are two versions of python-magic floating around, and annoyingly, the interface 
    changed between versions, so we try one method and if it fails, then we try the other.
    NOTE: you may need to alter the magic_file for your system to point to the magic file."""
    if sys.modules.has_key('magic'):
        try:
            ms = magic.open(magic.MAGIC_NONE) 
            ms.load() 
            return ms.buffer(data)
        except:
            try:
                return magic.from_buffer(data)
            except magic.MagicException:
                magic_custom = magic.Magic(magic_file='C:\windows\system32\magic')
                return magic_custom.from_buffer(data)
    return '' 
开发者ID:omriher,项目名称:CapTipper,代码行数:18,代码来源:pescanner.py

示例9: open

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def open(self):
            self.__inPos = 0
            self.__inPosOld = 0
            self.__outFile = None
            self.__current = DirHasher.FileIndex.Stat()
            try:
                if os.path.exists(self.__cachePath):
                    self.__inFile = open(self.__cachePath, "rb")
                    sig = self.__inFile.read(4)
                    if sig == DirHasher.FileIndex.SIGNATURE:
                        self.__mismatch = False
                        self.__inPos = self.__inPosOld = 4
                        self.__readEntry() # prefetch first entry
                    else:
                        logging.getLogger(__name__).info(
                            "Wrong signature at '%s': %s", self.__cachePath, sig)
                        self.__inFile.close()
                        self.__inFile = None
                        self.__mismatch = True
                else:
                    self.__inFile = None
                    self.__mismatch = True
            except OSError as e:
                raise BuildError("Error opening hash cache: " + str(e)) 
开发者ID:BobBuildTool,项目名称:bob,代码行数:26,代码来源:utils.py

示例10: summonMagic

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def summonMagic():
    import magic
    if hasattr(magic, 'from_file'):
        # https://pypi.python.org/pypi/python-magic
        return magic
    elif hasattr(magic, 'open'):
        # http://www.darwinsys.com/file/, in Debian as python3-magic
        class WrapMagic:
            def __init__(self):
                self.magic = magic.open(magic.NONE)
                self.magic.load()

            def from_file(self, name):
                return self.magic.file(name)
        return WrapMagic()
    else:
        raise NotImplementedError("I do not understand your magic")

### directory copy ### 
开发者ID:BobBuildTool,项目名称:bob,代码行数:21,代码来源:utils.py

示例11: _get_filetype

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def _get_filetype(self, data):
        """Gets filetype, uses libmagic if available.
        @param data: data to be analyzed.
        @return: file type or None.
        """
        if not HAVE_MAGIC:
            return None

        try:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()
            file_type = ms.buffer(data)
        except:
            try:
                file_type = magic.from_buffer(data)
            except Exception:
                return None
        finally:
            try:
                ms.close()
            except:
                pass

        return file_type 
开发者ID:phdphuc,项目名称:mac-a-mal-cuckoo,代码行数:26,代码来源:static.py

示例12: get_type

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def get_type(self):
        try:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()
            file_type = ms.file(self.path)
        except:
            try:
                file_type = magic.from_file(self.path)
            except:
                try:
                    import subprocess
                    file_process = subprocess.Popen(['file', '-b', self.path], stdout = subprocess.PIPE)
                    file_type = file_process.stdout.read().strip()
                except:
                    return ''
        finally:
            try:
                ms.close()
            except:
                pass

        return file_type 
开发者ID:opensourcesec,项目名称:CIRTKit,代码行数:24,代码来源:objects.py

示例13: get_type

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def get_type(data):
    try:
        ms = magic.open(magic.MAGIC_NONE)
        ms.load()
        file_type = ms.buffer(data)
    except:
        try:
            file_type = magic.from_buffer(data)
        except:
            return ''
    finally:
        try:
            ms.close()
        except:
            pass

    return file_type 
开发者ID:opensourcesec,项目名称:CIRTKit,代码行数:19,代码来源:utils.py

示例14: get_type

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def get_type (t, f):
    path = '/tmp/tar2db_evaluate'
    if not os.path.isdir(path):
        os.makedirs(path)
    t.extract(f, path);

    m = magic.open(magic.MAGIC_MIME)
    m.load()
    return m.file(path + f.name[1:]) 
开发者ID:kyechou,项目名称:firmanal,代码行数:11,代码来源:importdb.py

示例15: getFileHashes

# 需要导入模块: import magic [as 别名]
# 或者: from magic import open [as 别名]
def getFileHashes(infile):
    t = tarfile.open(infile)
    files = list()
    links = list()
    for f in t.getmembers():
        if f.isfile():
            mime = get_type (t, f)
            # we use f.name[1:] to get rid of the . at the beginning of the path
            files.append((f.name[1:], hashlib.md5(t.extractfile(f).read()).hexdigest(),
                          f.uid, f.gid, f.mode, mime, get_priority (f, mime)))
        elif f.issym():
            links.append((f.name[1:], f.linkpath))
    shutil.rmtree ('/tmp/tar2db_evaluate')
    return (files, links) 
开发者ID:kyechou,项目名称:firmanal,代码行数:16,代码来源:importdb.py


注:本文中的magic.open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。