Skip to content

Use of Regex Match subscript causes bug #11

@cefn

Description

@cefn

Not sure what version of python is involved but the code which WAS previously working in vanguard brainwash to introspect available firmware versions but it is choking now on a 16.10 Ubuntu with Python 3.5.2.

It previously employed subscripts to get matching groups (as if a Match could be treated as an array). Something like...

firmwares.append(FirmwareData(path=filePath, base=fileMatch[0], name=fileMatch[1], release=fileMatch[2]))

This caused an error like TypeError: '_sre.SRE_Match' object is not subscriptable

To fix it I had to manually change a line to read, as follows, making an explicit call to the group method instead of using the subscript syntax...

firmwares.append(FirmwareData(path=filePath, base=fileMatch.group(0), name=fileMatch.group(1), release=fileMatch.group(2)))

The full working function looks like...

def calculateImageFile(target, release):
    targetAliases = {
        None: "vanguard",
        "vanguard": "vanguard+rainbow",
        "python": "micropython",
        "javascript": "espruino",
        "basic": "esp8266basic",
        "forth": "punyforth",
        "lua": "nodemcu",
    }
    if target in targetAliases:
        target = targetAliases[target]
        return calculateImageFile(target, release)
    else:
        import os, re
        from collections import namedtuple
        from vgkits.vanguard import calculateDataDir

        FirmwareData = namedtuple("ImageData", "path base name release")

        firmwareDir = calculateDataDir("firmware")
        firmwarePattern = re.compile("(" + re.escape(target) + ")-?v?([0-9].*)\.bin")
        firmwares = list()

        for root, dirnames, filenames in os.walk(firmwareDir):
            for filename in filenames:
                filePath = os.path.join(root, filename)
                fileMatch = firmwarePattern.search(filePath)
                if fileMatch:
                    firmwares.append(FirmwareData(path=filePath, base=fileMatch.group(0), name=fileMatch.group(1), release=fileMatch.group(2)))

            if len(firmwares) > 0:
                if release is not None: # accept only matching release
                    for firmware in firmwares:
                        if firmware.release == release:
                            return firmware.path
                    else:
                        return None
                else: # choose latest by semver order
                    order = sorted(firmwares, key=lambda image:[int(part) for part in image.release.split(".")], reverse=True)
                    return order[0].path

            raise FileNotFoundError("No image matching {} available".format(target))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions