def test_unpack_url_bad_hash(self, tmpdir: Path, data: TestData) -> None: """ Test when the file url hash fragment is wrong """ self.prep(tmpdir, data) url = f"{self.dist_url.url}#md5=bogus" dist_url = Link(url) with pytest.raises(HashMismatch): unpack_url( dist_url, self.build_dir, download=self.no_download, hashes=Hashes({"md5": ["bogus"]}), verbosity=0, )
def downloadAll(self, session): archiveFile = parser.get_archiveFile_by_id(self.board_id) downloader = Downloader(session, progress_bar="on") ardupycoredir = parser.get_core_dir_by_id(self.board_id) if not os.path.exists(ardupycoredir): log.info('Downloading ' + archiveFile['archiveFileName'] + '...') try: unpack_url( Link(archiveFile['url']), ardupycoredir, downloader, download_dir=None, ) except Exception as e: log.error(e) os.remove(ardupycoredir) sys.exit(1) except Exception as e: log.error(e) os.remove(ardupycoredir) sys.exit(1) toolsDependencies = parser.get_toolsDependencies_url_by_id( self.board_id) toolsdir = parser.get_tool_dir_by_id(self.board_id) for tool in toolsDependencies: tooldir = str(Path(toolsdir, tool['name'], tool['version'])) if not os.path.exists(tooldir): log.info('Downloading ' + tool['name'] + '@' + tool['version'] + '...') try: unpack_url( Link(tool['url']), tooldir, downloader, download_dir=None, ) except Exception as e: log.error(e) os.remove(tooldir) sys.exit(1)
def downloadAll(self, session): link = Link("http://files.seeedstudio.com/ardupy/ardupy-core.zip") downloader = Downloader(session, progress_bar="on") ardupycoredir = user_config_dir + "/ardupycore" if not os.path.exists(ardupycoredir + "/ArduPy"): try: if not os.path.exists(ardupycoredir): os.makedirs(ardupycoredir) except OSError as error: log.warning("Directory '%s was exists' " % ardupycoredir) log.warning(error) unpack_url( link, ardupycoredir, downloader=downloader, download_dir=None, ) if not os.path.exists(ardupycoredir + "/Seeeduino/tools/arm-none-eabi-gcc"): if sys.platform == "linux": link = Link( "http://files.seeedstudio.com/arduino/tools/x86_64-pc-linux-gnu/gcc-arm-none-eabi-4.8.3-2014q1-linux64.tar.gz" ) if sys.platform == "win32": link = Link( "http://files.seeedstudio.com/arduino/tools/i686-mingw32/gcc-arm-none-eabi-4.8.3-2014q1-windows.tar.gz" ) if sys.platform == "darwin": link = Link( "http://files.seeedstudio.com/arduino/tools/x86_64-apple-darwin/gcc-arm-none-eabi-4.8.3-2014q1-mac.tar.gz" ) unpack_url( link, ardupycoredir + "/Seeeduino/tools/arm-none-eabi-gcc", downloader=downloader, download_dir=None, )
def test_unpack_url_with_urllib_response_without_content_type( data: TestData) -> None: """ It should download and unpack files even if no Content-Type header exists """ _real_session = PipSession() def _fake_session_get(*args: Any, **kwargs: Any) -> Dict[str, str]: resp = _real_session.get(*args, **kwargs) del resp.headers["Content-Type"] return resp session = Mock() session.get = _fake_session_get download = Downloader(session, progress_bar="on") uri = data.packages.joinpath("simple-1.0.tar.gz").as_uri() link = Link(uri) temp_dir = mkdtemp() try: unpack_url( link, temp_dir, download=download, download_dir=None, verbosity=0, ) assert set(os.listdir(temp_dir)) == { "PKG-INFO", "setup.cfg", "setup.py", "simple", "simple.egg-info", } finally: rmtree(temp_dir)
def pip_download_link(resconfig, url: str, destdir: str): with redirect_stdout(sys.stderr): netloc = urlsplit(resconfig['source']['repository']['index_url'])[1] hostname = netloc.split(':')[0] with PipSession(retries=RETRIES, trusted_hosts=[ hostname, ]) as session: session.timeout = TIMEOUT session.auth.prompting = False session.auth.passwords[netloc] = ( resconfig['source']['repository'].get('username', None), resconfig['source']['repository'].get('password', None)) # pip internals hardcode global tempdir manager. # need to copy to destdir before tempdir gets blown away. with global_tempdir_manager(): file = unpack_url( Link(url), destdir, Downloader(session, "pretty"), ) shutil.copy(file.path, destdir)
def test_unpack_url_no_download(self, tmpdir, data): self.prep(tmpdir, data) unpack_url(self.dist_url, self.build_dir, self.no_download) assert os.path.isdir(os.path.join(self.build_dir, 'simple')) assert not os.path.isfile( os.path.join(self.download_dir, self.dist_file))
def run(self, options, args): moduledir = Path(user_config_dir, "modules") session = self.get_default_session(options) downloader = Downloader(session, progress_bar="on") for package in args: package_url = self.get_archive_url(options, package) package_location = package_url[:package_url.find('/archive')] package_location = package_location.split('/')[ len(package_location.split('/')) - 1] package_location = str(Path(moduledir, package_location)) # form location if options.Force: if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) try: os.makedirs(package_location) except OSError as error: log.error(error) log.info("Use aip install -F Overwrite previous Library") return ERROR log.info("Downloading library......") try: unpack_url( Link(package_url), package_location, downloader=downloader, download_dir=None, ) except Exception as error: log.error(error) if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR # downling dependencies package_json_location = Path(package_location, 'library.json') try: #get library.json from package with open(package_json_location, 'r') as package_json: #get dependencies information from library.json package_json_dict = json.load(package_json) dependencies = package_json_dict["dependencies"] if len(dependencies) != 0: log.info("Downloading dependencies......") for dependency in dependencies: dependency_url = self.get_archive_url( options, dependency["url"]) dependency_location = package_location + '/' + dependency[ "name"] try: unpack_url( Link(dependency_url), dependency_location, downloader=downloader, download_dir=None, ) except Exception as error: log.error(error) if os.path.exists(package_location ): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR except Exception as error: log.error(error) log.error("Bad dependency format, please check library.json") if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR
def run(self, options, args): if len(args) == 0: log.warning("Please enter the url of the library!") log.info( 'Usage:\n\r aip install https://github.com/Seeed-Studio/seeed-ardupy-ultrasonic-sensor' ) return ERROR moduledir = Path(parser.user_config_dir, "modules") session = self.get_default_session(options) downloader = Downloader(session, progress_bar="on") for package in args: if options.local: package_location = package.split('/')[len(package.split('/')) - 1] else: package_url = self.get_archive_url(options, package) package_location = package_url[:package_url.find('/archive')] package_location = package_location.split('/')[ len(package_location.split('/')) - 1] package_location = str(Path(moduledir, package_location)) # form location if options.Force: if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) try: os.makedirs(package_location) except OSError as error: log.error(error) log.info("Use aip install -F Overwrite previous Library") return ERROR try: if options.local: log.info("Copying library......") shutil.copytree(package, package_location, dirs_exist_ok=True) else: log.info("Downloading library......") unpack_url( Link(package_url), package_location, downloader=downloader, download_dir=None, ) except Exception as error: log.error(error) if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR # downling dependencies package_json_location = Path(package_location, 'library.json') try: #get library.json from package with open(package_json_location, 'r') as package_json: #get dependencies information from library.json package_json_dict = json.load(package_json) dependencies = package_json_dict["dependencies"] if len(dependencies) != 0: log.info("Downloading dependencies......") for dependency in dependencies: dependency_url = self.get_archive_url( options, dependency["url"]) dependency_location = package_location + '/' + dependency[ "name"] try: unpack_url( Link(dependency_url), dependency_location, downloader=downloader, download_dir=None, ) except Exception as error: log.error(error) if os.path.exists(package_location ): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR except Exception as error: log.error(error) log.error("Bad dependency format, please check library.json") if os.path.exists(package_location): # remove the old package shutil.rmtree(package_location, onerror=readonly_handler) return ERROR return SUCCESS
def run(self, options, args): self.port = options.port bossacdir = str( Path(user_config_dir + "/ardupycore/Seeeduino/tools/bossac")) if not os.path.exists(bossacdir): os.makedirs(bossacdir) session = self.get_default_session(options) if sys.platform == "linux": link = Link( "http://files.seeedstudio.com/arduino/tools/i686-linux-gnu/bossac-1.9.1-seeeduino-linux.tar.gz" ) if sys.platform == "win32": link = Link( "http://files.seeedstudio.com/arduino/tools/i686-mingw32/bossac-1.9.1-seeeduino-windows.tar.bz2" ) if sys.platform == "darwin": link = Link( "http://files.seeedstudio.com/arduino/tools/x86_64-apple-darwin/bossac-1.8-48-gb176eee-i386-apple-darwin16.1.0.tar.gz" ) bossac = "" if platform.system() == "Windows": bossac = str(Path(bossacdir, "bossac.exe")) else: bossac = str(Path(bossacdir, "bossac")) if not os.path.exists(bossac): downloader = Downloader(session, progress_bar="on") unpack_url( link, bossacdir, downloader=downloader, download_dir=None, ) try_count = 0 do_bossac = True while True: stty = self.stty print(stty) if stty != "echo not support": os.system(stty % 1200) #os.system(str(bossac)+ " --help") port, desc, hwid, isbootloader = self.serial.getBootloaderBoard() print(port) time.sleep(1) if isbootloader == True: break try_count = try_count + 1 if try_count == 5: do_bossac = False break if do_bossac == True: name, version, url = self.serial.getBoardByPort(port) ardupybin = "" if len(args) > 0: ardupybin = args[0] if not os.path.exists(ardupybin): log.warning('The path of firmware didn\'t exists!') return ERROR elif options.origin == True: firmwaredir = str( Path(user_config_dir + "/deploy/firmware/" + name.replace(' ', '_'))) if not os.path.exists(firmwaredir): os.makedirs(firmwaredir) ardupybin = str(Path(firmwaredir, "ardupy_laster.bin")) if not os.path.exists(ardupybin): downloader = Downloader(session, progress_bar="on") _download_http_url(link=Link(url), downloader=downloader, temp_dir=firmwaredir, hashes=None) else: ardupybin = str(Path(user_config_dir + "/deploy/Ardupy.bin")) _flash_parm = flash_param[name.replace(' ', '_')] print((str(bossac) + _flash_parm) % (port, ardupybin)) os.system((str(bossac) + _flash_parm) % (port, ardupybin)) else: log.warning("Sorry, the device you should have is not plugged in.") return ERROR return SUCCESS
def test_unpack_url_no_download(self, tmpdir: Path, data: TestData) -> None: self.prep(tmpdir, data) unpack_url(self.dist_url, self.build_dir, self.no_download) assert os.path.isdir(os.path.join(self.build_dir, "simple")) assert not os.path.isfile(os.path.join(self.download_dir, self.dist_file))