Module: UnixUtils
- Defined in:
- lib/unix_utils.rb,
lib/unix_utils/version.rb
Constant Summary collapse
- BUFSIZE =
2**16
- VERSION =
"0.0.12"
Class Method Summary collapse
-
.available?(bin) ⇒ Boolean
:nodoc:.
-
.awk(infile, *expr) ⇒ Object
–.
- .bunzip2(infile) ⇒ Object
-
.bzip2(infile) ⇒ Object
–.
- .curl(url, form_data = nil) ⇒ Object
-
.cut(infile, character_positions) ⇒ Object
specify character_positions as a string like “3-5” or “3,9-10”.
- .dos2unix(infile) ⇒ Object
- .du_sk(srcdir) ⇒ Object
- .gunzip(infile) ⇒ Object
- .gzip(infile) ⇒ Object
- .head(infile, lines) ⇒ Object
- .iconv(infile, to, from) ⇒ Object
-
.md5sum(infile) ⇒ Object
– os x 10.6.8; most platforms $ openssl dgst -md5 .bashrc MD5(.bashrc)= 88f464fb6d1d6fe9141135248bf7b265 ubuntu 11.04; fedora 7; gentoo $ md5sum –binary .mysql_history 8d01e54ab8142d6786850e22d55a1b6c *.mysql_history.
- .method_missing(method_id, *args) ⇒ Object
-
.perl(infile, *expr) ⇒ Object
Yes, this is a very limited use of perl.
-
.sed(infile, *expr) ⇒ Object
POSIX sed, whether it’s provided by sed or gsed.
-
.shasum(infile, algorithm) ⇒ Object
– most platforms $ openssl dgst -sha256 .bash_profile SHA256(.bash_profile)= ae12206aaa35dc96273ed421f4e85ca26a1707455e3cc9f054c7f5e2e9c53df6 ubuntu 11.04 $ shasum -a 256 –portable .mysql_history 856aa27deb0b80b41031c2ddf722af28ba2a8c4999ff9cf2d45f33bc67d992ba ?.mysql_history fedora 7 $ sha256sum –binary .bash_profile 01b1210962b3d1e5e1ccba26f93d98efbb7b315b463f9f6bdb40ab496728d886 *.bash_profile.
-
.spawn(argv, options = {}) ⇒ Object
:nodoc:.
- .tail(infile, lines) ⇒ Object
- .tar(srcdir) ⇒ Object
-
.tmp_path(ancestor, extname = nil) ⇒ Object
:nodoc:.
- .unix2dos(infile) ⇒ Object
- .untar(infile) ⇒ Object
-
.unzip(infile) ⇒ Object
–.
- .wc(infile) ⇒ Object
- .zip(srcdir) ⇒ Object
Class Method Details
.available?(bin) ⇒ Boolean
:nodoc:
239 240 241 242 243 244 |
# File 'lib/unix_utils.rb', line 239 def self.available?(bin) # :nodoc: bin = bin.to_s return @@available_query[bin] if defined?(@@available_query) and @@available_query.is_a?(::Hash) and @@available_query.has_key?(bin) @@available_query ||= {} @@available_query[bin] = ::Kernel.system 'which', '-s', bin end |
.awk(infile, *expr) ⇒ Object
–
160 161 162 163 164 165 166 167 |
# File 'lib/unix_utils.rb', line 160 def self.awk(infile, *expr) infile = ::File. infile outfile = tmp_path infile bin = available?('gawk') ? 'gawk' : 'awk' argv = [bin, expr, infile].flatten spawn argv, :write_to => outfile outfile end |
.bunzip2(infile) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/unix_utils.rb', line 116 def self.bunzip2(infile) infile = ::File. infile outfile = tmp_path infile argv = ['bunzip2', '--stdout', infile] spawn argv, :write_to => outfile outfile end |
.bzip2(infile) ⇒ Object
–
126 127 128 129 130 131 132 |
# File 'lib/unix_utils.rb', line 126 def self.bzip2(infile) infile = ::File. infile outfile = tmp_path infile, '.bz2' argv = ['bzip2', '--keep', '--stdout', infile] spawn argv, :write_to => outfile outfile end |
.curl(url, form_data = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/unix_utils.rb', line 13 def self.curl(url, form_data = nil) outfile = tmp_path url if url.start_with?('file://') or url.start_with?('/') or url.count('/') == 0 # deal with local files infile = ::File. url.sub('file://', '') ::FileUtils.cp infile, outfile return outfile end uri = ::URI.parse url argv = [ 'curl', '--location', '--show-error', '--silent', '--compressed', '--header', 'Expect: ' ] if form_data argv += [ '--data', form_data ] end argv += [ uri.to_s, '--output', outfile ] spawn argv outfile end |
.cut(infile, character_positions) ⇒ Object
specify character_positions as a string like “3-5” or “3,9-10”
223 224 225 226 227 228 229 |
# File 'lib/unix_utils.rb', line 223 def self.cut(infile, character_positions) infile = ::File. infile outfile = tmp_path infile argv = ['cut', '-c', character_positions, infile] spawn argv, :write_to => outfile outfile end |
.dos2unix(infile) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/unix_utils.rb', line 187 def self.dos2unix(infile) infile = ::File. infile if available?('gawk') or available?('awk') awk infile, '{ sub(/\r/, ""); printf("%s\n", $0) }' else perl infile, 's/\r\n|\n|\r/\n/g' end end |
.du_sk(srcdir) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/unix_utils.rb', line 74 def self.du_sk(srcdir) srcdir = ::File. srcdir argv = ['du', '-sk', srcdir] stdout = spawn argv stdout.strip.split(/\s+/).first.to_i end |
.gunzip(infile) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/unix_utils.rb', line 108 def self.gunzip(infile) infile = ::File. infile outfile = tmp_path infile argv = ['gunzip', '--stdout', infile] spawn argv, :write_to => outfile outfile end |
.gzip(infile) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/unix_utils.rb', line 150 def self.gzip(infile) infile = ::File. infile outfile = tmp_path infile, '.gz' argv = ['gzip', '--stdout', infile] spawn argv, :write_to => outfile outfile end |
.head(infile, lines) ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/unix_utils.rb', line 214 def self.head(infile, lines) infile = ::File. infile outfile = tmp_path infile argv = ['head', '-n', lines.to_s, infile] spawn argv, :write_to => outfile outfile end |
.iconv(infile, to, from) ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/unix_utils.rb', line 231 def self.iconv(infile, to, from) infile = ::File. infile outfile = tmp_path infile argv = ['iconv', '-c', '-t', to, '-f', from, infile] spawn argv, :write_to => outfile outfile end |
.md5sum(infile) ⇒ Object
– os x 10.6.8; most platforms $ openssl dgst -md5 .bashrc MD5(.bashrc)= 88f464fb6d1d6fe9141135248bf7b265 ubuntu 11.04; fedora 7; gentoo $ md5sum –binary .mysql_history 8d01e54ab8142d6786850e22d55a1b6c *.mysql_history
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/unix_utils.rb', line 61 def self.md5sum(infile) infile = ::File. infile if available?('md5sum') argv = ['md5sum', '--binary', infile] stdout = spawn argv stdout.strip.split(' ').first else argv = ['openssl', 'dgst', '-md5', infile] stdout = spawn argv stdout.strip.split(' ').last end end |
.method_missing(method_id, *args) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/unix_utils.rb', line 328 def self.method_missing(method_id, *args) base_method_id = method_id.to_s.chomp('_s') if respond_to?(base_method_id) begin outfile = send(*([base_method_id]+args)) ::File.read outfile ensure ::FileUtils.rm_f outfile end else super end end |
.perl(infile, *expr) ⇒ Object
Yes, this is a very limited use of perl.
170 171 172 173 174 175 176 |
# File 'lib/unix_utils.rb', line 170 def self.perl(infile, *expr) infile = ::File. infile outfile = tmp_path infile argv = [ 'perl', expr.map { |e| ['-pe', e] }, infile ].flatten spawn argv, :write_to => outfile outfile end |
.sed(infile, *expr) ⇒ Object
POSIX sed, whether it’s provided by sed or gsed
197 198 199 200 201 202 203 204 |
# File 'lib/unix_utils.rb', line 197 def self.sed(infile, *expr) infile = ::File. infile outfile = tmp_path infile bin = available?('sed') ? 'sed' : ['gsed', '--posix'] argv = [ bin, expr.map { |e| ['-e', e] }, infile ].flatten spawn argv, :write_to => outfile outfile end |
.shasum(infile, algorithm) ⇒ Object
– most platforms $ openssl dgst -sha256 .bash_profile SHA256(.bash_profile)= ae12206aaa35dc96273ed421f4e85ca26a1707455e3cc9f054c7f5e2e9c53df6 ubuntu 11.04 $ shasum -a 256 –portable .mysql_history 856aa27deb0b80b41031c2ddf722af28ba2a8c4999ff9cf2d45f33bc67d992ba ?.mysql_history fedora 7 $ sha256sum –binary .bash_profile 01b1210962b3d1e5e1ccba26f93d98efbb7b315b463f9f6bdb40ab496728d886 *.bash_profile
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/unix_utils.rb', line 41 def self.shasum(infile, algorithm) infile = ::File. infile if available?('shasum') argv = ['shasum', '--binary', '-a', algorithm.to_s, infile] stdout = spawn argv stdout.strip.split(' ').first else argv = ['openssl', 'dgst', "-sha#{algorithm}", infile] stdout = spawn argv stdout.strip.split(' ').last end end |
.spawn(argv, options = {}) ⇒ Object
:nodoc:
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/unix_utils.rb', line 254 def self.spawn(argv, = {}) # :nodoc: = .dup input = if (read_from = .delete(:read_from)) ::File.open(read_from, 'r') end output = if (write_to = .delete(:write_to)) output_redirected = true ::File.open(write_to, 'wb') else output_redirected = false ::StringIO.new end error = ::StringIO.new pid, stdin, stdout, stderr = ::POSIX::Spawn.popen4(*(argv+[])) # lifted from posix-spawn # https://github.com/rtomayko/posix-spawn/blob/master/lib/posix/spawn/child.rb readers = [stdout, stderr] writers = if input [stdin] else stdin.close [] end while readers.any? or writers.any? ready = ::IO.select(readers, writers, readers + writers) # write to stdin stream ready[1].each do |fd| begin boom = nil size = fd.write(input.read(BUFSIZE)) rescue ::Errno::EPIPE => boom rescue ::Errno::EAGAIN, ::Errno::EINTR end if boom || size < BUFSIZE stdin.close input.close writers.delete(stdin) end end # read from stdout and stderr streams ready[0].each do |fd| buf = (fd == stdout) ? output : error begin buf << fd.readpartial(BUFSIZE) rescue ::Errno::EAGAIN, ::Errno::EINTR rescue ::EOFError readers.delete(fd) fd.close end end end # thanks @tmm1 and @rtomayko for showing how it's done! ::Process.waitpid pid error.rewind unless (whole_error = error.read).empty? $stderr.puts "[unix_utils] `#{argv.join(' ')}` STDERR:" $stderr.puts whole_error end unless output_redirected output.rewind output.read end ensure [stdin, stdout, stderr, input, output, error].each { |io| io.close if io and not io.closed? } end |
.tail(infile, lines) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/unix_utils.rb', line 206 def self.tail(infile, lines) infile = ::File. infile outfile = tmp_path infile argv = ['tail', '-n', lines.to_s, infile] spawn argv, :write_to => outfile outfile end |
.tar(srcdir) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/unix_utils.rb', line 134 def self.tar(srcdir) srcdir = ::File. srcdir outfile = tmp_path srcdir, '.tar' argv = ['tar', '-cf', outfile, '-C', srcdir, '.'] spawn argv outfile end |
.tmp_path(ancestor, extname = nil) ⇒ Object
:nodoc:
246 247 248 249 250 251 252 |
# File 'lib/unix_utils.rb', line 246 def self.tmp_path(ancestor, extname = nil) # :nodoc: ancestor = ancestor.to_s extname ||= ::File.extname ancestor basename = ::File.basename ancestor.gsub(/unix_utils_[a-f0-9]{8,}_/, '') basename.gsub! /\W+/, '_' ::File.join ::Dir.tmpdir, "unix_utils_#{::SecureRandom.hex(4)}_#{basename[0..(234-extname.length)]}#{extname}" end |
.unix2dos(infile) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/unix_utils.rb', line 178 def self.unix2dos(infile) infile = ::File. infile if available?('gawk') or available?('awk') awk infile, '{ sub(/\r/, ""); printf("%s\r\n", $0) }' else perl infile, 's/\r\n|\n|\r/\r\n/g' end end |
.untar(infile) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/unix_utils.rb', line 99 def self.untar(infile) infile = ::File. infile destdir = tmp_path infile ::FileUtils.mkdir destdir argv = ['tar', '-xf', infile, '-C', destdir] spawn argv destdir end |
.unzip(infile) ⇒ Object
–
90 91 92 93 94 95 96 97 |
# File 'lib/unix_utils.rb', line 90 def self.unzip(infile) infile = ::File. infile destdir = tmp_path infile ::FileUtils.mkdir destdir argv = ['unzip', '-qq', '-n', infile, '-d', destdir] spawn argv destdir end |
.wc(infile) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/unix_utils.rb', line 81 def self.wc(infile) infile = ::File. infile argv = ['wc', infile] stdout = spawn argv stdout.strip.split(/\s+/)[0..2].map { |s| s.to_i } end |
.zip(srcdir) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/unix_utils.rb', line 142 def self.zip(srcdir) srcdir = ::File. srcdir outfile = tmp_path srcdir, '.zip' argv = ['zip', '-rq', outfile, '.'] spawn argv, :chdir => srcdir outfile end |