views:

864

answers:

5

I have written a script that will keep itself up to date by downloading the latest version from a website and overwriting the running script.

I am not sure what the best way to restart the script after it has been updated.

Any ideas?

I don't really want to have a separate update script. oh and it has to work on both linux/windows too.

A: 

The pocoo team have a very good reloader for their development server inside of werkzueg. Check the code out here (it's towards the bottom of the file).

Bryan McLemore
+3  A: 

In Linux, or any other form of unix, os.execl and friends are a good choice for this -- you just need to re-exec sys.executable with the same parameters it was executed with last time (sys.argv, more or less) or any variant thereof if you need to inform your next incarnation that it's actually a restart. On Windows, os.spawnl (and friends) is about the best you can do (though it will transiently take more time and memory than os.execl and friends would during the transition).

Alex Martelli
Probably the best way to go if the script is simple enough.
Luper Rouch
+1  A: 

The cleanest solution is a separate update script!

Run your program inside it, report back (when exiting) that a new version is available. This allows your program to save all of its data, the updater to apply the update, and run the new version, which then loads the saved data and continues. To the user this can be completely transparent, as they just run the updater-shell which runs the real program.

Roger Pate
+1  A: 

You can use reload() to reload a module.

Alexandru
A: 

Hello, I think the best solution whould be something like this:

Your normal program:

...

# ... part that downloaded newest files and put it into the "newest" folder

from subprocess import Popen

Popen("/home/code/reloader.py", shell=True) # start reloader

exit("exit for updating all files")

The update script: (e.g.: home/code/reloader.py)

from shutil import copy2, rmtree
from sys import exit

# maybie you could do this automatic:
copy2("/home/code/newest/file1.py", "/home/code/") # copy file
copy2("/home/code/newest/file2.py", "/home/code/")
copy2("/home/code/newest/file3.py", "/home/code/")
...

rmtree('/home/code/newest') # will delete the folder itself

Popen("/home/code/program.py", shell=True) # go back to your program

exit("exit to restart the true program")

I hope this will help you.

Joschua