Skip to content

Commit

Permalink
Let Esky.auto_update take a callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
rfk committed Apr 11, 2011
1 parent f1efa2f commit 3d03d6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
23 changes: 18 additions & 5 deletions esky/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def _try_remove(self,tdir,path,manifest=[]):
_errors_to_ignore = (errno.ENOENT, errno.EPERM, errno.EACCES, errno.ENOTDIR,
errno.EISDIR, errno.EINVAL, errno.ENOTEMPTY,)

def auto_update(self):
def auto_update(self,callback=None):
"""Automatically install the latest version of the app.
This method automatically performs the following sequence of actions,
Expand All @@ -706,15 +706,19 @@ def auto_update(self):
"""
if self.version_finder is None:
raise NoVersionFinderError
if callback is None:
callback = lambda *args: True
got_root = False
cleaned = False
try:
callback({"status":"searching"})
version = self.find_update()
if version is not None:
callback({"status":"found", "new_version":version})
# Try to install the new version. If it fails with
# a permission error, escalate to root and try again.
try:
self._do_auto_update(version)
self._do_auto_update(version,callback)
except EnvironmentError:
exc_type,exc_value,exc_traceback = sys.exc_info()
if exc_value.errno != errno.EACCES or self.has_root():
Expand All @@ -725,11 +729,12 @@ def auto_update(self):
raise exc_type,exc_value,exc_traceback
else:
got_root = True
self._do_auto_update(version)
self._do_auto_update(version,callback)
self.reinitialize()
# Try to clean up the app dir. If it fails with a
# permission error, escalate to root and try again.
try:
callback({"status":"cleaning up"})
cleaned = self.cleanup()
except EnvironmentError:
exc_type,exc_value,exc_traceback = sys.exc_info()
Expand All @@ -741,21 +746,29 @@ def auto_update(self):
raise exc_type,exc_value,exc_traceback
else:
got_root = True
callback({"status":"cleaning up"})
cleaned = self.cleanup()
except Exception, e:
callback({"status":"error","exception":e})
raise
else:
callback({"status":"done"})
finally:
# Drop root privileges as soon as possible.
if not cleaned and self.needs_cleanup():
self.cleanup_at_exit()
if got_root:
self.drop_root()
callback({"status":"done"})

def _do_auto_update(self,version):
def _do_auto_update(self,version,callback):
"""Actual sequence of operations for auto-update.
This is a separate method so it can easily be retried after gaining
root privileges.
"""
self.fetch_version(version)
self.fetch_version(version,callback)
callback({"status":"installing", "new_version":version})
self.install_version(version)
try:
self.uninstall_version(self.version)
Expand Down
4 changes: 2 additions & 2 deletions esky/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ def _fetch_file_iter(self,app,url):
partfilenm = outfilenm + ".part"
partfile = open(partfilenm,"wb")
try:
data = infile.read(1024*512)
data = infile.read(1024*64)
while data:
yield {"status": "downloading",
"size": infile_size,
"received": partfile.tell(),
}
partfile.write(data)
outfile_size += len(data)
data = infile.read(1024*512)
data = infile.read(1024*64)
if infile_size is not None:
if outfile_size != infile_size:
self.version_graph.remove_all_links(url)
Expand Down
3 changes: 3 additions & 0 deletions tutorial/stage1/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
version = "0.1",
# All executables are listed in the "scripts" argument
scripts = ["example.py"],
options = {"py2exe": {
"dll_excludes":["pywintypes26.dll"],
}}
)

0 comments on commit 3d03d6e

Please sign in to comment.