Module: Photostat::OSUtils

Included in:
Flickr, Local, Query
Defined in:
lib/photostat/utils/os.rb

Instance Method Summary collapse

Instance Method Details

#exec(*params) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/photostat/utils/os.rb', line 43

def exec(*params)
  cmd = Escape.shell_command(params)
  out = `#{cmd} 2>&1`
  unless $?.exitstatus == 0
    raise "Command exit with error!\nCommand: #{cmd}\nOut: #{out}"
  end
  return out
end

#file_md5(file) ⇒ Object



90
91
92
# File 'lib/photostat/utils/os.rb', line 90

def file_md5(file)
  Digest::MD5.file(file).hexdigest
end

#files_in_dir(dir, options = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/photostat/utils/os.rb', line 52

def files_in_dir(dir, options=nil)
  files = []
  dirs  = []
  dir = File.expand_path dir
  current = dir

  return [] unless File.directory? current

  match = options[:match] if options
  not_match = options[:not_match] if options
  non_recursive = options[:non_recursive]
  is_abs = options[:absolute?] or false

  while current        
    Dir.entries(current).each do |name|
      next unless name != '.' and name != '..'
      path = File.join(current, name)

      valid = true
      valid = path =~ match if match and valid
      valid = path !~ not_match if not_match and valid

      if valid
        rpath = path[dir.length+1,path.length]
        yielded = is_abs ? File.join(dir, rpath) : rpath
        files.push yielded
        yield yielded if block_given?
      end

      dirs.push path if !non_recursive and File.directory? path 
    end

    current = dirs.pop
  end

  files
end

#input(msg, options = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/photostat/utils/os.rb', line 5

def input(msg, options=nil)
  options ||= {}
  default = options[:default]
  is_dir  = options[:dir?]

  if default and !default.empty?        
    msg = msg + " (#{default}): "
  else
    msg = msg + ": "
  end

  print msg
  resp = nil

  while not resp
    resp = STDIN.readline.strip
    resp = default if !resp or resp.empty?
    resp = File.expand_path resp if is_dir and resp and !resp.empty?

    error_msg = "Invalid response"

    is_not_dir = false
    if resp and !resp.empty? and !File.directory? resp
      is_not_dir = true if File.exists? resp
      is_not_dir = true unless File.directory? File.dirname(resp)
      error_msg = "Invalid directory path"
    end

    if !resp or resp.empty? or is_not_dir
      puts "ERROR: #{error_msg}!"
      print "\nTry again, #{msg}"
      resp = nil
    end
  end

  return resp
end

#partial_file_md5(file) ⇒ Object



94
95
96
97
98
# File 'lib/photostat/utils/os.rb', line 94

def partial_file_md5(file)
  digest = Digest::MD5.new
  digest << IO.read(file, 61440)
  digest.hexdigest
end

#string_md5(string) ⇒ Object



100
101
102
# File 'lib/photostat/utils/os.rb', line 100

def string_md5(string)
  Digest::MD5.hexdigest(string)
end