def get_flash_tool_by_id(self, board_id): flash_tool_dir = "" tool_dir = self.get_tool_dir_by_id(board_id) try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] _flash = self.boards[board_id]['architecture'] package_id = _package['package'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) flashs = json_dict['packages'][package_id]['flash'] for flash in flashs: if (flash['name'] == _flash): flash_tool_dir = str( Path(tool_dir, flash['tools'], flash['version'])) break except Exception as e: log.error(e) if flash_tool_dir == "": log.error( "Can't find flash tool, please check package_index.json!") return flash_tool_dir
def get_flash_command_by_id(self, board_id, port, firmware): flash_command = "" try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] _flash = self.boards[board_id]['architecture'] package_id = _package['package'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) flashs = json_dict['packages'][package_id]['flash'] for flash in flashs: if (flash['name'] == _flash): if self.system == "i686-mingw32": tool = flash['tools'] + '.exe' else: tool = flash['tools'] flash_command = tool + flash['command'] flash_command = flash_command.format( str(port), str(firmware)) except Exception as e: log.error(e) if flash_command == "": log.error( "Can't find flash tool depency, please check package_index.json!" ) return flash_command
def get_toolsDependencies_url_by_id(self, board_id): dependencies = [] try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] package_id = _package['package'] platform_id = _package['platform'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) platform = json_dict['packages'][package_id]['platforms'][ platform_id] tools = json_dict['packages'][package_id]['tools'] toolsDependencies = platform['toolsDependencies'] for _toolsDependencies in toolsDependencies: for _tools in tools: if _tools['name'] == _toolsDependencies[ 'name'] and _tools[ 'version'] == _toolsDependencies['version']: for _system in _tools['systems']: if _system['host'] == self.system: _system.update({'name': _tools['name']}) _system.update( {'version': _tools['version']}) dependencies.append(_system) return dependencies except Exception as e: log.error(e) return None
def get_ardupycore_dir_by_package_id(self, package_id): try: _package = self.packages[package_id] package_id = _package['package'] platform_id = _package['platform'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) platform = json_dict['packages'][package_id]['platforms'][ platform_id] archiveFile = { 'package': json_dict['packages'][package_id]['name'], 'arch': platform['architecture'], 'version': platform['version'], 'url': platform['url'], 'archiveFileName': platform['archiveFileName'], 'checksum': platform['checksum'], 'size': platform['size'] } ardupycoredir = str( Path(self.user_config_dir, 'ardupycore', archiveFile['package'], 'hardware', archiveFile['arch'], archiveFile['version'])) return ardupycoredir except Exception as e: log.error(e) return None
def update_loacl_library_json(self): log.info("update local library json...") for url in self.get_library_additional_url(): url = url.strip() log.info(url) url_path = url.split('/')[len(url.split('/')) - 1] try: urllib.request.urlretrieve( url, str(Path(self.user_config_dir, url_path))) except Exception as e: log.error(e) continue else: log.info("done!")
def get_toolsDependencies_by_id(self, board_id): try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] package_id = _package['package'] platform_id = _package['platform'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) platform = json_dict['packages'][package_id]['platforms'][ platform_id] return platform['toolsDependencies'] except Exception as e: log.error(e) return None
def get_build_pram_by_id(self, board_id): try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] _build = self.boards[board_id]['architecture'] package_id = _package['package'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) builds = json_dict['packages'][package_id]['build'] for build in builds: if (build['name'] == _build): return build except Exception as e: log.error(e) log.error("Can't find build pram") return ""
def get_flash_isTouch_by_id(self, board_id): isTouch = False try: _package_id = self.boards[board_id]['package_id'] _package = self.packages[_package_id] _flash = self.boards[board_id]['architecture'] package_id = _package['package'] with open(str(Path(self.user_config_dir, _package['path'])), 'r') as load_f: json_dict = json.load(load_f) flashs = json_dict['packages'][package_id]['flash'] for flash in flashs: isTouch = flash["isTouch"] except Exception as e: log.error(e) return isTouch
def parser_all_json(self): #parser all the json for path in os.listdir(self.user_config_dir): if path.find('json') and path.find('package') != -1: try: with open(str(Path(self.user_config_dir, path)), 'r') as load_f: json_dict = json.load(load_f) path = {'path': path} package_id = 0 # package index in file for _package in json_dict['packages']: package = {'package': package_id} package_name = {'package_name': _package['name']} platform_id = 0 # platform index in package for _platform in _package['platforms']: id = {'id': len(self.packages)} platform = {'platform': platform_id} platform_name = { 'platform_name': _platform['name'] } version = {'version': _platform['version']} arch = {'arch': _platform['architecture']} packages = {} # Organize data and record packages.update(id) packages.update(package) packages.update(package_name) packages.update(platform) packages.update(platform_name) packages.update(version) packages.update(package) packages.update(arch) packages.update(path) self.packages.append(packages) for _board in _platform['board']: _package_id = { 'package_id': packages['id'] } _board_id = {'id': len(self.boards)} _board.update(_package_id) _board.update(_board_id) self.boards.append(_board) platform_id += 1 package_id += 1 except Exception as e: log.error(e)
def update_loacl_board_json(self): log.info("update local board json...") self.config_file = open(self.config_file_path, 'r+') self.cp = ConfigParser() self.cp.read(self.config_file_path) for url in self.get_board_additional_url(): url = url.strip() log.info(url) url_path = url.split('/')[len(url.split('/')) - 1] try: urllib.request.urlretrieve( url, str(Path(self.user_config_dir, url_path))) except Exception as e: log.error(e) continue else: self.cp.set("board", 'update time', str(date.today().isoformat())) self.cp.write(self.config_file) log.info("done!")
def run(self, options, args): ser = SerialUtils() if options.scan: if options.desc == "": print(ser.listAvailableBoard()) else: print(ser.listDesignatedBoard(options.desc)) return SUCCESS if options.list == True: print(ser.listBoard()) return SUCCESS if options.port == "": port, desc, hwid, isbootloader = ser.getAvailableBoard() else: port = options.port if port == None: log.error("Sorry, the device you should have is not plugged in.") return ERROR try: if options.baudrate != "": baudrate = int(options.baudrate) else: baudrate = 115200 self.serial = serial.Serial(port, baudrate=baudrate, interCharTimeout=1) print(self.get_version()) except Exception as e: log.error(e) return ERROR return SUCCESS
def add_library_additional_url(self, url): if re.match(r'^https?:/{2}\w.+$', url): # is a url? if (url.find('json') != -1): _url = self.cp.get("library", "additional_url") _url_list = _url.split(',') if not (url in _url_list): # is existed? _url += ',' + url self.cp.set('library', "additional_url", _url) self.cp.write(self.config_file) #write to aip.conf return True else: log.error(url + " is existed.") return False else: log.error(url + " is not a json file.") return False else: log.error(url + " is not a url.") return False
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 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): if options.board == "": port, desc, hwid, isbootloader = self.serial.getAvailableBoard() if port != None: _board = self.serial.getBoardByPort(port) if _board != "": self.board = _board[0] else: log.warning( "please plug in a ArduPy Board or specify the board to build!" ) print("<usage> aip build [--board=<board>] ") return else: self.board = options.board self.board_id = self.serial.getBoardIdByName(self.board) if self.board_id == -1: log.error("Unable to find information about '" + self.board + "'") return ERROR self.verbose = options.verbose self.initBoard() seesion = self.get_default_session(options) self.downloadAll(seesion) # setup deploy dir deploydir = parser.get_deploy_dir_by_id(self.board_id) if not os.path.exists(deploydir): os.makedirs(deploydir) # create build dir, This folder will be deleted after compilation builddir = mktemp() os.makedirs(builddir) log.info("|---------------" + " Building Firmware for " + self.board + "---------------|") arduinodir = parser.get_arduino_dir_by_id(self.board_id) arduinocoredir = str(Path(arduinodir, "cores", "arduino")) # Converts the header file to the absolute path of the current system # 1 append Arduino Core PATH self.headerlist.append(arduinocoredir) for file in os.listdir(arduinocoredir): file_path = str(Path(arduinocoredir, file)) if os.path.isdir(file_path): self.headerlist.append(file_path) # 2 append Arduino variants PATH self.headerlist.append(parser.get_variant_dir_by_id(self.board_id)) # 3 append Arduino library PATH librariesdir = str(Path(arduinodir, "libraries")) for library in os.listdir(librariesdir): library_path = str(Path(librariesdir, library)) self.headerlist.append(library_path) if (os.path.exists(str(Path(library_path, "src")))): self.headerlist.append(str(Path(library_path, "src"))) # 4 append Ardupy core PATH self.headerlist.append(parser.get_ardupy_dir_by_id(self.board_id)) self.headerlist.append(parser.get_ardupy_board_by_id(self.board_id)) self.headerlist.append( str(Path(parser.get_ardupy_dir_by_id(self.board_id), "MicroPython"))) # 5 append moudules PATh moduledir = str(Path(parser.user_config_dir, "modules")) if not os.path.exists(moduledir): os.makedirs(moduledir) modules = os.listdir(moduledir) if modules: for m in modules: # Gets the source files for all modules for f in self.fileEndWith( os.path.join(str(Path(moduledir, m))), '.cpp', '.c'): self.srcfile.append(str(Path(f))) # Sets the root directory of the module to be where the header file is found for r, d, f in os.walk(str(Path(moduledir, m))): if r.find('.git') == -1 and r.find("examples") == -1: self.headerlist.append(r) # 6 Convert the necessary files in ardupycore into the absolute path of the system. self.srcfile.append( str( Path(parser.get_ardupy_dir_by_id(self.board_id), "MicroPython", "py", "objmodule.c"))) self.srcfile.append( str( Path(parser.get_ardupy_dir_by_id(self.board_id), "MicroPython", "py", "parse.c"))) self.srcfile.append( str( Path(parser.get_ardupy_dir_by_id(self.board_id), "MicroPython", "py", "qstr.c"))) # 7 generatrd Init file for binding self.generatedInitfile(builddir) # 8 Convert to the required format for GCC self.generatedQstrdefs(builddir) # 9 append build temp dir self.headerlist.append(str(Path(builddir))) # 10 inform headers headerlist self.headers = "-I" + " -I".join(self.headerlist) # 11 build firmware self.buildFirmware(builddir) # 12 remove the old firmware firmware_path = str(Path(str(deploydir), "Ardupy.bin")) if os.path.exists(firmware_path): os.remove(firmware_path) # 13 convert elf to bin self.objcopy_cmd = self.objcopy + " -O binary " \ + str(Path(builddir + "/Ardupy")) + " " \ + firmware_path self.doVerbose(self.objcopy_cmd) os.system(self.objcopy_cmd) # 14 print information self.sizetool_cmd = self.sizetool + \ " -A " + str(Path(builddir + "/Ardupy")) print("") os.system(self.sizetool_cmd) # 15 print information # delete build dir shutil.rmtree(builddir) if os.path.exists(firmware_path): log.info('Firmware path: ' + firmware_path) log.info('Usage:\n\r aip flash') else: raise Exception(print('compile error')) return SUCCESS
def run(self, options, args): if options.port == "": port, desc, hwid, isbootloader = self.serial.getAvailableBoard() self.port = port else: port = options.port if port == None: log.error("Sorry, the device you should have is not plugged in.") return ERROR board_id = self.serial.getBoardIdByPort(self.port) if parser.get_flash_isTouch_by_id(board_id): 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") time.sleep(1) port, desc, hwid, isbootloader = self.serial.getBootloaderBoard( ) if isbootloader == True: self.port = port break try_count = try_count + 1 if port == None: continue if try_count == 5: do_bossac = False break if do_bossac == True: flash_tools = parser.get_flash_tool_by_id(board_id) #print(flash_tools) if len(args) != 0: ardupybin = args[0] else: ardupybin = str( Path(parser.get_deploy_dir_by_id(board_id), "Ardupy.bin")) flash_command = parser.get_flash_command_by_id( board_id, self.port, ardupybin) print(flash_tools + "/" + flash_command) os.system(flash_tools + "/" + flash_command) else: log.warning( "Sorry, the device you should have is not plugged in.") return ERROR else: flash_tools = parser.get_flash_tool_by_id(board_id) ardupybin = "" if len(args) != 0: ardupybin = args[0] else: ardupybin = str( Path(parser.get_deploy_dir_by_id(board_id), "Ardupy.bin")) flash_command = parser.get_flash_command_by_id( board_id, self.port, ardupybin) print(flash_tools + "/" + flash_command) os.system(flash_tools + "/" + flash_command) return SUCCESS
def main(args=None): # type: (Optional[List[str]]) -> int if args is None: args = sys.argv[1:] # is update package_seeeduino_ardupy_index.json user_config_dir = str(appdirs.user_config_dir(appname="aip")) if not os.path.exists(user_config_dir): os.makedirs(user_config_dir) today = date.today() user_config_dir_files = os.listdir(user_config_dir) current_package_seeeduino_ardupy = "xxxxx" is_update = True for files in user_config_dir_files: if files[0:30] == "package_seeeduino_ardupy_index": file_data = datetime.strptime(files[31:41], '%Y-%m-%d').date() current_package_seeeduino_ardupy = files if file_data == today: is_update = False break if is_update: log.info("update latest package_seeeduino_ardupy_index.json ...") try: urllib.request.urlretrieve( 'https://files.seeedstudio.com/ardupy/package_seeeduino_ardupy_index.json', str( Path( user_config_dir, "package_seeeduino_ardupy_index_" + today.isoformat() + ".json"))) if os.path.exists( str(Path(user_config_dir, current_package_seeeduino_ardupy))): os.remove( str(Path(user_config_dir, current_package_seeeduino_ardupy))) except Exception as e: log.error(e) log.warning( "update latest package_seeeduino_ardupy_index.json Failed! Please check you network connction!" ) sys.exit(1) from aip.command import commands_dict, parse_command # Configure our deprecation warnings to be sent through loggers deprecation.install_warning_logger() autocomplete() try: cmd_name, cmd_args = parse_command(args) except PipError as exc: sys.stderr.write("ERROR: {}".format(exc)) sys.stderr.write(os.linesep) sys.exit(1) # Needed for locale.getpreferredencoding(False) to work # in pip._internal.utils.encoding.auto_decode try: locale.setlocale(locale.LC_ALL, '') except locale.Error as e: # setlocale can apparently crash if locale are uninitialized print("Ignoring error %s when setting locale", e) module = importlib.import_module("aip." + cmd_name) command_class = getattr(module, cmd_name + "Command") command = command_class(name=cmd_name, summary="...") return command.main(cmd_args)
def __init__(self): super().__init__() if len(parser.boards) == 0: log.error( "Unable to find any ardupy boards, please refer to aip core!") sys.exit(1)
func(path) # List of supported board USB IDs. Each board is a tuple of unique USB vendor # ID, USB product ID. user_config_dir = appdirs.user_config_dir(appname="aip") today = date.today() package_seeeduino_ardupy_index_json = str( Path(user_config_dir, "package_seeeduino_ardupy_index_" + today.isoformat() + ".json")) if os.path.exists(package_seeeduino_ardupy_index_json): with open(package_seeeduino_ardupy_index_json, 'r') as load_f: json_dict = json.load(load_f) BOARD_IDS = json_dict['board'] else: try: for file in os.listdir(user_config_dir): if "package_seeeduino_ardupy_index_" in file: package_seeeduino_ardupy_index_json = str( Path(user_config_dir, file)) with open(package_seeeduino_ardupy_index_json, 'r') as load_f: json_dict = json.load(load_f) BOARD_IDS = json_dict['board'] break except Exception as e: log.error(e) log.error( "The package_seeeduino_ardupy_index.json file is missing, execute aip to obtain it automatically" ) sys.exit(1)