问题
I have a python program which I then compile with cx_freeze that updates itself by downloading a zip file and then overwriting files. Thing is, whenever I execute it, this happens:
Traceback (most recent call last):
File ".\updater.py", line 69, in empezar_actualizacion
self.bajar_archivos()
File ".\updater.py", line 75, in bajar_archivos
self.extraer_archivos()
File ".\updater.py", line 80, in extraer_archivos
self.descarga.descomprimir(self.archivos)
File "utils.py", line 167, in descomprimir
raise(e)
IOError: [Errno 13] Permission denied: '_ctypes.pyd'
And here's the code that extracts the files:
class DescargaThread(threading.Thread):
def __init__(self):
self.url = config['servidor_de_actualizacion']
def descargar_actualizacion(self):
version = obtener_version()
if not version:
return 'Problemas de conexión, inténtelo después.'
try:
nueva_version = urllib.urlopen(self.url).read()
return nueva_version
except Exception as e:
raise(e)
def descomprimir(self, archivo):
try:
zip_file = zipfile.ZipFile(StringIO(archivo))
for f in zip_file.namelist():
self.file_unzipped = zip_file.extract(f)
return True
except Exception as e:
raise(e)
What should I do to get the files to overwrite themselves? Ask for higher permissions?
回答1:
When I deploy Windows application done in Python, I use cx_freeze and then pack it with Inno Setup. The user then downloads and installs the application painlessly.
The application checks the web server for updates when started. When the new version is found, it asks the user if he wants to update. If so, application downloads the new installer. You can show some progress bar to give user nice feedback.
When finished, it checks the downloaded installer against MD5 or SHA1 hash available from the repository, starts the installer and closes the application in a way similar to this:
data = open(installer_path, "rb").read()
sha = hashlib.sha1(data).hexdigest()
if sha != shaWeb:
blocking_message_box("Installer is corrupted.")
else:
blocking_message_box("Installer is correct, will be installed now.")
subprocess.Popen('"' + installer_path + '"', shell=True)
this_application.Close()
User now goes through the installation. Because it was already installed before, I think the Inno Setup will show up the same directory what was used before, so the effort is like Next
, Next
, Finish
. You can setup your installer the way so it preserves user settings etc. You could probably run the installer in a silent mode, but a lot of users hate when application does something behind their back.
You can also create an update-like installer for users who already have the application installed, skipping DLLs and other shared files, updating just the *.exe
and library.zip
. Because my applications usually have just one, I embed the library.zip
in executable anyway, using:
options = {"build_exe": {"build_exe": "../Bin",
"create_shared_zip": False,
}}
回答2:
Yes, I would ask for higher permissions using chmod
. I would also ask for higher permissions on the parent directory that you'd be downloading these files to, if you plan on writing new files.
来源:https://stackoverflow.com/questions/25745601/permission-to-overwrite-files