There’s a Python program that I update regularly. Every time I update the program, I need to modify a specific Python file to change its functionality. Instead of manually patching the file each time, I’d like to automate the process.

I’m looking for a way to automatically apply a patch to a Python file whenever the program updates or if the file doesn’t have the desired modifications.

Any advice or suggestions on how to automate this process would be greatly appreciated!

  • Diabolo96@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    7 months ago

    Easy. Don’t run the python files directly but create a a launcher script that use md5 hash to check if the python file you wanna run changed and then apply the patch before actually running the patched python file. This avoid ever running the unpatched version.

  • Avid Amoeba@lemmy.ca
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    7 months ago

    If you want to ensure no surprise breakage, you’d want to create your own package, apply the patch and version it a lot higher than the current version of the original and install it. The downside is you won’t get updates of the original unless you respin your package based on the latest.

    If you don’t care about inevitable breakage, the myriad proposed patching processes would work. At some point your patch won’t apply and you’d have to go in there and fix things.

  • JustTesting@lemmy.hogru.ch
    link
    fedilink
    arrow-up
    2
    ·
    7 months ago

    one way to do this from within python itself would be to use the site module with pth files to monkeypatch the code in question. This would amount to patching it each time it gets started, not modifying the python file permanently, and without having to touch the original python code at all.

    This write-up goes into more details and also links to this (unmaintained) tool for doing so.

  • Album@lemmy.ca
    link
    fedilink
    arrow-up
    1
    ·
    7 months ago

    Create a shell script to launch the Python script but make it the last call.

    Then before it add commands for whatever method you update with.

  • snake_case_lover@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    7 months ago

    host the version on the Internet. Make the script check for update before running. You can cache it on the filesystem too if you want

    • CoderSupreme@programming.devOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      7 months ago

      I don’t really understand what you mean, but this gave me an idea. I can save the version of the software to a file after applying the patch, and then set up a cron job to check if the new software has a different version. If it does, the cron job will apply the patch and update the file with the new version.