Module: Maid::Tools

Included in:
Maid
Defined in:
lib/maid/tools.rb

Overview

Collection of utility methods included in Maid::Maid (and thus available in the rules DSL).

In general, all paths are automatically expanded (e.g. ‘~/Downloads/foo.zip’ becomes ‘/home/username/Downloads/foo.zip’).

Some methods are not available on all platforms. An ArgumentError is raised when a command is not available. See tags: [Mac OS X]

Instance Method Summary collapse

Instance Method Details

#dir(glob) ⇒ Object

Give all files matching the given glob.

Delgates to Dir.

dir('~/Downloads/*.zip')


52
53
54
# File 'lib/maid/tools.rb', line 52

def dir(glob)
  Dir[File.expand_path(glob)]
end

#disk_usage(path) ⇒ Object

Calculate disk usage of a given path.

disk_usage('foo.zip') # => 136


103
104
105
106
# File 'lib/maid/tools.rb', line 103

def disk_usage(path)
  raw = cmd("du -s #{path.inspect}")
  raw.split(/\s+/).first.to_i
end

#downloaded_from(path) ⇒ Object

Mac OS X

Use Spotlight metadata to determine the site from which a file was downloaded.

downloaded_from('foo.zip') # => ['http://www.site.com/foo.zip', 'http://www.site.com/']


79
80
81
82
83
# File 'lib/maid/tools.rb', line 79

def downloaded_from(path)
  raw = cmd("mdls -raw -name kMDItemWhereFroms #{path.inspect}")
  clean = raw[1, raw.length - 2]
  clean.split(/,\s+/).map { |s| t = s.strip; t[1, t.length - 2] }
end

#duration_s(path) ⇒ Object

Mac OS X

Use Spotlight metadata to determine audio length.

duration_s('foo.mp3') # => 235.705


88
89
90
# File 'lib/maid/tools.rb', line 88

def duration_s(path)
  cmd("mdls -raw -name kMDItemDurationSeconds #{path.inspect}").to_f
end

#find(path, &block) ⇒ Object

Find matching files, akin to the Unix utility find.

Delegates to Find.find.

find '~/Downloads/' do |path|
  # ...
end


63
64
65
# File 'lib/maid/tools.rb', line 63

def find(path, &block)
  Find.find(File.expand_path(path), &block)
end

#git_piston(path) ⇒ Object

Pulls and pushes the given git repository.

git_piston('~/code/projectname')


118
119
120
121
122
# File 'lib/maid/tools.rb', line 118

def git_piston(path)
  full_path = File.expand_path(path)
  stdout = cmd("cd #{full_path.inspect} && git pull && git push 2>&1")
  @logger.info "Fired git piston on #{full_path.inspect}.  STDOUT:\n\n#{stdout}"
end

#last_accessed(path) ⇒ Object

In Unix speak, “atime”.

last_accessed('foo.zip') # => Sat Apr 09 10:50:01 -0400 2011


111
112
113
# File 'lib/maid/tools.rb', line 111

def last_accessed(path)
  File.atime(File.expand_path(path))
end

#locate(name) ⇒ Object

Mac OS X

Use Spotlight to locate all files matching the given filename.

locate('foo.zip') # => ['/a/foo.zip', '/b/foo.zip']

– TODO use ‘locate` elsewhere – it isn’t available by default on OS X starting with OS X Leopard.



72
73
74
# File 'lib/maid/tools.rb', line 72

def locate(name)
  cmd("mdfind -name #{name.inspect}").split("\n")
end

#move(from, to) ⇒ Object

Move from from to to.

The path is not moved if a file already exists at the destination with the same name. A warning is logged instead.

This method delegates to FileUtils. The instance-level file_options hash is passed to control the :noop option.

move('~/Downloads/foo.zip', '~/Archive/Software/Mac OS X/')


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/maid/tools.rb', line 18

def move(from, to)
  from = File.expand_path(from)
  to = File.expand_path(to)
  target = File.join(to, File.basename(from))

  unless File.exist?(target)
    @logger.info "mv #{from.inspect} #{to.inspect}"
    FileUtils.mv(from, to, @file_options)
  else
    @logger.warn "skipping #{from.inspect} because #{target.inspect} already exists"
  end
end

#trash(path) ⇒ Object

Move the given path to the trash (as set by trash_path).

The path is moved if a file already exists in the trash with the same name. However, the current date and time is appended to the filename.

trash('~/Downloads/foo.zip')


36
37
38
39
40
41
42
43
44
45
# File 'lib/maid/tools.rb', line 36

def trash(path)
  target = File.join(@trash_path, File.basename(path))
  safe_trash_path = File.join(@trash_path, "#{File.basename(path)} #{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}")

  if File.exist?(target)
    move(path, safe_trash_path)
  else
    move(path, @trash_path)
  end
end

#zipfile_contents(path) ⇒ Object

Inspect the contents of a .zip file.

zipfile_contents('foo.zip') # => ['foo/foo.exe', 'foo/README.txt']


95
96
97
98
# File 'lib/maid/tools.rb', line 95

def zipfile_contents(path)
  raw = cmd("unzip -Z1 #{path.inspect}")
  raw.split("\n")
end