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!

    • Strit
      link
      fedilink
      412 days ago

      Yeah, it basically means maintaining the package yourself on your system.

        • Responsabilidade
          link
          fedilink
          712 days ago

          Agreed. In Arch you can run a post-script after update in pacman, so you can update the python file. But I don’t know how other package managers deal with it

      • @wildbus8979@sh.itjust.works
        link
        fedilink
        10
        edit-2
        12 days ago

        I literally listed two ways this could be dependent on the package manager and distribution you’re using in the other comment. But hey evidently you know best as you already know the solution to this problem…

  • @Diabolo96@lemmy.dbzer0.com
    link
    fedilink
    5
    edit-2
    12 days 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
    link
    fedilink
    3
    edit-2
    12 days 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
    212 days 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.

    • @CoderSupreme@programming.devOP
      link
      fedilink
      3
      edit-2
      12 days 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.

  • Album
    link
    fedilink
    112 days 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.