Rat-OS / RatOS

The preconfigured Raspberry Pi image that makes it easy to run Klipper + Moonraker + Mainsail on your printer.
https://os.ratrig.com
GNU General Public License v3.0
189 stars 113 forks source link

Use regex to extract pinned commits from moonraker.conf and reset repo's to the correct commit hashes during build in respective modules. #156

Open miklschmidt opened 3 weeks ago

miklschmidt commented 3 weeks ago

The following somewhat janky regex should work

\[update_manager\s(?<module>\w+)]((?!\[)|(?!\n\n)\X)+pinned_commit:\s(?<commit>[[:xdigit:]]+)
CrashTestCharlie commented 5 days ago

What's the context for this regex?

Specifically,

  1. What environment does it run it?
  2. Does whatever is doing the calling know which repo it's trying to rebuild? (I would assume so, but need to check.)
  3. Could the caller call out to an external script with the repo name and path to the moonraker.conf file?

If 3, then a python script that reads the conf file and properly parses it could be a better solution.

Sample code would look something like this:

#! /usr/bin/env python3

import re
import configparser
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-r", "--repo", dest="repo")

def extract_commit(repo, file_name):
    section_name = "update_manager" + " " + repo
    config = configparser.ConfigParser()
    config.read(file_name)

    if section_name not in config.sections():
        return None

    if "pinned_commit" not in config[section_name]:
        return None

    return config[section_name]["pinned_commit"]

if __name__ == "__main__":
    (opts, args) = parser.parse_args()
    pin = extract_commit(opts.repo, args[0])
    if pin is None:
        exit(1)

    print(pin)

Result:

$ /tmp/regex.py -r KlipperScreen moonraker.conf
71eef9ee1f23aa4fd6b68169cfe5dd7908e478b2

or

$ /tmp/regex.py -r foo moonraker.conf
$ echo $?
1