From d7beb8d8603fa46f1ee65aa8f344fbfd978ed74f Mon Sep 17 00:00:00 2001
From: Joey Payne <jyapayne@gmail.com>
Date: Thu, 14 Jan 2016 07:43:08 -0700
Subject: [PATCH] Fixed long paths with windows (finally)

---
 command_line.py | 42 +++++++++++++++++++++---------------------
 main.py         |  1 -
 utils.py        | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 22 deletions(-)

diff --git a/command_line.py b/command_line.py
index 81e11d5..60e6ab0 100644
--- a/command_line.py
+++ b/command_line.py
@@ -197,7 +197,7 @@ def set_extra_attributes_from_keyword_args(self, **kwargs):
 
     def extract(self, ex_path, version):
         if os.path.exists(ex_path):
-            shutil.rmtree(ex_path, ignore_errors=True)
+            utils.rmtree(ex_path, ignore_errors=True)
 
         path = self.save_file_path(version)
 
@@ -225,8 +225,8 @@ def extract(self, ex_path, version):
         if os.path.exists(dir_name):
             for p in os.listdir(dir_name):
                 abs_file = utils.path_join(dir_name, p)
-                shutil.move(abs_file, ex_path)
-            shutil.rmtree(dir_name, ignore_errors=True)
+                utils.move(abs_file, ex_path)
+            utils.rmtree(dir_name, ignore_errors=True)
 
     def get_file_bytes(self, version):
         fbytes = []
@@ -637,7 +637,7 @@ def create_icns_for_app(self, icns_path):
             if not icon_path.endswith('.icns'):
                 save_icns(icon_path, icns_path)
             else:
-                shutil.copy(icon_path, icns_path)
+                utils.copy(icon_path, icns_path)
 
     def replace_icon_in_exe(self, exe_path):
         icon_setting = self.get_setting('icon')
@@ -658,11 +658,11 @@ def make_output_dirs(self):
 
             output_dir = utils.path_join(self.output_dir(), self.project_name())
             if os.path.exists(output_dir):
-                shutil.rmtree(output_dir, ignore_errors=True)
+                utils.rmtree(output_dir, ignore_errors=True)
 
             temp_dir = utils.path_join(TEMP_DIR, 'webexectemp')
             if os.path.exists(temp_dir):
-                shutil.rmtree(temp_dir, ignore_errors=True)
+                utils.rmtree(temp_dir, ignore_errors=True)
 
             self.progress_text = 'Making new directories...\n'
 
@@ -689,8 +689,8 @@ def make_output_dirs(self):
 
             app_nw_folder = utils.path_join(temp_dir, self.project_name()+'.nwf')
 
-            shutil.copytree(self.project_dir(), app_nw_folder,
-                            ignore=shutil.ignore_patterns(output_dir))
+            utils.copytree(self.project_dir(), app_nw_folder,
+                           ignore=shutil.ignore_patterns(output_dir))
 
             zip_files(zip_file, self.project_dir(), exclude_paths=[output_dir])
             for ex_setting in self.settings['export_settings'].values():
@@ -706,13 +706,13 @@ def make_output_dirs(self):
                         export_dest = export_dest.replace('node-webkit', 'nwjs')
 
                     if os.path.exists(export_dest):
-                        shutil.rmtree(export_dest, ignore_errors=True)
+                        utils.rmtree(export_dest, ignore_errors=True)
 
                     # shutil will make the directory for us
-                    shutil.copytree(get_data_path('files/'+ex_setting.name),
-                                    export_dest,
+                    utils.copytree(get_data_path('files/'+ex_setting.name),
+                                   export_dest,
                                     ignore=shutil.ignore_patterns('place_holder.txt'))
-                    shutil.rmtree(get_data_path('files/'+ex_setting.name), ignore_errors=True)
+                    utils.rmtree(get_data_path('files/'+ex_setting.name), ignore_errors=True)
                     self.progress_text += '.'
 
                     if 'mac' in ex_setting.name:
@@ -722,13 +722,13 @@ def make_output_dirs(self):
                                                 self.project_name()+'.app')
 
                         try:
-                            shutil.move(utils.path_join(export_dest,
+                            utils.move(utils.path_join(export_dest,
                                                      'nwjs.app'),
-                                        app_path)
+                                       app_path)
                         except IOError:
-                            shutil.move(utils.path_join(export_dest,
+                            utils.move(utils.path_join(export_dest,
                                                      'node-webkit.app'),
-                                        app_path)
+                                       app_path)
 
                         plist_path = utils.path_join(app_path, 'Contents', 'Info.plist')
 
@@ -751,9 +751,9 @@ def make_output_dirs(self):
                                                   'app.nw')
 
                         if uncompressed:
-                            shutil.copytree(app_nw_folder, app_nw_res)
+                            utils.copytree(app_nw_folder, app_nw_res)
                         else:
-                            shutil.copy(zip_file, app_nw_res)
+                            utils.copy(zip_file, app_nw_res)
                         self.create_icns_for_app(utils.path_join(app_path,
                                                               'Contents',
                                                               'Resources',
@@ -802,13 +802,13 @@ def make_output_dirs(self):
             self.logger.error(error)
             self.output_err += error
         finally:
-            shutil.rmtree(temp_dir, ignore_errors=True)
+            utils.rmtree(temp_dir, ignore_errors=True)
 
     def make_desktop_file(self, nw_path, export_dest):
         icon_set = self.get_setting('icon')
         icon_path = utils.path_join(self.project_dir(), icon_set.value)
         if os.path.exists(icon_path) and icon_set.value:
-            shutil.copy(icon_path, export_dest)
+            utils.copy(icon_path, export_dest)
             icon_path = utils.path_join(export_dest, os.path.basename(icon_path))
         else:
             icon_path = ''
@@ -886,7 +886,7 @@ def copy_files_to_project_folder(self):
                     f_path = setting.value.replace(self.project_dir(), '')
                     if os.path.isabs(f_path):
                         try:
-                            shutil.copy(setting.value, self.project_dir())
+                            utils.copy(setting.value, self.project_dir())
                             self.logger.info(u'Copying file {} to {}'.format(setting.value, self.project_dir()))
                         except shutil.Error as e:  # same file warning
                             self.logger.warning(u'Warning: {}'.format(e))
diff --git a/main.py b/main.py
index 573a63d..a89817e 100644
--- a/main.py
+++ b/main.py
@@ -1037,7 +1037,6 @@ def create_export_settings(self):
         script_label.setMinimumWidth(150)
 
         self.script_line = QtGui.QLineEdit()
-        self.script_line.setReadOnly(True)
 
         script_setting = self.get_setting('custom_script')
         self.script_line.setObjectName(script_setting.name)
diff --git a/utils.py b/utils.py
index 2241c3e..b6fc636 100644
--- a/utils.py
+++ b/utils.py
@@ -3,6 +3,7 @@
 import sys, tempfile
 import subprocess
 from appdirs import AppDirs
+import shutil
 
 #try:
 #    import zlib
@@ -29,7 +30,9 @@ def path_join(base, *rest):
             new_rest.append(rest[i].decode('utf-8'))
         except UnicodeEncodeError:
             new_rest.append(unicode(rest[i]))
+
     rpath = u'/'.join(new_rest)
+
     if os.path.isabs(rpath):
         return rpath
     else:
@@ -40,6 +43,9 @@ def get_data_path(dir_path):
     dirs = AppDirs('Web2Executable', 'Web2Executable')
     data_path = path_join(dirs.user_data_dir, *parts)
 
+    if is_windows():
+        data_path = data_path.replace(u'\\', u'/')
+
     if not os.path.exists(data_path):
         os.makedirs(data_path)
 
@@ -50,6 +56,37 @@ def get_data_file_path(file_path):
     data_path = get_data_path('/'.join(parts[:-1]))
     return path_join(data_path, parts[-1])
 
+def rmtree(path, **kwargs):
+    if is_windows():
+        if os.path.isabs(path):
+            path = u'//?/'+path
+    shutil.rmtree(path, **kwargs)
+
+def copy(src, dest, **kwargs):
+    if is_windows():
+        if os.path.isabs(src):
+            src = u'//?/'+src
+        if os.path.isabs(dest):
+            dest = u'//?/'+dest
+    shutil.copy(src, dest, **kwargs)
+
+def move(src, dest, **kwargs):
+    if is_windows():
+        if os.path.isabs(src):
+            src = u'//?/'+src
+        if os.path.isabs(dest):
+            dest = u'//?/'+dest
+    shutil.move(src, dest, **kwargs)
+
+def copytree(src, dest, **kwargs):
+    if is_windows():
+        if os.path.isabs(src):
+            src = u'//?/'+src
+        if os.path.isabs(dest):
+            dest = u'//?/'+dest
+    shutil.copytree(src, dest, **kwargs)
+
+
 def log(*args):
     if DEBUG:
         print(*args)