[27473] users/pipping/merge.rb

source_changes at macosforge.org source_changes at macosforge.org
Sat Aug 4 10:25:31 PDT 2007


Revision: 27473
          http://trac.macosforge.org/projects/macports/changeset/27473
Author:   pipping at macports.org
Date:     2007-08-04 10:25:31 -0700 (Sat, 04 Aug 2007)

Log Message:
-----------
r63:
trivial formatting change
r64:
use logger library to write logs to STDOUT/STDERR/files
add switches for level of verbosity and target to log to, replacing --verbose
move adding of processed files to the `processed` set to the top to avoid processing a file twice when 'Find.prune' or 'next' was invoked
add function create_directory
minor fixes, including re-indention
r65:
trivial changes/fixes

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

Modified: users/pipping/merge.rb
===================================================================
--- users/pipping/merge.rb	2007-08-04 16:27:58 UTC (rev 27472)
+++ users/pipping/merge.rb	2007-08-04 17:25:31 UTC (rev 27473)
@@ -22,6 +22,7 @@
 
 require 'fileutils'
 require 'find'
+require 'logger'
 require 'optparse'
 require 'ostruct'
 require 'pathname'
@@ -40,8 +41,11 @@
     # set up defaults
     options = OpenStruct.new
     options.dryrun  = false
-    options.verbose = false
 
+
+    options.level   = Logger::INFO
+    options.log     = 'STDOUT'
+
     options.exclude = %w{.svn CVS}
 
     options.input   = Pathname.pwd
@@ -51,15 +55,38 @@
       opts.banner = 'Usage: %s [options] arch arch [arch [arch ...]]' % $0
 
       opts.on( '-i', '--input DIRECTORY',
-        'Change input directory', 'Defaults to \'.\'') {|i|
+        'Change input directory', %{Defaults to '.'.} ) {|i|
         options.input = Pathname.new( i ).expand_path || options.input
       }
 
       opts.on( '-o', '--output DIRECTORY',
-        'Change output directory', 'Defaults to \'./out\'') {|o|
+        'Change output directory', %{Defaults to './out'.} ) {|o|
         options.output = Pathname.new( o ).expand_path || options.output
       }
 
+      opts.on( '-v', '--verbosity LEVEL', 'Change level of verbosity',
+         %{Valid arguments are 'debug', 'info',},
+         %{'warn', 'error', and 'fatal'.}, %{Defaults to 'info'.} ) {|v|
+        case v
+        when 'debug'
+          options.level = Logger::DEBUG
+        when 'info'
+          options.level = Logger::INFO
+        when 'warn'
+          options.level = Logger::WARN
+        when 'error'
+          options.level = Logger::ERROR
+        when 'fatal'
+          options.level = Logger::FATAL
+        end
+      }
+
+      opts.on( '-l', '--log TARGET', 'Change target to log to',
+        %{Valid arguments are 'STDOUT', 'STDERR',},
+        %{and '/path/to/file'.}, %{Defaults to 'STDOUT'.} ) {|l|
+        options.log = l
+      }
+
       opts.on( '-e', '--exclude PATTERN1,PATTERN2', Array,
         'Exclude files/directories (glob-style)') {|e|
         options.exclude = e
@@ -69,10 +96,6 @@
         options.dryrun = d
       }
 
-      opts.on( '-v', '--[no-]verbose', 'Run verbosely' ) {|v|
-        options.verbose = v
-      }
-
       opts.on_tail( '-h', '--help', 'Show this message' ) {
         puts opts
         exit
@@ -101,13 +124,25 @@
   return result
 end
 
-def copy ( origin, target, opts )
+def create_directory ( dir, logPath, opts )
+# creates a directory
+  unless dir.exist?
+    dir.mkpath unless opts.dryrun
+    $log.debug( 'created : %s' % logPath )
+  else
+    $log.debug( 'exists  : %s' % logPath )
+  end
+end
+
+def copy ( origin, target, logPath, opts )
 # copies files
   unless target.exist?
     FileUtils.cp( origin, target,
-      :noop => opts.dryrun,
-      :verbose => opts.verbose
+      :noop => opts.dryrun
     )
+    $log.debug( 'copied  : %s' % logPath )
+  else
+    $log.debug( 'exists  : %s' % logPath )
   end
 end
 
@@ -121,7 +156,10 @@
     lipoCallOutput = %x{lipo -info "#{ filePath }"}.chomp
     lipoCallOutput =~ %r{is architecture: #{ arch }$}
   }
-  raise 'type or architecture mismatch' unless type_matches and arch_matches
+  unless type_matches and arch_matches
+    $log.error( 'type or architecture mismatch' )
+    raise( 'an error occurred. see the log for details.' )
+  end
 end
 
 def lipo ( filePath, opts )
@@ -131,10 +169,14 @@
     lipoArgs << sprintf( '-arch %s %s', arch, opts.input + arch + filePath )
   }
   lipoTarget = opts.output + filePath
-  lipoCommand = sprintf( 'lipo %s -create -o %s', lipoArgs.join( ' ' ), lipoTarget )
+  lipoCommand = sprintf(
+    'lipo %s -create -o %s', lipoArgs.join( ' ' ), lipoTarget
+  )
   unless lipoTarget.exist?
-    puts lipoCommand if opts.verbose
     system lipoCommand unless opts.dryrun
+    $log.debug( 'merged  : %s' % filePath )
+  else
+    $log.debug( 'exists  : %s' % filePath )
   end
 end
 
@@ -144,11 +186,11 @@
   unless wrapperTarget.exist?
     wrapperTarget.open( 'w' ) {|wrapper|
       wrapper.puts '#! /bin/sh'
-      wrapper.puts 'DIR="%s"' % filePath.dirname
+      wrapper.puts 'DIR="/%s"' % filePath.dirname
       wrapper.puts 'args=$@'
       wrapper.puts 'if [ "${args/-arch/}" != "$args" ]; then'
       wrapper.puts '  arch=`echo "$args" | ' +
-                   'sed \'s!.*-arch  *\([^ ][^ ]*\).*!\1!\'`;'
+                   %q{sed 's!.*-arch  *\([^ ][^ ]*\).*!\1!'`;}
       wrapper.puts 'else'
       wrapper.puts '  arch=`uname -p`'
       wrapper.puts 'fi'
@@ -163,9 +205,30 @@
 ORIGIN = Pathname.pwd
 processed = Set.new
 
+case options.log
+when 'STDERR'
+  $log = Logger.new( STDERR )
+when 'STDOUT'
+  $log = Logger.new( STDOUT )
+else
+  logTarget = Pathname.new( options.log ).expand_path
+  if logTarget.writable?
+    $log = Logger.new( logTarget )
+  elsif !logTarget.exist? and logTarget.dirname.writable?
+    $log = Logger.new( logTarget )
+  else
+    $log = Logger.new( STDOUT )
+    $log.error( 'cannot create log file' )
+    raise( 'an error occurred.' )
+  end
+end
+$log.level = options.level
+$log.info( 'starting up' )
+
 # make sure we're given a valid root directory
 unless options.input.directory?
-  raise 'invalid input directory: %s' % options.input
+  $log.fatal( 'invalid input directory: %s' % options.input )
+  raise( 'an error occurred. see the log for details.' )
 end
 
 # make sure the requested architectures have corresponding subdirectories
@@ -173,7 +236,8 @@
 unless true_for_all?( '.', true, options) {|filePath, arch|
   filePath.directory?
 }
-  raise 'architecture missing from input directory'
+  $log.fatal( 'architecture missing from input directory' )
+  raise( 'an error occurred. see the log for details.' )
 end
 
 ARGS.each {|architecture|
@@ -186,24 +250,23 @@
       end
     }
     unless processed.include? subPath
+      processed << subPath
       firstPath = options.input + ARGS[0] + subPath
       unless true_for_all?( subPath, false, options ) {|filePath, arch|
         begin
           filePath.ftype == firstPath.ftype
         rescue Errno::ENOENT
-          # DEBUG: file does not exist
           false
         end
       }
-        puts 'DEBUG: skipping %s' % subPath
-        Find.prune
+        $log.warn( 'skipped: %s' % subPath )
+        next
       end
 
       case firstPath.ftype
       # handle file type: directory
       when 'directory'
-        dirToCreate = options.output + subPath
-        dirToCreate.mkpath unless options.dryrun
+        create_directory( options.output + subPath, subPath, options )
       # handle file type: symlinks
       when 'link'
         linkTarget = firstPath.readlink
@@ -225,8 +288,8 @@
         if true_for_all?( subPath, false, options ) {|filePath, arch|
           FileUtils.identical?( filePath, firstPath )
         }
-          copy( firstPath, options.output + subPath, options )
-          Find.prune
+          copy( firstPath, options.output + subPath, subPath, options )
+          next
         end
 
         case subPath.extname
@@ -241,14 +304,19 @@
                 headerTarget.puts '#endif'
               }
             }
+            $log.debug( 'merged: %s' % subPath )
+          else
+            $log.debug( 'exists: %s' % subPath )
           end
         # handle file type: pkg-config files
         when '.pc'
           ARGS.each {|arch|
-            copyTarget = options.output + subPath.dirname + arch + subPath.basename
-            copyTarget.dirname.mkpath unless options.dryrun
-            copy( options.input + arch + subPath, copyTarget, options )
+            subArch = subPath.dirname + arch + subPath.basename
+            copyTarget = options.output + subArch
+            create_directory( copyTarget.dirname, subArch.dirname, options )
+            copy( options.input + arch + subPath, copyTarget, subArch, options )
           }
+          $log.debug( 'spread  : %s' % subPath )
         # handle file type: gzip-compressed man pages
         when '.gz'
           temporaryFiles = Hash.new
@@ -259,11 +327,13 @@
             uncompressed.close
             temporaryFiles[arch] = uncompressed.path
           }
-        if true_for_all?( subPath, false, options ) {|filePath, arch|
-          FileUtils.identical?( temporaryFiles[arch], temporaryFiles[ARGS[0]] )
-        }
-          copy( firstPath, options.output + subPath, options )
-        end
+          if true_for_all?( subPath, false, options ) {|filePath, arch|
+            FileUtils.identical?( temporaryFiles[arch], temporaryFiles[ARGS[0]] )
+          }
+            copy( firstPath, options.output + subPath, subPath, options )
+          else
+            $log.warn( 'skipped: %s' % subPath )
+          end
         # handle file type: other files
         else
           fileOutput = %x{ #{ FILE } -b "#{ firstPath }" }.chomp
@@ -297,27 +367,33 @@
             unless true_for_all?( subPath, false, options ) {|filePath, arch|
               links[arch] == links[ARGS[0]]
             }
-              raise 'difference in linking encountered: file %s' % subPath
+              $log.warn( 'skipped: %s' % subPath )
+              next
             end
             lipo( subPath, options )
           when 'Bourne shell script text executable'
             # DEBUG: no way to handle a shell script in general
-            Find.prune unless subPath.basename.to_s[-7..-1] == '-config'
+            next unless subPath.basename.to_s[-7..-1] == '-config'
             # handle file type: config script
             ARGS.each {|arch|
-              copyTarget=options.output + subPath.dirname + arch + subPath.basename
-              copyTarget.dirname.mkpath unless options.dryrun
-              copy( options.input + arch + subPath, copyTarget, options )
+              subArch = subPath.dirname + arch + subPath.basename
+              copyTarget = options.output + subArch
+              create_directory( copyTarget.dirname, subArch.dirname, options )
+              copy(
+                options.input + arch + subPath, copyTarget, subArch, options
+              )
             }
             make_wrapper( subPath, firstPath, options )
+            $log.debug( 'wrapped : %s' % subPath )
           else
             # DEBUG: unable to determine file type
+            $log.warn( 'skipped: %s' % subPath )
           end
         end
       end
-      processed << subPath
     end
   }
 }
 
 FileUtils.cd ORIGIN
+$log.info( 'shutting down' )

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


More information about the macports-changes mailing list