def setex(binfile): localfile = System.upgrade_dir() + "/" + os.path.basename(binfile) if not os.path.exists(localfile): warn("Unable to set executable permission on {}".format(localfile)) return info("Setting executable permission to {}".format(localfile)) os.chmod(localfile, 0755)
def do_search(self): info('Initializing search. Please wait ...') try: resp = self.session.get(self.endpoint, proxies=self.proxies) except requests.exceptions.ConnectionError: error('Unable to connect to build hub server. Is the server running?') except requests.exceptions.ConnectTimeout: error('Timeout while connecting to build hub server') if resp.status_code > 200: error('HTTP Error: Status Code {}'.format(resp.status_code)) try: resp.raise_for_status() except requests.exceptions: error('Error looking up downloads from repository') soup = BeautifulSoup(resp.content, 'html.parser') all_links = [link.get('href') for link in soup.find_all('a', title='Download')] netstorm_links = filter(self.regexp_netstorm.search, all_links) thirdparty_links = filter(self.regexp_thirdparty.search, all_links) if len(netstorm_links) > 0: self.netstorm_bin = netstorm_links[0] if len(thirdparty_links) > 0: self.thirdparty_bin = thirdparty_links[0]
def __init__(self, endpoint=config.endpoint, pattern=None, forced=False, proxies=None): self.endpoint = endpoint self.forced = forced if pattern is None: self.regexp_netstorm = re.compile(r'.*netstorm.*[0-9]+.*Ubuntu.*.bin') self.regexp_thirdparty = re.compile(r'.*thirdparty.*[0-9]+.*Ubuntu.*.bin') else: self.regexp_netstorm = re.compile(r'.*netstorm.*' + pattern + '[._].*Ubuntu.*.bin') self.regexp_thirdparty = re.compile(r'.*thirdparty.*' + pattern + '[._].*Ubuntu.*.bin') if self.forced is True: info('Forced flag has been specified. Local copies will be discarded') self.netstorm_bin = None self.thirdparty_bin = None self.session = requests.session() self.proxies = dict(http=proxies, https=proxies) self.do_search()
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())