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!
This is going to be quite dependent on the package manager and distribution you use…
Yeah, it basically means maintaining the package yourself on your system.
Not necessarily, you could add post install scripts, dpkg-divert, or other techniques.
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
deleted by creator
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…
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.
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.
Is it not kept in a git repository?
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.
That’s literally what /usr/bin/patch is for.
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.
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
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.