Module: FPM::Util

Extended by:
FFI::Library
Included in:
Command, Command::Validator, Package
Defined in:
lib/fpm/util.rb

Overview

Some utility functions

Defined Under Namespace

Classes: ExecutableNotFound, ProcessFailed

Instance Method Summary collapse

Instance Method Details

#copy_entry(src, dst) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fpm/util.rb', line 145

def copy_entry(src, dst)
  case File.ftype(src)
  when 'fifo', 'characterSpecial', 'blockSpecial', 'socket'
    st = File.stat(src)
    rc = mknod_w(dst, st.mode, st.dev)
    raise SystemCallError.new("mknod error", FFI.errno) if rc == -1
  when 'directory'
    FileUtils.mkdir(dst) unless File.exists? dst
  else
    FileUtils.copy_entry(src, dst)
  end
end

#mknod_w(path, mode, dev) ⇒ Object

wrapper around mknod ffi calls



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fpm/util.rb', line 133

def mknod_w(path, mode, dev)
  rc = -1
  case %x{uname -s}.chomp
  when 'Linux'
    # bits/stat.h #define _MKNOD_VER_LINUX  0
    rc = xmknod(0, path, mode, FFI::MemoryPointer.new(dev))
  else
    rc = mknod(path, mode, dev)
  end
  rc
end

#program_in_path?(program) ⇒ Boolean

Is the given program in the system’s PATH?

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/fpm/util.rb', line 26

def program_in_path?(program)
  # Scan path to find the executable
  # Do this to help the user get a better error message.
  envpath = ENV["PATH"].split(":")
  return envpath.select { |p| File.executable?(File.join(p, program)) }.any?
end

#safesystem(*args) ⇒ Object

Run a command safely in a way that gets reports useful errors.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fpm/util.rb', line 34

def safesystem(*args)
  # ChildProcess isn't smart enough to run a $SHELL if there's
  # spaces in the first arg and there's only 1 arg.
  if args.size == 1
    args = [ ENV["SHELL"], "-c", args[0] ]
  end
  program = args[0]

  # Scan path to find the executable
  # Do this to help the user get a better error message.
  if !program.include?("/") and !program_in_path?(program)
    raise ExecutableNotFound.new(program)
  end

  @logger.debug("Running command", :args => args)

  # Create a pair of pipes to connect the
  # invoked process to the cabin logger
  stdout_r, stdout_w = IO.pipe
  stderr_r, stderr_w = IO.pipe

  process           = ChildProcess.build(*args)
  process.io.stdout = stdout_w
  process.io.stderr = stderr_w

  process.start
  stdout_w.close; stderr_w.close
  @logger.debug('Process is running', :pid => process.pid)
  # Log both stdout and stderr as 'info' because nobody uses stderr for
  # actually reporting errors and as a result 'stderr' is a misnomer.
  @logger.pipe(stdout_r => :info, stderr_r => :info)

  process.wait
  success = (process.exit_code == 0)

  if !success
    raise ProcessFailed.new("#{program} failed (exit code #{process.exit_code})" \
                            ". Full command was:#{args.inspect}")
  end
  return success
end

#safesystemout(*args) ⇒ Object

Run a command safely in a way that captures output and status.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fpm/util.rb', line 77

def safesystemout(*args)
  if args.size == 1
    args = [ ENV["SHELL"], "-c", args[0] ]
  end
  program = args[0]

  if !program.include?("/") and !program_in_path?(program)
    raise ExecutableNotFound.new(program)
  end

  @logger.debug("Running command", :args => args)

  stdout_r, stdout_w = IO.pipe
  stderr_r, stderr_w = IO.pipe

  process           = ChildProcess.build(*args)
  process.io.stdout = stdout_w
  process.io.stderr = stderr_w

  process.start
  stdout_w.close; stderr_w.close
  stdout_r_str = stdout_r.read
  stdout_r.close; stderr_r.close
  @logger.debug("Process is running", :pid => process.pid)

  process.wait
  success = (process.exit_code == 0)

  if !success
    raise ProcessFailed.new("#{program} failed (exit code #{process.exit_code})" \
                            ". Full command was:#{args.inspect}")
  end

  return stdout_r_str
end

#tar_cmdObject

Get the recommended ‘tar’ command for this platform.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fpm/util.rb', line 114

def tar_cmd
  # Rely on gnu tar for solaris and OSX.
  case %x{uname -s}.chomp
  when "SunOS"
    return "gtar"
  when "Darwin"
    return "gnutar"
  else
    return "tar"
  end
end

#with(value, &block) ⇒ Object

Run a block with a value. Useful in lieu of assigning variables



128
129
130
# File 'lib/fpm/util.rb', line 128

def with(value, &block)
  block.call(value)
end