當前位置: 首頁>>代碼示例>>Python>>正文


Python System.controller方法代碼示例

本文整理匯總了Python中system.System.controller方法的典型用法代碼示例。如果您正苦於以下問題:Python System.controller方法的具體用法?Python System.controller怎麽用?Python System.controller使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在system.System的用法示例。


在下文中一共展示了System.controller方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: download_int

# 需要導入模塊: from system import System [as 別名]
# 或者: from system.System import controller [as 別名]
    def download_int(self, link):
        localfile = os.path.basename(link) 
        dotrel = '{}/.rel/{}'.format(System.controller(), localfile)
        need_to_copy = False
        if os.path.exists('{}/{}'.format(System.upgrade_dir(), localfile)) and not self.forced:
            info('{} is present in upgrade directory. Skipping download'.format(localfile))
            cached = True
        elif os.path.exists(dotrel) and not self.forced: 
            info('{} is present in dotrel directory. Skipping download'.format(localfile))
            need_to_copy = True
            cached = True
        else:
            cached = False

        if cached is False or self.forced is True:
            try:
                info('Downloading {}'.format(localfile))
                start_time = time.time()
                try:
                    resp = self.session.get(
                        self.endpoint + '/' + link, 
                        stream=True, 
                        proxies=self.proxies
                    )
                    content_length = resp.headers.get('content-length')
                except requests.exceptions.ConnectionError:
                    warn('Unable to connect to build hub')
                    return
                except requests.exceptions.ConnectTimeout:
                    warn('Timeout while downloading')
                    return
                if resp.status_code > 200: 
                    warn('Status code: %s' % resp.status_code)
                    return

                bytes_read = 0
                to_read = int(content_length) 
                f = open(System.upgrade_dir() + '/' + localfile, 'wb')
                for data in resp.iter_content(config.READ_BUFF_SIZE):
                    bytes_read += len(data)
                    f.write(data)
                    done = (50 * bytes_read / to_read)
                    percent_completed = float(bytes_read)/to_read
                    percent_completed = round(percent_completed * 100, 2)
                    sys.stdout.write('\r     [get] [%s%s] %0.2f%%' % (
                        '#' * done, 
                        ' ' * (50 - done), 
                        percent_completed)
                    )
                    sys.stdout.flush()
            except KeyboardInterrupt:
                f.close()
                print 
                info('Cleaning up incompleted files')
                os.remove(System.upgrade_dir() + '/' + localfile)
                warn('Download interrupted by user')
                return
            except IOError:
                warn('Unable to write to upgrade dir. Make sure directory exists and is writable')
                return

            end_time = time.time() 
            f.close()
            print ''
            info('Downloaded {} in {:.2f} seconds'.format(humanize(bytes_read), end_time - start_time))

        if need_to_copy is True:
            info('Copying {} from dotrel to upgrade dir'.format(localfile)) 
            shutil.copy2(dotrel, System.upgrade_dir()) 
開發者ID:srvstva,項目名稱:nsu_easy_upgrade,代碼行數:71,代碼來源:agent.py


注:本文中的system.System.controller方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。