module Drupid::Utils

Public Instance Methods

blah(notice) click to toggle source

Prints a notice if in verbose mode.

    # File lib/drupid/utils.rb
119 def blah notice
120   return unless $VERBOSE
121   puts "#{Tty.green}==>#{Tty.reset} #{notice}"
122 end
bzr(*args) click to toggle source
    # File lib/drupid/utils.rb
190 def bzr *args
191   bzr = Pathname.new(which 'bzr')
192   raise "bzr not found" unless bzr.exist?
193   raise "bzr is not executable" unless bzr.executable?
194   runBabyRun bzr, args, :redirect_stderr_to_stdout => true    
195 end
compare_paths(src, tgt, additional_rsync_options = []) click to toggle source

Compares the content of two directories using rsync. Changes in timestamps only are ignored. Returns a possibly empty list of differences.

    # File lib/drupid/utils.rb
248 def compare_paths src, tgt, additional_rsync_options = []
249   p1 = Pathname.new(src).realpath.to_s + '/'
250   p2 = Pathname.new(tgt).realpath.to_s + '/'
251   args = Array.new
252   args << '-rlD'
253   args << '--dry-run'
254   args << '--delete'
255   args << '--itemize-changes'
256   args += additional_rsync_options
257   args << p1
258   args << p2
259   output = runBabyRun 'rsync', args, :verbose => false
260   changes = Array.new
261   output.each_line do |l|
262     next if l =~ /[fdLDS]\.\.[tT]\.\.\.\./ # Skip changes in timestamps only
263     changes << l.strip
264   end
265   return changes
266 end
curl(*args) click to toggle source
    # File lib/drupid/utils.rb
153 def curl *args
154   curl = Pathname.new(which 'curl')
155   args = ['-qf#LA', USER_AGENT, *args]
156   args << "--insecure" #if MacOS.version < 10.6
157   args << "--silent" unless $VERBOSE
158 
159   runBabyRun curl, args
160 end
cvs(*args) click to toggle source
    # File lib/drupid/utils.rb
176 def cvs *args
177   cvs = Pathname.new(which 'cvs')
178   raise "cvs not found" unless cvs.exist?
179   raise "cvs is not executable" unless cvs.executable?
180   runBabyRun cvs, args, :redirect_stderr_to_stdout => true
181 end
debug(title, *info) click to toggle source

Prints debug information.

    # File lib/drupid/utils.rb
108 def debug title, *info
109   return unless $DEBUG
110   puts "#{Tty.purple}[DEBUG]#{Tty.white} #{title}#{Tty.reset}"
111   info.each do |chunk|
112     chunk.each_line do |l|
113       puts "#{Tty.purple}[DEBUG]#{Tty.reset} #{l.chomp!}"
114     end
115   end
116 end
dont_debug() { || ... } click to toggle source

Temporarily forces $DEBUG = false in the given block.

This is used to suppress some harmless but annoying exception messages from some methods, e.g., FileUtils.mkpath.

    # File lib/drupid/utils.rb
300 def dont_debug
301   saved_debug = $DEBUG
302   $DEBUG = false
303   begin
304     yield
305   ensure
306     $DEBUG = saved_debug
307   end
308 end
git(*args) click to toggle source
    # File lib/drupid/utils.rb
162 def git *args
163   git = Pathname.new(which 'git')
164   raise "git not found" unless git.exist?
165   raise "git is not executable" unless git.executable?
166   runBabyRun git, args, :redirect_stderr_to_stdout => true
167 end
hg(*args) click to toggle source
    # File lib/drupid/utils.rb
183 def hg *args
184   hg = Pathname.new(which 'hg')
185   raise "hg not found" unless hg.exist?
186   raise "hg is not executable" unless hg.executable?
187   runBabyRun hg, args, :redirect_stderr_to_stdout => true   
188 end
ignore_interrupts() { || ... } click to toggle source
    # File lib/drupid/utils.rb
273 def ignore_interrupts
274   std_trap = trap("INT") {}
275   yield
276 ensure
277   trap("INT", std_trap)
278 end
interactive_shell() click to toggle source
    # File lib/drupid/utils.rb
280 def interactive_shell
281   fork {exec ENV['SHELL'] }
282   Process.wait
283   unless $?.success?
284     owarn "Non-zero exit status: #{$?}"
285   end
286 end
odie(error) click to toggle source

Prints an error message and exits.

    # File lib/drupid/utils.rb
102 def odie error
103   ofail error
104   exit 1
105 end
ofail(error, *info) click to toggle source

Prints an error message.

   # File lib/drupid/utils.rb
96 def ofail error, *info
97   puts "#{Tty.red}Fail#{Tty.reset}: #{error}"
98   puts info unless info.empty?
99 end
ohai(title, *sput) click to toggle source

Prints a message.

   # File lib/drupid/utils.rb
85 def ohai title, *sput
86   puts "#{Tty.green}==>#{Tty.white} #{title}#{Tty.reset}"
87   puts sput unless sput.empty?
88 end
owarn(warning) click to toggle source

Print a warning message.

   # File lib/drupid/utils.rb
91 def owarn warning
92   puts "#{Tty.red}Warn#{Tty.reset}: #{warning}"
93 end
runBabyRun(command, arguments = [], options = {}) click to toggle source

Executes a command. Returns the output of the command. Raises a Drupid::ErrorDuringExecution error if the command does not exit successfully.

command

A String or Pathname object

arguments

An optional Array of arguments

options

An optional Hash of options

Options: out, err, redirect_stderr_to_stdout, dry

    # File lib/drupid/utils.rb
133 def runBabyRun command, arguments = [], options = {}
134   opts = { :dry => false }.merge!(options)
135   cmd = String.new(command.to_s)
136   raise "Not an array" unless arguments.is_a?(Array)
137   args = arguments.map { |arg| arg.to_s }
138   cmd << ' '     + args.shelljoin
139   cmd << ' >'    + Shellwords.shellescape(opts[:out]) if opts[:out]
140   cmd << ' 2>'   + Shellwords.shellescape(opts[:err]) if opts[:err]
141   cmd << ' 2>&1' if opts[:redirect_stderr_to_stdout]
142   debug "Pwd: #{Dir.pwd}"
143   debug cmd
144   return cmd if opts[:dry]
145   output = %x|#{cmd}| # Run baby run!
146   unless $?.success?
147     debug 'Command failed', output
148     raise ErrorDuringExecution, output
149   end
150   return output
151 end
svn(*args) click to toggle source
    # File lib/drupid/utils.rb
169 def svn *args
170   svn = Pathname.new(which 'svn')
171   raise "svn not found" unless svn.exist?
172   raise "svn is not executable" unless svn.executable?
173   runBabyRun svn, args, :redirect_stderr_to_stdout => true
174 end
tempdir() { || ... } click to toggle source

Creates a temporary directory then yield. When the block returns, recursively delete the temporary directory.

    # File lib/drupid/utils.rb
224 def tempdir
225   # I used /tmp rather than `mktemp -td` because that generates a directory
226   # name with exotic characters like + in it, and these break badly written
227   # scripts that don't escape strings before trying to regexp them :(
228 
229   # If the user has FileVault enabled, then we can't mv symlinks from the
230   # /tmp volume to the other volume. So we let the user override the tmp
231   # prefix if they need to.
232   tmp_prefix = '/tmp'
233   tmp = Pathname.new `mktemp -d #{tmp_prefix}/temp_item-XXXXXX`.strip
234   raise "Couldn't create temporary directory" if not tmp.directory? or $? != 0
235   begin
236     wd = Dir.pwd
237     FileUtils.chdir tmp
238     yield
239   ensure
240     FileUtils.chdir wd
241     dont_debug { tmp.rmtree }
242   end
243 end
uncompress(archive, options = {}) click to toggle source

Uncompresses an archive in the current directory.

archive

A Pathname object representing the full path to the archive.

The the :type options is used, the archive is interpreted as the given type (:zip, :gzip, :bzip2, :compress, :tar, :xz, :rar), otherwise the type is guessed based on the file content.

Options: type

    # File lib/drupid/utils.rb
204 def uncompress archive, options = {}
205   type = options[:type] ? options[:type] : archive.compression_type
206   case type
207   when :zip
208     runBabyRun 'unzip', ['-qq', archive]
209   when :gzip, :bzip2, :compress, :tar
210     # Assume these are also tarred
211     # TODO check if it's really a tar archive
212     runBabyRun 'tar', ['xf', archive]
213   when :xz
214     runBabyRun "xz -dc \"#{archive}\" | tar xf -"
215   when :rar
216     runBabyRun 'unrar', ['x', '-inul', archive]
217   else
218     raise NotAnArchiveError
219   end
220 end
which(cmd) click to toggle source
    # File lib/drupid/utils.rb
268 def which cmd
269   path = `which #{cmd} 2>/dev/null`.chomp
270   path.empty? ? nil : Pathname.new(path)
271 end
writeFile(path, content) click to toggle source

Creates the specified file with the given content. The file is overwritten if it exists.

    # File lib/drupid/utils.rb
290 def writeFile path, content
291   p = Pathname.new(path)
292   blah "Writing #{p}"
293   p.open("w") { |f| f.write(content) }
294 end