Пример #1
0
def decode(pathnm):
    h = win32api.LoadLibraryEx(pathnm, 0, LOAD_LIBRARY_AS_DATAFILE)
    res = win32api.EnumResourceNames(h, pefile.RESOURCE_TYPE['RT_VERSION'])
    if not len(res):
        return None
    data = win32api.LoadResource(h, pefile.RESOURCE_TYPE['RT_VERSION'], res[0])
    vs = VSVersionInfo()
    j = vs.fromRaw(data)
    win32api.FreeLibrary(h)
    return vs
Пример #2
0
def _GetResources(hsrc, types=None, names=None, languages=None):
    """
    Get resources from hsrc.

    types = a list of resource types to search for (None = all)
    names = a list of resource names to search for (None = all)
    languages = a list of resource languages to search for (None = all)
    Return a dict of the form {type_: {name: {language: data}}}, which might also be empty if no matching resources
    were found.
    """
    if types:
        types = set(types)
    if names:
        names = set(names)
    if languages:
        languages = set(languages)
    res = {}
    try:
        # logger.debug("Enumerating resource types")
        enum_types = win32api.EnumResourceTypes(hsrc)
        if types and "*" not in types:
            enum_types = filter(lambda type_: type_ in types, enum_types)
        for type_ in enum_types:
            # logger.debug("Enumerating resources of type %s", type_)
            enum_names = win32api.EnumResourceNames(hsrc, type_)
            if names and "*" not in names:
                enum_names = filter(lambda name: name in names, enum_names)
            for name in enum_names:
                # logger.debug("Enumerating resources of type %s name %s", type_, name)
                enum_languages = win32api.EnumResourceLanguages(
                    hsrc, type_, name)
                if languages and "*" not in languages:
                    enum_languages = filter(
                        lambda language: language in languages, enum_languages)
                for language in enum_languages:
                    data = win32api.LoadResource(hsrc, type_, name, language)
                    if type_ not in res:
                        res[type_] = {}
                    if name not in res[type_]:
                        res[type_][name] = {}
                    res[type_][name][language] = data
    except pywintypes.error as exception:
        if exception.args[0] in (
                ERROR_RESOURCE_DATA_NOT_FOUND,
                ERROR_RESOURCE_TYPE_NOT_FOUND,
                ERROR_RESOURCE_NAME_NOT_FOUND,
                ERROR_RESOURCE_LANG_NOT_FOUND,
        ):
            # logger.info('%s: %s', exception.args[1:3])
            pass
        else:
            raise exception
    return res
Пример #3
0
def CopyIcons(dstpath, srcpath):
    '''
    Called from building/api.py to handle icons. If the input was by
    --icon on the command line, srcpath is a single string. However it
    is possible to modify the spec file adding icon=['foo.ico','bar.ico']
    to the EXE() statement. In that case, srcpath is a list of strings.

    The string format is either path-to-.ico or path-to-.exe,n for n an
    integer resource index in the .exe. In either case the path can be
    relative or absolute.
    '''

    if isinstance(srcpath, str):
        # just a single string, make it a one-element list
        srcpath = [srcpath]

    def splitter(s):
        '''
        Convert "pathname" to tuple ("pathname", None)
        Convert "pathname,n" to tuple ("pathname", n)
        '''
        try:
            srcpath, index = s.split(',')
            return srcpath.strip(), int(index)
        except ValueError:
            return s, None

    # split all the items in the list into tuples as above.
    srcpath = list(map(splitter, srcpath))

    if len(srcpath) > 1:
        # More than one icon source given. We currently handle multiple
        # icons by calling CopyIcons_FromIco(), which only allows .ico.
        # In principle we could accept a mix of .ico and .exe, but it
        # would complicate things. If you need it submit a pull request.
        #
        # Note that a ",index" on a .ico is just ignored in the single
        # or multiple case.
        srcs = []
        for s in srcpath:
            e = os.path.splitext(s[0])[1]
            if e.lower() != '.ico':
                raise ValueError(
                    'Multiple icons supported only from .ico files')
            srcs.append(s[0])
        return CopyIcons_FromIco(dstpath, srcs)

    # Just one source given.
    srcpath, index = srcpath[0]
    srcext = os.path.splitext(srcpath)[1]
    # Handle the simple case of foo.ico, ignoring any ,index.
    if srcext.lower() == '.ico':
        return CopyIcons_FromIco(dstpath, [srcpath])

    # Single source is not .ico, presumably it is .exe (and if not, some
    # error will occur). If relative, make it relative to the .spec file.
    if not os.path.isabs(srcpath):
        srcpath = os.path.join(config.CONF['specpath'], srcpath)
    if index is not None:
        logger.info("Copying icon from %s, %d", srcpath, index)
    else:
        logger.info("Copying icons from %s", srcpath)

    # Bail out quickly if the input is invalid. Letting images in the wrong
    # format be passed to Window's API gives very cryptic error messages as
    # it's generally unclear why PyInstaller would treat an image file as an
    # executable.
    if srcext != ".exe":
        raise ValueError(
            f"Received icon path '{srcpath}' which exists but is not in the "
            f"correct format. On Windows, only '.ico' images or other "
            f"'.exe' files may be used as icons. Please convert your "
            f"'{srcext}' file to a '.ico' then try again.")

    try:
        # Attempt to load the .ico or .exe containing the icon into memory
        # using the same mechanism as if it were a DLL. If this fails for
        # any reason (for example if the file does not exist or is not a
        # .ico/.exe) then LoadLibraryEx returns a null handle and win32api
        # raises a unique exception with a win error code and a string.
        hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE)
    except pywintypes.error as W32E:
        # We could continue with no icon (i.e. just return) however it seems
        # best to terminate the build with a message.
        raise SystemExit(
            "Unable to load icon file {}\n    {} (Error code {})".format(
                srcpath, W32E.strerror, W32E.winerror))
    hdst = win32api.BeginUpdateResource(dstpath, 0)
    if index is None:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0]
    elif index >= 0:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index]
    else:
        grpname = -index
    data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname)
    win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data)
    for iconname in win32api.EnumResourceNames(hsrc, RT_ICON):
        data = win32api.LoadResource(hsrc, RT_ICON, iconname)
        win32api.UpdateResource(hdst, RT_ICON, iconname, data)
    win32api.FreeLibrary(hsrc)
    win32api.EndUpdateResource(hdst, 0)