SHARE
TWEET

Untitled

a guest Jun 20th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # frozen-string-literal: true
  2. require 'fileutils'
  3. require 'shellwords'
  4. require 'date'
  5.  
  6. Outdir = 'out'
  7.  
  8. def run(cmd)
  9.   puts "!#{cmd}"
  10.   system cmd
  11. end
  12.  
  13. def make_extract_path(name)
  14.   File.join(Outdir, "#{Time.now.strftime '%Y%m%d'}_#{name}")
  15. end
  16.  
  17. def each_extract_command(dst, src)
  18.   yield %{7za x -y -o#{Shellwords.escape dst} #{Shellwords.escape zip}}
  19. end
  20.  
  21. def each_compress_command(dst, src)
  22.   yield %{7za a -mx9 #{Shellwords.escape dst} #{Shellwords.escape dir}}
  23. end
  24.  
  25. def main
  26.   paths = nil
  27.   loop do
  28.     curr = Dir['*']
  29.     if curr != paths
  30.       sweep(curr)
  31.       paths = curr
  32.     end
  33.     sleep 3
  34.   end
  35. end
  36.  
  37. def sweep(paths)
  38.   ok = 0
  39.   paths.each do |path|
  40.     if path =~ /\.zip\z|\.7z\z/i
  41.       extract path
  42.     elsif path != Outdir && File.directory?(path)
  43.       compress path
  44.     else
  45.       puts "##{path}: skipped unknown"
  46.     end
  47.     ok = $?.to_i if ok == 0
  48.   end
  49.   ok == 0
  50. end
  51.  
  52. def compress(dir)
  53.   dst = File.join(Outdir, "#{dir}.7z")
  54.   mtime = File.mtime(dir)
  55.   if File.exist?(dst) && mtime < File.mtime(dst)
  56.     puts "##{dir}: skipped compressing"
  57.     return
  58.   end
  59.   each_compress_command(dst, dir) do |cmd|
  60.     run cmd
  61.   end
  62. end
  63.  
  64. def extract(zip)
  65.   name = File.basename(zip, '.*')
  66.   prev = Dir[File.join(Outdir, "*#{name}*")]
  67.  
  68.   if prev.any? {|x| File.directory? x }
  69.     puts "File exists: #{prev}"
  70.     return
  71.   end
  72.  
  73.   dst = make_extract_path(name)
  74.   mtime = File.mtime(zip)
  75.   if File.exist?(dst) && mtime < File.mtime(dst)
  76.     puts "##{zip}: skipped extracting"
  77.     return
  78.   end
  79.   FileUtils.mkdir_p File.dirname(dst)
  80.   each_extract_command(dst, zip) do |cmd|
  81.     run cmd and return
  82.     FileUtils.rm_rf dst
  83.   end
  84. end
  85.  
  86. main
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top