Module: Zillabyte::Helpers

Extended by:
Helpers
Included in:
API::Base, Auth, Command::Base, Helpers
Defined in:
lib/zillabyte/helpers.rb,
lib/zillabyte/cli/helpers/table_output_builder.rb

Defined Under Namespace

Classes: DataSchemaBuilder, TableOutputBuilder

Instance Method Summary collapse

Instance Method Details

#appObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zillabyte/helpers.rb', line 77

def app
  @app ||= if options[:confirm].is_a?(String)
    if options[:app] && (options[:app] != options[:confirm])
      error("Mismatch between --app and --confirm")
    end
    options[:confirm]
  elsif options[:app].is_a?(String)
    options[:app]
  elsif ENV.has_key?('ZILLABYTE_APP')
    ENV['ZILLABYTE_APP']
  elsif app_from_dir = extract_app_in_dir(Dir.pwd)
    app_from_dir
  else
    # raise instead of using error command to enable rescuing when app is optional
    raise Zillabyte::Command::CommandFailed.new("No app specified.\nRun this command from an app folder or specify which app to use with --app APP.") unless options[:ignore_no_app]
  end
end

#askObject



40
41
42
# File 'lib/zillabyte/helpers.rb', line 40

def ask
  $stdin.gets.to_s.strip
end

#command(arg = "--execute_live", type = nil, dir = Dir.pwd, ignore_stderr = false, options = {}) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/zillabyte/helpers.rb', line 292

def command(arg="--execute_live", type = nil, dir = Dir.pwd, ignore_stderr = false, options = {})
  require("zillabyte/cli/config")
  meta = Zillabyte::CLI::Config.get_config_info(dir, self, options)

#meta = Zillabyte::API::Functions.get_rich_meta_info_from_script(dir, self)
  error("could not extract meta information. missing zillabyte.conf.yml?", type) if meta.nil?
  error(meta["error_message"], type) if meta['status'] == "error"
  full_script = File.join(dir, meta["script"])
  stderr_opt = "2> /dev/null" if ignore_stderr

  case meta["language"]
  when "ruby"
  # Execute in the bundler context
    cmd = "cd \"#{dir}\"; unset BUNDLE_GEMFILE; ZILLABYTE_HARNESS=1 bundle exec ruby \"#{full_script}\" #{arg}  #{stderr_opt}"

  when "python"#{
    if(File.directory?("#{dir}/vEnv"))
      cmd = "cd \"#{dir}\"; PYTHONPATH=~/zb1/multilang/python/Zillabyte #{dir}/vEnv/bin/python \"#{full_script}\" #{arg}  #{stderr_opt}"
    else
      cmd = "cd \"#{dir}\"; PYTHONPATH=~/zb1/multilang/python/Zillabyte python \"#{full_script}\" #{arg}  #{stderr_opt}"
    end

  when "js"
#      cmd = "#{Zillabyte::API::CASPERJS_BIN} #{Zillabyte::API::API_CLIENT_JS}  \"#{full_script}\" #{arg}"
    cmd = "cd \"#{dir}\"; NODE_PATH=~/zb1/multilang/js/src/lib #{Zillabyte::API::NODEJS_BIN}  \"#{full_script}\" #{arg} #{stderr_opt}"

  else
    error("no language specified", type)
  end

  return cmd

end

#create_git_remote(remote, url) ⇒ Object



68
69
70
71
72
73
# File 'lib/zillabyte/helpers.rb', line 68

def create_git_remote(remote, url)
  return if git('remote').split("\n").include?(remote)
  return unless File.exists?(".git")
  git "remote add #{remote} #{url}"
  display "git remote #{remote} added"
end

#display(msg = "", new_line = true) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/zillabyte/helpers.rb', line 7

def display(msg="", new_line=true)
  if new_line
   puts(msg)
  else
    print(msg)
    $stdout.flush
  end
end

#error(message, format = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/zillabyte/helpers.rb', line 17

def error(message, format=nil)
  if format == "json"
    $stdout.puts(({"error"=>message}).to_json)
    exit
  else
    $stderr.puts(format_with_bang(message))
    exit(1)
  end
end

#extract_app_from_git_configObject



114
115
116
117
# File 'lib/zillabyte/helpers.rb', line 114

def extract_app_from_git_config
  remote = git("config zillabyte.remote")
  remote == "" ? nil : remote
end

#extract_app_in_dir(dir) ⇒ Object



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

def extract_app_in_dir(dir)
  return unless remotes = git_remotes(dir)

  if remote = options[:remote]
    remotes[remote]
  elsif remote = extract_app_from_git_config
    remotes[remote]
  else
    apps = remotes.values.uniq
    if apps.size == 1
      apps.first
    else
      raise(Heroku::Command::CommandFailed, "Multiple apps in folder and no app specified.\nSpecify app with --app APP.") unless options[:ignore_no_app]
    end
  end
end

#format_with_bang(message) ⇒ Object



27
28
29
30
# File 'lib/zillabyte/helpers.rb', line 27

def format_with_bang(message)
  return '' if message.to_s.strip == ""
  " !    " + message.split("\n").join("\n !    ")
end

#friendly_dir(dir) ⇒ Object



327
328
329
330
# File 'lib/zillabyte/helpers.rb', line 327

def friendly_dir(dir)
  require 'pathname'
  Pathname.new(dir).relative_path_from(Pathname.new(Dir.pwd)).to_s
end


332
333
334
335
336
337
338
339
340
341
342
# File 'lib/zillabyte/helpers.rb', line 332

def get_flow_ui_link(res) 
  if res['id']
    if ENV['ZILLABYTE_API_HOST'] == "api.zillabyte.com"
      "http://app.zillabyte.com/flows/#{res['id']}"
    else
      "http://#{ENV['ZILLABYTE_API_HOST']}:#{ENV['ZILLABYTE_API_PORT']}/flows/#{res['id']}"
    end
  else
    "http://app.zillabyte.com/flows"
  end
end

#get_info(dir, options = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/zillabyte/helpers.rb', line 232

def get_info(dir, options = {})

  require 'socket'
  host = "0.0.0.0"
  type = options[:output_type]
  raw_lines = []
  server = TCPServer.new(0)
  port = server.addr[1]

  server_thread = Thread.new do 
  
    client = server.accept()
    loop do
      raw_line = client.gets()
      break if raw_line.nil?
      raw_lines << raw_line
    end
    client.close()
  end

  cmd = command("--info --host \"#{host}\" --port #{port}", type, dir)
  system(cmd)
  server_thread.join()

  if($?.exitstatus == 1)
    if options[:output_type].nil?
      exit(1)
    else
      Zillabyte::Helpers.error("error: #{response}", type)
    end
  end
  if (raw_lines.size == 0)
    if options[:output_type].nil?
      exit(1)
    else
      Zillabyte::Helpers.error("process did not emit any information", type)
    end
  end

  flow_info = {}
  raw_lines.each do |raw_line|
    line = JSON.parse(raw_line)
    if(line["class"] == "node")
      flow_info["nodes"] << line["info"]
    elsif(line["class"] == "arc")
      flow_info["arcs"] << line["info"]
    else
      flow_info = line
      flow_info["nodes"] = []
      flow_info["arcs"] = []
    end
  end

  flow_info

end

#get_rich_info(dir, session = nil, options = {}) ⇒ Object

Formerly: get_rich_meta_info_from_script. Combines the zilabyte.conf.yaml with ‘zillabyte info’



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/zillabyte/helpers.rb', line 217

def get_rich_info(dir, session = nil, options = {})
  require("zillabyte/cli/config")
  hash = Zillabyte::CLI::Config.get_config_info(dir, session, options)
  if hash.nil?
    return {"status" => "error", "error" => "invalid_directory", "error_message" => "The specified directory (#{dir}) does not appear to contain a valid Zillabyte configuration file."}
  end

  hash["flow_type"] = "app" if hash["flow_type"].nil? # default to app
  flow_info = get_info(dir, options)

  return hash.merge(flow_info)

end

#git(args) ⇒ Object



62
63
64
65
66
# File 'lib/zillabyte/helpers.rb', line 62

def git(args)
  return "" unless has_git?
  flattened_args = [args].flatten.compact.join(" ")
  %x{ git #{flattened_args} 2>&1 }.strip
end

#handle_downloading_manifest(file, res, type = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
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
# File 'lib/zillabyte/helpers.rb', line 172

def handle_downloading_manifest(file, res, type = nil)
  require("open-uri")
  if res["manifest"]
    num_parts = res["manifest"].size
    if res["decoder"].nil? or (type.nil? and res["decoder"] == "json")
      require("csv")
      file = "#{file}.csv" unless File.extname(file) == ".csv"
      display("writing to #{file}")

      f = CSV.open(file, "w")
      res["manifest"].each_with_index do |uri, index|
        display "\tdownloading part #{index}/#{num_parts}" unless type == "json"
        open(uri) do |gz|
          Zlib::GzipReader.new(gz).each_line do |line|
            hash = JSON.parse(line)
            values = []
            res["schema"].each do |col|
              values << hash[col.keys.first]
            end
            f << values
          end
        end
      end
      f.close
    elsif type == res["decoder"] or (type.nil? and res["decoder"] == "csv")
      file = "#{file}.gz" unless File.extname(file) == ".gz"
      display("writing to #{file}")

      f = File.open(file, "w")
      res["manifest"].each_with_index do |uri, index|
        display "\tdownloading part #{index}/#{num_parts}" unless type == "json"
        f.write open(uri).read
      end
    end
    
  else
    error "expected remote server to return file manifest"
  end
end

#has_git?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/zillabyte/helpers.rb', line 53

def has_git?
  begin
    %x{ git --version }
  rescue
    return false
  end
  $?.success?
end

#longest(items) ⇒ Object



32
33
34
# File 'lib/zillabyte/helpers.rb', line 32

def longest(items)
  items.map { |i| i.to_s.length }.sort.last
end

#read_multilineObject



36
37
38
# File 'lib/zillabyte/helpers.rb', line 36

def read_multiline
  $stdin.gets
end

#truncate_message(msg, t_length = 50) ⇒ Object

Format a message for display



164
165
166
167
168
169
# File 'lib/zillabyte/helpers.rb', line 164

def truncate_message(msg, t_length = 50)
  return msg if(!msg.instance_of?(String))
  m_length = msg.length
  msg_out = m_length > t_length ? msg[0..t_length-3]+"..." : msg
  msg_out
end

#version_okay?(current_version, min_version) ⇒ Boolean

Returns:

  • (Boolean)


344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/zillabyte/helpers.rb', line 344

def version_okay?(current_version, min_version)
  current_version = current_version.split(".").map {|v| v.to_i}
  minimum_version = min_version.split(".").map {|v| v.to_i}
  if current_version[0] > minimum_version[0]
    return true
  elsif current_version[0] == minimum_version[0]
    if current_version[1] > minimum_version[1]
      return true
    elsif current_version[1] == minimum_version[1]
      return true if current_version[2] >= minimum_version[2]
    end
  end
  return false
end

#with_tty(&block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/zillabyte/helpers.rb', line 44

def with_tty(&block)
  return unless $stdin.isatty
  begin
    yield
  rescue
    # fails on windows
  end
end