Module: Himawari::OsUtils

Included in:
Download
Defined in:
lib/himawari/os_utils.rb

Overview

all the misc. functions for dealing with CLI and/or OS…

Class Method Summary collapse

Class Method Details

.crontab(cmd, action) ⇒ Object

to silence the status mails, add MAILTO=” at the top of the crontab manually Sets or clears the crontab!

Parameters:

  • cmd (String)

    the himawari script + all the passed args for it

  • action (Symbol)

    what to do? :set the cron? or :clear the cron?

Returns:

  • nothing useful



140
141
142
143
144
145
146
# File 'lib/himawari/os_utils.rb', line 140

def self.crontab(cmd, action)
  if action == :set
    `(crontab -l ; echo \"#{cmd}\") 2>&1 | grep -v \"no crontab\" | sort | uniq | crontab -`
  else
    `(crontab -l ; echo \"#{cmd}\") 2>&1 | grep -v \"no crontab\" | grep -v himawari | sort | uniq | crontab -`
  end
end

.osSymbol

we gotta know what the user’s OS is! (because we rely on command line utils to process images)

Returns:

  • (Symbol)

    representing the OS being used



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/himawari/os_utils.rb', line 8

def self.os
  case RUBY_PLATFORM
  when /win32/
    :win
  when /linux/
    :linux
  when /darwin/
    :mac
  when /freebsd/
    :freebsd
  else
    :unknown
  end
end

.parse_cli_argsHash

rubocop:disable Metrics/MethodLength, Metrics/BlockLength This is just a list of args that the CLI can accept

Returns:

  • (Hash)

    of cleaned/verified params



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/himawari/os_utils.rb', line 26

def self.parse_cli_args
  params = {}

  OptionParser.new do |opts|
    opts.banner = 'Usage: himawari [params]'

    opts.on('-f', '--focus STRING', String, 'Which section of the planet to focus on? ' \
                                            'Valid values are `full`, `top`, `mid`, `low`. Default is `top`.') do |o|
      params[:focus] = o.to_sym if %w[full top mid low].include? o
    end

    opts.on('-m', '--mode STRING', String, 'Valid values are `day` (cycles pics in the `destination` folder from the' \
                                           'most recent day) or `live` (copies the latest photo downloaded ' \
                                           'to `destination`) Default is `day`.') do |o|
      params[:mode] = :live if o == 'live'
    end

    opts.on('-r', '--resolution INT', Integer, 'Adjust the resolution of the downloaded image. Valid numbers are ' \
                                               '2, 4, 8, 16, 20. 20 is the highest resolution and 2 is the default. ' \
                                               'For a 4k-monitor a setting of 4 seems sufficient.') do |o|
      params[:resolution] = o if o <= 20 && o.positive? && o.even?
    end

    opts.on('-d', '--destination PATH', String, 'The folder where to copy a background image. If left blank, images will ' \
                                                'just be downloaded, but won\'t be copied anywhere afterward.') do |o|
      params[:destination] = o if File.directory?(o)
    end

    opts.on('-w', '--workdir PATH', String, 'The folder where to save all the downloaded pics. If left blank, ' \
                                            'images will be saved to the `./data` directory relative to your ' \
                                            'current path of working dir.') do |o|
      params[:workdir] = o if File.directory?(o)
    end

    opts.on('-b', '--blacklist STRING,STRING...', Array, 'Blacklist SSIDs of networks from which we do not want to go ' \
                                                         'online to download new images.') do |o|
      params[:blacklist] = o
    end

    opts.on('-c', '--cron STRING', String, 'Can `set`/`clear` cron with the specified params, so we can update the images ' \
                                           'automatically') do |o|
      params[:cron] = o.to_sym if %w[set clear].include? o
    end

    opts.on('-v', '--verbose', 'Increase verbosity: mostly for debugging') do |o|
      params[:verbose] = o
    end

    opts.on('-s', '--schedule', 'Flag for determining when the script is run by schedule/automatically.') do |o|
      params[:by_schedule] = o
    end

    opts.on('-h', '--help', 'Prints this help & exits') do
      puts opts
      exit
    end
  end.parse! # (into: params)

  params
end

.scriptify_sys(script, command) ⇒ Object

Ok, this is a weird method. On Mac, ‘script` just ran normally through ruby. But, on linux… It is refusing to interpolate variables within the `script` string properly. So, we’ll make a temporary bash script, execute it, and then delete it.

Parameters:

  • script (String)

    the temp script’s filename

  • command (String)

    what we actually want to be done… (probably process pics in parallel)

Returns:

  • nothing useful (true on success)



94
95
96
97
98
# File 'lib/himawari/os_utils.rb', line 94

def self.scriptify_sys(script, command)
  `echo "#!/bin/bash\n#{command}" > #{script}`
  `chmod +x #{script} && #{script}`
  `rm #{script}`
end

.tenmin(from = 0, to = 59) ⇒ String

tiny helper method. converts normal minutes into the closest (floored) round ten-minutes. (ie like 10, 20, 30, 40, 50). Himawari website uses these in the tile filenames…

Parameters:

  • from (Integer) (defaults to: 0)
  • to (Integer) (defaults to: 59)

    these should be pretty self-explanatory. should be positive and w/in 0-59 range.

Returns:

  • (String)

    the formatted minute range string for ‘parallel` to use to download this range of pics



105
106
107
# File 'lib/himawari/os_utils.rb', line 105

def self.tenmin(from = 0, to = 59)
  "{#{from / 10}..#{to / 10}}"
end