linux_stat 0.3.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  $-v = true
3
- %w(bundler/setup linux_stat irb).each(&method(:require))
3
+ %w(linux_stat irb).each(&method(:require))
4
4
  IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,18 +1,43 @@
1
1
  #!/usr/bin/env ruby
2
+ $-v = true
3
+
2
4
  begin
3
5
  require 'linux_stat'
4
6
  rescue LoadError
5
- require 'bundler/setup'
6
- require 'linux_stat'
7
+ abort "The Gem needs to be installed before this test can be run!"
7
8
  end
8
9
 
9
- $-v = true
10
+ # Check which conflicting argument (e.g., `-md -html` together) is passed last
11
+ # Always use the last argument
12
+ conflicting, hash = [
13
+ "markdown|html", /^\-(\-markdown|md)$/, /^\-(\-html|html)$/
14
+ ].each_slice(3).to_a, {}
15
+
16
+ conflicting.each do |x, y, z|
17
+ o1, o2 = *x.split(?|.freeze).map(&:to_sym)
18
+ m1, m2 = ARGV.any? { |_x| _x[y] }, ARGV.any? { |_x| _x[z] }
19
+
20
+ if m1 && m2
21
+ rev = ARGV.reverse
22
+
23
+ if rev.index { |_x| _x[y] } < rev.index { |_x| _x[z] }
24
+ hash.merge!(o1 => true)
25
+ else
26
+ hash.merge!(o2 => true)
27
+ end
28
+ elsif m1
29
+ hash.merge!(o1 => true)
30
+ elsif m2
31
+ hash.merge!(o2 => true)
32
+ end
33
+ end
34
+
35
+ MARKDOWN, HTML = hash[:markdown], hash[:html]
10
36
 
11
37
  # Print time each method takes unless --no-time or -nt option is passed
12
- MARKDOWN = ARGV.any? { |x| x[/^\-\-markdown$/] || x[/^\-md$/] }
13
- PRINT_TIME = MARKDOWN ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
38
+ PRINT_TIME = (MARKDOWN || HTML) ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
14
39
 
15
- %w(--markdown -md --no-time -nt).each(&ARGV.method(:delete))
40
+ %w(--markdown -md --no-time -nt --html -html).each(&ARGV.method(:delete))
16
41
 
17
42
  # Run only desired classes / modules
18
43
  constants = LinuxStat.constants
@@ -29,10 +54,13 @@ execute.sort.each do |c|
29
54
  next if e.class != Module && e.class != Class
30
55
 
31
56
  meths = e.methods(false).sort
57
+ next if meths.any? { |a| e.method(a).arity > 0 }
32
58
 
33
59
  if meths.length > 0
34
60
  if MARKDOWN
35
61
  puts "### LinuxStat::#{c}\n```"
62
+ elsif HTML
63
+ puts "<h3>LinuxStat::#{c}</h3>\n<pre>"
36
64
  else
37
65
  puts "\e[1;4;38;2;255;240;0mLinuxStat::#{c}\e[0m"
38
66
  end
@@ -49,6 +77,8 @@ execute.sort.each do |c|
49
77
 
50
78
  if MARKDOWN
51
79
  puts "#{e}.#{meth}\n=> #{dis}"
80
+ elsif HTML
81
+ puts "#{e}.#{meth}\n=> #{dis}"
52
82
  else
53
83
  puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m\n=> #{dis}"
54
84
  end
@@ -66,5 +96,11 @@ execute.sort.each do |c|
66
96
  puts
67
97
  end
68
98
 
69
- puts "```\n\n" if MARKDOWN && meths.length > 0
99
+ if meths.length > 0
100
+ if MARKDOWN
101
+ puts "```\n\n"
102
+ elsif HTML
103
+ puts "</pre>"
104
+ end
105
+ end
70
106
  end
@@ -1,5 +1,7 @@
1
1
  #include <sys/statvfs.h>
2
2
  #include "ruby.h"
3
+ #pragma GCC optimize ("O3")
4
+ #pragma clang optimize on
3
5
 
4
6
  static VALUE statfs(VALUE obj, VALUE dir) {
5
7
  struct statvfs buf ;
@@ -8,21 +10,22 @@ static VALUE statfs(VALUE obj, VALUE dir) {
8
10
 
9
11
  if(statvfs(d, &buf) < 0) return hash ;
10
12
 
11
- rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2NUM(buf.f_bsize)) ;
12
- rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2NUM(buf.f_frsize)) ;
13
- rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2NUM(buf.f_blocks)) ;
14
- rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2NUM(buf.f_bfree)) ;
15
- rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2NUM(buf.f_bavail)) ;
16
- rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2NUM(buf.f_files)) ;
17
- rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2NUM(buf.f_ffree)) ;
18
- rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2NUM(buf.f_fsid)) ;
19
- rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2NUM(buf.f_flag)) ;
20
- rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2NUM(buf.f_namemax)) ;
13
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX(buf.f_bsize)) ;
14
+ rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2FIX(buf.f_frsize)) ;
15
+ rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2FIX(buf.f_blocks)) ;
16
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2FIX(buf.f_bfree)) ;
17
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2FIX(buf.f_bavail)) ;
18
+ rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2FIX(buf.f_files)) ;
19
+ rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2FIX(buf.f_ffree)) ;
20
+ rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2FIX(buf.f_fsid)) ;
21
+ rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2FIX(buf.f_flag)) ;
22
+ rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2FIX(buf.f_namemax)) ;
21
23
 
22
24
  return hash ;
23
25
  }
24
26
 
25
27
  void Init_fs_stat() {
26
- VALUE fs = rb_define_module("FS") ;
28
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
29
+ VALUE fs = rb_define_module_under(_linux_stat, "FS") ;
27
30
  rb_define_module_function(fs, "stat", statfs, 1) ;
28
31
  }
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ unless (have_header('sys/unistd.h') && have_header('ruby.h'))
4
+ abort('Missing header')
5
+ end
6
+
7
+ create_makefile 'linux_stat/sysconf'
@@ -0,0 +1,77 @@
1
+ #include <unistd.h>
2
+ #include "ruby.h"
3
+
4
+ static VALUE getTick(VALUE obj) {
5
+ return INT2FIX(sysconf(_SC_CLK_TCK)) ;
6
+ }
7
+
8
+ static VALUE getChildMax(VALUE obj) {
9
+ return INT2FIX(sysconf(_SC_CHILD_MAX)) ;
10
+ }
11
+
12
+ static VALUE getHostnameMax(VALUE obj) {
13
+ return INT2FIX(sysconf(_SC_HOST_NAME_MAX)) ;
14
+ }
15
+
16
+ static VALUE getLoginNameMax(VALUE obj) {
17
+ return INT2FIX(sysconf(_SC_LOGIN_NAME_MAX)) ;
18
+ }
19
+
20
+ static VALUE getOpenMax(VALUE obj) {
21
+ return INT2FIX(sysconf(_SC_OPEN_MAX)) ;
22
+ }
23
+
24
+ static VALUE getPageSizeMax(VALUE obj) {
25
+ return INT2FIX(sysconf(_SC_PAGESIZE)) ;
26
+ }
27
+
28
+ static VALUE getStreamMax(VALUE obj) {
29
+ return INT2FIX(sysconf(_SC_STREAM_MAX)) ;
30
+ }
31
+
32
+ static VALUE getTTYNameMax(VALUE obj) {
33
+ return INT2FIX(sysconf(_SC_TTY_NAME_MAX)) ;
34
+ }
35
+
36
+ static VALUE getPosixVersion(VALUE obj) {
37
+ return INT2FIX(sysconf(_SC_VERSION)) ;
38
+ }
39
+
40
+ static VALUE getUser(VALUE obj) {
41
+ char *name = getlogin() ;
42
+ return name ? rb_str_new_cstr(name) : rb_str_new_cstr("") ;
43
+ }
44
+
45
+ static VALUE getUID(VALUE obj) {
46
+ return INT2FIX(getuid()) ;
47
+ }
48
+
49
+ static VALUE getGID(VALUE obj) {
50
+ return INT2FIX(getgid()) ;
51
+ }
52
+
53
+ static VALUE getEUID(VALUE obj) {
54
+ return INT2FIX(geteuid()) ;
55
+ }
56
+
57
+ void Init_sysconf() {
58
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
59
+ VALUE _sysconf = rb_define_module_under(_linux_stat, "Sysconf") ;
60
+
61
+ rb_define_module_function(_sysconf, "sc_clk_tck", getTick, 0) ;
62
+ rb_define_module_function(_sysconf, "child_max", getChildMax, 0) ;
63
+ rb_define_module_function(_sysconf, "hostname_max", getHostnameMax, 0) ;
64
+ rb_define_module_function(_sysconf, "login_name_max", getLoginNameMax, 0) ;
65
+ rb_define_module_function(_sysconf, "open_max", getOpenMax, 0) ;
66
+ rb_define_module_function(_sysconf, "page_size_max", getPageSizeMax, 0) ;
67
+ rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
68
+ rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
69
+ rb_define_module_function(_sysconf, "posix_version", getPosixVersion, 0) ;
70
+
71
+ rb_define_module_function(_sysconf, "get_uid", getUID, 0) ;
72
+ rb_define_module_function(_sysconf, "get_gid", getGID, 0) ;
73
+ rb_define_module_function(_sysconf, "get_euid", getEUID, 0) ;
74
+
75
+ rb_define_module_function(_sysconf, "get_user", getUser, 0) ;
76
+ rb_define_module_function(_sysconf, "get_login", getUser, 0) ;
77
+ }
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ unless (have_header('sys/utsname.h') && have_header('ruby.h'))
4
+ abort('Missing header')
5
+ end
6
+
7
+ create_makefile 'linux_stat/utsname'
@@ -0,0 +1,49 @@
1
+ #include <sys/utsname.h>
2
+ #include "ruby.h"
3
+ #pragma GCC optimize ("O3")
4
+ #pragma clang optimize on
5
+
6
+ static struct utsname buf ;
7
+ static short status ;
8
+
9
+ void init_buf() {
10
+ status = uname(&buf) ;
11
+ }
12
+
13
+ static VALUE getSysname(VALUE obj) {
14
+ VALUE sysname = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.sysname) ;
15
+ return sysname ;
16
+ }
17
+
18
+ static VALUE getNodename(VALUE obj) {
19
+ VALUE nodename = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.nodename) ;
20
+ return nodename ;
21
+ }
22
+
23
+ static VALUE getRelease(VALUE obj) {
24
+ VALUE release = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.release) ;
25
+ return release ;
26
+ }
27
+
28
+ static VALUE getVersion(VALUE obj) {
29
+ VALUE version = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.version) ;
30
+ return version ;
31
+ }
32
+
33
+ static VALUE getMachine(VALUE obj) {
34
+ VALUE machine = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.machine) ;
35
+ return machine ;
36
+ }
37
+
38
+ void Init_utsname() {
39
+ init_buf() ;
40
+
41
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
42
+ VALUE _uname = rb_define_module_under(_linux_stat, "Uname") ;
43
+
44
+ rb_define_module_function(_uname, "sysname", getSysname, 0) ;
45
+ rb_define_module_function(_uname, "nodename", getNodename, 0) ;
46
+ rb_define_module_function(_uname, "release", getRelease, 0) ;
47
+ rb_define_module_function(_uname, "version", getVersion, 0) ;
48
+ rb_define_module_function(_uname, "machine", getMachine, 0) ;
49
+ }
@@ -2,12 +2,20 @@ require "linux_stat/version"
2
2
  require "linux_stat/battery"
3
3
  require "linux_stat/bios"
4
4
  require "linux_stat/cpu"
5
- require "linux_stat/kernel"
6
5
  require "linux_stat/memory"
7
6
  require "linux_stat/net"
7
+
8
+ require 'linux_stat/utsname'
8
9
  require "linux_stat/os"
10
+
9
11
  require "linux_stat/process"
10
12
  require "linux_stat/swap"
11
13
  require "linux_stat/mounts"
14
+
12
15
  require "linux_stat/fs_stat"
13
16
  require "linux_stat/filesystem"
17
+
18
+ require "linux_stat/sysconf"
19
+ require "linux_stat/kernel"
20
+ require "linux_stat/process_info"
21
+ require 'linux_stat/user'
@@ -9,7 +9,7 @@ module LinuxStat
9
9
  end
10
10
 
11
11
  # Returns the details of the battery.
12
- #If the battery is not present it will return an empty Hash.
12
+ # If the battery is not present it will return an empty Hash.
13
13
  def stat
14
14
  st = status.downcase
15
15
  return {} unless present?
@@ -27,7 +27,7 @@ module LinuxStat
27
27
  end
28
28
 
29
29
  # Returns the model of the battery.
30
- #If the battery is not present or the information isn't available it will return an empty String.
30
+ # If the battery is not present or the information isn't available it will return an empty String.
31
31
  def model
32
32
  return ''.freeze unless model_readable?
33
33
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
@@ -3,9 +3,10 @@ module LinuxStat
3
3
  class << self
4
4
  # Returns the model of the BIOS.
5
5
  # If the information is not available it will return a frozen empty string.
6
- # The output is also cached ; as changing the value in runtime is unexpected.
6
+ #
7
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
8
  def model
8
- # Cached ; as changing the value in runtime is unexpected
9
+ # cached (memoized) ; as changing the value in runtime is unexpected
9
10
  @@model ||= if File.readable?('/sys/devices/virtual/dmi/id/product_name')
10
11
  IO.read('/sys/devices/virtual/dmi/id/product_name').tap(&:strip!)
11
12
  elsif File.readable?('/sys/firmware/devicetree/base/model')
@@ -17,9 +18,10 @@ module LinuxStat
17
18
 
18
19
  # Returns the vendor of the BIOS.
19
20
  # If the information is not available it will return a frozen empty string.
20
- # The output is also cached ; as changing the value in runtime is unexpected.
21
+ #
22
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
21
23
  def vendor
22
- # Cached ; as changing the value in runtime is unexpected
24
+ # cached (memoized) ; as changing the value in runtime is unexpected
23
25
  @@vendor ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_vendor')
24
26
  IO.read('/sys/devices/virtual/dmi/id/bios_vendor').tap(&:strip!)
25
27
  else
@@ -29,7 +31,8 @@ module LinuxStat
29
31
 
30
32
  # Returns the version of the BIOS.
31
33
  # If the information is not available it will return a frozen empty string.
32
- # The output is also cached ; as changing the value in runtime is unexpected.
34
+ #
35
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
33
36
  def version
34
37
  @@version ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_version')
35
38
  IO.read('/sys/devices/virtual/dmi/id/bios_version').tap(&:strip!)
@@ -40,7 +43,8 @@ module LinuxStat
40
43
 
41
44
  # Returns the date of the BIOS.
42
45
  # If the information is not available it will return a frozen empty string.
43
- # The output is also cached ; as changing the value in runtime is unexpected.
46
+ #
47
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
44
48
  def date
45
49
  @@date ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_date')
46
50
  IO.read('/sys/devices/virtual/dmi/id/bios_date').tap(&:strip!)
@@ -19,7 +19,14 @@ module LinuxStat
19
19
  sleep(sleep)
20
20
  data2 = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
21
21
 
22
- data.size.times.reduce({}) do |h, x|
22
+ # On devices like android, the core count can change anytime.
23
+ # I had crashes on Termux.
24
+ # So better just count the min number of CPU and iterate over that
25
+ # If data.length is smaller than data2.length, we don't have enough data to compare.
26
+ dl, d2l = data.length, data2.length
27
+ min = dl > d2l ? d2l : dl
28
+
29
+ min.times.reduce({}) do |h, x|
23
30
  user, nice, sys, idle, iowait, irq, softirq, steal = *data[x].drop(1)
24
31
  user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x].drop(1)
25
32
 
@@ -63,7 +70,8 @@ module LinuxStat
63
70
 
64
71
  # Returns the model of processor.
65
72
  # If the information isn't available, it will return en empty string.
66
- # The output is also cached ; as changing the value in runtime is unexpected.
73
+ #
74
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
67
75
  def model
68
76
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
69
77
  end
@@ -1,9 +1,5 @@
1
- # require 'linux_stat/fs_stat'
2
-
3
1
  module LinuxStat
4
2
  module Filesystem
5
- extend FS
6
-
7
3
  class << self
8
4
  # stat(fs = '/')
9
5
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
@@ -14,7 +10,7 @@ module LinuxStat
14
10
  # 3. used space (in kilobytes)
15
11
  #
16
12
  # In a hash format:
17
- # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
13
+ # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
18
14
  #
19
15
  # If the stat can't be acquired, this method will return an empty Hash.
20
16
  def stat(fs = ?/.freeze)
@@ -25,11 +21,11 @@ module LinuxStat
25
21
  {
26
22
  total: s[:block_size] * s[:blocks],
27
23
  free: s[:block_size] * s[:block_free],
28
- used: s[:blocks].-(s[:block_free]) * s[:block_size],
24
+ used: s[:blocks].-(s[:block_free]) * s[:block_size]
29
25
  }
30
26
  end
31
27
 
32
- # stat(fs = '/')
28
+ # total(fs = '/')
33
29
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
34
30
  # It returns the total size of a given disk in bytes.
35
31
  #
@@ -41,7 +37,7 @@ module LinuxStat
41
37
  s[:block_size] * s[:blocks]
42
38
  end
43
39
 
44
- # stat(fs = '/')
40
+ # free(fs = '/')
45
41
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
46
42
  # It returns the total free space in a disk in bytes.
47
43
  # It is to be noted that free is not same as available.
@@ -55,7 +51,7 @@ module LinuxStat
55
51
  s[:block_size] * s[:block_free]
56
52
  end
57
53
 
58
- # stat(fs = '/')
54
+ # used(fs = '/')
59
55
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
60
56
  # It returns the used space of a given disk in bytes.
61
57
  #
@@ -67,7 +63,7 @@ module LinuxStat
67
63
  s[:blocks].-(s[:block_free]) * s[:block_size]
68
64
  end
69
65
 
70
- # stat(fs = '/')
66
+ # available(fs = '/')
71
67
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
72
68
  # It returns the total free space in a disk in bytes.
73
69
  # It is to be noted that free is not same as available.
@@ -81,15 +77,15 @@ module LinuxStat
81
77
  s[:block_size] * s[:block_avail_unpriv]
82
78
  end
83
79
 
84
- # stat(fs = '/')
80
+ # stat_raw(fs = '/')
85
81
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
86
82
  #
87
83
  # It returns a Hash with the following data (for example):
88
- # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
84
+ # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
89
85
  #
90
86
  # If the stat can't be acquired, this method will return an empty Hash.
91
- def stat_raw(fs = '/'.freeze)
92
- FS.stat(fs)
87
+ def stat_raw(fs = ?/.freeze)
88
+ LinuxStat::FS.stat(fs)
93
89
  end
94
90
  end
95
91
  end