[26681] users/pipping/merge.rb

source_changes at macosforge.org source_changes at macosforge.org
Tue Jul 3 08:21:03 PDT 2007


Revision: 26681
          http://trac.macosforge.org/projects/macports/changeset/26681
Author:   pipping at macports.org
Date:     2007-07-03 08:21:03 -0700 (Tue, 03 Jul 2007)

Log Message:
-----------
extract checks for existance of input directory and its subdir from main loop, reindent accordingly
rename --root (-r) to --input (-i)
make `File.join( arguments[:input], ARGS[0], path )` into a variable (long, frequently used)
major reformatting/cleanup
 * http://elias.svn.binera.de/bin/cgi/viewvc.cgi/merge.rb?view=log
   * http://elias.svn.binera.de/bin/cgi/viewvc.cgi/merge.rb?r1=35&r2=36
   * http://elias.svn.binera.de/bin/cgi/viewvc.cgi/merge.rb?r1=36&r2=37
   * http://elias.svn.binera.de/bin/cgi/viewvc.cgi/merge.rb?r1=37&r2=38
   * http://elias.svn.binera.de/bin/cgi/viewvc.cgi/merge.rb?r1=38&r2=39
for details

Modified Paths:
--------------
    users/pipping/merge.rb

Modified: users/pipping/merge.rb
===================================================================
--- users/pipping/merge.rb	2007-07-03 15:18:09 UTC (rev 26680)
+++ users/pipping/merge.rb	2007-07-03 15:21:03 UTC (rev 26681)
@@ -27,13 +27,13 @@
 require 'set'
 
 ###
-# we need GNU File with Apple's patches applied, available here:
-# http://www.opensource.apple.com/darwinsource/Current/file-6.2/patches/
-FILE="/usr/bin/file"
+# we need GNU File with Apple's patches applied, otherwise we will not be
+# able to recognize x86_64 mach-o files
+FILE = '/usr/bin/file'
 
 class MergeArguments < Hash
-  def initialize(args)
-    super()
+  def initialize( args )
+    super( )
 
     # set up defaults
     self[:dry_run] = false
@@ -41,24 +41,24 @@
     self[:verbose] = false
 
     self[:exclude] = Set.new << '.svn' << 'CVS'
-    self[:output]  = File.join(Dir.pwd,'out')
-    self[:root]    = Dir.pwd
+    self[:output]  = File.join( Dir.pwd, 'out' )
+    self[:input]    = Dir.pwd
 
     opts = OptionParser.new {|opts|
       opts.banner = "Usage: #$0 [options] <arch> <arch> [<arch> ...]"
 
       opts.on(
-        '-r', '--root DIRECTORY',
-        'specify root directory'
-      ) {|root|
-        self[:root] = root || Dir.pwd
+        '-i', '--input DIRECTORY',
+        'specify input directory'
+      ) {|input|
+        self[:input] = File.expand_path( input ) || Dir.pwd
       }
 
       opts.on(
         '-o', '--output DIRECTORY',
         'specify output directory'
       ) {|out|
-        self[:output] = File.expand_path(out)
+        self[:output] = File.expand_path( out ) || Dir.pwd
       }
 
       opts.on(
@@ -100,26 +100,26 @@
         exit
       }
     }
-    opts.parse!(args)
+    opts.parse!( args )
   end
 end
 
-arguments = MergeArguments.new(ARGV)
+arguments = MergeArguments.new( ARGV )
 
-# avoid duplicates and trailing slashes
-ARGS=ARGV.collect {|arg| arg.chomp('/')}.uniq
+# ignore duplicates and trailing slashes
+ARGS = ARGV.collect {|arg| arg.chomp( '/' )}.uniq
 
-def true_for_all? (path, args, first_too, &block)
-  result=true
+def true_for_all? ( path, args, first_too, &block )
+  result = true
   iter_over = first_too ? ARGS : ARGS[1..-1]
   iter_over.each {|arch|
-    result=false unless yield(File.join(args[:root], arch, path), arch)
+    result = false unless yield( File.join( args[:input], arch, path ), arch )
   }
   return result
 end
 
-def copy (origin, target, args)
-  if !File.exist?(target) or args[:force]
+def copy ( origin, target, args )
+  if !File.exist?( target ) or args[:force]
     FileUtils.cp(
       origin,
       target,
@@ -130,233 +130,225 @@
   end
 end
 
-def lipo (filepath,architectures,args)
+def lipo ( filepath, architectures, args )
   lipoargs = Array.new
   architectures.each {|arch|
     lipoargs << sprintf(
       '-arch %s %s',
       arch, File.join(
-        args[:root], arch, filepath
+        args[:input], arch, filepath
       )
     )
   }
-  lipotarget=File.join(args[:output], filepath)
+  lipotarget = File.join( args[:output], filepath )
   lipocmd = sprintf(
     'lipo %s -create -o %s',
-    lipoargs.join(' '),
+    lipoargs.join( ' ' ),
     lipotarget
   )
-  if !File.exist?(lipotarget) or args[:force]
+  if !File.exist?( lipotarget ) or args[:force]
     puts lipocmd if args[:verbose]
     system lipocmd unless args[:dry_run]
   end
 end
 
-ORIGINAL_DIR=Dir.pwd
-processed=Set.new
+ORIGINAL_DIR = Dir.pwd
+processed = Set.new
 
-if File.directory?(File.expand_path(arguments[:root]))
-  ARGS.each {|architecture|
-    FileUtils.cd ORIGINAL_DIR, :verbose => false
-    if File.directory? File.join(
-      File.expand_path(arguments[:root]), architecture
-    )
-      FileUtils.cd(
-        File.join(arguments[:root], architecture),
-        :verbose => false
-      )
-      Find.find('.') {|path|
-        arguments[:exclude].each {|exclude_me|
-          Find.prune if File.basename(path) == exclude_me
+# make sure we're given a valid root directory
+unless File.directory?( arguments[:input] )
+  raise sprintf(
+    'invalid input directory: %s',
+    arguments[:input]
+  )
+end
+
+# make sure the requested architectures have corresponding subdirectories
+# in the the given input directory
+unless true_for_all?( '.', arguments, true) {|filepath, arch|
+  File.directory? filepath
+}
+  raise 'architecture missing from input directory'
+end
+
+ARGS.each {|architecture|
+  FileUtils.cd(
+    File.join( arguments[:input], architecture )
+  )
+  Find.find( '.' ) {|path|
+    arguments[:exclude].each {|exclude_me|
+      Find.prune if File.basename( path ) == exclude_me
+    }
+    unless processed.include? path
+      my_dir = File.dirname( File.join( arguments[:output], path ) )
+      # TODO: maybe mkdir should only be called *after* we've decided what
+      #       needs to be done with the file
+      unless File.exist? my_dir
+        FileUtils.mkdir_p(
+          my_dir,
+          :noop => arguments[:dry_run],
+          :verbose => arguments[:verbose]
+        )
+      end
+      first_path = File.join( arguments[:input], ARGS[0], path )
+      if true_for_all?( path, arguments, true ) {|filepath, arch|
+        !FileTest.symlink? filepath
+      }
+        if true_for_all?( path, arguments, true ) {|filepath, arch|
+          File.exist? filepath
         }
-        unless processed.include? path
-          my_dir=File.dirname(File.join(arguments[:output], path))
-          # TODO: what if ppc/foo is a dir and i386/foo is a file (symlink)? (1)
-          # TODO: maybe mkdir should only be called *after* we've decided what
-          #       needs to be done with the file
-          unless File.exist? my_dir
-            FileUtils.mkdir_p(
-              my_dir,
-              :noop => arguments[:dry_run],
-              :verbose => arguments[:verbose]
-            )
-          end
-          if true_for_all?(path,arguments,true) {|filepath,arch|
-            !FileTest.symlink? filepath
+          if true_for_all?( path, arguments, true ) {|filepath, arch|
+            !File.directory? filepath
           }
-            if true_for_all?(path, arguments,true) {|filepath,arch|
-              File.exist? filepath
+            if true_for_all?( path, arguments, false ) {|filepath, arch|
+              FileUtils.identical?( filepath, first_path )
             }
-              if true_for_all?(path, arguments,true) {|filepath,arch|
-                !File.directory? filepath
-              }
-                if true_for_all?(path, arguments,false) {|filepath,arch|
-                  FileUtils.identical?(
-                    filepath, 
-                    File.join(arguments[:root], ARGS[0], path)
+              copy( first_path, File.join( arguments[:output], path ), arguments )
+            else
+              case File.basename path
+              when %r{\.h$}, %r{\.hpp$}
+                if arguments[:force] or !File.exists?(
+                  File.join( arguments[:output], path )
+                )
+                  open( File.join( arguments[:output], path ), 'w' ) {|file|
+                    ARGS.each {|arch|
+                      file.printf '#ifdef __%s__\n', arch
+                      file.puts open(
+                        File.join( arguments[:input], arch, path ), 'r'
+                      ).read
+                      file.puts '#endif'
+                    }
+                  }
+                end
+              when %r{\.pc$}
+                ARGS.each {|arch|
+                  copy_target = File.join(
+                    arguments[:output],
+                    File.dirname( path ),
+                    arch,
+                    File.basename( path )
                   )
-                }
+                  unless File.exists? File.dirname( copy_target )
+                    FileUtils.mkdir_p(
+                      File.dirname( copy_target ),
+                      :verbose => arguments[:verbose],
+                      :noop => arguments[:dry_run]
+                    )
+                  end
                   copy(
-                    File.join(arguments[:root],ARGS[0],path),
-                    File.join(arguments[:output],path),
+                    File.join( arguments[:input], arch, path ),
+                    copy_target,
                     arguments
                   )
-                else
-                  case File.basename path
-                  # TODO: more cases
-                  when /\.h$/, /\.hpp$/
-                    if !File.exists?(File.join(arguments[:output], path)) or arguments[:force]
-                      open(File.join(arguments[:output], path), 'w') {|file|
-                        ARGS.each {|arch|
-                          file.printf "#ifdef __%s__\n", arch
-                          file.puts open(File.join(arguments[:root],arch,path), 'r').read
-                          file.puts '#endif'
-                        }
-                      }
-                    end
-                  when /\.pc$/
-                    ARGS.each {|arch|
-                      copy_target=File.join(
-                        arguments[:output],
-                        File.dirname(path),
-                        arch,
-                        File.basename(path)
-                      )
-                      unless File.exists? File.dirname(copy_target)
-                        FileUtils.mkdir_p(
-                          File.dirname(copy_target),
-                          :verbose => arguments[:verbose],
-                          :noop => arguments[:dry_run]
-                        )
-                      end
-                      copy(
-                        File.join(arguments[:root],arch,path),
-                        copy_target,
-                        arguments
-                      )
+                }
+              else
+                file_output = %x{ #{ FILE } -b "#{ first_path }" }.chomp
+                case file_output
+                when %r{^current ar archive}
+                  if true_for_all?( path, arguments, false ) {|filepath, arch|
+                    filecall_out = %x{#{ FILE } -b "#{ filepath }"}.chomp
+                    filecall_out =~ %r{^current ar archive}
+                  }
+                    if true_for_all?( path, arguments, true ) {|filepath, arch|
+                      lipocall_out = %x{lipo -info "#{ filepath }"}.chomp
+                      lipocall_out =~ %r{is architecture: #{ arch }$}
                     }
+                      lipo( path, ARGS, arguments )
+                    else
+                      raise sprintf(
+                        'universal binary or wrong architecture: %s',
+                        path
+                      )
+                    end
                   else
-                    file_output = %x{
-                      #{FILE} -b "#{
-                        File.join(arguments[:root],ARGS[0],path)
-                      }"
-                    }.chomp
-                    case file_output
-                    when /^current ar archive/
-                      if true_for_all?(path, arguments,false) {|filepath,arch|
-                        %x{#{FILE} -b "#{filepath}"}.chomp =~ /^current ar archive/
+                    # some files are archives, some are not
+                  end
+                when %r{^Mach-O}
+                  if true_for_all?( path, arguments, false ) {|filepath, arch|
+                    filecall_out = %x{#{ FILE } -b "#{ filepath }"}.chomp
+                    filecall_out =~ %r{^Mach-O}
+                  }
+                    if true_for_all?( path, arguments, true ) {|filepath, arch|
+                      lipocall_out = %x{lipo -info "#{ filepath }"}.chomp
+                      lipocall_out =~ %r{is architecture: #{arch}$}
+                    }
+                      links = Hash.new
+                      ARGS.each {|my_arch|
+                        links[my_arch] = %x{
+                          #{
+                            my_arch =~ %r{64$} ? 'otool64' : 'otool'
+                          } -arch #{ my_arch } -LX #{
+                            File.join( arguments[:input], my_arch, path )
+                          }
+                        }.split( "\n" ).collect {|dep_line|
+                          dep_line.lstrip.gsub(
+                            %r{ \(compatibility version \d+(\.\d+)*, current version \d+(\.\d+)*\)}, ''
+                          )
+                        }.reject {|dep|
+                          dep =~ %r{^\/usr\/lib\/}
+                        }.to_set
                       }
-                        if true_for_all?(path, arguments,true) {|filepath,arch|
-                          %x{lipo -info "#{filepath}"}.chomp =~ /is architecture: #{arch}$/
-                        }
-                          lipo(path,ARGS,arguments)
-                        else
-                          # ERROR: processing universal binary or wrong arch
+                      ARGS.each {|my_arch|
+                        unless links[my_arch] == links[ARGS[0]]
+                          missing_in  = links[my_arch]-links[ARGS[0]]
+                          missing_out = links[ARGS[0]]-links[my_arch]
+                          if missing_in.any? or missing_out.any?
+                            raise sprintf(
+                              'difference in linking encountered: file %s',
+                              path
+                            )
+                          end
                         end
-                      else
-                        # ERROR: mixed filetypes
-                      end
-                    when /^Mach-O/
-                      if true_for_all?(path, arguments,false) {|filepath,arch|
-                        %x{#{FILE} -b "#{filepath}"}.chomp =~ /^Mach-O/
                       }
-                        if true_for_all?(path, arguments,true) {|filepath,arch|
-                          %x{lipo -info "#{filepath}"}.chomp =~ /is architecture: #{arch}$/
-                        }
-                          links=Hash.new
-                          ARGS.each {|my_arch|
-                            links[my_arch]=%x{
-                              #{
-                                my_arch =~ /64$/ ? 'otool64' : 'otool'
-                              } -arch #{my_arch} -LX #{
-                                File.join(arguments[:root],my_arch,path)
-                              }
-                            }.split("\n").collect {|depline|
-                              depline.lstrip.gsub(
-                                / \(compatibility version \d+(\.\d+)*, current version \d+(\.\d+)*\)/, ''
-                              )
-                            }.reject {|dep|
-                              dep =~ /^\/usr\/lib\//
-                            }.to_set
-                          }
-                          ARGS.each {|my_arch|
-                            unless links[my_arch] == links[ARGS[0]]
-                              missing_in  = links[my_arch]-links[ARGS[0]]
-                              missing_out = links[ARGS[0]]-links[my_arch]
-                              # TODO: come up with a better error message
-                              if missing_in.any? or missing_out.any?
-                                raise sprintf(
-                                  'difference in linking, file %s', path
-                                )
-                              end
-                            end
-                          }
-                          lipo(path,ARGS,arguments)
-                        else
-                          raise 'processing universal binary or wrong architecture'
-                        end
-                      else
-                        # ERROR: mixed filetypes
-                      end
-                    # TODO: handle more filetypes
+                      lipo( path, ARGS, arguments )
                     else
-                      puts "dunno type: #{path}"
+                      raise sprintf(
+                        'universal binary or wrong architecture: %s',
+                        path
+                      )
                     end
+                  else
+                    # some files are mach-o, some are not
                   end
+                else
+                  # unable to determine file type
                 end
-              elsif !true_for_all?(path, arguments,true) {|filepath,arch|
-                File.directory? filepath
-              }
-                # TODO: we have a mix of at least one directory and items that
-                # are not directories
               end
-            else
-              # TODO: a file is not present in all trees. what's wrong?
             end
-          elsif true_for_all?(path,arguments,true) {|filepath,arch|
-            FileTest.symlink? filepath
-          }
-            link_target = Pathname.new(
-              File.join( arguments[:root], ARGS[0],path)
-            ).readlink
-            if true_for_all?(path,arguments,false) {|filepath,arch|
-              my_link_target = Pathname.new(
-                File.join( arguments[:root], arch, path)
-              ).readlink
-              my_link_target == link_target
-            }
-              link_output=File.join(arguments[:output],path)
-              if !File.exists? link_output or arguments[:force]
-                FileUtils.symlink(
-                  link_target,
-                  link_output, 
-                  :force => arguments[:force],
-                  :verbose => arguments[:verbose],
-                  :noop => arguments [:dry_run]
-                )
-              end
-            else
-              # ERROR: link targets differ
-            end
-          else
-            # TODO: dealing with a mix of at least one symlink and items that
-            # are not symlinks
           end
-          processed << path
+        else
+          # a file is not present in all trees.
         end
+      elsif true_for_all?( path, arguments, true ) {|filepath, arch|
+        FileTest.symlink? filepath
       }
-    else
-      raise sprintf(
-        'architecture missing or not a directory: %s',
-        architecture
-      )
+        link_target = Pathname.new( first_path ).readlink
+        if true_for_all?( path, arguments, false ) {|filepath, arch|
+          my_link_target = Pathname.new(
+            File.join( arguments[:input], arch, path )
+          ).readlink
+          my_link_target == link_target
+        }
+          link_output = File.join( arguments[:output], path )
+          if !File.exists? link_output or arguments[:force]
+            FileUtils.symlink(
+              link_target,
+              link_output, 
+              :force => arguments[:force],
+              :verbose => arguments[:verbose],
+              :noop => arguments [:dry_run]
+            )
+          end
+        else
+          # link targets differ
+        end
+      else
+        # dealing with a mix of at least one symlink and items that are not
+        # symlinks
+      end
+      processed << path
     end
   }
-else
-  raise sprintf(
-    'invalid root directory: %s',
-    arguments[:root]
-  )
-end
+}
 
-FileUtils.cd ORIGINAL_DIR, :verbose => false
+FileUtils.cd ORIGINAL_DIR

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20070703/fb8eb008/attachment.html


More information about the macports-changes mailing list