Class: Fig::OS

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/os.rb

Constant Summary collapse

SUCCESS =
0
NOT_MODIFIED =
3
NOT_FOUND =
4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login) ⇒ OS

Returns a new instance of OS.



18
19
20
21
22
# File 'lib/fig/os.rb', line 18

def initialize()
  @login = 
  @username = ENV["FIG_USERNAME"]
  @password = ENV["FIG_PASSWORD"]
end

Class Method Details

.java?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/fig/os.rb', line 306

def self.java?
  RUBY_PLATFORM == 'java'
end

.unix?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/fig/os.rb', line 310

def self.unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


302
303
304
# File 'lib/fig/os.rb', line 302

def self.windows?
  Config::CONFIG['host_os'] =~ /mswin|mingw/
end

Instance Method Details

#clear_directory(dir) ⇒ Object



235
236
237
238
# File 'lib/fig/os.rb', line 235

def clear_directory(dir)
  FileUtils.rm_rf(dir)
  FileUtils.mkdir_p(dir)
end

#copy(source, target) ⇒ Object



249
250
251
252
# File 'lib/fig/os.rb', line 249

def copy(source, target)
  FileUtils.mkdir_p(File.dirname(target))
  FileUtils.cp_r(source, target)
end

#create_archive(archive_name, files_to_archive) ⇒ Object

Expects files_to_archive as an Array of filenames.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/fig/os.rb', line 263

def create_archive(archive_name, files_to_archive)
  if OS.java?
    `tar czvf #{archive_name} #{files_to_archive.join(' ')}`
  else
    # TODO: Need to verify files_to_archive exists.
    ::Archive.write_open_filename(archive_name, ::Archive::COMPRESSION_GZIP, ::Archive::FORMAT_TAR) do |ar|
      files_to_archive.each do |fn|
        ar.new_entry do |entry|
          entry.copy_stat(fn)
          entry.pathname = fn
          ar.write_header(entry)
          if !entry.directory?
            ar.write_data(open(fn) {|f| f.binmode; f.read })
          end
        end
      end
    end
  end
end

#download(url, path) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fig/os.rb', line 136

def download(url, path)
   FileUtils.mkdir_p(File.dirname(path))
   uri = URI.parse(url)
   case uri.scheme
   when "ftp"
     ftp = Net::FTP.new(uri.host)
     (ftp, uri.host)
     begin
       if File.exist?(path) && ftp.mtime(uri.path) <= File.mtime(path)
         return false
       else 
         $stderr.puts "downloading #{url}"
         ftp.getbinaryfile(uri.path, path, 256*1024)
         return true
       end
     rescue Net::FTPPermError
       raise NotFoundException.new
     end
   when "http"
     http = Net::HTTP.new(uri.host)
     $stderr.puts "downloading #{url}"
     File.open(path, "wb") do |file|
       file.binmode
       http.get(uri.path) do |block|
         file.write(block)
       end
     end
   when "ssh"
     # TODO need better way to do conditional download
     #       timestamp = `ssh #{uri.user + '@' if uri.user}#{uri.host} "ruby -e 'puts File.mtime(\\"#{uri.path}\\").to_i'"`.to_i
     timestamp = File.exist?(path) ? File.mtime(path).to_i : 0 
     cmd = `which fig-download`.strip + " #{timestamp} #{uri.path}"
     ssh_download(uri.user, uri.host, path, cmd)
   else
     $stderr.puts "Unknown protocol: #{url}"
     exit 10
   end
end

#download_archive(url, dir) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fig/os.rb', line 180

def download_archive(url, dir)
  FileUtils.mkdir_p(dir)
  basename = URI.parse(url).path.split('/').last
  path = File.join(dir, basename)
  download(url, path)
  case basename
  when /\.tar\.gz$/
    unpack_archive(dir, path)
  when /\.tgz$/
    unpack_archive(dir, path)
  when /\.tar\.bz2$/
    unpack_archive(dir, path)
  when /\.zip$/
    unpack_archive(dir, path)
  else
    $stderr.puts "Unknown archive type: #{basename}"
    exit 10
  end
end

#download_ftp_list(uri, dirs) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fig/os.rb', line 105

def download_ftp_list(uri, dirs)
  # Run a bunch of these in parallel since they're slow as hell
  num_threads = (ENV["FIG_FTP_THREADS"] || "16").to_i
  threads = []
  all_packages = []        
  (0..num_threads-1).each { |num| all_packages[num] = [] }
  (0..num_threads-1).each do |num|
    threads << Thread.new do
      packages = all_packages[num]
      ftp = Net::FTP.new(uri.host)
      (ftp, uri.host)
      ftp.chdir(uri.path)
      pos = num
      while pos < dirs.length
        pkg = dirs[pos]
        begin
          ftp.nlst(dirs[pos]).each do |ver|
            packages << pkg + '/' + ver
          end
        rescue Net::FTPPermError
          # ignore
        end
        pos += num_threads
      end
      ftp.close
    end
  end
  threads.each { |thread| thread.join }
  all_packages.flatten.sort
end

#download_list(url) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fig/os.rb', line 70

def download_list(url)
  begin
    uri = URI.parse(url)
  rescue 
    $stderr.puts "Unable to parse url: '#{url}'"
    exit 10
  end
  case uri.scheme
  when "ftp"
    ftp = Net::FTP.new(uri.host)
    (ftp, uri.host)
    ftp.chdir(uri.path)
    dirs = ftp.nlst
    ftp.close

    download_ftp_list(uri, dirs)
  when "ssh"
    packages = []
    Net::SSH.start(uri.host, uri.user) do |ssh|
      ls = ssh.exec!("[ -d #{uri.path} ] && find #{uri.path}")
      if not ls.nil?
        ls = ls.gsub(uri.path + "/", "").gsub(uri.path, "")
        ls.each do |line|
          parts = line.gsub(/\\/, '/').sub(/^\.\//, '').sub(/:$/, '').chomp().split('/')
          packages << parts.join('/') if parts.size == 2
        end
      end
    end
    packages
  else
    $stderr.puts "Protocol not supported: #{url}"
    exit 10
  end
end

#download_resource(url, dir) ⇒ Object



175
176
177
178
# File 'lib/fig/os.rb', line 175

def download_resource(url, dir)
  FileUtils.mkdir_p(dir)
  download(url, File.join(dir, URI.parse(url).path.split('/').last))
end

#exec(dir, command) ⇒ Object



240
241
242
243
244
245
246
247
# File 'lib/fig/os.rb', line 240

def exec(dir,command)
  Dir.chdir(dir) {
    unless system command
      $stderr.puts "Command failed"
      exit 10
    end
  }
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fig/os.rb', line 50

def exist?(path)
  File.exist?(path)
end

#ftp_login(ftp, host) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fig/os.rb', line 32

def (ftp, host)
  if @login
    rc = Net::Netrc.locate(host)
    if rc
      @username = rc.
      @password = rc.password
    end
    ftp.(get_username, get_password)
  else 
    ftp.()
  end
  ftp.passive = true
end

#get_passwordObject



28
29
30
# File 'lib/fig/os.rb', line 28

def get_password()
  @password ||= ask("Password: ") { |q| q.echo = false }
end

#get_usernameObject



24
25
26
# File 'lib/fig/os.rb', line 24

def get_username()
  @username ||= ask("Username: ") { |q| q.echo = true }
end

#list(dir) ⇒ Object



46
47
48
# File 'lib/fig/os.rb', line 46

def list(dir)
  Dir.entries(dir) - ['.','..']
end

#log_info(msg) ⇒ Object



258
259
260
# File 'lib/fig/os.rb', line 258

def log_info(msg)
  $stderr.puts msg
end

#move_file(dir, from, to) ⇒ Object



254
255
256
# File 'lib/fig/os.rb', line 254

def move_file(dir, from, to)
  Dir.chdir(dir) { FileUtils.mv(from, to, :force => true) }
end

#mtime(path) ⇒ Object



54
55
56
# File 'lib/fig/os.rb', line 54

def mtime(path)
  File.mtime(path)
end

#read(path) ⇒ Object



58
59
60
# File 'lib/fig/os.rb', line 58

def read(path)
  File.read(path)
end

#shell_exec(cmd) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/fig/os.rb', line 314

def shell_exec(cmd)
  if OS.windows?
    Windows.shell_exec_windows(cmd)
  else
    shell_exec_unix(cmd)
  end
end

#unpack_archive(dir, file) ⇒ Object

This method can handle the following archive types: .tar.bz2 .tar.gz .tgz .zip



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/fig/os.rb', line 288

def unpack_archive(dir, file)
  Dir.chdir(dir) do
    if OS.java?
      `tar xzvf #{file}`
    else
      ::Archive.read_open_filename(file) do |ar|
        while entry = ar.next_header
          ar.extract(entry)
        end
      end
    end
  end
end

#upload(local_file, remote_file, user) ⇒ Object



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
232
233
# File 'lib/fig/os.rb', line 200

def upload(local_file, remote_file, user)
  puts "uploading #{local_file} to #{remote_file}"
  uri = URI.parse(remote_file)
  case uri.scheme
  when "ssh"
    ssh_upload(uri.user, uri.host, local_file, remote_file)
  when "ftp"
    #      fail unless system "curl -T #{local_file} --create-dirs --ftp-create-dirs #{remote_file}"
   require 'net/ftp'
    ftp_uri = URI.parse(ENV["FIG_REMOTE_URL"])
    ftp_root_path = ftp_uri.path
    ftp_root_dirs = ftp_uri.path.split("/")
    remote_publish_path = uri.path[0, uri.path.rindex("/")]
    remote_publish_dirs = remote_publish_path.split("/")
    # Use array subtraction to deduce which project/version folder to upload to,
    # i.e. [1,2,3] - [2,3,4] = [1]
    remote_project_dirs = remote_publish_dirs - ftp_root_dirs
    Net::FTP.open(uri.host) do |ftp|
      (ftp, uri.host)
      # Assume that the FIG_REMOTE_URL path exists.
      ftp.chdir(ftp_root_path)
      remote_project_dirs.each do |dir|
        # Can't automatically create parent directories, so do it manually.
       if ftp.nlst().index(dir).nil?
         ftp.mkdir(dir)
         ftp.chdir(dir)
       else
         ftp.chdir(dir)
       end
      end
      ftp.putbinaryfile(local_file)
    end
  end
end

#write(path, content) ⇒ Object



62
63
64
# File 'lib/fig/os.rb', line 62

def write(path, content)
  File.open(path, "wb") { |f| f.binmode; f << content }
end