From d8961d6bf2d7228e73bcffd4f0ed199bba20ad2e Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 12:17:15 +0100 Subject: [PATCH 1/7] Update from Git implemented --- Menu.py | 9 +++++++-- Utilities.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- devsimpy.py | 10 +++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Menu.py b/Menu.py index 59ffd1dd..0a34a03c 100644 --- a/Menu.py +++ b/Menu.py @@ -97,6 +97,7 @@ ID_HELP = wx.ID_HELP ID_API_HELP = wx.NewIdRef() ID_UPDATE_PIP_PACKAGE = wx.NewIdRef() +ID_UPDATE_FROM_GIT = wx.NewIdRef() ID_CONTACT = wx.NewIdRef() ID_ABOUT = wx.ID_ABOUT @@ -479,12 +480,14 @@ def __init__(self, parent): helpModel = wx.MenuItem(self, ID_HELP, _('&DEVSimPy Help\tF1'), _("Help for DEVSimPy user")) apiModel = wx.MenuItem(self, ID_API_HELP, _('&DEVSimPy API\tF2'), _("API for DEVSimPy user")) - updatePipPackage = wx.MenuItem(self, ID_UPDATE_PIP_PACKAGE, _('Update PIP Packages\tF3'), _("Update pip packages")) + updatePipPackage = wx.MenuItem(self, ID_UPDATE_PIP_PACKAGE, _('Update PIP Packages\tF3'), _("Update of dependant pip packages")) + updateFromGit = wx.MenuItem(self, ID_UPDATE_FROM_GIT, _('Update From Git'), _("Update of DEVSimPy from Git archive")) contactModel = wx.MenuItem(self, ID_CONTACT, _('Contact the Author...'), _("Send mail to the author")) aboutModel = wx.MenuItem(self, ID_ABOUT, _('About DEVSimPy...'), _("About DEVSimPy")) helpModel.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'search.png'))) updatePipPackage.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'update.png'))) + updateFromGit.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'update.png'))) apiModel.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'api.png'))) contactModel.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'mail.png'))) aboutModel.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'info.png'))) @@ -495,13 +498,15 @@ def __init__(self, parent): AppendItem(apiModel) self.AppendSeparator() AppendItem(updatePipPackage) + AppendItem(updateFromGit) self.AppendSeparator() AppendItem(aboutModel) AppendItem(contactModel) parent.Bind(wx.EVT_MENU, parent.OnHelp, id=ID_HELP) parent.Bind(wx.EVT_MENU, parent.OnAPI, id=ID_API_HELP) - parent.Bind(wx.EVT_MENU, parent.OnUpdatePiPPackage, id=ID_UPDATE_PIP_PACKAGE) + parent.Bind(wx.EVT_MENU, parent.OnUpdatPiPPackage, id=ID_UPDATE_PIP_PACKAGE) + parent.Bind(wx.EVT_MENU, parent.OnUpdatFromGit, id=ID_UPDATE_FROM_GIT) parent.Bind(wx.EVT_MENU, parent.OnAbout, id=ID_ABOUT) parent.Bind(wx.EVT_MENU, parent.OnContact, id=ID_CONTACT) diff --git a/Utilities.py b/Utilities.py index a5f84ad0..5d02e847 100644 --- a/Utilities.py +++ b/Utilities.py @@ -34,13 +34,14 @@ import configparser import linecache import imp +import tempfile from copy import deepcopy import gettext _ = gettext.gettext from itertools import combinations - +from zipfile import ZipFile from io import StringIO if builtins.__dict__.get('GUI_FLAG',True): @@ -62,8 +63,10 @@ # Used to recurse subdirectories import fnmatch -import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client +import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client, urllib.urlretrieve +import requests + import pip import importlib @@ -175,8 +178,53 @@ def PyBuzyInfo(msg, time): del busy def updatePiP(): + """ + """ call("python -m pip install --upgrade pip", shell=True) +def downloadFromURL(url): + """ + """ + + # downloading with requests + # download the file contents in binary format + r = requests.get(url) + + if r.status_code == 200: + # 200 means a successful request + + tempdir = tempfile.gettempdir() + fn = os.path.join(tempdir, "DEVSimPy.zip") + # open method to open a file on your system and write the contents + with open(fn, "wb") as code: + code.write(r.content) + + # downloading with urllib + # Copy a network object to a local file + urllib.urlretrieve(url, fn) + + return fn + + else: + return None + +def updateFromGit(): + """ + """ + # specifying the zip file name + fn = downloadFromURL(os.path.join("https://github.com/capocchi/DEVSimPy/archive/version-",__version__,".zip") + + if fn: + # opening the zip file in READ mode + with ZipFile(fn, 'r') as zip: + # printing all the contents of the zip file + zip.printdir() + + # extracting all the files + print('Extracting all the files now...') + #zip.extractall() + print('Done!') + def updatePackageWithPiP(): """ Update all installed package using pip """ diff --git a/devsimpy.py b/devsimpy.py index 835b8bd3..0b908842 100644 --- a/devsimpy.py +++ b/devsimpy.py @@ -187,7 +187,7 @@ from PreferencesGUI import PreferencesGUI from pluginmanager import load_plugins, enable_plugin from which import which -from Utilities import GetUserConfigDir, install, install_and_import, updatePackageWithPiP, NotificationMessage +from Utilities import GetUserConfigDir, install, install_and_import, updatePackageWithPiP, updateFromGit, NotificationMessage from Decorators import redirectStdout, BuzyCursorNotification, ProgressNotification, cond_decorator from DetachedFrame import DetachedFrame from LibraryTree import LibraryTree @@ -2037,10 +2037,14 @@ def OnHelp(self, event): else: self.help.Display(os.path.join('html','toc.html')) - @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification("DEVSimPy Update pip packages")) - def OnUpdatePiPPackage(self, event): + @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("Update dependant pip packages"))) + def OnUpdatPiPPackage(self, event): updatePackageWithPiP() + @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("DEVSimPy Update from git (Version %s).")%__version__)) + def OnUpdatFromGit(self, event): + updateFromGit() + def OnAPI(self, event): """ Shows the DEVSimPy API help file. """ From 67d66addcffa07dc8e2669627f40029466fc0558 Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 12:18:06 +0100 Subject: [PATCH 2/7] bug fix --- Utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Utilities.py b/Utilities.py index 5d02e847..a29e8adc 100644 --- a/Utilities.py +++ b/Utilities.py @@ -212,7 +212,7 @@ def updateFromGit(): """ """ # specifying the zip file name - fn = downloadFromURL(os.path.join("https://github.com/capocchi/DEVSimPy/archive/version-",__version__,".zip") + fn = downloadFromURL(os.path.join("https://github.com/capocchi/DEVSimPy/archive/version-",__version__,".zip")) if fn: # opening the zip file in READ mode From c9e4ce917da1e36b36328734aaf5ff1bb2becade Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 12:22:29 +0100 Subject: [PATCH 3/7] bug fix --- Utilities.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Utilities.py b/Utilities.py index a29e8adc..0407dcee 100644 --- a/Utilities.py +++ b/Utilities.py @@ -63,7 +63,8 @@ # Used to recurse subdirectories import fnmatch -import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client, urllib.urlretrieve +import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client +from urllib.request import urlretrieve import requests @@ -201,7 +202,7 @@ def downloadFromURL(url): # downloading with urllib # Copy a network object to a local file - urllib.urlretrieve(url, fn) + urlretrieve(url, fn) return fn From b00b3b0eee3128193be4e0a9e388ec618249c069 Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 12:24:13 +0100 Subject: [PATCH 4/7] bug fix --- devsimpy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/devsimpy.py b/devsimpy.py index 0b908842..7d9a3545 100644 --- a/devsimpy.py +++ b/devsimpy.py @@ -82,6 +82,8 @@ sys.stdout.write("Importing wxPython %s%s for python %s on %s (%s) platform...\n"%(wx.version(), " from devsimpy.ini" if ini_exist else '', platform.python_version(), platform.system(), platform.version())) +_ = wx.GetTranslation + try: import wx.aui as aui except: From 4dc54b860e609868b21d26d0690ab4614e09edb6 Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 18:36:51 +0100 Subject: [PATCH 5/7] improve of the update from git function --- Decorators.py | 21 ++++++++++----------- Utilities.py | 7 +++++-- devsimpy.py | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Decorators.py b/Decorators.py index 12728e8f..b4b60463 100644 --- a/Decorators.py +++ b/Decorators.py @@ -41,7 +41,7 @@ else: import wx.lib.agw.aui.framemanager AuiFloatingFrame = wx.lib.agw.aui.framemanager.AuiFloatingFrame - + def cond_decorator(flag, dec): def decorate(fn): return dec(fn) if flag else fn @@ -143,11 +143,10 @@ def wrapper(*args): return wrapper class ThreadWithReturnValue(threading.Thread): - def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None): - threading.Thread.__init__(self, group, target, name, args, kwargs, daemon=daemon) - + def __init__(self, *args, **kwargs): + super(ThreadWithReturnValue, self).__init__(*args, **kwargs) self._return = None - + def run(self): if self._target is not None: self._return = self._target(*self._args, **self._kwargs) @@ -160,9 +159,6 @@ def join(self): def ProgressNotification(f, arg): def wrapper(*args): - thread = ThreadWithReturnValue(target = f, args = args) - thread.start() - title = arg new_path = args[-1] if isinstance(new_path, str) and os.path.isfile(new_path): @@ -170,15 +166,18 @@ def wrapper(*args): else: message = _('Please wait..') - progress_dlg = wx.ProgressDialog(title, message, style=wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME) + progress_dlg = wx.ProgressDialog(title, message, style=wx.PD_APP_MODAL|wx.PD_CAN_ABORT) + + thread = ThreadWithReturnValue(target = f, args = args) + thread.start() while thread.isAlive(): wx.MilliSleep(300) - progress_dlg.Pulse() + progress_dlg.Pulse() wx.SafeYield() progress_dlg.Destroy() - + return thread.join() return wrapper diff --git a/Utilities.py b/Utilities.py index 0407dcee..9ddfafa0 100644 --- a/Utilities.py +++ b/Utilities.py @@ -212,19 +212,22 @@ def downloadFromURL(url): def updateFromGit(): """ """ + # specifying the zip file name - fn = downloadFromURL(os.path.join("https://github.com/capocchi/DEVSimPy/archive/version-",__version__,".zip")) + fn = downloadFromURL("https://github.com/capocchi/DEVSimPy/archive/master.zip") if fn: # opening the zip file in READ mode with ZipFile(fn, 'r') as zip: # printing all the contents of the zip file - zip.printdir() + zip.printdir() # extracting all the files print('Extracting all the files now...') #zip.extractall() print('Done!') + else: + print('error!') def updatePackageWithPiP(): """ Update all installed package using pip diff --git a/devsimpy.py b/devsimpy.py index 7d9a3545..92368e1b 100644 --- a/devsimpy.py +++ b/devsimpy.py @@ -2043,7 +2043,7 @@ def OnHelp(self, event): def OnUpdatPiPPackage(self, event): updatePackageWithPiP() - @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("DEVSimPy Update from git (Version %s).")%__version__)) + @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("DEVSimPy Update from git."))) def OnUpdatFromGit(self, event): updateFromGit() From 4f2a0a83f1e99107455d5f2ad6dae4d3798dae36 Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 18:50:25 +0100 Subject: [PATCH 6/7] bug fix --- Decorators.py | 3 +++ Utilities.py | 14 +++++++------- devsimpy.py | 8 +++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Decorators.py b/Decorators.py index b4b60463..f2b32148 100644 --- a/Decorators.py +++ b/Decorators.py @@ -166,6 +166,9 @@ def wrapper(*args): else: message = _('Please wait..') + # main window + mainW = wx.GetApp().GetTopWindow() + progress_dlg = wx.ProgressDialog(title, message, style=wx.PD_APP_MODAL|wx.PD_CAN_ABORT) thread = ThreadWithReturnValue(target = f, args = args) diff --git a/Utilities.py b/Utilities.py index 9ddfafa0..0d02e3f7 100644 --- a/Utilities.py +++ b/Utilities.py @@ -181,7 +181,7 @@ def PyBuzyInfo(msg, time): def updatePiP(): """ """ - call("python -m pip install --upgrade pip", shell=True) + return call("python -m pip install --upgrade pip", shell=True) def downloadFromURL(url): """ @@ -225,9 +225,11 @@ def updateFromGit(): # extracting all the files print('Extracting all the files now...') #zip.extractall() - print('Done!') + print('Done!') + + return True else: - print('error!') + return False def updatePackageWithPiP(): """ Update all installed package using pip @@ -238,12 +240,10 @@ def updatePackageWithPiP(): if pip.__version__ > '10.0.1': import pkg_resources packages = [dist.project_name for dist in pkg_resources.working_set if 'PyPubSub' not in dist.project_name] - call("pip install --user --upgrade -r requirements.txt", shell=True) + return call("pip install --user --upgrade -r requirements.txt", shell=True) else: packages = [dist.project_name for dist in pip.get_installed_distributions() if 'PyPubSub' not in dist.project_name] - call("pip install --user --upgrade " + ' '.join(packages), shell=True) - - NotificationMessage(_('Information'), 'All pip packages have been updated!', None, timeout=5) + return call("pip install --user --upgrade " + ' '.join(packages), shell=True) def install_and_import(package): """ Install and import the package diff --git a/devsimpy.py b/devsimpy.py index 92368e1b..b2d4e25d 100644 --- a/devsimpy.py +++ b/devsimpy.py @@ -2039,13 +2039,15 @@ def OnHelp(self, event): else: self.help.Display(os.path.join('html','toc.html')) - @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("Update dependant pip packages"))) + @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("Update of dependant pip packages."))) def OnUpdatPiPPackage(self, event): - updatePackageWithPiP() + if updatePackageWithPiP(): + NotificationMessage(_('Information'), _('All pip packages that DEVSimPy depends have been updated!'), None, timeout=5) @cond_decorator(builtins.__dict__.get('GUI_FLAG',True), ProgressNotification(_("DEVSimPy Update from git."))) def OnUpdatFromGit(self, event): - updateFromGit() + if updateFromGit(): + NotificationMessage(_('Information'), _('Update of DEVSimPy from git done!'), parent=self, timeout=5) def OnAPI(self, event): """ Shows the DEVSimPy API help file. """ From e63c585fca9f3b2242249381e7eec1607d4df319 Mon Sep 17 00:00:00 2001 From: Capocchi L Date: Tue, 24 Mar 2020 19:11:16 +0100 Subject: [PATCH 7/7] bug fix --- Utilities.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Utilities.py b/Utilities.py index 0d02e3f7..de9f33fb 100644 --- a/Utilities.py +++ b/Utilities.py @@ -71,7 +71,7 @@ import pip import importlib -from subprocess import call +from subprocess import call, check_output, check_call, CalledProcessError # Used for smooth (spectrum) try: @@ -181,7 +181,13 @@ def PyBuzyInfo(msg, time): def updatePiP(): """ """ - return call("python -m pip install --upgrade pip", shell=True) + try: + check_call("python -m pip install --upgrade pip", shell=True) + except CalledProcessError as ee: + print(ee.output) + return False + else: + return True def downloadFromURL(url): """ @@ -238,12 +244,18 @@ def updatePackageWithPiP(): updatePiP() if pip.__version__ > '10.0.1': - import pkg_resources - packages = [dist.project_name for dist in pkg_resources.working_set if 'PyPubSub' not in dist.project_name] - return call("pip install --user --upgrade -r requirements.txt", shell=True) + command = "pip install --user --upgrade -r requirements.txt" else: packages = [dist.project_name for dist in pip.get_installed_distributions() if 'PyPubSub' not in dist.project_name] - return call("pip install --user --upgrade " + ' '.join(packages), shell=True) + command = "pip install --user --upgrade " + ' '.join(packages) + + try: + check_call(command, shell=True) + except CalledProcessError as ee: + print(ee.output) + return False + else: + return True def install_and_import(package): """ Install and import the package