From 0e77c0d9a83f9d119830c9eb930d3f8187f5e34e Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Wed, 25 Apr 2018 20:55:17 -0400 Subject: deal with windows file copy permissions issues --- Makefile | 2 +- embroider_install_palettes.py | 111 ---------------------------------- embroider_palettes.py | 121 +++++++++++++++++++++++++++++++++++++ inkstitch/utils/inkscape.py | 7 ++- inx/embroider_install_palettes.inx | 16 ----- inx/embroider_palettes.inx | 16 +++++ messages.po | 4 +- requirements.txt | 2 + 8 files changed, 148 insertions(+), 131 deletions(-) delete mode 100644 embroider_install_palettes.py create mode 100644 embroider_palettes.py delete mode 100644 inx/embroider_install_palettes.inx create mode 100644 inx/embroider_palettes.inx diff --git a/Makefile b/Makefile index 03a34109..fc82757a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -EXTENSIONS:=embroider embroider_params embroider_simulate embroider_print embroider_input embroider_install_palettes +EXTENSIONS:=embroider embroider_params embroider_simulate embroider_print embroider_input embroider_palettes # This gets the branch name or the name of the tag VERSION:=$(TRAVIS_BRANCH) diff --git a/embroider_install_palettes.py b/embroider_install_palettes.py deleted file mode 100644 index ba23fb2c..00000000 --- a/embroider_install_palettes.py +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/python -# - -import sys -import traceback -import os -from os.path import realpath, dirname -from threading import Thread -import socket -import errno -import time -import logging -import wx -import inkex -import shutil -from inkstitch.utils import guess_inkscape_config_path - - -class InstallPalettesFrame(wx.Frame): - def __init__(self, *args, **kwargs): - wx.Frame.__init__(self, *args, **kwargs) - - default_path = os.path.join(guess_inkscape_config_path(), "palettes") - - panel = wx.Panel(self) - sizer = wx.BoxSizer(wx.VERTICAL) - - text = wx.StaticText(panel, label=_("Directory in which to install palettes:")) - font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL) - text.SetFont(font) - sizer.Add(text, proportion=0, flag=wx.ALL|wx.EXPAND, border=10) - - path_sizer = wx.BoxSizer(wx.HORIZONTAL) - self.path_input = wx.TextCtrl(panel, wx.ID_ANY, value=default_path) - path_sizer.Add(self.path_input, proportion=3, flag=wx.RIGHT|wx.EXPAND, border=20) - chooser_button = wx.Button(panel, wx.ID_OPEN, _('Choose another directory...')) - path_sizer.Add(chooser_button, proportion=1, flag=wx.EXPAND) - sizer.Add(path_sizer, proportion=0, flag=wx.ALL|wx.EXPAND, border=10) - - buttons_sizer = wx.BoxSizer(wx.HORIZONTAL) - install_button = wx.Button(panel, wx.ID_ANY, _("Install")) - install_button.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK)) - buttons_sizer.Add(install_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5) - cancel_button = wx.Button(panel, wx.ID_CANCEL, _("Cancel")) - buttons_sizer.Add(cancel_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5) - sizer.Add(buttons_sizer, proportion=0, flag=wx.ALIGN_RIGHT) - - outer_sizer = wx.BoxSizer(wx.HORIZONTAL) - outer_sizer.Add(sizer, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL) - - panel.SetSizer(outer_sizer) - panel.Layout() - - chooser_button.Bind(wx.EVT_BUTTON, self.chooser_button_clicked) - cancel_button.Bind(wx.EVT_BUTTON, self.cancel_button_clicked) - install_button.Bind(wx.EVT_BUTTON, self.install_button_clicked) - - def cancel_button_clicked(self, event): - self.Destroy() - - def chooser_button_clicked(self, event): - dialog = wx.DirDialog(self, _("Choose Inkscape palettes directory")) - if dialog.ShowModal() != wx.ID_CANCEL: - self.path_input.SetValue(dialog.GetPath()) - - def install_button_clicked(self, event): - try: - self.install_palettes() - except Exception, e: - wx.MessageDialog(self, - _('Thread palette installation failed: ' + str(e)), - _('Installation Failed'), - wx.OK).ShowModal() - else: - wx.MessageDialog(self, - _('Thread palette files have been installed. Please restart Inkscape to load the new palettes.'), - _('Installation Completed'), - wx.OK).ShowModal() - - self.Destroy() - - def install_palettes(self): - path = self.path_input.GetValue() - - if not os.path.exists(path): - os.makedirs(path) - - palettes_dir = self.get_bundled_palettes_dir() - - for palette_file in os.listdir(palettes_dir): - shutil.copy(os.path.join(palettes_dir, palette_file), path) - - def get_bundled_palettes_dir(self): - if getattr(sys, 'frozen', None) is not None: - return os.path.join(sys._MEIPASS, 'palettes') - else: - return os.path.join(dirname(realpath(__file__)), 'palettes') - -class InstallPalettes(inkex.Effect): - def effect(self): - app = wx.App() - installer_frame = InstallPalettesFrame(None, title=_("Ink/Stitch Thread Palette Installer"), size=(450, 200)) - installer_frame.Show() - app.MainLoop() - - -if __name__ == '__main__': - #save_stderr() - effect = InstallPalettes() - effect.affect() - #restore_stderr() diff --git a/embroider_palettes.py b/embroider_palettes.py new file mode 100644 index 00000000..a3b59834 --- /dev/null +++ b/embroider_palettes.py @@ -0,0 +1,121 @@ +#!/usr/bin/python +# + +import sys +import traceback +import os +from os.path import realpath, dirname +from glob import glob +from threading import Thread +import socket +import errno +import time +import logging +import wx +import inkex +from inkstitch.utils import guess_inkscape_config_path + + +class InstallPalettesFrame(wx.Frame): + def __init__(self, *args, **kwargs): + wx.Frame.__init__(self, *args, **kwargs) + + default_path = os.path.join(guess_inkscape_config_path(), "palettes") + + panel = wx.Panel(self) + sizer = wx.BoxSizer(wx.VERTICAL) + + text = wx.StaticText(panel, label=_("Directory in which to install palettes:")) + font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL) + text.SetFont(font) + sizer.Add(text, proportion=0, flag=wx.ALL|wx.EXPAND, border=10) + + path_sizer = wx.BoxSizer(wx.HORIZONTAL) + self.path_input = wx.TextCtrl(panel, wx.ID_ANY, value=default_path) + path_sizer.Add(self.path_input, proportion=3, flag=wx.RIGHT|wx.EXPAND, border=20) + chooser_button = wx.Button(panel, wx.ID_OPEN, _('Choose another directory...')) + path_sizer.Add(chooser_button, proportion=1, flag=wx.EXPAND) + sizer.Add(path_sizer, proportion=0, flag=wx.ALL|wx.EXPAND, border=10) + + buttons_sizer = wx.BoxSizer(wx.HORIZONTAL) + install_button = wx.Button(panel, wx.ID_ANY, _("Install")) + install_button.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK)) + buttons_sizer.Add(install_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5) + cancel_button = wx.Button(panel, wx.ID_CANCEL, _("Cancel")) + buttons_sizer.Add(cancel_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5) + sizer.Add(buttons_sizer, proportion=0, flag=wx.ALIGN_RIGHT) + + outer_sizer = wx.BoxSizer(wx.HORIZONTAL) + outer_sizer.Add(sizer, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL) + + panel.SetSizer(outer_sizer) + panel.Layout() + + chooser_button.Bind(wx.EVT_BUTTON, self.chooser_button_clicked) + cancel_button.Bind(wx.EVT_BUTTON, self.cancel_button_clicked) + install_button.Bind(wx.EVT_BUTTON, self.install_button_clicked) + + def cancel_button_clicked(self, event): + self.Destroy() + + def chooser_button_clicked(self, event): + dialog = wx.DirDialog(self, _("Choose Inkscape palettes directory")) + if dialog.ShowModal() != wx.ID_CANCEL: + self.path_input.SetValue(dialog.GetPath()) + + def install_button_clicked(self, event): + try: + self.install_palettes() + except Exception, e: + wx.MessageDialog(self, + _('Thread palette installation failed') + ': \n' + traceback.format_exc(), + _('Installation Failed'), + wx.OK).ShowModal() + else: + wx.MessageDialog(self, + _('Thread palette files have been installed. Please restart Inkscape to load the new palettes.'), + _('Installation Completed'), + wx.OK).ShowModal() + + self.Destroy() + + def install_palettes(self): + path = self.path_input.GetValue() + palettes_dir = self.get_bundled_palettes_dir() + self.copy_files(glob(os.path.join(palettes_dir, "*")), path) + + def get_bundled_palettes_dir(self): + if getattr(sys, 'frozen', None) is not None: + return realpath(os.path.join(sys._MEIPASS, '..', 'palettes')) + else: + return os.path.join(dirname(realpath(__file__)), 'palettes') + + if (sys.platform == "win32"): + # If we try to just use shutil.copy it says the operation requires elevation. + def copy_files(self, files, dest): + import winutils + + winutils.copy(files, dest) + else: + def copy_files(self, files, dest): + import shutil + + if not os.path.exists(dest): + os.makedirs(dest) + + for palette_file in files: + shutil.copy(palette_file, dest) + +class InstallPalettes(inkex.Effect): + def effect(self): + app = wx.App() + installer_frame = InstallPalettesFrame(None, title=_("Ink/Stitch Thread Palette Installer"), size=(450, 200)) + installer_frame.Show() + app.MainLoop() + + +if __name__ == '__main__': + #save_stderr() + effect = InstallPalettes() + effect.affect() + #restore_stderr() diff --git a/inkstitch/utils/inkscape.py b/inkstitch/utils/inkscape.py index 4aba1bad..2d0298bc 100644 --- a/inkstitch/utils/inkscape.py +++ b/inkstitch/utils/inkscape.py @@ -3,7 +3,12 @@ import sys def guess_inkscape_config_path(): if getattr(sys, 'frozen', None): - path = realpath(path_join(sys._MEIPASS, "..")) + path = realpath(path_join(sys._MEIPASS, "..", "..", "..")) + if sys.platform == "win32": + import win32api + + # This expands ugly things like EXTENS~1 + path = win32api.GetLongPathName(path) else: path = expanduser("~/.config/inkscape") diff --git a/inx/embroider_install_palettes.inx b/inx/embroider_install_palettes.inx deleted file mode 100644 index 063d6670..00000000 --- a/inx/embroider_install_palettes.inx +++ /dev/null @@ -1,16 +0,0 @@ - - - <_name>Install thread manufacturer color palettes - org.inkstitch.palettes - embroider_install_palettes.py - inkex.py - - all - - - - - - diff --git a/inx/embroider_palettes.inx b/inx/embroider_palettes.inx new file mode 100644 index 00000000..47ff6cf3 --- /dev/null +++ b/inx/embroider_palettes.inx @@ -0,0 +1,16 @@ + + + <_name>Install thread manufacturer color palettes + org.inkstitch.palettes + embroider_palettes.py + inkex.py + + all + + + + + + diff --git a/messages.po b/messages.po index 192ff456..956d1986 100644 --- a/messages.po +++ b/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2018-04-28 17:58-0400\n" +"POT-Creation-Date: 2018-04-28 17:59-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -38,7 +38,7 @@ msgstr "" msgid "Choose Inkscape palettes directory" msgstr "" -msgid "Thread palette installation failed: " +msgid "Thread palette installation failed" msgstr "" msgid "Installation Failed" diff --git a/requirements.txt b/requirements.txt index 70baadc6..7771f0d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,5 @@ numpy jinja2 flask requests +pywinutils; sys.platform == 'win32' +pywin32; sys.platform == 'win32' -- cgit v1.2.3