Class: SphinxTv

Inherits:
Object
  • Object
show all
Defined in:
lib/sphinx_tv.rb,
lib/sphinx_tv/version.rb

Constant Summary collapse

SUDO_PROMPT =
"-p \"#{"Your administrator password is required to peform this step: ".yellow}\""
CONFIG_DIRECTORY =
File.join(Etc.getpwuid.dir, ".sphinx_tv")
MODULES =
["MySQL", "MythTv", "Shepherd"]
MODULE_DEFAULTS =
{
    "MySQL" => {
        :active => true,
        :setup => false,
        :configure => true,
    },
    "MythTv" => {
        :active => true,
        :setup => false,
        :configure => true,
        :depends => ["MySQL"],
    },
    "Shepherd" => {
        :active => false,
        :setup => false,
        :configure => true,
        :depends => ["MythTv"],
    },
}
VERSION =
"0.9.5"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSphinxTv

Returns a new instance of SphinxTv.



36
37
38
39
40
41
# File 'lib/sphinx_tv.rb', line 36

def initialize
  @config = {
      :version => SphinxTv::VERSION,
      :modules => MODULE_DEFAULTS
  }
end

Class Method Details

.cache_path(file = "") ⇒ Object



57
58
59
60
61
# File 'lib/sphinx_tv.rb', line 57

def self.cache_path(file = "")
  cache_directory = File.join(CONFIG_DIRECTORY, "cache")
  Dir.mkdir(cache_directory) unless File.exists? cache_directory
  return File.join(cache_directory, file)
end

.copy_template(source, dest, b) ⇒ Object



63
64
65
66
67
68
# File 'lib/sphinx_tv.rb', line 63

def self.copy_template(source, dest, b)
  t = ERB.new File.new(source).read, nil, "%"
  outfile = File.open(dest, "w")
  outfile.write(t.result(b || binding))
  outfile.close
end

.download_path(file = "") ⇒ Object



51
52
53
54
55
# File 'lib/sphinx_tv.rb', line 51

def self.download_path(file = "")
  download_directory = File.join(CONFIG_DIRECTORY, "downloads")
  Dir.mkdir(download_directory) unless File.exists? download_directory
  return File.join(download_directory, file)
end

.get_password_with_confirmation(prompt = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/sphinx_tv.rb', line 70

def self.get_password_with_confirmation(prompt = nil)
  pass = ask(prompt || "Enter a password: ") { |q| q.echo = "*" }
  pass_confirm = ask("Re-enter the password: ") { |q| q.echo = "*" }
  if pass != pass_confirm
    puts "Passwords do not match!".red
    return nil
  end
  pass
end

.resources_path(file = "") ⇒ Object



47
48
49
# File 'lib/sphinx_tv.rb', line 47

def self.resources_path(file = "")
  File.join(root_directory, "resources", file)
end

.root_directoryObject



43
44
45
# File 'lib/sphinx_tv.rb', line 43

def self.root_directory
  File.expand_path '../..', __FILE__
end

Instance Method Details

#load_configurationObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/sphinx_tv.rb', line 80

def load_configuration
  config_file = File.join(CONFIG_DIRECTORY, "sphinx.yml")
  unless File.exists? config_file
    puts "Configuration file does not exist. Using defaults.".yellow
    return false
  end
  c = YAML::load_file config_file
  @config.merge! c
  return true
end

#module_menu(action, title) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sphinx_tv.rb', line 130

def module_menu(action, title)
  exit = false
  until exit do
    choose do |menu|
      menu.header = "\n#{title}".cyan
      menu.prompt = "Select a module: "
      MODULES.each do |m|
        options = @config[:modules][m]
        if (options[action])
          menu.choice(m) do |m|
            require File.join("modules", m)
            mod = eval("#{m}.new")
            mod.send(action)
          end
        end
      end
      menu.choice(:done) {
        exit = true
      }
    end
  end
end

#module_selectObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sphinx_tv.rb', line 97

def module_select
  exit = false
  until exit do
    choose do |menu|
      menu.header = "\nSelect Modules".cyan
      menu.prompt = "Select optional modules to toggle: "
      MODULES.each do |m|
        options = @config[:modules][m]
        menu.choice("#{m}: " + (options[:active] ? "On".green : "Off".red)) do |choice|
          m = /^([^:]*)/.match(choice)[0]
          toggle_module m
        end
      end
      menu.choice(:done) {
        exit = true
      }
    end
  end
end

#runObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/sphinx_tv.rb', line 185

def run
  Dir.mkdir(CONFIG_DIRECTORY) unless File.exists? CONFIG_DIRECTORY
  @no_config = load_configuration

  action = nil

  optparse = OptionParser.new do |opts|
    # Set a banner, displayed at the top
    # of the help screen.
    opts.banner = "Usage: sphinx [options] action"

    # This displays the help screen, all programs are
    # assumed to have this option.
    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end

  # Parse the command-line. Remember there are two forms
  # of the parse method. The 'parse' method simply parses
  # ARGV, while the 'parse!' method parses ARGV and removes
  # any options found there, as well as any parameters for
  # the options. What's left is the list of files to resize.
  optparse.parse!
  # Set the selected modules unless it was overridden

  action = ARGV[0]
  case action
    when "setup"
      setup
    when "configure"
      module_menu(:configure, "Configure Modules")
    when "check"
      run_selected_modules { |m| m.check }
    when "install"
      run_selected_modules { |m| m.install }
    when "uninstall"
      run_selected_modules { |m| m.uninstall }
    when "download"
      run_selected_modules { |m| m.download }
    when "update"
      run_selected_modules { |m| m.update }
    else
      puts "Use the --help switch if you need a list of valid actions"
  end
end

#run_selected_modules(&block) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/sphinx_tv.rb', line 153

def run_selected_modules &block
  MODULES.each do |m|
    if @config[:modules][m][:active]
      require File.join("modules", m)
      mod = eval("#{m}.new")
      yield mod
    end
  end
end

#save_configurationObject



91
92
93
94
95
# File 'lib/sphinx_tv.rb', line 91

def save_configuration
  File.open(File.join(CONFIG_DIRECTORY, "sphinx.yml"), "w") do |file|
    file.write @config.to_yaml
  end
end

#setupObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sphinx_tv.rb', line 163

def setup
  exit = false
  until exit do
    choose do |menu|
      menu.header = "\nSphinx Installer".cyan
      menu.prompt = "Select an option: "

      menu.choice("Select Modules") { module_select }
      menu.choice("Setup Modules") { module_menu(:setup, "Module Setup") }
      menu.choice("Save Configuration") {
        save_configuration
        puts "Configuration Saved.".green
        exit = true
      }
      menu.choice("Cancel") {
        puts "Exiting without saving configuration.".red
        exit = true
      }
    end
  end
end

#toggle_module(m) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sphinx_tv.rb', line 117

def toggle_module(m)
  options = @config[:modules][m]

  options[:active] = !options[:active]
  if (options[:active])
    if (options[:depends])
      options[:depends].each do |d|
        toggle_module d unless @config[:modules][d][:active]
      end
    end
  end
end