[Tutor] How to deploy seamless script updates to your "clients"?
mats at wichmann.us
Wed May 24 20:59:17 EDT 2017
On 05/24/2017 04:10 PM, Juan C. wrote:
> I have some Python 3.6.0 scripts that my co-workers use for some small
> and medium tasks. Whenever I have time I fix some bugs and add some
> features to said scripts to make their lives (and mine :D) easier, but
> there's a problem: I need to send a new script via email/chat/whatever
> and they have to replace it wherever they use it, such a hassle.
> How would I go to put a "update module" inside my script? I was
> thinking about using Git, because those scripts are already in
> personal repositories so that I can keep track of changes. Will I have
> to setup any special permissions so that the scripts can pull requests
> from my private repositories?
> I was thinking of something like "check for update before start", if
> an update is found the script would replace itself with the newer
> version and restart, is that possible? For example, 'cool-tool.py'
> v0.2 starts and find that version v0.3 is out, so it downloads and
> replace 'cool-tool.py' code with newer code and restart as v0.3.
Yes, this is definitely a problem others have built solutions for.
It differs a bit depending on whether the script to potentially be
updated is a long-running one or one that can afford to "check on
startup", but not actually that much.
You can use os.execv to restart a script, there are several permutations
but they look something like (pick the bits you need, might not need all
args = sys.argv[:]
os.chdir(foo) # foo being a saved copy of the directory you start in,
in case the program changes directories while running
that's the easy part, though. self-updating software is tricky... much
easier if you're just talking about a single file, but then what if
updating fails, of if the update itself is broken? Then the user is left
with a non-working setup; you'll have to make sure there's a way to get
back to working that they can figure out how to operate.
For a single file you could put on github, or a local server of course,
and on update check download the file (requests module, perhaps), decide
if that's a new version, if so replace self and restart. If that's too
expensive you can put in a timer - if it's been a week since the last
update (for example), do the download-and-check. If neither of those
work, you'll have to build an API that can query versions... that's more
work for you, but maybe worth it. I think there's an update checker
program somewhere on PyPi, but maybe it only works for things located on
More information about the Tutor