[gtk-mac-bundler] Move Bundler::copy_file to Path::copy_target.



commit 4df174f79df9ab8305c07eb0ab20dbaebbef84fe
Author: John Ralls <jralls ceridwen us>
Date:   Sat Dec 31 10:06:33 2016 -0800

    Move Bundler::copy_file to Path::copy_target.
    
    More appropriate responsibility.

 bundler/bundler.py |  122 +++++----------------------------------------------
 bundler/project.py |   95 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 110 deletions(-)
---
diff --git a/bundler/bundler.py b/bundler/bundler.py
index f97b89c..79482db 100644
--- a/bundler/bundler.py
+++ b/bundler/bundler.py
@@ -3,8 +3,6 @@ import os, errno, glob
 import shutil
 import re
 from plistlib import Plist
-from distutils import dir_util, file_util
-
 from .project import *
 from . import utils
 
@@ -65,7 +63,7 @@ class Bundler:
     def copy_plist(self):
         path = Path(self.project.get_plist_path(),
                     self.project.get_bundle_path("Contents/Info.plist"))
-        self.copy_path(path)
+        path.copy_target(self.project)
 
     def create_pango_setup(self):
         if utils.has_pkgconfig_module("pango") and \
@@ -225,7 +223,7 @@ class Bundler:
         for path in binaries:
             if os.path.islink(path.source):
                 continue
-            dest = self.copy_path(path)
+            dest = path.copy_target(self.project)
 
             self.binary_paths.append(dest)
 
@@ -238,101 +236,6 @@ class Bundler:
                     for name in [l for l in files if l.endswith(".la") or l.endswith(".a")]:
                         os.remove(os.path.join(root, name))
 
-    # Copies from Path.source to Path.dest, evaluating any variables
-    # in the paths, and returns the real dest.
-    def copy_path(self, Path):
-        _doRecurse = False
-        source = self.project.evaluate_path(Path.source)
-        if Path.dest:
-            dest = self.project.evaluate_path(Path.dest)
-        else:
-            # Source must begin with a prefix if we don't have a
-            # dest. Skip past the source prefix and replace it with
-            # the right bundle path instead.
-            p = re.compile("^\${prefix(:.*?)?}/")
-            m = p.match(Path.source)
-            if m:
-                relative_dest = self.project.evaluate_path(Path.source[m.end():])
-                dest = self.project.get_bundle_path("Contents/Resources", relative_dest)
-            else:
-                print("Invalid bundle file, missing or invalid 'dest' property: " + Path.dest)
-                sys.exit(1)
-
-        (dest_parent, dest_tail) = os.path.split(dest)
-        utils.makedirs(dest_parent)
-
-        # Check that the source only has wildcards in the last component.
-        p = re.compile("[\*\?]")
-        (source_parent, source_tail) = os.path.split(source)
-        if p.search(source_parent):
-            print("Can't have wildcards except in the last path component: " + source)
-            sys.exit(1)
-
-        if p.search(source_tail):
-            source_check = source_parent
-            if Path.recurse:
-                _doRecurse = True
-        else:
-            source_check = source
-        if not os.path.exists(source_check):
-            print("Cannot find source to copy: " + source)
-            sys.exit(1)
-
-        # If the destination has a wildcard as last component (copied
-        # from the source in dest-less paths), ignore the tail.
-        if p.search(dest_tail):
-            dest = dest_parent
-
-        if _doRecurse:
-            for root, dirs, files in os.walk(source_parent):
-                destdir = os.path.join(dest,
-                                       os.path.relpath(root, source_parent))
-                utils.makedirs(destdir)
-                for globbed_source in glob.glob(os.path.join(root,
-                                                             source_tail)):
-                    try:
-#                        print "Copying %s to %s" % (globbed_source, destdir)
-                        shutil.copy(globbed_source, destdir)
-                    except EnvironmentError as e:
-                        if e.errno == errno.ENOENT:
-                            print("Warning, source file missing: " + globbed_source)
-                        elif e.errno == errno.EEXIST:
-                            print("Warning, path already exits: " + dest)
-                        else:
-                            print("Error %s when copying file: %s" % ( str(e), globbed_source ))
-                            sys.exit(1)
-
-        else:
-            for globbed_source in glob.glob(source):
-                try:
-                    if os.path.isdir(globbed_source):
-                        #print "dir: %s => %s" % (globbed_source, dest)
-                        dir_util.copy_tree (str(globbed_source), str(dest),
-                                            preserve_mode=1,
-                                            preserve_times=1,
-                                            preserve_symlinks=1,
-                                            update=1,
-                                            verbose=1,
-                                            dry_run=0)
-                    else:
-                        #print "file: %s => %s" % (globbed_source, dest)
-                        file_util.copy_file (str(globbed_source), str(dest),
-                                            preserve_mode=1,
-                                            preserve_times=1,
-                                            update=1,
-                                            link=None,
-                                            verbose=1,
-                                            dry_run=0)
-                except EnvironmentError as e:
-                    if e.errno == errno.ENOENT:
-                        print("Warning, source file missing: " + globbed_source)
-                    elif e.errno == errno.EEXIST:
-                        print("Warning, path already exits: " + dest)
-                    else:
-                        print("Error %s when copying file: %s" %( str(e), globbed_source ))
-                        sys.exit(1)
-        return dest
-
     # Lists all the binaries copied in so far. Used in the library
     # dependency resolution and icon theme lookup.
     def list_copied_binaries(self):
@@ -374,7 +277,7 @@ class Bundler:
 
         def prefix_filter(line):
             if not "(compatibility" in line:
-                print "Removed %s" % line
+                # print "Removed %s" % line
                 return False
 
             if line.startswith("/usr/X11"):
@@ -513,7 +416,7 @@ class Bundler:
         themes = self.project.get_icon_themes()
 
         for theme in themes:
-            self.copy_path(Path(os.path.join(theme.source, "index.theme")))
+            Path(os.path.join(theme.source, "index.theme")).copy_target(self.project)
 
         for theme in themes:
             if theme.icons == IconTheme.ICONS_NONE:
@@ -561,8 +464,8 @@ class Bundler:
                             continue
 
                         # Replace the real paths with the prefix macro
-                        # so we can use copy_path.
-                        self.copy_path(Path("${prefix}" + path[len(prefix):]))
+                        # so we can use copy_target.
+                        Path("${prefix}" + path[len(prefix):]).copy_target(self.project)
 
         # Generate icon caches.
         for theme in themes:
@@ -595,8 +498,7 @@ class Bundler:
             for root, trees, files in os.walk(source):
                 for file in filter(name_filter, files):
                     path = os.path.join(root, file)
-                    self.copy_path(Path("${prefix}" + path[len(prefix):],
-                                        program.dest))
+                    Path("${prefix}" + path[len(prefix):], program.dest).copy_target(self.project)
 
 
     def install_gir(self):
@@ -651,7 +553,7 @@ class Bundler:
         self.copy_plist()
 
         # Note: could move this to xml file...
-        self.copy_path(Path("${prefix}/lib/charset.alias"))
+        Path("${prefix}/lib/charset.alias").copy_target(self.project)
 
         # Main binary
         path = self.project.get_main_binary()
@@ -660,7 +562,7 @@ class Bundler:
             print("Cannot find main binary: " + source)
             sys.exit(1)
 
-        dest = self.copy_path(path)
+        dest = path.copy_target(self.project)
         self.binary_paths.append(dest)
 
         # Additional binaries (executables, libraries, modules)
@@ -672,7 +574,7 @@ class Bundler:
 
         # Data
         for path in self.project.get_data():
-            self.copy_path(path)
+            path.copy_target(self.project)
 
         # Translations
         self.copy_translations()
@@ -680,7 +582,7 @@ class Bundler:
         # Frameworks
         frameworks = self.project.get_frameworks()
         for path in frameworks:
-            dest = self.copy_path(path)
+            dest = path.copy_target(self.project)
             self.frameworks.append(dest)
 
         self.copy_icon_themes()
@@ -699,7 +601,7 @@ class Bundler:
         # Launcher script, if necessary.
         launcher_script = self.project.get_launcher_script()
         if launcher_script:
-            path = self.copy_path(launcher_script)
+            path = launcher_script.copy_target(self.project)
             if "APPLICATION_CERT" in os.environ:
                 cert = os.environ["APPLICATION_CERT"]
                 ident = self.project.get_bundle_id()
diff --git a/bundler/project.py b/bundler/project.py
index 4aaf14e..662aad2 100644
--- a/bundler/project.py
+++ b/bundler/project.py
@@ -2,10 +2,13 @@ import errno
 import sys
 import re
 import os
+import glob
+import shutil
 import xml.dom.minidom
 from xml.dom.minidom import Node
 from plistlib import Plist
 from . import utils
+from distutils import dir_util, file_util
 
 # Base class for anything that can be copied into a bundle with a
 # source and dest.
@@ -91,6 +94,98 @@ class Path(object):
 
         return True
 
+    # Copies from source to dest, evaluating any variables
+    # in the paths, and returns the real dest.
+    def copy_target(self, project):
+        source = project.evaluate_path(self.source)
+        if self.dest:
+            dest = project.evaluate_path(self.dest)
+        else:
+            # Source must begin with a prefix if we don't have a
+            # dest. Skip past the source prefix and replace it with
+            # the right bundle path instead.
+            p = re.compile("^\${prefix(:.*?)?}/")
+            m = p.match(self.source)
+            if m:
+                relative_dest = project.evaluate_path(self.source[m.end():])
+                dest = project.get_bundle_path("Contents/Resources", relative_dest)
+            else:
+                print("Invalid bundle file, missing or invalid 'dest' property: " + self.dest)
+                sys.exit(1)
+
+        (dest_parent, dest_tail) = os.path.split(dest)
+        utils.makedirs(dest_parent)
+
+        # Check that the source only has wildcards in the last component.
+        p = re.compile("[\*\?]")
+        (source_parent, source_tail) = os.path.split(source)
+        if p.search(source_parent):
+            print("Can't have wildcards except in the last path component: " + source)
+            sys.exit(1)
+
+        if p.search(source_tail):
+            source_check = source_parent
+        else:
+            source_check = source
+        if not os.path.exists(source_check):
+            print("Cannot find source to copy: " + source)
+            sys.exit(1)
+
+        # If the destination has a wildcard as last component (copied
+        # from the source in dest-less paths), ignore the tail.
+        if p.search(dest_tail):
+            dest = dest_parent
+
+        if self.recurse:
+            for root, dirs, files in os.walk(source_parent):
+                destdir = os.path.join(dest,
+                                       os.path.relpath(root, source_parent))
+                utils.makedirs(destdir)
+                for globbed_source in glob.glob(os.path.join(root,
+                                                             source_tail)):
+                    try:
+#                        print "Copying %s to %s" % (globbed_source, destdir)
+                        shutil.copy(globbed_source, destdir)
+                    except EnvironmentError as e:
+                        if e.errno == errno.ENOENT:
+                            print("Warning, source file missing: " + globbed_source)
+                        elif e.errno == errno.EEXIST:
+                            print("Warning, path already exits: " + dest)
+                        else:
+                            print("Error %s when copying file: %s" % ( str(e), globbed_source ))
+                            sys.exit(1)
+
+        else:
+            for globbed_source in glob.glob(source):
+                try:
+                    if os.path.isdir(globbed_source):
+                        #print "dir: %s => %s" % (globbed_source, dest)
+                        dir_util.copy_tree (str(globbed_source), str(dest),
+                                            preserve_mode=1,
+                                            preserve_times=1,
+                                            preserve_symlinks=1,
+                                            update=1,
+                                            verbose=1,
+                                            dry_run=0)
+                    else:
+                        #print "file: %s => %s" % (globbed_source, dest)
+                        file_util.copy_file (str(globbed_source), str(dest),
+                                            preserve_mode=1,
+                                            preserve_times=1,
+                                            update=1,
+                                            link=None,
+                                            verbose=1,
+                                            dry_run=0)
+                except EnvironmentError as e:
+                    if e.errno == errno.ENOENT:
+                        print("Warning, source file missing: " + globbed_source)
+                    elif e.errno == errno.EEXIST:
+                        print("Warning, path already exits: " + dest)
+                    else:
+                        print("Error %s when copying file: %s" %( str(e), globbed_source ))
+                        sys.exit(1)
+        return dest
+
 # Used for anything that has a name and value.
 class Variable:
     def __init__(self, node):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]