Module: Knj::Os
- Defined in:
- lib/knj/os.rb
Class Method Summary collapse
- .chdir_file(filepath) ⇒ Object
-
.check_display_env ⇒ Object
Checks if the display variable and xauth is set - if not sets it to the GDM xauth and defaults the display to :0.0.
- .class_exist(classstr) ⇒ Object
-
.executed_cmd ⇒ Object
Returns the command used to execute the current process.
-
.executed_executable ⇒ Object
Returns the Ruby executable that is running the current process if possible.
-
.homedir ⇒ Object
Returns the path of the home-dir as a string.
-
.os ⇒ Object
Returns the operating system a string.
- .realpath(path) ⇒ Object
-
.shellcmd(cmd) ⇒ Object
Runs a command and returns output.
-
.subproc(cmd) ⇒ Object
Runs a command as a process of its own and wont block or be depended on this process.
-
.tmpdir ⇒ Object
This method was created to make up for the fact that Dir.tmpdir sometimes returns empty strings?? ===Examples tmp_db_path = “#Os.tmpdir/temp_db.sqlite3”.
-
.toolkit ⇒ Object
Returns the current graphical toolkit running.
-
.whoami ⇒ Object
This method returns the username of the current user.
-
.xauth_file ⇒ Object
Returns the xauth file for GDM.
Class Method Details
.chdir_file(filepath) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/knj/os.rb', line 93 def self.chdir_file(filepath) if File.symlink?(filepath) Dir.chdir(File.dirname(File.readlink(filepath))) else Dir.chdir(File.dirname(filepath)) end end |
.check_display_env ⇒ Object
Checks if the display variable and xauth is set - if not sets it to the GDM xauth and defaults the display to :0.0.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/knj/os.rb', line 176 def self.check_display_env ret = {} if ENV["DISPLAY"].to_s.strip.length <= 0 x_procs = Knj::Unix_proc.list("grep" => "/usr/bin/X") set_disp = nil x_procs.each do |x_proc| if match = x_proc["cmd"].match(/(:\d+)/) set_disp = match[1] break end end raise "Could not figure out display." if !set_disp ENV["DISPLAY"] = set_disp ret["display"] = set_disp else ret["display"] = ENV["DISPLAY"] end if !ENV["XAUTHORITY"] res = Knj::Os.xauth_file ENV["XAUTHORITY"] = res ret["xauth"] = res else ret["xauth"] = ENV["XAUTHORITY"] end return ret end |
.class_exist(classstr) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/knj/os.rb', line 85 def self.class_exist(classstr) if Module.constants.index(classstr) != nil return true end return false end |
.executed_cmd ⇒ Object
Returns the command used to execute the current process.
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/knj/os.rb', line 210 def self.executed_cmd return ENV["SUDO_COMMAND"] if ENV["SUDO_COMMAND"] proc_self = Knj::Unix_proc.find_self cmd = proc_self["cmd"] cmd.gsub!(/^ruby([\d\.]+)/, ENV["_"]) if ENV["_"] return cmd end |
.executed_executable ⇒ Object
Returns the Ruby executable that is running the current process if possible.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/knj/os.rb', line 222 def self.executed_executable return ENV["rvm_ruby_string"] if !ENV["rvm_ruby_string"].to_s.empty? if ENV["MY_RUBY_HOME"] ruby_bin_path = "#{ENV["MY_RUBY_HOME"]}/bin/ruby" return ruby_bin_path if File.exists?(ruby_bin_path) end #Try to look the executeable up by command. if self.os == "linux" unix_proc = Knj::Unix_proc.find_self if unix_proc if match_cmd = unix_proc["cmd"].match(/^(\/usr\/bin\/|)((j|iron|)ruby([\d\.-]*))(\s+|$)/) return "#{match_cmd[1]}#{match_cmd[2]}" else raise "Could not match the executed command from the process." end else raise "Could not find the self-process." end end # Code for returning RBEnv Ruby path if running through RBEnv. if ENV["RBENV_VERSION"] && ENV["RBENV_ROOT"] rbenv_path = "#{ENV["RBENV_ROOT"]}/versions/#{ENV["RBENV_VERSION"]}/bin/ruby" return rbenv_path if File.exists?(rbenv_path) end raise "Could not figure out the executed executable from the environment: '#{ENV.to_hash}'." end |
.homedir ⇒ Object
Returns the path of the home-dir as a string.
Examples
print “Looks like the current user uses Mozilla software?” if File.exists?(“#homedir/.mozilla”)
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/knj/os.rb', line 5 def self.homedir if ENV["USERPROFILE"] homedir = ENV["USERPROFILE"] else homedir = File.("~") end if homedir.length <= 0 raise "Could not figure out the homedir." end return homedir end |
.os ⇒ Object
Returns the operating system a string.
Examples
print “Can I please move to another machine?” if Knj::Os.os == “windows” print “I like it better now.” if Knj::Os.os == “linux”
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/knj/os.rb', line 55 def self.os if ENV["OS"] teststring = ENV["OS"].to_s.downcase elsif RUBY_PLATFORM teststring = RUBY_PLATFORM.to_s.downcase end if teststring.include?("windows") return "windows" elsif teststring.include?("linux") return "linux" elsif teststring.include?("darwin") else raise "Could not figure out OS: '#{teststring}'." end end |
.realpath(path) ⇒ Object
101 102 103 104 |
# File 'lib/knj/os.rb', line 101 def self.realpath(path) return self.realpath(File.readlink(path)) if File.symlink?(path) return path end |
.shellcmd(cmd) ⇒ Object
Runs a command and returns output. Also throws an exception of something is outputted to stderr.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/knj/os.rb', line 107 def self.shellcmd(cmd) res = { :out => "", :err => "" } if RUBY_ENGINE == "jruby" begin IO.popen4(cmd) do |pid, stdin, stdout, stderr| res[:out] << stdout.read res[:err] << stderr.read end rescue Errno::EBADF => e #Catch and rescue retarted JRuby. if e. == "Bad file descriptor - Bad file descriptor" retry else raise e end end else require "open3" Open3.popen3(cmd) do |stdin, stdout, stderr| res[:out] << stdout.read res[:err] << stderr.read end end if res[:err].to_s.strip.length > 0 raise res[:err] end return res[:out] end |
.subproc(cmd) ⇒ Object
Runs a command as a process of its own and wont block or be depended on this process.
143 144 145 |
# File 'lib/knj/os.rb', line 143 def self.subproc(cmd) %x[#{cmd} >> /dev/null 2>&1 &] end |
.tmpdir ⇒ Object
This method was created to make up for the fact that Dir.tmpdir sometimes returns empty strings??
Examples
tmp_db_path = “#tmpdir/temp_db.sqlite3”
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/knj/os.rb', line 22 def self.tmpdir require "tmpdir" tmpdir = Dir.tmpdir.to_s.strip return tmpdir if tmpdir.length >= 3 and File.exists?(tmpdir) return ENV["TEMP"] if ENV["TEMP"].to_s.strip.length > 0 and File.exists?(ENV["TMP"]) return ENV["TMP"] if ENV["TMP"].to_s.strip.length > 0 and File.exists?(ENV["TMP"]) return "/tmp" if File.exists?("/tmp") raise "Could not figure out temp-dir." end |
.toolkit ⇒ Object
Returns the current graphical toolkit running.
Examples
Knj::Os.toolkit #=> ‘kde’
75 76 77 78 79 80 81 82 83 |
# File 'lib/knj/os.rb', line 75 def self.toolkit if self.os == "linux" if ENV["DESKTOP_SESSION"].index("plasma") != nil return "kde" end end raise "Could not figure out the toolkit." end |
.whoami ⇒ Object
This method returns the username of the current user.
Examples
print “I can do what I want, I am root!” if Knj::Os.whoami == “root”
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/knj/os.rb', line 37 def self.whoami if ENV["USERNAME"] whoami = ENV["USERNAME"] else whoami = %x[whoami].strip end if whoami.length <= 0 raise "Could not figure out the user who is logged in." end return whoami end |
.xauth_file ⇒ Object
Returns the xauth file for GDM.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/knj/os.rb', line 148 def self.xauth_file authfile = "" if File.exists?("/var/run/gdm") Dir.foreach("/var/run/gdm") do |file| next if file == "." or file == ".." or !file.match(/^auth-for-gdm-.+$/) authfile = "/var/run/gdm/#{file}/database" end end if File.exists?("/var/run/lightdm") Dir.foreach("/var/run/lightdm") do |file| next if file == "." or file == ".." Dir.foreach("/var/run/lightdm/#{file}") do |f2| authfile = "/var/run/lightdm/#{file}/#{f2}" if f2.match(/^:(\d+)$/) end end end if authfile.to_s.length <= 0 raise "Could not figure out authfile for GDM." end return authfile end |