zoukankan      html  css  js  c++  java
  • the progressbar and Tempfile

    https://github.com/peleteiro/progressbar

    gem install progressbar

    gem 'progressbar', '~> 0.21.0'

    % irb --simple-prompt -r progressbar
    >> pbar = ProgressBar.new("test", 100)
    => (ProgressBar: 0/100)
    >> 100.times {sleep(0.1); pbar.inc}; pbar.finish
    test:          100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
    => nil
    
    >> pbar = ProgressBar.new("test", 100)
    => (ProgressBar: 0/100)
    >> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
    test:           67% |oooooooooooooooooooooooooo              | ETA:  00:00:03
    
    >> ProgressBar.new("test", 100) do |pbar|
    >>   100.times { sleep(0.1); pbar.inc }
    >> end
    test:          100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10

    def download_apk!
      require 'open-uri'
      require 'digest'
      require 'fileutils'

      return false if is_dead_download_url?
      return false if is_apk_downloaded?

      tmp_file = Tempfile.new [package_name, ".apk"]
      File.open(tmp_file, 'wb') do |file_to_save|
        pbar = nil

        open(download_url, "rb", :content_length_proc => lambda {|t|
          if t && 0 < t

            pbar = ProgressBar.new("...", t)
         pbar.file_transfer_mode
          end
        },
        :progress_proc => lambda {|s|
          pbar.set s if pbar
        }) { |file_to_download| file_to_save.write(file_to_download.read) }  
      end

      apk_md5 = Digest::MD5.file(tmp_file).hexdigest
      apk_path = File.join Settings.apks_dir, "#{package_name}.#{apk_md5}.apk"

      FileUtils.mkdir_p Settings.apks_dir
      FileUtils.mv tmp_file, apk_path

      package_info = dump_package_info apk_path
      update_attributes package_info.merge(md5: apk_md5)
      apk_path
    rescue OpenURI::HTTPError, OpenURI::HTTPRedirect
      false
    end

     

    http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html

    Tempfile

    默认存储路径是 /tmp
    A utility class for managing temporary files. When you create a Tempfile object, it will create a temporary file with a unique filename. A Tempfile objects behaves just like a File object, and you can perform all the usual file operations on it: reading data, writing data, changing its permissions, etc. So although this class does not explicitly document all instance methods supported by File, you can in fact call any File instance method on a Tempfile object.


    require 'tempfile'
    
    file = Tempfile.new('foo')
    file.path      # => A unique filename in the OS's temp directory,
                   #    e.g.: "/tmp/foo.24722.0"
                   #    This filename contains 'foo' in its basename.
    file.write("hello world")
    file.rewind
    file.read      # => "hello world"
    file.close
    file.unlink    # deletes the temp file





  • 相关阅读:
    中台入门系列1
    微服务 2.0 技术栈选型手册
    mysql计划任务每天定时执行
    更高效地提高redis client多线程操作的并发吞吐设计
    azure之MSSQL服务性能测试
    .NET Socket服务编程之-高效连接接入编
    轻易实现基于linux或win运行的聊天服务端程序
    零配置Socket TCP消息通讯服务容器EC
    azure存储压测的问题(农码主观意识太强被坑了)
    业务逻辑层缓存应该设计
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/3795259.html
Copyright © 2011-2022 走看看