storazzo 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63792a40e1f0416183c8522847f67b89514b5f5a64c9d7532fd1887de690ea68
4
- data.tar.gz: c859febfb0a88b7cbe160828c350e31e715ebac5af6901715b5d94fcd247ead6
3
+ metadata.gz: 0c1d447526136c87cfefa48b69232971dd9e6df07a07ac1410eede18d0fa6b5e
4
+ data.tar.gz: c295db76797061749062a49f01b0bf08132ff86b5c48f61b0549d10bbe8066e0
5
5
  SHA512:
6
- metadata.gz: 9fc27b51ba17693dbbbc030383f3c5ae5c17655c3e9bd78a53d997cfd33964f59769e5ea1873e8040a18059f4b01089ea99b5e2e84284bb5cf403f873197edfe
7
- data.tar.gz: 69d1ebeef668df51940f913b7ef0f8c47bf4ef584a0856cf9c9d0b78ad9fc17b9f6d679305d2fba24c1d555c0a4e827ddc3561cae2e2ae9b204b40d61eb7078b
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,10 +1,16 @@
1
- # TODO
1
+ # Use EXTEND vs INCLUDE and magically the Class will inherit instead of instance. Magical! :)
2
+ # http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
3
+
4
+ module Storazzo
5
+ # needs to be defined before
6
+ end
2
7
 
3
8
  module Storazzo::Colors
4
- # class Storazzo::Colors
9
+ #class Storazzo::Colors1
5
10
  PREPEND_ME = "[Storazzo::Colors] "
6
11
 
7
- def deb(s); puts "#DEB #{s}" if $DEBUG; end
12
+ def deb(s); puts "#DEB #{gray(s)}" if $DEBUG; end
13
+
8
14
  # colors 16
9
15
  def yellow(s) "\033[1;33m#{s}\033[0m" ; end
10
16
  def gray(s) "\033[1;30m#{s}\033[0m" ; end
@@ -20,15 +26,15 @@ module Storazzo::Colors
20
26
 
21
27
  # i dont undertstand why i need self :/
22
28
  # SELF version because I'm just stupid or lazy or both.
23
- def self.yellow(s) "#{PREPEND_ME}\033[1;33m#{s}\033[0m" ; end
24
- def self.green(s) "#{PREPEND_ME}\033[1;32m#{s}\033[0m" ; end
25
- def self.gray(s) "#{PREPEND_ME}\033[1;30m#{s}\033[0m" ; end
26
- def self.green(s) "#{PREPEND_ME}\033[1;32m#{s}\033[0m" ; end
27
- def self.red(s) "#{PREPEND_ME}\033[1;31m#{s}\033[0m" ; end
28
- def self.blue(s) "#{PREPEND_ME}\033[1;34m#{s}\033[0m" ; end
29
- def self.purple(s) "#{PREPEND_ME}\033[1;35m#{s}\033[0m" ; end
30
- def self.azure(s) "#{PREPEND_ME}\033[1;36m#{s}\033[0m" ; end
31
- def self.white(s) "#{PREPEND_ME}\033[1;37m#{s}\033[0m" ; end
29
+ # def self.yellow(s) "#{PREPEND_ME}\033[1;33m#{s}\033[0m" ; end
30
+ # def self.green(s) "#{PREPEND_ME}\033[1;32m#{s}\033[0m" ; end
31
+ # def self.gray(s) "#{PREPEND_ME}\033[1;30m#{s}\033[0m" ; end
32
+ # def self.green(s) "#{PREPEND_ME}\033[1;32m#{s}\033[0m" ; end
33
+ # def self.red(s) "#{PREPEND_ME}\033[1;31m#{s}\033[0m" ; end
34
+ # def self.blue(s) "#{PREPEND_ME}\033[1;34m#{s}\033[0m" ; end
35
+ # def self.purple(s) "#{PREPEND_ME}\033[1;35m#{s}\033[0m" ; end
36
+ # def self.azure(s) "#{PREPEND_ME}\033[1;36m#{s}\033[0m" ; end
37
+ # def self.white(s) "#{PREPEND_ME}\033[1;37m#{s}\033[0m" ; end
32
38
 
33
39
  # p<COLOR> Carlessian functions..
34
40
  def pwhite(s) puts(white(s)); end
@@ -36,5 +42,5 @@ module Storazzo::Colors
36
42
  def pred(s) puts(red(s)); end
37
43
  def pyellow(s) puts(yellow(s)); end
38
44
 
39
-
40
- 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,39 +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
- #include Storazzo::Colors
25
- extend Storazzo::Colors
26
-
27
- pwhite "All tests BEGIN"
28
- #puts "This is Storazzo v#{StorazzoMod::VERSION}"
29
- hi
30
- puts(yellow "Just YELLOW 0")
31
- #puts(Storazzo::Colors.yellow "Test YELLOW 1 self")
32
- puts(Colors.yellow "Test YELLOW 1 self")
33
- puts(Colors.green "Test YELLOW 2 ohne self")
34
- pwhite "All tests END"
35
- #puts "All tests END"
18
+ def self.version
19
+ File.read('./VERSION').chomp # "10.0.0"
36
20
  end
37
- end
21
+ end
38
22
 
23
+ require 'storazzo/colors'
24
+ require 'storazzo/hashify'
25
+ require 'storazzo/ric_disk'
26
+ require 'storazzo/main'
39
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,24 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storazzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Riccardo Carlesso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-10 00:00:00.000000000 Z
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple hello world gem.. for now. Then... more!
13
+ description: A simple gem to manage your external hard drives and extract MD5 and
14
+ common stuff from them.
14
15
  email: name dot surname at popular Google-owned Mail
15
- executables: []
16
+ executables:
17
+ - ricdisk-magic
16
18
  extensions: []
17
19
  extra_rdoc_files: []
18
20
  files:
21
+ - Gemfile
22
+ - LICENSE
23
+ - Makefile
24
+ - README.md
25
+ - Rakefile
26
+ - VERSION
27
+ - bin/ricdisk-magic
19
28
  - lib/storazzo.rb
20
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
21
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
22
42
  homepage: https://rubygems.org/gems/storazzo
23
43
  licenses:
24
44
  - MIT
@@ -42,4 +62,8 @@ rubygems_version: 3.1.4
42
62
  signing_key:
43
63
  specification_version: 4
44
64
  summary: storazzo is an amazing gem. Code is in https://github.com/palladius/storazzo
45
- 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