stat_file 1.0.18

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.

Potentially problematic release.


This version of stat_file might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05d456bb5611af8a04ee050f8f37bba5dd2b81c7e1d7833dfc4dc9085380893b
4
+ data.tar.gz: 5207fc33de2f8fca603a0a617449630fef76f67ed1e433d80936062cc725f99f
5
+ SHA512:
6
+ metadata.gz: 3b9f16acc34dcb01794852ee9ba9d2c9f39d65fa2905332957b8603f9b8b3eefaadc8cf566351c8f10ae71b60c781983e8e87e507c3b868ea9e592932526c1d8
7
+ data.tar.gz: 8aa691805ac73572831f38c97ee4d538a343ca7f935eca5ab5962d1a23bb3d70f6402d91d1f2225308084b81e6e0a5bbf46233963c8cfae3b053d1f08e70dce6
@@ -0,0 +1,276 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === StatFile
6
+ #
7
+ # require 'stat_file/stat_file.rb'
8
+ # =========================================================================== #
9
+ # Bug:
10
+ # The access entry is not completely correct - the number is ok but
11
+ # the rxw--- format is wrong.
12
+ # =========================================================================== #
13
+ class StatFile
14
+
15
+ require 'etc'
16
+
17
+ begin
18
+ require 'colours'
19
+ include Colours
20
+ rescue LoadError; end
21
+
22
+ begin
23
+ require 'opn'
24
+ rescue LoadError; end
25
+
26
+ N = "\n" unless defined? N
27
+
28
+ # ========================================================================= #
29
+ # === SHALL_WE_DEBUG
30
+ # ========================================================================= #
31
+ SHALL_WE_DEBUG = false unless defined? SHALL_WE_DEBUG
32
+
33
+ # ========================================================================= #
34
+ # === RED_ON_BLACK
35
+ # ========================================================================= #
36
+ RED_ON_BLACK = "\e[31;48m" unless defined? RED_ON_BLACK
37
+
38
+ # ========================================================================= #
39
+ # === REVERT
40
+ # ========================================================================= #
41
+ REVERT = '\e[0m' unless defined? REVERT
42
+
43
+ # ========================================================================= #
44
+ # === initialize
45
+ # ========================================================================= #
46
+ def initialize(
47
+ this_file,
48
+ be_verbose = true,
49
+ run_already = true
50
+ )
51
+ reset # This must be called before set_be_verbose()
52
+ be_verbose = false if be_verbose == :dont_be_verbose
53
+ set_be_verbose if be_verbose
54
+ set_file(this_file)
55
+ run if run_already
56
+ end
57
+
58
+ # ========================================================================= #
59
+ # === reset (reset tag)
60
+ # ========================================================================= #
61
+ def reset
62
+ # ======================================================================= #
63
+ # === @be_verbose
64
+ # ======================================================================= #
65
+ @be_verbose = false # Default.
66
+ # ======================================================================= #
67
+ # === @debug
68
+ # ======================================================================= #
69
+ @debug = SHALL_WE_DEBUG
70
+ # ======================================================================= #
71
+ # === @padding_left
72
+ # ======================================================================= #
73
+ @padding_left = ' '
74
+ end
75
+
76
+ # ========================================================================= #
77
+ # === file?
78
+ # ========================================================================= #
79
+ def file?
80
+ @file
81
+ end
82
+
83
+ # ========================================================================= #
84
+ # === set_file
85
+ # ========================================================================= #
86
+ def set_file(i, get_rid_of_slashdot = false)
87
+ if get_rid_of_slashdot
88
+ i.delete!('/') if i.include? '/' # Since 06.12.2011 we try to get rid of possible '/' in the string.
89
+ end
90
+ i = i.first if i.is_a? Array
91
+ i = i.to_s.dup
92
+ @file = i
93
+ end
94
+
95
+ # ========================================================================= #
96
+ # === set_be_verbose
97
+ # ========================================================================= #
98
+ def set_be_verbose
99
+ @be_verbose = true
100
+ end
101
+
102
+ # ========================================================================= #
103
+ # === stat_file
104
+ # ========================================================================= #
105
+ def stat_file(f = @file)
106
+ if f.to_s.empty?
107
+ opn; e 'No such file could be found. Perhaps you '
108
+ opn; e 'forgot to pass in a valid filename?'
109
+ end
110
+ if f
111
+ if f.is_a? Array
112
+ f.each { |this_file| stat_file(this_file) }
113
+ else
114
+ @is_symlink = File.symlink?(f) ? true : false
115
+ if @debug
116
+ e "Debugging in stat_file.rb: Is `#{f}` a symlink or not? #{@is_symlink}"
117
+ end
118
+ if is_on_roebe? and !File.exist?(f)
119
+ begin
120
+ require 'beautiful_url'
121
+ f = BeautifulUrl[f]
122
+ f = f.first if f.is_a? Array
123
+ rescue LoadError; end
124
+ end
125
+ string = ' File: `'+f+"`"
126
+ # =================================================================== #
127
+ # If it is a symlink we have to check whether the target exists
128
+ # or not.
129
+ # =================================================================== #
130
+ if @is_symlink
131
+ f = File.readlink(f)
132
+ string << ' → `'+f+"`"
133
+ end
134
+ # =================================================================== #
135
+ # Now we can retrieve the information.
136
+ # =================================================================== #
137
+ if File.exist?(f)
138
+ _ = File.stat(f)
139
+ file_type = File.ftype(f)
140
+ case file_type
141
+ when 'directory'
142
+ target_type = 'directory'
143
+ when 'file'
144
+ target_type = 'regular file'
145
+ when 'symlink','link'
146
+ target_type = 'symlink'
147
+ end
148
+ # ================================================================= #
149
+ # Prepend a newline to it. I think it looks better that way.
150
+ # ================================================================= #
151
+ if are_the_colours_available?
152
+ use_this_regex = /`(.+)`/ # See: https://rubular.com/r/EnETP5vkCYgGEB
153
+ string.prepend(::Colours.rev)
154
+ string.sub!(
155
+ use_this_regex,
156
+ ::Colours.steelblue('\1')
157
+ )
158
+ end
159
+ @result = N+string+N # reset again here.
160
+ @result << @padding_left+' Size: '+
161
+ (@size = _.size.to_s).to_s+' Blocks: '+_.blocks.to_s+' '+target_type.to_s+N
162
+ @result << @padding_left+
163
+ return_device_string+' Inode: '+_.ino.to_s+N
164
+ begin
165
+ gid = File.stat(f).gid.to_s
166
+ gid_name = Etc.getgrgid(File.stat(f).gid).name # i.e. "root"
167
+ uid = File.stat(f).uid.to_s
168
+ uid_name = Etc.getpwuid(File.stat(f).uid).name # i.e. "root"
169
+ # =============================================================== #
170
+ # Next we will add the access entry. The Access entry may
171
+ # have a leading 0 or 1.
172
+ # =============================================================== #
173
+ access_mode = File.stat(f).mode.to_s(8)[-4,4] # Get the last 4 chars only.
174
+ access_entry = 'Access: ('+access_mode+'/-rwxr-xr-x) Uid: ( '+uid+'/ '+uid_name+') Gid: ( '+gid+'/ '+gid_name+')'
175
+ @result << @padding_left+access_entry+N
176
+ rescue Exception => error; pp error; end
177
+ @result << "#{@padding_left}Created at: #{File.ctime(f)}#{N}"
178
+ @result << "#{@padding_left}Last accessed: #{File.atime(f)}#{N}"
179
+ @result << "#{@padding_left}Last modified: #{File.mtime(f)}#{N}"
180
+ @result << N
181
+ e @result if @be_verbose
182
+ else # Else the file does not exit.
183
+ if be_verbose?
184
+ opn; ewarn "No target file exists at: `#{sfile(f)}"
185
+ # File.readlink(@file)
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ # ========================================================================= #
193
+ # === result?
194
+ # ========================================================================= #
195
+ def result?
196
+ @result
197
+ end; alias result result? # === result
198
+ alias text? result? # === text?
199
+
200
+ # ========================================================================= #
201
+ # === be_verbose?
202
+ # ========================================================================= #
203
+ def be_verbose?
204
+ @be_verbose
205
+ end
206
+
207
+ # ========================================================================= #
208
+ # === size?
209
+ # ========================================================================= #
210
+ def size?
211
+ @size
212
+ end; alias size size? # === size()
213
+
214
+ # ========================================================================= #
215
+ # === is_on_roebe?
216
+ # ========================================================================= #
217
+ def is_on_roebe?
218
+ ENV['IS_ROEBE'].to_s == '1'
219
+ end
220
+
221
+ # ========================================================================= #
222
+ # === are_the_colours_available?
223
+ # ========================================================================= #
224
+ def are_the_colours_available?
225
+ Object.const_defined? :Colours
226
+ end
227
+
228
+ # ========================================================================= #
229
+ # === return_device_string
230
+ #
231
+ # This method is assumed to return an entry such as the following one:
232
+ #
233
+ # Device: 801h/2049d
234
+ #
235
+ # Note that this may differ from systemd to non-systemd systems.
236
+ # ========================================================================= #
237
+ def return_device_string
238
+ result = 'Device: '.dup
239
+ # ======================================================================= #
240
+ # First, find out the current device:
241
+ # ======================================================================= #
242
+ base_dir = File.dirname(file?)
243
+ cmd = `df #{base_dir} 2>&1` # The 2>&1 is necessary for windows.
244
+ device_number = cmd.split(N)[1].split(' ')[0]
245
+ # ======================================================================= #
246
+ # device_number now has something like /dev/sda1
247
+ # In order to obtain the major and minor version,
248
+ # we must do this:
249
+ # ======================================================================= #
250
+ cmd = "ls -al #{device_number} 2>&1"
251
+ major_minor = `#{cmd}` # To get major/minor = 8/1
252
+ # ======================================================================= #
253
+ # This may however had fail. We assume that it will fail when it
254
+ # includes the string "by-uuid".
255
+ # ======================================================================= #
256
+ applied_match = major_minor.match(/disk (\d, \d)/)
257
+ if applied_match # Protect against nil values.
258
+ major, minor = applied_match[1].split(',').map(&:strip)
259
+ major_and_minor = "#{major}0#{minor}"
260
+ result << major_and_minor+'h/'+major_and_minor.to_i(16).to_s+'d '
261
+ end
262
+ result
263
+ end
264
+
265
+ # ========================================================================= #
266
+ # === run
267
+ # ========================================================================= #
268
+ def run
269
+ stat_file
270
+ end
271
+
272
+ end
273
+
274
+ if __FILE__ == $PROGRAM_NAME
275
+ StatFile.new(ARGV.first, true)
276
+ end # sfile $SYSBIN/ruby
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class StatFile
6
+
7
+ # ========================================================================= #
8
+ # === VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.18'
11
+
12
+ # ========================================================================= #
13
+ # === LAST_UPDATE
14
+ # ========================================================================= #
15
+ LAST_UPDATE = '18.09.2022'
16
+
17
+ end
data/lib/stat_file.rb ADDED
@@ -0,0 +1 @@
1
+ require 'stat_file/stat_file.rb'
data/stat_file.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project StatFile.
3
+ # =========================================================================== #
4
+ require 'stat_file/version/version.rb'
5
+ require 'roebe'
6
+
7
+ Gem::Specification.new { |s|
8
+
9
+ s.name = 'stat_file'
10
+ s.version = StatFile::VERSION
11
+ s.date = Time.now.strftime('%Y-%m-%d')
12
+
13
+ s.summary = <<-EOF
14
+
15
+ This library is called stat_file. It allows you to
16
+ do basic stat functionality, like the Unix Tool.
17
+
18
+ EOF
19
+
20
+ s.description = <<-EOF
21
+
22
+ This library is called stat_file. It allows you to
23
+ do basic stat functionality, like the Unix Tool.
24
+
25
+ EOF
26
+
27
+ s.authors = ['Robert A. Heiler']
28
+ s.email = Roebe.email?
29
+ s.files = Dir['**/*']
30
+ s.license = 'MIT'
31
+ s.homepage = 'https://rubygems.org/gems/stat_file'
32
+
33
+ s.required_ruby_version = '>= '+RUBY_VERSION
34
+ s.required_rubygems_version = '>= '+Gem::VERSION
35
+ s.rubygems_version = '>= '+Gem::VERSION
36
+
37
+ s.add_dependency 'opn'
38
+ s.add_dependency 'colours'
39
+
40
+ }
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stat_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.18
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opn
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colours
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2+
42
+
43
+ This library is called stat_file. It allows you to
44
+ do basic stat functionality, like the Unix Tool.
45
+
46
+ email: shevy@inbox.lt
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - lib/stat_file.rb
52
+ - lib/stat_file/stat_file.rb
53
+ - lib/stat_file/version/version.rb
54
+ - stat_file.gemspec
55
+ homepage: https://rubygems.org/gems/stat_file
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.1.2
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.3.22
73
+ requirements: []
74
+ rubygems_version: 3.3.22
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: This library is called stat_file. It allows you to do basic stat functionality,
78
+ like the Unix Tool.
79
+ test_files: []
80
+ ...