storazzo 0.0.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,251 @@
1
+
2
+
3
+ module Storazzo
4
+ class Storazzo::RicDiskUgly
5
+ RICDISK_VERSION = "1.0ugly"
6
+ DEFAULT_MEDIA_DIRS = %w{
7
+ /media/riccardo/
8
+ /Volumes/
9
+ /mnt/
10
+ ~/storazzo/var/test/disks/
11
+ }.append(Storazzo.root + "/var/test/disks/")
12
+
13
+ include Hashify
14
+ extend Storazzo::Colors
15
+
16
+ # todo substitute with protobuf..
17
+ attr_accessor :name, :description, :ricdisk_file, :local_mountpoint, :wr
18
+
19
+ def self.interesting_mount_points(opts={})
20
+ #https://unix.stackexchange.com/questions/177014/showing-only-interesting-mount-points-filtering-non-interesting-types
21
+ `mount | grep -Ev 'type (proc|sysfs|tmpfs|devpts|debugfs|rpc_pipefs|nfsd|securityfs|fusectl|devtmpfs) '`.split(/\n+/)
22
+ end
23
+
24
+ def initialize(path, ricdisk_file)
25
+ puts "[DEB] RicDiskUgly initialize.. path=#{path}"
26
+ @local_mountpoint = path
27
+ @description = "This is an automated RicDiskUgly description from v.#{VERSION}. Riccardo feel free to edit away with characteristicshs of this device.. Created on #{Time.now}'"
28
+ @ricdisk_version = VERSION
29
+ @ricdisk_file = ricdisk_file
30
+ #@questo_non_esiste = :sobenme
31
+ @label = path.split("/").last
32
+ @name = path.split("/").last
33
+ @wr = File.writable?("#{path}/#{ricdisk_file}" ) # .writeable?
34
+ @tags = 'ricdisk'
35
+ puts :beleza
36
+ @config = RicDiskConfig.instance.get_config
37
+ #puts @config if @config
38
+ find_info_from_mount(path)
39
+ find_info_from_df()
40
+ end
41
+
42
+ def ricdisk_absolute_path
43
+ @local_mountpoint + "/" + @ricdisk_file
44
+ end
45
+
46
+ def add_tag(tag)
47
+ @tags += ", #{tag}"
48
+ end
49
+
50
+ # might have other things in the future...
51
+ def find_info_from_mount(path)
52
+ mount_table_lines = interesting_mount_points()
53
+ mount_line = nil
54
+ mount_table_lines.each do |line|
55
+ next if line =~ /^map /
56
+ dev, on, mount_path, mode = line.split(/ /)
57
+ if mount_path==path
58
+ mount_line = line
59
+ else
60
+ @info_from_mount = false
61
+ end
62
+ end
63
+ @info_from_mount = ! (mount_line.nil?)
64
+ if @info_from_mount
65
+ #@mount_line = mount_line
66
+ @description += "\nMount line:\n" + mount_line
67
+ @remote_mountpoint = mount_line.split(/ /)[0]
68
+ @fstype = mount_line.split(/ /)[3].gsub(/[\(,]/, '')
69
+ add_tag(:synology) if @remote_mountpoint.match('1.0.1.10')
70
+ end
71
+ end
72
+
73
+ def find_info_from_df()
74
+ path = @local_mountpoint
75
+ df_info = `df -h "#{path}"`
76
+ @df_info = df_info
77
+ lines = df_info.split(/\n+/)
78
+ raise "I need exactly TWO lines! Or no info is served here..." unless lines.size == 2
79
+ mount, @size_readable, used_size, avail_size, @disk_utilization, other = lines[1].split(/\s+/) # second line..
80
+ end
81
+
82
+
83
+
84
+ def self.sbrodola_ricdisk(subdir)
85
+ # given a path, if .ricdisk exists i do stuff with it..
86
+ disk_info = nil
87
+ unless self.ok_dir?(subdir)
88
+ puts("Nothing for me here. Existing")
89
+ return
90
+ end
91
+ if File.exists?( "#{subdir}/.ricdisk") and File.empty?( "#{subdir}/.ricdisk")
92
+ deb("Interesting1. Empty file! Now I write YAML with it.")
93
+ disk_info = RicDiskUgly.new(subdir, '.ricdisk')
94
+ #puts(x)
95
+ #puts(yellow x.to_yaml)
96
+ end
97
+ if File.exists?( "#{subdir}/.ricdisk.yaml") and File.empty?( "#{subdir}/.ricdisk.yaml")
98
+ deb("Interesting2. Empty file! TODO write YAML with it.")
99
+ disk_info = RicDiskUgly.new(subdir, '.ricdisk.yaml')
100
+ # todo write
101
+ #puts(x)
102
+ puts(yellow disk_info.to_yaml)
103
+ end
104
+ if disk_info
105
+ if File.empty?(disk_info.ricdisk_absolute_path) and (disk_info.wr)
106
+ puts(green("yay, we can now write the file '#{disk_info.ricdisk_absolute_path}' (which is R/W, I just checked!) with proper YAML content.."))
107
+ ret = File.write(disk_info.ricdisk_absolute_path, disk_info.to_yaml)
108
+ puts("Written file! ret=#{ret}")
109
+ else
110
+ puts(red("Nope, qualcosa non va.. #{File.empty?(disk_info.ricdisk_absolute_path)}"))
111
+ puts("File size: #{File.size(disk_info.ricdisk_absolute_path)}")
112
+ end
113
+ end
114
+ if File.exists?( "#{subdir}/.ricdisk") and ! File.empty?( "#{subdir}/.ricdisk")
115
+ puts("Config File found with old-style name: '#{subdir}/.ricdisk' !")
116
+ #puts(white `cat "#{subdir}/.ricdisk"`)
117
+ end
118
+ end
119
+
120
+ # separiamo cosi usiamo meglio...
121
+ def self.ok_dir?(subdir)
122
+ File.exists?( "#{subdir}/.ricdisk") or File.exists?( "#{subdir}/.ricdisk.yaml")
123
+ end
124
+
125
+ def self.obsolescence_seconds file_path
126
+ creation_time = File.stat(file_path).ctime
127
+ deb("[obsolescence_seconds] File #{file_path}: #{creation_time} - #{(Time.now - creation_time)} seconds ago")
128
+ (Time.now - creation_time).to_i
129
+ end
130
+ def self.obsolescence_days(file_path)
131
+ return obsolescence_seconds(file_path) / 86400
132
+ end
133
+
134
+ def self.backquote_execute(cmd)
135
+ # executed a command wrapped by dryrun though
136
+ return "DRYRUN backquote_execute(#{cmd})" if $opts[:dryrun]
137
+ `#{cmd}`
138
+ end
139
+
140
+ def self.upload_to_gcs(file, opts={})
141
+ deb("upload_to_gcs(#{file}). TODO(ricc) after breafast upload to GCS : #{file}")
142
+ mount_name = file.split('/')[-2]
143
+ filename = "#{mount_name}-#{File.basename file}"
144
+ hostname = Socket.gethostname[/^[^.]+/]
145
+ command = "gsutil cp '#{file}' gs://#{$gcs_bucket}/backup/ricdisk-magic/#{ hostname }-#{filename}"
146
+ deb("Command: #{command}")
147
+ ret = backquote_execute(command)
148
+ # if $opts[:debug] do
149
+ # puts "+ Current list of files:"
150
+ # ret = backquote_execute("gsutil ls -al gs://#{$gcs_bucket}/backup/ricdisk-magic/")
151
+ # puts ret
152
+ # end
153
+ ret
154
+ end
155
+
156
+ # Create RDS file.
157
+ def self.calculate_stats_files(dir, opts={})
158
+ opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
159
+ full_file_path = "#{dir}/#{$stats_file}"
160
+
161
+ puts("calculate_stats_files(#{white dir}): #{white full_file_path}")
162
+ puts "TEST1 DIR EXISTS: #{dir} -> #{File.directory? dir}"
163
+ Dir.chdir(dir)
164
+ if File.exists?(full_file_path) and ($opts[:force] == false)
165
+ puts "File '#{$stats_file}' exists already." # - now should see if its too old, like more than 1 week old"
166
+ # TODO check for file time...
167
+ print "Lines found: #{yellow `wc -l "#{full_file_path}" `.chomp }. File obsolescence (days): #{yellow obsolescence_days(full_file_path)}."
168
+ if obsolescence_days(full_file_path) > 7
169
+ puts("*** ACHTUNG *** FIle is pretty old. You might consider rotating: #{yellow "mv #{full_file_path} #{full_file_path}_old"}. Or invoke with --force")
170
+ end
171
+ upload_to_gcs(full_file_path) if opts_upload_to_gcs
172
+ else
173
+ puts "Crunching data stats from '#{dir}' into '#{$stats_file}' ... please bear with me.. [maybe file didnt exist, maybe $opts[:force] is true]"
174
+ command = "find . -print0 | xargs -0 stats-with-md5.rb --no-color | tee '#{full_file_path}'"
175
+ puts("[#{`pwd`.chomp}] Executing: #{azure command}")
176
+ ret = backquote_execute command
177
+ puts "Done. #{ret.split("\n").count} files processed."
178
+ end
179
+ end
180
+
181
+ def self.find_active_dirs(base_dirs=nil, also_mountpoints=true)
182
+ base_dirs = DEFAULT_MEDIA_DIRS if base_dirs.nil?
183
+ active_dirs = []
184
+ base_dirs.each do |ugly_dir|
185
+ # https://stackoverflow.com/questions/1899072/getting-a-list-of-folders-in-a-directory#:~:text=Dir.chdir(%27/destination_directory%27)%0ADir.glob(%27*%27).select%20%7B%7Cf%7C%20File.directory%3F%20f%7D
186
+ dir = File.expand_path(ugly_dir)
187
+ begin
188
+ x=[]
189
+ # puts "TEST2 DIR EXISTS: #{dir} -> #{Dir.exists?(dir)}"
190
+ unless Dir.exists?(dir)
191
+ deb "Dir doesnt exist, skipping: #{dir}"
192
+ next
193
+ end
194
+ Dir.chdir(dir)
195
+ x = Dir.glob('*').select {|f| File.directory? f}
196
+ subdirs = x.map{|subdir| "#{dir}#{subdir}"}
197
+ subdirs.each{|subdir|
198
+ #puts `ls -al "#{subdir}"`
199
+ active_dirs << subdir if self.ok_dir?(subdir)
200
+ }
201
+ #puts(white x)
202
+ rescue Exception => e # optionally: `rescue Exception => ex`
203
+ puts "Exception: '#{e}'"
204
+ ensure # will always get executed
205
+ #deb 'Always gets executed.'
206
+ #x = []
207
+ end
208
+ end
209
+
210
+ if also_mountpoints
211
+ =begin
212
+ Example output from mount:
213
+
214
+ devfs on /dev (devfs, local, nobrowse)
215
+ /dev/disk3s6 on /System/Volumes/VM (apfs, local, noexec, journaled, noatime, nobrowse)
216
+ /dev/disk3s2 on /System/Volumes/Preboot (apfs, local, journaled, nobrowse)
217
+ /dev/disk3s4 on /System/Volumes/Update (apfs, local, journaled, nobrowse)
218
+ /dev/disk1s2 on /System/Volumes/xarts (apfs, local, noexec, journaled, noatime, nobrowse)
219
+ /dev/disk1s1 on /System/Volumes/iSCPreboot (apfs, local, journaled, nobrowse)
220
+ /dev/disk1s3 on /System/Volumes/Hardware (apfs, local, journaled, nobrowse)
221
+ /dev/disk3s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse, protect)
222
+ map auto_home on /System/Volumes/Data/home (autofs, automounted, nobrowse)
223
+ //riccardo@1.0.1.10/video on /Volumes/video (afpfs, nodev, nosuid, mounted by ricc)
224
+ //riccardo@1.0.1.10/photo on /Volumes/photo (afpfs, nodev, nosuid, mounted by ricc)
225
+ =end
226
+ # add directories from current mountpoints...
227
+ mount_table_lines = interesting_mount_points()
228
+ mount_table_lines.each{|line|
229
+ next if line =~ /^map /
230
+ dev, on, path, mode = line.split(/ /)
231
+ #puts line
232
+ #deb yellow(path)
233
+ active_dirs << path if self.ok_dir?(path)
234
+ }
235
+ end
236
+ active_dirs.uniq!
237
+ puts("find_active_dirs(): found dirs " + green(active_dirs))
238
+ return active_dirs
239
+ end
240
+
241
+
242
+ def analyze_local_system()
243
+ puts :TODO
244
+ puts "1. Interesting Mounts: #{green interesting_mount_points}"
245
+ puts "2. Sbrodoling everything: :TODO"
246
+ # find_info_from_mount(path)
247
+ # find_info_from_df()
248
+ end
249
+
250
+ end
251
+ end
@@ -0,0 +1,6 @@
1
+ # copied from https://github.com/rails/strong_parameters/edit/master/lib/strong_parameters/version.rb
2
+ # I;'m copying the DHH philosophy here.
3
+ module Storazzo
4
+ DHH_VERSION = "0.2.3_TODOFileRead" # TODO file.read ../../VERSION . chomp
5
+ end
6
+
data/lib/storazzo.rb CHANGED
@@ -1,43 +1,42 @@
1
- # copied from https://guides.rubygems.org/make-your-own-gem/#introduction
1
+ # Inspired from https://guides.rubygems.org/make-your-own-gem/#introduction
2
2
 
3
- #require 'storazzo/colors'
4
3
 
5
- module StorazzoMod
6
- VERSION = File.read('./VERSION').chomp # "10.0.0"
4
+ module Storazzo
5
+ #VERSION = File.read('./VERSION').chomp # "10.0.0"
7
6
  #require 'storazzo/translator'
8
7
 
9
- def latest_version
8
+ def latest_parser_version
10
9
  "1.2"
11
10
  end
12
- end
13
11
 
14
- class Storazzo
15
- require 'storazzo/colors'
12
+ # Finds RAILS_ROOT for Storazzo Gem. Copied from:
13
+ # https://stackoverflow.com/questions/10132949/finding-the-gem-root
14
+ def self.root
15
+ File.expand_path '../..', __FILE__
16
+ end
16
17
 
17
- # version 1.2
18
- def self.hi
19
- puts "Hello from Storazzo v#{StorazzoMod::VERSION rescue :ERROR}!"
18
+ def self.version
19
+ File.read(self.root + '/VERSION').chomp # "10.0.0"
20
20
  end
21
21
 
22
- def self.all_tests
23
- # include vs extend: https://stackoverflow.com/questions/15097929/ruby-module-require-and-include
24
- # => http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
25
- #include Storazzo::Colors
26
- extend Storazzo::Colors
27
-
28
- pwhite "All tests BEGIN"
29
- deb "Maybe debug is enabled?"
30
- #puts "This is Storazzo v#{StorazzoMod::VERSION}"
31
- hi
32
- # This works with EXTEND..
33
- puts(yellow "Just YELLOW 0")
34
- # This reqwuires a INCLUDE.
35
- #puts(Storazzo::Colors.yellow "Test YELLOW 1 self")
36
- #puts(Colors.yellow "Test YELLOW 1 self")
37
- #puts(Colors.green "Test YELLOW 2 ohne self")
38
- pwhite "All tests END"
39
- #puts "All tests END"
22
+ # alias_method :VERSION, :version
23
+ def self.VERSION
24
+ version
40
25
  end
41
- end
26
+ end
42
27
 
28
+ # nice to paste nice output
29
+ require 'pp'
30
+
31
+ require 'storazzo/common'
32
+ require 'storazzo/colors'
33
+ require 'storazzo/hashify'
34
+ require 'storazzo/ric_disk' # NEW and will build from ground up using multiple files..
35
+ require 'storazzo/ric_disk_ugly' # OLD and 90% working
36
+ require 'storazzo/ric_disk_config'
37
+ require 'storazzo/ric_disk_statsfile'
38
+ require 'storazzo/main'
43
39
  require 'storazzo/translator'
40
+
41
+ puts Storazzo::Main.hi
42
+
data/storazzo.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "storazzo"
3
+ # TOSDO: copy approach from here to dry version calculating: https://github.com/rails/strong_parameters/blob/master/strong_parameters.gemspec#L15
4
+ s.version = File.read("VERSION").chomp # TODO cat version File.read(@,.VERSION).chomp
5
+ s.summary = "storazzo is an amazing gem. Code is in https://github.com/palladius/storazzo"
6
+ s.description = "A simple gem to manage your external hard drives and extract MD5 and common stuff from them."
7
+ s.authors = ["Riccardo Carlesso"]
8
+ s.email = "name dot surname at popular Google-owned Mail"
9
+ # Autoglob as per https://stackoverflow.com/questions/11873294/determining-the-gems-list-of-files-for-the-specification
10
+ s.files = %w(Gemfile LICENSE README.md Makefile Rakefile storazzo.gemspec VERSION) + Dir["{bin,lib,test,var}/**/*"]
11
+ s.test_files = Dir["test/**/*"] + Dir["var/test/**/*"]
12
+ s.executables << "ricdisk-magic"
13
+
14
+ s.homepage = "https://rubygems.org/gems/storazzo" # maybe https://github.com/palladius/storazzo
15
+ s.license = "MIT"
16
+
17
+ #s.add_dependency "activesupport", "~> 3.0"
18
+ end
@@ -0,0 +1,34 @@
1
+ require "minitest/autorun"
2
+ require "storazzo"
3
+
4
+ class StorazzoTest < Minitest::Test
5
+ def test_storazzo_hi_with_argument
6
+ assert_match "Hello from Storazzo", Storazzo::Main.hi("ruby this should fail")
7
+ assert_match "ruby this should fail", Storazzo::Main.hi("ruby this should fail")
8
+ end
9
+ def test_storazzo_hi_without_argument
10
+ assert_match "Hello from Storazzo", Storazzo::Main.hi()
11
+ end
12
+
13
+ def test_storazzo_version_should_have_3_numbers_and_2_dots
14
+ # puts Storazzo::version
15
+ assert_equal Storazzo::version.split('.').size , 3, "should be 3 parts, like A.B.C"
16
+ #major, minor, minuscule = Storazzo::VERSION.split('.')
17
+ # assert_match Storazzo::VERSION, "....."
18
+ end
19
+
20
+ # def test_english_hello
21
+ # assert_equal "hello world",
22
+ # Hola.hi("english")
23
+ # end
24
+
25
+ # def test_any_hello
26
+ # assert_equal "hello world",
27
+ # Hola.hi("ruby")
28
+ # end
29
+
30
+ # def test_spanish_hello
31
+ # assert_equal "hola mundo",
32
+ # Hola.hi("spanish")
33
+ # end
34
+ end
@@ -0,0 +1 @@
1
+ Just to test two disks: one with stuff and one empty.
File without changes
@@ -0,0 +1,6 @@
1
+ total 0
2
+ drwxr-xr-x 5 ricc primarygroup 160 Jul 13 07:18 .
3
+ drwxr-xr-x 4 ricc primarygroup 128 Jul 13 07:17 ..
4
+ -rw-r--r-- 1 ricc primarygroup 0 Jul 13 07:17 .ricdisk
5
+ -rw-r--r-- 1 ricc primarygroup 0 Jul 13 07:18 fake file.touch
6
+ -rw-r--r-- 1 ricc primarygroup 0 Jul 13 07:18 ls.txt
metadata CHANGED
@@ -1,31 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storazzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Riccardo Carlesso
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-11 00:00:00.000000000 Z
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple gem to manage your external hard drives and extract MD5 and
14
14
  common stuff from them.
15
15
  email: name dot surname at popular Google-owned Mail
16
- executables: []
16
+ executables:
17
+ - ricdisk-magic
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
20
- - bin/ricdisk-magic.rb
21
+ - Gemfile
22
+ - LICENSE
23
+ - Makefile
24
+ - README.md
25
+ - Rakefile
26
+ - VERSION
27
+ - bin/ricdisk-magic
21
28
  - lib/storazzo.rb
22
29
  - lib/storazzo/colors.rb
30
+ - lib/storazzo/common.rb
31
+ - lib/storazzo/debug.rb
32
+ - lib/storazzo/hashify.rb
33
+ - lib/storazzo/main.rb
34
+ - lib/storazzo/parser/parser.rb
35
+ - lib/storazzo/ric_disk.rb
36
+ - lib/storazzo/ric_disk_config.rb
37
+ - lib/storazzo/ric_disk_statsfile.rb
38
+ - lib/storazzo/ric_disk_ugly.rb
23
39
  - lib/storazzo/translator.rb
40
+ - lib/storazzo/version.rb
41
+ - storazzo.gemspec
42
+ - test/test_storazzo.rb
43
+ - var/test/README.md
44
+ - var/test/disks/disk02-full/fake file.touch
45
+ - var/test/disks/disk02-full/ls.txt
24
46
  homepage: https://rubygems.org/gems/storazzo
25
47
  licenses:
26
48
  - MIT
27
49
  metadata: {}
28
- post_install_message:
50
+ post_install_message:
29
51
  rdoc_options: []
30
52
  require_paths:
31
53
  - lib
@@ -41,7 +63,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
63
  version: '0'
42
64
  requirements: []
43
65
  rubygems_version: 3.1.4
44
- signing_key:
66
+ signing_key:
45
67
  specification_version: 4
46
68
  summary: storazzo is an amazing gem. Code is in https://github.com/palladius/storazzo
47
- test_files: []
69
+ test_files:
70
+ - test/test_storazzo.rb
71
+ - var/test/README.md
72
+ - var/test/disks/disk02-full/fake file.touch
73
+ - var/test/disks/disk02-full/ls.txt