storazzo 0.0.6 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06f3ee99606822e20e1d8410578fd8194b416f9ccdcc4cf2a17d1b7a44e09d45
4
- data.tar.gz: c92b3712561fb31e049052f35ffe500800b5dab6537eb0cb8f2313e898b2558d
3
+ metadata.gz: 0c1d447526136c87cfefa48b69232971dd9e6df07a07ac1410eede18d0fa6b5e
4
+ data.tar.gz: c295db76797061749062a49f01b0bf08132ff86b5c48f61b0549d10bbe8066e0
5
5
  SHA512:
6
- metadata.gz: 3c05485035eee8f1092a8c8c05a765f3d1492233ec22b8bf3539ad82bc6ded2cc46cbaacb49bb86539fdea6ee48c8a99c491a6daab1f0b3f6d408af48f610669
7
- data.tar.gz: 79c252ed51045da677895a8c5e891de78e7490354212eaa4a93594914734ee66eff0423e643aecb3d476b5d8c2367f6900bd625a21e7bbc9f0cf0a3a790397ed
6
+ metadata.gz: 6127acae73ae0e07e3b000344e8b784964e50aa40a99404d5d0b1653a04c93c9f92270531ae5add2247b0e8d91fb5f501af1535d6dfa28abc123df6ce964b957
7
+ data.tar.gz: 1abb40a264233b9df3f87cb46222b235dbc64baef885e8589c30c18d31fef103dba95291d83787b07c856f4ce54f85602747068c88e90bb0f411d389a44cc269
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ # useless now
data/LICENSE ADDED
@@ -0,0 +1 @@
1
+ # todo
data/Makefile ADDED
@@ -0,0 +1,38 @@
1
+
2
+ VER = $(shell cat VERSION)
3
+
4
+ .PHONY: test clean
5
+
6
+
7
+ help:
8
+ cat Makefile
9
+
10
+ build-local:
11
+ gem build storazzo.gemspec
12
+
13
+ build: build-local
14
+
15
+ install:
16
+ gem install ./storazzo-$(VER).gem
17
+
18
+ push-to-rubygems: build-local test
19
+ gem push ./storazzo-$(VER).gem
20
+
21
+ list:
22
+ gem list -r storazzo
23
+
24
+ test:
25
+ echo 1. Testing the library end to end by requiring it..
26
+ echo "Storazzo::Main.all_tests " | irb -Ilib -rstorazzo
27
+ #echo "Storazzo::Main.all_mounts " | irb -Ilib -rstorazzo
28
+ #echo 2. Testing ricdisk-magic
29
+ #bin/ricdisk-magic Ciao-da-Makefile
30
+ echo 3. run rake test.. ont configured yet just a memo for the future.
31
+ RUBYOPT="-W0" rake test
32
+ @echo 'OK: ALL TESTS PASSED. #STIKA'
33
+
34
+ irb:
35
+ irb -Ilib -rstorazzo
36
+
37
+ watch-test:
38
+ watch -c make test
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+
2
+ # storazzo Gem
3
+
4
+ Storazzo 💎 gem - a Gem to automatically parse your FS for mounts and compute MD5 of all files therein and then collect in central DB through StorazzoApp (TM).
5
+
6
+ # INSTALL
7
+
8
+ `gem install storazzo`
9
+
10
+ (Latest version is hosted in https://rubygems.org/gems/storazzo)
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ #require 'rake'
2
+
3
+ # from hola: https://guides.rubygems.org/make-your-own-gem/#adding-an-executable
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.verbose = false
9
+ t.warning = false
10
+ end
11
+
12
+ desc "Run tests"
13
+ task default: :test
14
+
15
+
16
+ # begin
17
+ # require 'bundler/setup'
18
+ # Bundler::GemHelper.install_tasks
19
+ # rescue LoadError
20
+ # puts 'although not required, bundler is recommended for running the tests'
21
+ # end
22
+
23
+ # task default: :spec
24
+
25
+ # require 'rspec/core/rake_task'
26
+ # RSpec::Core::RakeTask.new(:spec)
27
+
28
+ # require 'rubocop/rake_task'
29
+ # RuboCop::RakeTask.new do |task|
30
+ # task.requires << 'rubocop-performance'
31
+ # task.requires << 'rubocop-rspec'
32
+ # end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/bin/ricdisk-magic ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+
5
+ ############# ############# ############# ############# ############# ############# ############# #############
6
+ # placeholder until i make it work..
7
+ # https://guides.rubygems.org/make-your-own-gem/#adding-an-executable
8
+ require 'storazzo'
9
+ #require 'lib/storazzo'
10
+ puts "First I need to figure out how to bring in all the libraries in here.."
11
+ puts Storazzo::Main.hi(ARGV[0])
12
+ exit(0)
13
+ ############# ############# ############# ############# ############# ############# ############# #############
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+ #require 'google/protobuf'
44
+ #require_relative '../etc/protos/out/ricdisk_pb'
45
+ require 'yaml'
46
+ require 'socket'
47
+ require 'optparse' # http://ruby.about.com/od/advancedruby/a/optionparser.htm
48
+
49
+ # I believe this is wrong
50
+ #require 'storazzo'
51
+ # required to have methods wiothout self.
52
+ #include 'lib/ric_disk'
53
+ #extend Storazzo::Colors
54
+
55
+ if RUBY_VERSION.split('.')[0] == 1
56
+ puts "Refusing to launch a script form Ruby 1. Sorry Ric, its 2020 damn it!"
57
+ exit 2020
58
+ end
59
+
60
+ $PROG_VER = '0.3'
61
+ $DEBUG = false
62
+
63
+ HISTORY = <<-BIG_LONG_MULTILINE
64
+ 2022-07-11 v0.3 Ported from private files in GIC into storazzo (open source on gitHub) and cleaned up italian and libs
65
+ 2022-07-DD v0.2 Some private stuff on GIC
66
+ BIG_LONG_MULTILINE
67
+
68
+ =begin
69
+
70
+ ############################################################
71
+ @author: Riccardo Carlesso
72
+ @email: riccardo.carlesso@gmail.com
73
+ @maturity: development
74
+ @language: Ruby
75
+ @synopsis: Brief Description here
76
+ @tags: development, rcarlesso, test
77
+ @description: See description
78
+ ############################################################
79
+
80
+ =end
81
+
82
+
83
+ $myconf = {
84
+ :app_name => "RicDisk Magic should be sth like #{$0}",
85
+ :description => "
86
+ This program is loosely inspired to ricdisk-magic.sh but its much better.
87
+ Idea di base: trovare tutti le directory con file ricdisk e da esso estrarre info e magari METTERE info.
88
+ Il tutto condito con un bel protobuf e un'entita che metto in Lib.
89
+ ".strip.gsub(/^\s+/, "").gsub(/\s+$/, ""),
90
+ # TODO move to some class default
91
+ :media_dirs => %w{ /media/riccardo/ /Volumes/ /mnt/ ~/git/storazzo/test/ /sobenme/giusto/per/imparare/ad/ammutolire/gli/errori/ },
92
+ :mount_types => %w{ vfat ntfs },
93
+ }
94
+ $stats_file = "ricdisk_stats_v11.rds"
95
+ $gcs_bucket = 'palladius'
96
+
97
+ # This template from scripta.rb. from 2.1.0 removed aby ric gem dependency.
98
+ # 2022-04-26 2.1.1 Added more colors
99
+ # 2022-04-26 2.1.0 Historical momemnt: removed gem 'ric' dependency
100
+ $TEMPLATE_VER = '2.1.1'
101
+
102
+
103
+ def usage(comment=nil)
104
+ puts white($optparse.banner)
105
+ puts($optparse.summarize)
106
+ puts("Description: " + gray($myconf[:description]))
107
+ puts red(comment) if comment
108
+ #puts "Description: #{ $myconf[:description] }"
109
+ exit 13
110
+ end
111
+
112
+ # include it in main if you want a custome one
113
+ def init() # see lib_autoinit in lib/util.rb
114
+ $opts = {}
115
+ # setting defaults
116
+ $opts[:verbose] = false
117
+ $opts[:dryrun] = false
118
+ $opts[:debug] = false
119
+ $opts[:force] = false
120
+
121
+ $optparse = OptionParser.new do |opts|
122
+ opts.banner = "#{$0} v.#{$PROG_VER}\n Usage: #{File.basename $0} [options] file1 file2 ..."
123
+ opts.on( '-d', '--debug', 'enables debug (DFLT=false)' ) { $opts[:debug] = true ; $DEBUG = true }
124
+ opts.on( '-f', '--force', 'force stuff (DFLT=false)' ) { $opts[:force] = true }
125
+ opts.on( '-h', '--help', 'Display this screen' ) { usage }
126
+ #opts.on( '-j', '--jabba', 'Activates my Jabber powerful CLI' ) { $opts[:jabba] = true }
127
+ opts.on( '-n', '--dryrun', "Don't really execute code" ) { $opts[:dryrun] = true }
128
+ opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) {|file| $opts[:logfile] = file }
129
+ opts.on( '-v', '--verbose', 'Output more information' ) { $opts[:verbose] = true}
130
+ end
131
+ $optparse.parse!
132
+ end
133
+
134
+ def real_program
135
+ deb("Hello world from a templated '#{yellow $0 }'")
136
+ deb "+ Options are: #{gray $opts}"
137
+ deb "+ Depured args: #{azure ARGV}"
138
+ deb "+ Script-specifig super-cool conf: #{green $prog_conf_d}"
139
+ deb "+ Your configuration: #{purple $myconf.inspect}"
140
+
141
+ # Your code goes here...
142
+ puts white("Hello world from #{$myconf[:app_name]}!")
143
+ puts "Description: '''#{white $myconf[:description] }'''"
144
+
145
+ if ARGV == [] # empty -> ALL
146
+ dirs = RicDisk.find_active_dirs()
147
+ dirs.each {|dir|
148
+ RicDisk.sbrodola_ricdisk(dir)
149
+ RicDisk.calculate_stats_files(dir) # dir is inutile
150
+ } # TODO refactor in option sbrodola_afterwards=true. :)
151
+ else
152
+ deb "I consider ARGV come la lista di directories da parsare :)"
153
+ dirs = RicDisk.find_active_dirs()
154
+ ARGV.each{ |dir|
155
+ dir = File.expand_path(dir)
156
+ if dirs.include?(dir)
157
+ deb "Legit dir: #{green dir}"
158
+ RicDisk.sbrodola_ricdisk(dir)
159
+ RicDisk.calculate_stats_files(dir) # dir is inutile
160
+ else
161
+ deb "Figghiu ri buttana: doesnt exist #{red dir}"
162
+ end
163
+ }
164
+ end
165
+ end
166
+
167
+ def main(filename)
168
+ deb "I'm called by #{white filename}"
169
+ deb "HISTORY: #{gray HISTORY}"
170
+ #deb "To remove this shit, just set $DEBUG=false :)"
171
+ init # Enable this to have command line parsing capabilities!
172
+ #warn "[warn] template v#{$TEMPLATE_VER }: proviamo il warn che magari depreca il DEB"
173
+ real_program
174
+ end
175
+
176
+ main(__FILE__)
@@ -1,8 +1,12 @@
1
1
  # Use EXTEND vs INCLUDE and magically the Class will inherit instead of instance. Magical! :)
2
2
  # http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
3
3
 
4
+ module Storazzo
5
+ # needs to be defined before
6
+ end
7
+
4
8
  module Storazzo::Colors
5
- # class Storazzo::Colors
9
+ #class Storazzo::Colors1
6
10
  PREPEND_ME = "[Storazzo::Colors] "
7
11
 
8
12
  def deb(s); puts "#DEB #{gray(s)}" if $DEBUG; end
@@ -38,5 +42,5 @@ module Storazzo::Colors
38
42
  def pred(s) puts(red(s)); end
39
43
  def pyellow(s) puts(yellow(s)); end
40
44
 
41
-
42
- end
45
+ end
46
+ #end
@@ -0,0 +1,10 @@
1
+
2
+ # idea from https://medium.com/@leo_hetsch/ruby-modules-include-vs-prepend-vs-extend-f09837a5b073
3
+ module Debug
4
+
5
+ def log(message)
6
+ # debug output
7
+ puts "TODO(ricc): LOG '#{message}' on GCP :)"
8
+ end
9
+
10
+ end
@@ -0,0 +1,33 @@
1
+ # copied from https://dev.to/ayushn21/how-to-generate-yaml-from-ruby-objects-without-type-annotations-4fli
2
+ module Storazzo
3
+ module Hashify
4
+ # Classes that include this module can exclude certain
5
+ # instance variable from its hash representation by overriding
6
+ # this method
7
+ def ivars_excluded_from_hash
8
+ [ 'this_doesnt_exist' ]
9
+ end
10
+
11
+ def to_hash
12
+ hash = {}
13
+ excluded_ivars = ivars_excluded_from_hash
14
+
15
+ # Iterate over all the instance variables and store their
16
+ # names and values in a hash
17
+ instance_variables.each do |var|
18
+ next if excluded_ivars.include? var.to_s
19
+
20
+ value = instance_variable_get(var)
21
+ value = value.map(&:to_hash) if value.is_a? Array
22
+
23
+ hash[var.to_s.delete("@")] = value
24
+ end
25
+
26
+ return hash
27
+ end
28
+
29
+ def to_yaml
30
+ to_hash.to_yaml
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ # main entrypoint for tests and silly stuff from Makefile..
2
+ # This Class externalizes all relevant things from other libs while I learn how to do it from there
3
+ # eg from RicDisk.
4
+
5
+ module Storazzo
6
+ class Storazzo::Main # Can be same name as Module: https://stackoverflow.com/questions/13261474/ruby-modules-and-classes-same-name-in-structure
7
+ require 'storazzo/colors'
8
+ extend Storazzo::Colors
9
+
10
+ # version 1.2
11
+ def self.hi(message=nil)
12
+ str = "Hello from Storazzo v#{white Storazzo::version rescue "Error: #{$!}"}!"
13
+ str += " Message: '#{yellow message.to_s}'" if message
14
+ puts str
15
+ str
16
+ end
17
+
18
+ def self.all_mounts(opts={})
19
+ extend Storazzo::RicDisk
20
+ opts_verbose = opts.fetch :verbose, true
21
+
22
+ pwhite "TODO(ricc): show a list of all RicDisk relevant mounts" if opts_verbose
23
+ #sbrodola_ricdisk("/Volumes/")
24
+ sbrodola_ricdisk(StorazzoMod::root + "./var/disks/") rescue "[Storazzo::AllMount] SomeError: #{$!}"
25
+ end
26
+
27
+ def self.all_tests
28
+ # include vs extend: https://stackoverflow.com/questions/15097929/ruby-module-require-and-include
29
+ # => http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
30
+ #include Storazzo::Colors
31
+ extend Storazzo::Colors
32
+
33
+ pwhite "All tests BEGIN"
34
+ deb "Maybe debug is enabled?"
35
+ #puts "This is Storazzo v#{StorazzoMod::VERSION}"
36
+ hi
37
+ # This works with EXTEND..
38
+ puts(yellow "Just YELLOW 0")
39
+ # This reqwuires a INCLUDE.
40
+ #puts(Storazzo::Colors.yellow "Test YELLOW 1 self")
41
+ #puts(Colors.yellow "Test YELLOW 1 self")
42
+ #puts(Colors.green "Test YELLOW 2 ohne self")
43
+ pwhite "All tests END"
44
+ #puts "All tests END"
45
+ end
46
+ end
47
+ end
File without changes
@@ -0,0 +1,232 @@
1
+
2
+
3
+ module Storazzo
4
+ class Storazzo::RicDisk
5
+ RICDISK_VERSION = "1.0"
6
+ include Hashify
7
+ extend Storazzo::Colors
8
+
9
+ # todo substitute wqith protobuf..
10
+ attr_accessor :name, :description, :ricdisk_file, :local_mountpoint, :wr
11
+
12
+ def self.interesting_mount_points(opts={})
13
+ #https://unix.stackexchange.com/questions/177014/showing-only-interesting-mount-points-filtering-non-interesting-types
14
+ `mount | grep -Ev 'type (proc|sysfs|tmpfs|devpts|debugfs|rpc_pipefs|nfsd|securityfs|fusectl|devtmpfs) '`.split(/\n+/)
15
+ end
16
+
17
+ def initialize(path, ricdisk_file)
18
+ @local_mountpoint = path
19
+ @description = "This is an automated RicDisk description from v.#{VERSION}. Riccardo feel free to edit away with characteristicshs of this device.. Created on #{Time.now}'"
20
+ @ricdisk_version = VERSION
21
+ @ricdisk_file = ricdisk_file
22
+ #@questo_non_esiste = :sobenme
23
+ @label = path.split("/").last
24
+ @name = path.split("/").last
25
+ @wr = File.writable?("#{path}/#{ricdisk_file}" ) # .writeable?
26
+ @tags = 'ricdisk'
27
+ find_info_from_mount(path)
28
+ find_info_from_df()
29
+ end
30
+
31
+ def ricdisk_absolute_path
32
+ @local_mountpoint + "/" + @ricdisk_file
33
+ end
34
+
35
+ def add_tag(tag)
36
+ @tags += ", #{tag}"
37
+ end
38
+
39
+ # might have other things in the future...
40
+ def find_info_from_mount(path)
41
+ mount_table_lines = interesting_mount_points()
42
+ mount_line = nil
43
+ mount_table_lines.each do |line|
44
+ next if line =~ /^map /
45
+ dev, on, mount_path, mode = line.split(/ /)
46
+ if mount_path==path
47
+ mount_line = line
48
+ else
49
+ @info_from_mount = false
50
+ end
51
+ end
52
+ @info_from_mount = ! (mount_line.nil?)
53
+ if @info_from_mount
54
+ #@mount_line = mount_line
55
+ @description += "\nMount line:\n" + mount_line
56
+ @remote_mountpoint = mount_line.split(/ /)[0]
57
+ @fstype = mount_line.split(/ /)[3].gsub(/[\(,]/, '')
58
+ add_tag(:synology) if @remote_mountpoint.match('1.0.1.10')
59
+ end
60
+ end
61
+
62
+ def find_info_from_df()
63
+ path = @local_mountpoint
64
+ df_info = `df -h "#{path}"`
65
+ @df_info = df_info
66
+ lines = df_info.split(/\n+/)
67
+ raise "I need exactly TWO lines! Or no info is served here..." unless lines.size == 2
68
+ mount, @size_readable, used_size, avail_size, @disk_utilization, other = lines[1].split(/\s+/) # second line..
69
+ end
70
+
71
+
72
+
73
+ def self.sbrodola_ricdisk(subdir)
74
+ # given a path, if .ricdisk exists i do stuff with it..
75
+ disk_info = nil
76
+ unless self.ok_dir?(subdir)
77
+ puts("Nothing for me here. Existing")
78
+ return
79
+ end
80
+ if File.exists?( "#{subdir}/.ricdisk") and File.empty?( "#{subdir}/.ricdisk")
81
+ deb("Interesting1. Empty file! Now I write YAML with it.")
82
+ disk_info = RicDisk.new(subdir, '.ricdisk')
83
+ #puts(x)
84
+ #puts(yellow x.to_yaml)
85
+ end
86
+ if File.exists?( "#{subdir}/.ricdisk.yaml") and File.empty?( "#{subdir}/.ricdisk.yaml")
87
+ deb("Interesting2. Empty file! TODO write YAML with it.")
88
+ disk_info = RicDisk.new(subdir, '.ricdisk.yaml')
89
+ # todo write
90
+ #puts(x)
91
+ puts(yellow disk_info.to_yaml)
92
+ end
93
+ if disk_info
94
+ if File.empty?(disk_info.ricdisk_absolute_path) and (disk_info.wr)
95
+ 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.."))
96
+ ret = File.write(disk_info.ricdisk_absolute_path, disk_info.to_yaml)
97
+ puts("Written file! ret=#{ret}")
98
+ else
99
+ puts(red("Nope, qualcosa non va.. #{File.empty?(disk_info.ricdisk_absolute_path)}"))
100
+ puts("File size: #{File.size(disk_info.ricdisk_absolute_path)}")
101
+ end
102
+ end
103
+ if File.exists?( "#{subdir}/.ricdisk") and ! File.empty?( "#{subdir}/.ricdisk")
104
+ puts("Config File found with old-style name: '#{subdir}/.ricdisk' !")
105
+ #puts(white `cat "#{subdir}/.ricdisk"`)
106
+ end
107
+ end
108
+
109
+ # separiamo cosi usiamo meglio...
110
+ def self.ok_dir?(subdir)
111
+ File.exists?( "#{subdir}/.ricdisk") or File.exists?( "#{subdir}/.ricdisk.yaml")
112
+ end
113
+
114
+ def self.obsolescence_seconds file_path
115
+ creation_time = File.stat(file_path).ctime
116
+ deb("[obsolescence_seconds] File #{file_path}: #{creation_time} - #{(Time.now - creation_time)} seconds ago")
117
+ (Time.now - creation_time).to_i
118
+ end
119
+ def self.obsolescence_days(file_path)
120
+ return obsolescence_seconds(file_path) / 86400
121
+ end
122
+
123
+ def self.backquote_execute(cmd)
124
+ # executed a command wrapped by dryrun though
125
+ return "DRYRUN backquote_execute(#{cmd})" if $opts[:dryrun]
126
+ `#{cmd}`
127
+ end
128
+
129
+ def self.upload_to_gcs(file, opts={})
130
+ deb("upload_to_gcs(#{file}). TODO(ricc) after breafast upload to GCS : #{file}")
131
+ mount_name = file.split('/')[-2]
132
+ filename = "#{mount_name}-#{File.basename file}"
133
+ hostname = Socket.gethostname[/^[^.]+/]
134
+ command = "gsutil cp '#{file}' gs://#{$gcs_bucket}/backup/ricdisk-magic/#{ hostname }-#{filename}"
135
+ deb("Command: #{command}")
136
+ ret = backquote_execute(command)
137
+ # if $opts[:debug] do
138
+ # puts "+ Current list of files:"
139
+ # ret = backquote_execute("gsutil ls -al gs://#{$gcs_bucket}/backup/ricdisk-magic/")
140
+ # puts ret
141
+ # end
142
+ ret
143
+ end
144
+
145
+ # Create RDS file.
146
+ def self.calculate_stats_files(dir, opts={})
147
+ opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
148
+ full_file_path = "#{dir}/#{$stats_file}"
149
+
150
+ puts("calculate_stats_files(#{white dir}): #{white full_file_path}")
151
+ puts "TEST1 DIR EXISTS: #{dir} -> #{Dir.directory? dir}"
152
+ Dir.chdir(dir)
153
+ if File.exists?(full_file_path) and ($opts[:force] == false)
154
+ puts "File '#{$stats_file}' exists already." # - now should see if its too old, like more than 1 week old"
155
+ # TODO check for file time...
156
+ print "Lines found: #{yellow `wc -l "#{full_file_path}" `.chomp }. File obsolescence (days): #{yellow obsolescence_days(full_file_path)}."
157
+ if obsolescence_days(full_file_path) > 7
158
+ puts("*** ACHTUNG *** FIle is pretty old. You might consider rotating: #{yellow "mv #{full_file_path} #{full_file_path}_old"}. Or invoke with --force")
159
+ end
160
+ upload_to_gcs(full_file_path) if opts_upload_to_gcs
161
+ else
162
+ puts "Crunching data stats from '#{dir}' into '#{$stats_file}' ... please bear with me.. [maybe file didnt exist, maybe $opts[:force] is true]"
163
+ command = "find . -print0 | xargs -0 stats-with-md5.rb --no-color | tee '#{full_file_path}'"
164
+ puts("[#{`pwd`.chomp}] Executing: #{azure command}")
165
+ ret = backquote_execute command
166
+ puts "Done. #{ret.split("\n").count} files processed."
167
+ end
168
+ end
169
+
170
+ def self.find_active_dirs(base_dirs=nil, also_mountpoints=true)
171
+ base_dirs = $myconf[:media_dirs] if base_dirs.nil?
172
+ active_dirs = []
173
+ base_dirs.each do |ugly_dir|
174
+ # 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
175
+ dir = File.expand_path(ugly_dir)
176
+ begin
177
+ x=[]
178
+ # puts "TEST2 DIR EXISTS: #{dir} -> #{Dir.exists?(dir)}"
179
+ unless Dir.exists?(dir)
180
+ deb "Dir doesnt exist, skipping: #{dir}"
181
+ next
182
+ end
183
+ Dir.chdir(dir)
184
+ x = Dir.glob('*').select {|f| File.directory? f}
185
+ subdirs = x.map{|subdir| "#{dir}#{subdir}"}
186
+ subdirs.each{|subdir|
187
+ #puts `ls -al "#{subdir}"`
188
+ active_dirs << subdir if self.ok_dir?(subdir)
189
+ }
190
+ #puts(white x)
191
+ rescue Exception => e # optionally: `rescue Exception => ex`
192
+ puts "Exception: '#{e}'"
193
+ ensure # will always get executed
194
+ #deb 'Always gets executed.'
195
+ #x = []
196
+ end
197
+ end
198
+
199
+ if also_mountpoints
200
+ =begin
201
+ Example output from mount:
202
+
203
+ devfs on /dev (devfs, local, nobrowse)
204
+ /dev/disk3s6 on /System/Volumes/VM (apfs, local, noexec, journaled, noatime, nobrowse)
205
+ /dev/disk3s2 on /System/Volumes/Preboot (apfs, local, journaled, nobrowse)
206
+ /dev/disk3s4 on /System/Volumes/Update (apfs, local, journaled, nobrowse)
207
+ /dev/disk1s2 on /System/Volumes/xarts (apfs, local, noexec, journaled, noatime, nobrowse)
208
+ /dev/disk1s1 on /System/Volumes/iSCPreboot (apfs, local, journaled, nobrowse)
209
+ /dev/disk1s3 on /System/Volumes/Hardware (apfs, local, journaled, nobrowse)
210
+ /dev/disk3s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse, protect)
211
+ map auto_home on /System/Volumes/Data/home (autofs, automounted, nobrowse)
212
+ //riccardo@1.0.1.10/video on /Volumes/video (afpfs, nodev, nosuid, mounted by ricc)
213
+ //riccardo@1.0.1.10/photo on /Volumes/photo (afpfs, nodev, nosuid, mounted by ricc)
214
+ =end
215
+ # add directories from current mountpoints...
216
+ mount_table_lines = interesting_mount_points()
217
+ mount_table_lines.each{|line|
218
+ next if line =~ /^map /
219
+ dev, on, path, mode = line.split(/ /)
220
+ #puts line
221
+ #deb yellow(path)
222
+ active_dirs << path if self.ok_dir?(path)
223
+ }
224
+ end
225
+ active_dirs.uniq!
226
+ puts("find_active_dirs(): found dirs " + green(active_dirs))
227
+ return active_dirs
228
+ end
229
+
230
+
231
+ end
232
+ 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,30 @@
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
-
14
- class Storazzo
15
- require 'storazzo/colors'
16
11
 
17
- # version 1.2
18
- def self.hi
19
- puts "Hello from Storazzo v#{StorazzoMod::VERSION rescue :ERROR}!"
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__
20
16
  end
21
17
 
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"
18
+ def self.version
19
+ File.read('./VERSION').chomp # "10.0.0"
40
20
  end
41
- end
21
+ end
42
22
 
23
+ require 'storazzo/colors'
24
+ require 'storazzo/hashify'
25
+ require 'storazzo/ric_disk'
26
+ require 'storazzo/main'
43
27
  require 'storazzo/translator'
28
+
29
+ puts Storazzo::Main.hi
30
+
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,49 @@
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.1.1
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/debug.rb
31
+ - lib/storazzo/hashify.rb
32
+ - lib/storazzo/main.rb
33
+ - lib/storazzo/parser/parser.rb
34
+ - lib/storazzo/ric_disk.rb
23
35
  - lib/storazzo/translator.rb
36
+ - lib/storazzo/version.rb
37
+ - storazzo.gemspec
38
+ - test/test_storazzo.rb
39
+ - var/test/README.md
40
+ - var/test/disks/disk02-full/fake file.touch
41
+ - var/test/disks/disk02-full/ls.txt
24
42
  homepage: https://rubygems.org/gems/storazzo
25
43
  licenses:
26
44
  - MIT
27
45
  metadata: {}
28
- post_install_message:
46
+ post_install_message:
29
47
  rdoc_options: []
30
48
  require_paths:
31
49
  - lib
@@ -41,7 +59,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
59
  version: '0'
42
60
  requirements: []
43
61
  rubygems_version: 3.1.4
44
- signing_key:
62
+ signing_key:
45
63
  specification_version: 4
46
64
  summary: storazzo is an amazing gem. Code is in https://github.com/palladius/storazzo
47
- test_files: []
65
+ test_files:
66
+ - test/test_storazzo.rb
67
+ - var/test/README.md
68
+ - var/test/disks/disk02-full/fake file.touch
69
+ - var/test/disks/disk02-full/ls.txt
data/bin/ricdisk-magic.rb DELETED
@@ -1,395 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #require 'google/protobuf'
4
- #require_relative '../etc/protos/out/ricdisk_pb'
5
- require 'yaml'
6
- require 'socket'
7
- require 'optparse' # http://ruby.about.com/od/advancedruby/a/optionparser.htm
8
- require 'storazzo'
9
- # required to have methods wiothout self.
10
- extend Storazzo::Colors
11
-
12
- if RUBY_VERSION.split('.')[0] == 1
13
- puts "Refusing to launch a script form Ruby 1. Sorry Ric, its 2020 damn it!"
14
- exit 2020
15
- end
16
-
17
- $PROG_VER = '0.3'
18
- $DEBUG = false
19
-
20
- HISTORY = <<-BIG_LONG_MULTILINE
21
- 2022-07-11 v0.3 Ported from private files in GIC into storazzo (open source on gitHub) and cleaned up italian and libs
22
- 2022-07-DD v0.2 Some private stuff on GIC
23
- BIG_LONG_MULTILINE
24
-
25
- =begin
26
-
27
- ############################################################
28
- @author: Riccardo Carlesso
29
- @email: riccardo.carlesso@gmail.com
30
- @maturity: development
31
- @language: Ruby
32
- @synopsis: Brief Description here
33
- @tags: development, rcarlesso, test
34
- @description: See description
35
- ############################################################
36
-
37
- =end
38
-
39
-
40
- $myconf = {
41
- :app_name => "RicDisk Magic should be sth like #{$0}",
42
- :description => "
43
- This program is loosely inspired to ricdisk-magic.sh but its much better.
44
- Idea di base: trovare tutti le directory con file ricdisk e da esso estrarre info e magari METTERE info.
45
- Il tutto condito con un bel protobuf e un'entita che metto in Lib.
46
- ".strip.gsub(/^\s+/, "").gsub(/\s+$/, ""),
47
- # TODO move to some class default
48
- :media_dirs => %w{ /media/riccardo/ /Volumes/ /mnt/ ~/git/storazzo/test/ /sobenme/giusto/per/imparare/ad/ammutolire/gli/errori/ },
49
- :mount_types => %w{ vfat ntfs },
50
- }
51
- $stats_file = "ricdisk_stats_v11.rds"
52
- $gcs_bucket = 'palladius'
53
-
54
- # This template from scripta.rb. from 2.1.0 removed aby ric gem dependency.
55
- # 2022-04-26 2.1.1 Added more colors
56
- # 2022-04-26 2.1.0 Historical momemnt: removed gem 'ric' dependency
57
- $TEMPLATE_VER = '2.1.1'
58
-
59
- # copied from https://dev.to/ayushn21/how-to-generate-yaml-from-ruby-objects-without-type-annotations-4fli
60
- module Hashify
61
- # Classes that include this module can exclude certain
62
- # instance variable from its hash representation by overriding
63
- # this method
64
- def ivars_excluded_from_hash
65
- [ 'this_doesnt_exist' ]
66
- end
67
-
68
- def to_hash
69
- hash = {}
70
- excluded_ivars = ivars_excluded_from_hash
71
-
72
- # Iterate over all the instance variables and store their
73
- # names and values in a hash
74
- instance_variables.each do |var|
75
- next if excluded_ivars.include? var.to_s
76
-
77
- value = instance_variable_get(var)
78
- value = value.map(&:to_hash) if value.is_a? Array
79
-
80
- hash[var.to_s.delete("@")] = value
81
- end
82
-
83
- return hash
84
- end
85
-
86
- def to_yaml
87
- to_hash.to_yaml
88
- end
89
- end
90
-
91
- # TODO move to lib/ric_disk/
92
- class RicDisk
93
- VERSION = "1.0"
94
- include Hashify
95
- extend Storazzo::Colors
96
-
97
-
98
- # todo substitute wqith protobuf..
99
- attr_accessor :name, :description, :ricdisk_file, :local_mountpoint, :wr
100
-
101
- def self.interesting_mount_points(opts={})
102
- #https://unix.stackexchange.com/questions/177014/showing-only-interesting-mount-points-filtering-non-interesting-types
103
- `mount | grep -Ev 'type (proc|sysfs|tmpfs|devpts|debugfs|rpc_pipefs|nfsd|securityfs|fusectl|devtmpfs) '`.split(/\n+/)
104
- end
105
-
106
- def initialize(path, ricdisk_file)
107
- @local_mountpoint = path
108
- @description = "This is an automated RicDisk description from v.#{VERSION}. Riccardo feel free to edit away with characteristicshs of this device.. Created on #{Time.now}'"
109
- @ricdisk_version = VERSION
110
- @ricdisk_file = ricdisk_file
111
- #@questo_non_esiste = :sobenme
112
- @label = path.split("/").last
113
- @name = path.split("/").last
114
- @wr = File.writable?("#{path}/#{ricdisk_file}" ) # .writeable?
115
- @tags = 'ricdisk'
116
- find_info_from_mount(path)
117
- find_info_from_df()
118
- end
119
-
120
- def ricdisk_absolute_path
121
- @local_mountpoint + "/" + @ricdisk_file
122
- end
123
-
124
- def add_tag(tag)
125
- @tags += ", #{tag}"
126
- end
127
-
128
- # might have other things in the future...
129
- def find_info_from_mount(path)
130
- mount_table_lines = interesting_mount_points()
131
- mount_line = nil
132
- mount_table_lines.each do |line|
133
- next if line =~ /^map /
134
- dev, on, mount_path, mode = line.split(/ /)
135
- if mount_path==path
136
- mount_line = line
137
- else
138
- @info_from_mount = false
139
- end
140
- end
141
- @info_from_mount = ! (mount_line.nil?)
142
- if @info_from_mount
143
- #@mount_line = mount_line
144
- @description += "\nMount line:\n" + mount_line
145
- @remote_mountpoint = mount_line.split(/ /)[0]
146
- @fstype = mount_line.split(/ /)[3].gsub(/[\(,]/, '')
147
- add_tag(:synology) if @remote_mountpoint.match('1.0.1.10')
148
- end
149
- end
150
-
151
- def find_info_from_df()
152
- path = @local_mountpoint
153
- df_info = `df -h "#{path}"`
154
- @df_info = df_info
155
- lines = df_info.split(/\n+/)
156
- raise "I need exactly TWO lines! Or no info is served here..." unless lines.size == 2
157
- mount, @size_readable, used_size, avail_size, @disk_utilization, other = lines[1].split(/\s+/) # second line..
158
- end
159
-
160
-
161
-
162
- def self.sbrodola_ricdisk(subdir)
163
- # given a path, if .ricdisk exists i do stuff with it..
164
- disk_info = nil
165
- unless self.ok_dir?(subdir)
166
- puts("Nothing for me here. Existing")
167
- return
168
- end
169
- if File.exists?( "#{subdir}/.ricdisk") and File.empty?( "#{subdir}/.ricdisk")
170
- deb("Interesting1. Empty file! Now I write YAML with it.")
171
- disk_info = RicDisk.new(subdir, '.ricdisk')
172
- #puts(x)
173
- #puts(yellow x.to_yaml)
174
- end
175
- if File.exists?( "#{subdir}/.ricdisk.yaml") and File.empty?( "#{subdir}/.ricdisk.yaml")
176
- deb("Interesting2. Empty file! TODO write YAML with it.")
177
- disk_info = RicDisk.new(subdir, '.ricdisk.yaml')
178
- # todo write
179
- #puts(x)
180
- puts(yellow disk_info.to_yaml)
181
- end
182
- if disk_info
183
- if File.empty?(disk_info.ricdisk_absolute_path) and (disk_info.wr)
184
- 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.."))
185
- ret = File.write(disk_info.ricdisk_absolute_path, disk_info.to_yaml)
186
- puts("Written file! ret=#{ret}")
187
- else
188
- puts(red("Nope, qualcosa non va.. #{File.empty?(disk_info.ricdisk_absolute_path)}"))
189
- puts("File size: #{File.size(disk_info.ricdisk_absolute_path)}")
190
- end
191
- end
192
- if File.exists?( "#{subdir}/.ricdisk") and ! File.empty?( "#{subdir}/.ricdisk")
193
- puts("Config File found with old-style name: '#{subdir}/.ricdisk' !")
194
- #puts(white `cat "#{subdir}/.ricdisk"`)
195
- end
196
- end
197
-
198
- # separiamo cosi usiamo meglio...
199
- def self.ok_dir?(subdir)
200
- File.exists?( "#{subdir}/.ricdisk") or File.exists?( "#{subdir}/.ricdisk.yaml")
201
- end
202
-
203
- def self.obsolescence_seconds file_path
204
- creation_time = File.stat(file_path).ctime
205
- deb("[obsolescence_seconds] File #{file_path}: #{creation_time} - #{(Time.now - creation_time)} seconds ago")
206
- (Time.now - creation_time).to_i
207
- end
208
- def self.obsolescence_days(file_path)
209
- return obsolescence_seconds(file_path) / 86400
210
- end
211
-
212
- def self.backquote_execute(cmd)
213
- # executed a command wrapped by dryrun though
214
- return "DRYRUN backquote_execute(#{cmd})" if $opts[:dryrun]
215
- `#{cmd}`
216
- end
217
-
218
- def self.upload_to_gcs(file, opts={})
219
- deb("upload_to_gcs(#{file}). TODO(ricc) after breafast upload to GCS : #{file}")
220
- mount_name = file.split('/')[-2]
221
- filename = "#{mount_name}-#{File.basename file}"
222
- hostname = Socket.gethostname[/^[^.]+/]
223
- command = "gsutil cp '#{file}' gs://#{$gcs_bucket}/backup/ricdisk-magic/#{ hostname }-#{filename}"
224
- deb("Command: #{command}")
225
- ret = backquote_execute(command)
226
- # if $opts[:debug] do
227
- # puts "+ Current list of files:"
228
- # ret = backquote_execute("gsutil ls -al gs://#{$gcs_bucket}/backup/ricdisk-magic/")
229
- # puts ret
230
- # end
231
- ret
232
- end
233
-
234
- # Create RDS file.
235
- def self.calculate_stats_files(dir, opts={})
236
- opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
237
- full_file_path = "#{dir}/#{$stats_file}"
238
-
239
- puts("calculate_stats_files(#{white dir}): #{white full_file_path}")
240
- puts "TEST1 DIR EXISTS: #{dir} -> #{Dir.directory? dir}"
241
- Dir.chdir(dir)
242
- if File.exists?(full_file_path) and ($opts[:force] == false)
243
- puts "File '#{$stats_file}' exists already." # - now should see if its too old, like more than 1 week old"
244
- # TODO check for file time...
245
- print "Lines found: #{yellow `wc -l "#{full_file_path}" `.chomp }. File obsolescence (days): #{yellow obsolescence_days(full_file_path)}."
246
- if obsolescence_days(full_file_path) > 7
247
- puts("*** ACHTUNG *** FIle is pretty old. You might consider rotating: #{yellow "mv #{full_file_path} #{full_file_path}_old"}. Or invoke with --force")
248
- end
249
- upload_to_gcs(full_file_path) if opts_upload_to_gcs
250
- else
251
- puts "Crunching data stats from '#{dir}' into '#{$stats_file}' ... please bear with me.. [maybe file didnt exist, maybe $opts[:force] is true]"
252
- command = "find . -print0 | xargs -0 stats-with-md5.rb --no-color | tee '#{full_file_path}'"
253
- puts("[#{`pwd`.chomp}] Executing: #{azure command}")
254
- ret = backquote_execute command
255
- puts "Done. #{ret.split("\n").count} files processed."
256
- end
257
- end
258
-
259
- def self.find_active_dirs(base_dirs=nil, also_mountpoints=true)
260
- base_dirs = $myconf[:media_dirs] if base_dirs.nil?
261
- active_dirs = []
262
- base_dirs.each do |ugly_dir|
263
- # 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
264
- dir = File.expand_path(ugly_dir)
265
- begin
266
- x=[]
267
- # puts "TEST2 DIR EXISTS: #{dir} -> #{Dir.exists?(dir)}"
268
- unless Dir.exists?(dir)
269
- deb "Dir doesnt exist, skipping: #{dir}"
270
- next
271
- end
272
- Dir.chdir(dir)
273
- x = Dir.glob('*').select {|f| File.directory? f}
274
- subdirs = x.map{|subdir| "#{dir}#{subdir}"}
275
- subdirs.each{|subdir|
276
- #puts `ls -al "#{subdir}"`
277
- active_dirs << subdir if self.ok_dir?(subdir)
278
- }
279
- #puts(white x)
280
- rescue Exception => e # optionally: `rescue Exception => ex`
281
- puts "Exception: '#{e}'"
282
- ensure # will always get executed
283
- #deb 'Always gets executed.'
284
- #x = []
285
- end
286
- end
287
-
288
- if also_mountpoints
289
- =begin
290
- Example output from mount:
291
-
292
- devfs on /dev (devfs, local, nobrowse)
293
- /dev/disk3s6 on /System/Volumes/VM (apfs, local, noexec, journaled, noatime, nobrowse)
294
- /dev/disk3s2 on /System/Volumes/Preboot (apfs, local, journaled, nobrowse)
295
- /dev/disk3s4 on /System/Volumes/Update (apfs, local, journaled, nobrowse)
296
- /dev/disk1s2 on /System/Volumes/xarts (apfs, local, noexec, journaled, noatime, nobrowse)
297
- /dev/disk1s1 on /System/Volumes/iSCPreboot (apfs, local, journaled, nobrowse)
298
- /dev/disk1s3 on /System/Volumes/Hardware (apfs, local, journaled, nobrowse)
299
- /dev/disk3s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse, protect)
300
- map auto_home on /System/Volumes/Data/home (autofs, automounted, nobrowse)
301
- //riccardo@1.0.1.10/video on /Volumes/video (afpfs, nodev, nosuid, mounted by ricc)
302
- //riccardo@1.0.1.10/photo on /Volumes/photo (afpfs, nodev, nosuid, mounted by ricc)
303
- =end
304
- # add directories from current mountpoints...
305
- mount_table_lines = interesting_mount_points()
306
- mount_table_lines.each{|line|
307
- next if line =~ /^map /
308
- dev, on, path, mode = line.split(/ /)
309
- #puts line
310
- #deb yellow(path)
311
- active_dirs << path if self.ok_dir?(path)
312
- }
313
- end
314
- active_dirs.uniq!
315
- puts("find_active_dirs(): found dirs " + green(active_dirs))
316
- return active_dirs
317
- end
318
-
319
-
320
- end
321
-
322
- def usage(comment=nil)
323
- puts white($optparse.banner)
324
- puts($optparse.summarize)
325
- puts("Description: " + gray($myconf[:description]))
326
- puts red(comment) if comment
327
- #puts "Description: #{ $myconf[:description] }"
328
- exit 13
329
- end
330
-
331
- # include it in main if you want a custome one
332
- def init() # see lib_autoinit in lib/util.rb
333
- $opts = {}
334
- # setting defaults
335
- $opts[:verbose] = false
336
- $opts[:dryrun] = false
337
- $opts[:debug] = false
338
- $opts[:force] = false
339
-
340
- $optparse = OptionParser.new do |opts|
341
- opts.banner = "#{$0} v.#{$PROG_VER}\n Usage: #{File.basename $0} [options] file1 file2 ..."
342
- opts.on( '-d', '--debug', 'enables debug (DFLT=false)' ) { $opts[:debug] = true ; $DEBUG = true }
343
- opts.on( '-f', '--force', 'force stuff (DFLT=false)' ) { $opts[:force] = true }
344
- opts.on( '-h', '--help', 'Display this screen' ) { usage }
345
- #opts.on( '-j', '--jabba', 'Activates my Jabber powerful CLI' ) { $opts[:jabba] = true }
346
- opts.on( '-n', '--dryrun', "Don't really execute code" ) { $opts[:dryrun] = true }
347
- opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) {|file| $opts[:logfile] = file }
348
- opts.on( '-v', '--verbose', 'Output more information' ) { $opts[:verbose] = true}
349
- end
350
- $optparse.parse!
351
- end
352
-
353
- def real_program
354
- deb("Hello world from a templated '#{yellow $0 }'")
355
- deb "+ Options are: #{gray $opts}"
356
- deb "+ Depured args: #{azure ARGV}"
357
- deb "+ Script-specifig super-cool conf: #{green $prog_conf_d}"
358
- deb "+ Your configuration: #{purple $myconf.inspect}"
359
-
360
- # Your code goes here...
361
- puts white("Hello world from #{$myconf[:app_name]}!")
362
- puts "Description: '''#{white $myconf[:description] }'''"
363
-
364
- if ARGV == [] # empty -> ALL
365
- dirs = RicDisk.find_active_dirs()
366
- dirs.each {|dir|
367
- RicDisk.sbrodola_ricdisk(dir)
368
- RicDisk.calculate_stats_files(dir) # dir is inutile
369
- } # TODO refactor in option sbrodola_afterwards=true. :)
370
- else
371
- deb "I consider ARGV come la lista di directories da parsare :)"
372
- dirs = RicDisk.find_active_dirs()
373
- ARGV.each{ |dir|
374
- dir = File.expand_path(dir)
375
- if dirs.include?(dir)
376
- deb "Legit dir: #{green dir}"
377
- RicDisk.sbrodola_ricdisk(dir)
378
- RicDisk.calculate_stats_files(dir) # dir is inutile
379
- else
380
- deb "Figghiu ri buttana: doesnt exist #{red dir}"
381
- end
382
- }
383
- end
384
- end
385
-
386
- def main(filename)
387
- deb "I'm called by #{white filename}"
388
- deb "HISTORY: #{gray HISTORY}"
389
- #deb "To remove this shit, just set $DEBUG=false :)"
390
- init # Enable this to have command line parsing capabilities!
391
- #warn "[warn] template v#{$TEMPLATE_VER }: proviamo il warn che magari depreca il DEB"
392
- real_program
393
- end
394
-
395
- main(__FILE__)