em english, Python, windows

How to create an application with auto-update using Python and Esky

This is the 2nd part of  Distributing Python Apps for Windows Desktops series. The 1st part is here: How to create a Python .exe with MSI Installer and CX_freeze

Every time a program has to be updated  is a burden. Remember Java! It feels so uncomfortable, even if you’re an IT guy. You don’t like it, nor does your users. So be nice with them and make an auto-update on your program so they don’t have to download new versions.

To show how to create an application with auto-update using Python and Esky I’ll use the boneca app from part 1. The program was written and compiled but it doesn’t have an auto-update yet. You just generated a installer and the clients using that version will never have updates again. So, now we’re creating a new version using Esky:

pip install esky

 

Let’s modify the boneca.py script to import esky and find for updates on internet when they’re available.

# right after import win32con
import esky

if hasattr(sys,"frozen"):
    app = esky.Esky(sys.executable,"http://teenspirit.com.br/exemplo_boneca/")
    app.auto_update()

When the program initializes it will look up the given URL for some update to download. It does that based on version number. You just have to create a folder and enable it on apache. I’m using one of my sites: http://teenspirit.com.br/exemplo_boneca/.

Now, instead of using the boneca.jpg I’ll use this image (chuck.jpg):

chuck


#replace boneca.jpg to chuck.jpg
os.startfile(os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])),"chuck.jpg"))

Now, let’s update setup.py to use esky:

#setup.py
import esky.bdist_esky
from esky.bdist_esky import Executable as Executable_Esky
from cx_Freeze import setup, Executable

include_files = ['boneca.jpg','chuck.jpg']

setup(
    name = 'boneca',
    version = '1.0.1',
    options = {
        'build_exe': {
            'packages': ['os','sys','ctypes','win32con'],
            'excludes': ['tkinter','tcl','ttk'],
            'include_files': include_files,
            'include_msvcr': True,
        },
        'bdist_esky': {
            'freezer_module': 'cx_freeze',
        }
    },
    data_files = include_files,
    scripts = [
        Executable_Esky(
            "boneca.py",
            gui_only = True,
            #icon = XPTO  # Use an icon if you want.
            ),
    ],
    executables = [Executable('boneca.py',base='Win32GUI')]
    )

As you can see, I’ve changed the version number to 1.0.1 and from this version our program will have an auto-update. This file is different from the previous one, so I’ll try to explain everything that is happening here.

1. Importing bdist_esky and an Executable Esky.

import esky.bdist_esky
from esky.bdist_esky import Executable as Executable_Esky

2. Defining options for the new argument bdist_esky:

        'bdist_esky': {
            'freezer_module': 'cx_freeze',
        }

3. Adding  data_files cause Esky uses it to include files on the app’s folder.

      data_files = include_files,

4. Adding scripts so Esky will now which files will be the executables.

    scripts = [
        Executable_Esky(
            "boneca.py",
            gui_only = True,
            #icon = XPTO  # Use an icon if you want,
            ),
    ],

Run the setup.py with bdist_esky version to generate the version.

python setup.py bdist_esky

On your dist folder:

boneca_esky
Inside the zip file you will see this:

boneca-esky-conteudo

So what happened? Esky created a program boneca.exe that will be responsable for the update of your app, including itself. When it’s open it’ll look for new Zip Files with New versions on the URL that I’ve specified. If it’s a new version it will download and change the folder content cleaning the last version.

Esky handles issues such as internet connection problems, energy failure or any other problems while downloading the new version.

So from now on, our app have auto-update which is So Cool, isn’t it??? But unfortunately this version doesn’t have MSVCR support, so in the next and last part of this series I’ll show how to create your own installer with Inno Setup.

To show how the update works I’ll create one more version (1.0.2) and I’ll change the image again from chuck.jpg to seu-boneco.jpg (it means mr doll in portuguese):

os.startfile(os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])),"seu-boneco.jpg"))

Don’t forget to add seu-boneco.jpg in the include_files of setup.py:

include_files = ['boneca.jpg','chuck.jpg', 'seu-boneco.jpg']

Now, let’s generate the new Esky file:

python setup.py bdist_esky

New file on our dist folder boneca-1.0.2.win32.zip. We just have to put this on the URL that we provided and next time someone uses the boneca-1.0.1 it will be auto-updated.

If you want to test this app, download boneca-1.0.1.win32.zip, unzip the file and open boneca.exe

When you press Print Screen it will show the Chuck Image, but, at this point it will be looking for updates and the next time you open the program it will show “Seu Boneco” image.

seu-boneco

Code on Github.

Wait for the 3rd Part!

 

Originally published on portuguese!


Also published on Medium.

Escreva um comentário

Comentário