簡體   English   中英

如何在Python中判斷文件在Windows上是否可執行?

[英]How to tell whether a file is executable on Windows in Python?

我正在編寫grepath實用程序,它在%PATH%中查找與模式匹配的可執行文件。 我需要定義路徑中給定的文件名是否可執行(重點是命令行腳本)。

基於“告訴文件是否可執行”我得到:

import os
from pywintypes import error
from win32api   import FindExecutable, GetLongPathName

def is_executable_win(path):
    try:
        _, executable = FindExecutable(path)
        ext = lambda p: os.path.splitext(p)[1].lower()
        if (ext(path) == ext(executable) # reject *.cmd~, *.bat~ cases
            and samefile(GetLongPathName(executable), path)):
            return True
        # path is a document with assoc. check whether it has extension
        # from %PATHEXT% 
        pathexts = os.environ.get('PATHEXT', '').split(os.pathsep)
        return any(ext(path) == e.lower() for e in pathexts)
    except error:
        return None # not an exe or a document with assoc.

其中samefile是:

try: samefile = os.path.samefile
except AttributeError:    
    def samefile(path1, path2):
        rp = lambda p: os.path.realpath(os.path.normcase(p))
        return rp(path1) == rp(path2)

如何在給定的上下文中改進is_executable_win Win32 API的哪些功能可以提供幫助?

PS

  • 時間表現無關緊要
  • subst驅動器和UNC,unicode路徑不在考慮之中
  • 如果它使用Windows XP上提供的功能,則可以使用C ++答案

例子

  • notepad.exe是可執行的(作為規則)
  • 如果它與某些可執行文件(例如,python.exe)相關聯,則which.py是可執行的,而.PY%PATHEXT%'C:\\> which'可以啟動:

     some\\path\\python.exe another\\path\\in\\PATH\\which.py 
  • somefile.doc很可能不可執行(例如,當它與Word關聯時)

  • another_file.txt 不可執行(作為規則)
  • ack.pl是可執行的,如果它與某些可執行文件(最可能是perl.exe)相關聯.PL放在%PATHEXT% (即如果它在路徑中,我可以運行ack而不指定擴展名)

這個問題中的“可執行文件”是什么

def is_executable_win_destructive(path):
    #NOTE: it assumes `path` <-> `barename` for the sake of example
    barename = os.path.splitext(os.path.basename(path))[0]
    p = Popen(barename, stdout=PIPE, stderr=PIPE, shell=True)
    stdout, stderr = p.communicate()
    return p.poll() != 1 or stdout != '' or stderr != error_message(barename)

error_message()取決於語言。 英文版是:

def error_message(barename):
    return "'%(barename)s' is not recognized as an internal" \
           " or external\r\ncommand, operable program or batch file.\r\n" \
           %  dict(barename=barename)

如果is_executable_win_destructive()在定義路徑是否指向此問題的可執行文件時返回。

例:

>>> path = r"c:\docs\somefile.doc"
>>> barename = "somefile"

之后它執行%COMSPEC%(默認為cmd.exe):

c:\cwd> cmd.exe /c somefile

如果輸出如下所示:

'somefile' is not recognized as an internal or external
command, operable program or batch file.

然后path不是其他可執行文件(假設為了示例, pathbarename之間存在一對一的對應關系)。

另一個例子:

>>> path = r'c:\bin\grepath.py'
>>> barename = 'grepath'

如果%PATHEXT%c:\\bin .PY%PATHEXT% %PATH%則:

c:\docs> grepath
Usage:
  grepath.py [options] PATTERN
  grepath.py [options] -e PATTERN

grepath.py: error: incorrect number of arguments

上面的輸出不等於error_message(barename)因此'c:\\bin\\grepath.py'是一個“可執行文件”。

所以問題是如何找出path是否會產生錯誤而不實際運行它? 什么Win32 API函數和用於觸發'的條件不被識別為內部..'錯誤?

我認為,這應該足夠了:

  1. 在PATHEXT中檢查文件擴展名 - 文件是否可直接執行
  2. 使用cmd.exe命令“assoc .ext”可以查看文件是否與某些可執行文件相關聯(啟動此文件時將啟動某些可執行文件)。 您可以解析不帶參數的assoc的捕獲輸出,並收集所有關聯的擴展並檢查測試的文件擴展名。
  3. 其他文件擴展名將觸發錯誤“命令無法識別...”因此您可以假設此類文件不可執行。

我真的不明白你如何分辨somefile.py和somefile.txt之間的區別,因為關聯可能真的是一樣的。 您可以將系統配置為以.py文件的相同方式運行.txt文件。

Windows PE始終以字符“MZ”開頭。 然而,這也包括任何類型的DLL,它們不一定是可執行的。
要檢查這一點,你必須打開文件並閱讀標題,這可能不是你想要的。

shoosh打敗了我:)

如果我沒記錯的話,你應該嘗試讀取文件中的前2個字符。 如果你回到“MZ”,你就有了一個exe。


hnd = open(file,"rb")
if hnd.read(2) == "MZ":
  print "exe"

這是我在問題中鏈接的grepath.py

#!/usr/bin/env python
"""Find executables in %PATH% that match PATTERN.

"""
#XXX: remove --use-pathext option

import fnmatch, itertools, os, re, sys, warnings
from optparse import OptionParser
from stat import S_IMODE, S_ISREG, ST_MODE
from subprocess import PIPE, Popen


def warn_import(*args):
    """pass '-Wd' option to python interpreter to see these warnings."""
    warnings.warn("%r" % (args,), ImportWarning, stacklevel=2)


class samefile_win:
    """
http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html
"""
    @staticmethod
    def get_read_handle (filename):
        return win32file.CreateFile (
            filename,
            win32file.GENERIC_READ,
            win32file.FILE_SHARE_READ,
            None,
            win32file.OPEN_EXISTING,
            0,
            None
            )

    @staticmethod
    def get_unique_id (hFile):
        (attributes,
         created_at, accessed_at, written_at,
         volume,
         file_hi, file_lo,
         n_links,
         index_hi, index_lo
         ) = win32file.GetFileInformationByHandle (hFile)
        return volume, index_hi, index_lo

    @staticmethod
    def samefile_win(filename1, filename2):
        """Whether filename1 and filename2 represent the same file.

It works for subst, ntfs hardlinks, junction points.
It works unreliably for network drives.

Based on GetFileInformationByHandle() Win32 API call.
http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html
"""
        if samefile_generic(filename1, filename2): return True
        try:
            hFile1 = samefile_win.get_read_handle (filename1)
            hFile2 = samefile_win.get_read_handle (filename2)
            are_equal = (samefile_win.get_unique_id (hFile1)
                         == samefile_win.get_unique_id (hFile2))
            hFile2.Close ()
            hFile1.Close ()
            return are_equal
        except win32file.error:
            return None


def canonical_path(path):
    """NOTE: it might return wrong path for paths with symbolic links."""
    return os.path.realpath(os.path.normcase(path))


def samefile_generic(path1, path2):
    return canonical_path(path1) == canonical_path(path2)


class is_executable_destructive:
    @staticmethod
    def error_message(barename):
        r"""
"'%(barename)s' is not recognized as an internal or external\r\n
command, operable program or batch file.\r\n"

in Russian:
"""
        return '"%(barename)s" \xad\xa5 \xef\xa2\xab\xef\xa5\xe2\xe1\xef \xa2\xad\xe3\xe2\xe0\xa5\xad\xad\xa5\xa9 \xa8\xab\xa8 \xa2\xad\xa5\xe8\xad\xa5\xa9\r\n\xaa\xae\xac\xa0\xad\xa4\xae\xa9, \xa8\xe1\xaf\xae\xab\xad\xef\xa5\xac\xae\xa9 \xaf\xe0\xae\xa3\xe0\xa0\xac\xac\xae\xa9 \xa8\xab\xa8 \xaf\xa0\xaa\xa5\xe2\xad\xeb\xac \xe4\xa0\xa9\xab\xae\xac.\r\n' % dict(barename=barename)

    @staticmethod
    def is_executable_win_destructive(path):
        # assume path <-> barename that is false in general
        barename = os.path.splitext(os.path.basename(path))[0]
        p = Popen(barename, stdout=PIPE, stderr=PIPE, shell=True)
        stdout, stderr = p.communicate()
        return p.poll() != 1 or stdout != '' or stderr != error_message(barename)


def is_executable_win(path):
    """Based on:
http://timgolden.me.uk/python/win32_how_do_i/tell-if-a-file-is-executable.html

Known bugs: treat some "*~" files as executable, e.g. some "*.bat~" files
"""
    try:
        _, executable = FindExecutable(path)
        return bool(samefile(GetLongPathName(executable), path))
    except error:
        return None # not an exe or a document with assoc.


def is_executable_posix(path):
    """Whether the file is executable.

Based on which.py from stdlib
"""
    #XXX it ignores effective uid, guid?
    try: st = os.stat(path)
    except os.error:
        return None

    isregfile = S_ISREG(st[ST_MODE])
    isexemode = (S_IMODE(st[ST_MODE]) & 0111)
    return bool(isregfile and isexemode)

try:
    #XXX replace with ctypes?
    from win32api import FindExecutable, GetLongPathName, error
    is_executable = is_executable_win
except ImportError, e:
    warn_import("is_executable: fall back on posix variant", e)
    is_executable = is_executable_posix

try: samefile = os.path.samefile
except AttributeError, e:
    warn_import("samefile: fallback to samefile_win", e)
    try:
        import win32file
        samefile = samefile_win.samefile_win
    except ImportError, e:
        warn_import("samefile: fallback to generic", e)
        samefile = samefile_generic

def main():
    parser = OptionParser(usage="""
%prog [options] PATTERN
%prog [options] -e PATTERN""", description=__doc__)
    opt = parser.add_option
    opt("-e", "--regex", metavar="PATTERN",
        help="use PATTERN as a regular expression")
    opt("--ignore-case", action="store_true", default=True,
        help="""[default] ignore case when --regex is present; for \
non-regex PATTERN both FILENAME and PATTERN are first \
case-normalized if the operating system requires it otherwise \
unchanged.""")
    opt("--no-ignore-case", dest="ignore_case", action="store_false")
    opt("--use-pathext", action="store_true", default=True,
        help="[default] whether to use %PATHEXT% environment variable")
    opt("--no-use-pathext", dest="use_pathext", action="store_false")
    opt("--show-non-executable", action="store_true", default=False,
        help="show non executable files")

    (options, args) = parser.parse_args()

    if len(args) != 1 and not options.regex:
       parser.error("incorrect number of arguments")
    if not options.regex:
       pattern = args[0]
    del args

    if options.regex:
       filepred = re.compile(options.regex, options.ignore_case and re.I).search
    else:
       fnmatch_ = fnmatch.fnmatch if options.ignore_case else fnmatch.fnmatchcase
       for file_pattern_symbol in "*?":
           if file_pattern_symbol in pattern:
               break
       else: # match in any place if no explicit file pattern symbols supplied
           pattern = "*" + pattern + "*"
       filepred = lambda fn: fnmatch_(fn, pattern)

    if not options.regex and options.ignore_case:
       filter_files = lambda files: fnmatch.filter(files, pattern)
    else:
       filter_files = lambda files: itertools.ifilter(filepred, files)

    if options.use_pathext:
       pathexts = frozenset(map(str.upper,
            os.environ.get('PATHEXT', '').split(os.pathsep)))

    seen = set()
    for dirpath in os.environ.get('PATH', '').split(os.pathsep):
        if os.path.isdir(dirpath): # assume no expansion needed
           # visit "each" directory only once
           # it is unaware of subst drives, junction points, symlinks, etc
           rp = canonical_path(dirpath)
           if rp in seen: continue
           seen.add(rp); del rp

           for filename in filter_files(os.listdir(dirpath)):
               path = os.path.join(dirpath, filename)
               isexe = is_executable(path)

               if isexe == False and is_executable == is_executable_win:
                  # path is a document with associated program
                  # check whether it is a script (.pl, .rb, .py, etc)
                  if not isexe and options.use_pathext:
                     ext = os.path.splitext(path)[1]
                     isexe = ext.upper() in pathexts

               if isexe:
                  print path
               elif options.show_non_executable:
                  print "non-executable:", path


if __name__=="__main__":
   main()

解析PE格式。

http://code.google.com/p/pefile/

除了使用python實際嘗試運行程序之外,這可能是您獲得的最佳解決方案。

編輯:我看到你也想要有關聯的文件。 這將需要在我沒有信息的注冊表中。

Edit2:我也看到你區分.doc和.py。 這是一個相當隨意的區分,必須使用手動規則來指定,因為對於Windows,它們都是程序讀取的文件擴展名。

你的問題無法回答。 Windows無法區分與腳本語言相關的文件與其他任意程序之間的區別。 就Windows而言,.PY文件只是一個由python.exe打開的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM