Module: SystemHelper

Includes:
BashHelper
Included in:
MultimediaHelper
Defined in:
lib/sapis/system_helper.rb

Constant Summary collapse

SEARCH_FILES =
'f'
SEARCH_DIRECTORIES =
'd'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BashHelper

#encode_bash_filenames, encode_bash_filenames, safe_execute, #safe_execute, #simple_bash_execute, simple_bash_execute

Class Method Details

.current_timezoneObject



35
36
37
38
39
40
41
# File 'lib/sapis/system_helper.rb', line 35

def self.current_timezone
  if mac?
    `systemsetup -gettimezone`.strip.sub('Time Zone: ', '')
  else
    IO.read('/etc/timezone').chomp
  end
end

.find_files(raw_pattern, raw_search_paths, options = {}) ⇒ Object

Case insensitive search

options:

:file_type:  [nil] either SEARCH_FILES or SEARCH_DIRECTORIES, or nil for both.
:skip_paths: [nil] array of (full) paths to skip


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sapis/system_helper.rb', line 115

def self.find_files(raw_pattern, raw_search_paths, options={})
  search_paths = raw_search_paths.map { |path| path.shellescape }.join(' ')
  pattern      = raw_pattern.shellescape

  case options[:file_type]
  when SEARCH_FILES
    file_type = '-type f'
  when SEARCH_DIRECTORIES
    file_type = '-type d'
  when nil
    # nothing
  else
    raise "Unrecognized :file_type option for find_files: #{options[:file_type]}"
  end

  skip_paths = options[:skip_paths].to_a.map do |path|
    path_with_pattern = File.join(path, '*')
    " -not -path " + path_with_pattern.shellescape
  end.join(' ')

  raw_result = `find #{search_paths} -iname #{pattern} #{file_type} #{skip_paths}`.chomp

  raw_result.split("\n")
end

.mac?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sapis/system_helper.rb', line 31

def self.mac?
  `uname`.strip == 'Darwin'
end

.open_file(filename) ⇒ Object

Opens :filename using the default executable.



104
105
106
107
# File 'lib/sapis/system_helper.rb', line 104

def self.open_file(filename)
  opener = mac? ? 'open' : 'xdg-open'
  `#{opener} #{filename.shellescape}`
end


43
44
45
46
47
48
49
50
51
# File 'lib/sapis/system_helper.rb', line 43

def self.symlink(source, destination)
  # Doesn't work! At least one bug in the library:
  # - File.exists? returns false if a symlink exists, but points to a non-existent file
  # - symlink causes an abrupt exit if the destination exists
  #
#    File.delete( wine_drive_link ) if File.exists?( wine_drive_link )
#    File.symlink( entry, wine_drive_link )
  BashHelper.simple_bash_execute "ln -sf", source, destination
end

Instance Method Details

#system_cores_numberObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sapis/system_helper.rb', line 73

def system_cores_number
  if RUBY_PLATFORM =~ /darwin/i
    raw_result = safe_execute "system_profiler SPHardwareDataType | grep 'Total Number Of Cores'"
    raw_result[/: (\d+)/, 1].to_i
  else
    # See https://www.ibm.com/developerworks/community/blogs/brian/entry/linux_show_the_number_of_cpu_cores_on_your_system17?lang=en
    # Bash form:
    #
    #   cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l
    #
    IO.readlines('/proc/cpuinfo').grep(/core id|physical id/).each_slice(2).to_a.uniq.size
  end
end

#unmount_base_mountpoint(filename) ⇒ Object

REFACTOR: decide not/self for all the following

‘mountpoint’ has inconsistent exit behavior, because if the path is not a mountpoint, it returns 1, but prints the message to stdout.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sapis/system_helper.rb', line 58

def unmount_base_mountpoint(filename)
  while filename != '/'
    is_mountpoint = `mountpoint #{encode_bash_filenames(filename)}` =~ /is a mountpoint\n$/

    if is_mountpoint
      simple_bash_execute "umount", filename
      return
    end

    filename = File.dirname(filename)
  end

  raise "Couldn't find base mount point for file: #{filename}"
end

#unrar(file, options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sapis/system_helper.rb', line 87

def unrar(file, options={})
  delete = !!options[:delete]

  original_dir    = Dir.pwd
  destination_dir = File.dirname(file)

  Dir.chdir(destination_dir)

  simple_bash_execute "unrar x", file

  File.delete(file) if delete
ensure
  Dir.chdir(original_dir)
end