Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of Regex Match subscript causes bug #11

Open
cefn opened this issue Apr 22, 2020 · 0 comments
Open

Use of Regex Match subscript causes bug #11

cefn opened this issue Apr 22, 2020 · 0 comments

Comments

@cefn
Copy link
Contributor

cefn commented Apr 22, 2020

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant