def _find(self, key, items, places, human_name, join, do_glob=False): if key in self: return self[key] human_name = human_name or key # expand env variables and globs in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) if do_glob: places = map(glob, places) places = list(itertools.chain.from_iterable(places)) # Flatten after globbing places = map(os.path.expanduser, places) print 'Searching for', human_name, '...', results = [] for p in places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p if do_glob: results += [result] else: print colorize(result, 'green') self[key] = result return results print ', '.join(colorize(r, 'green') for r in results) if results: self[key] = results return results else: print colorize('FAILED', 'red') raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))
def discover(self): self.e.find_arduino_dir('arduino_core_dir', ['hardware', 'arduino', 'cores', 'arduino'], ['WProgram.h'], 'Arduino core library') self.e.find_arduino_dir('arduino_libraries_dir', ['libraries'], human_name='Arduino standard libraries') self.e.find_arduino_file('version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self.e: with open(self.e['version.txt']) as f: print 'Detecting Arduino software version ... ', version_string = f.read().strip() # Extract numeric part. String could be something like: # 0022 # 0022ubuntu0.1 # 0022-macosx-20110822 # in any case we need just 0022 casted to int, i.e. 22 version_int = int(re.split(r'\D', version_string)[0]) self.e['arduino_lib_version'] = version_int print colorize("%s (%s)" % (version_int, version_string), 'green') self.e.find_tool('cc', ['avr-gcc'], human_name='avr-gcc') self.e.find_tool('cxx', ['avr-g++'], human_name='avr-g++') self.e.find_tool('ar', ['avr-ar'], human_name='avr-ar') self.e.find_tool('objcopy', ['avr-objcopy'], human_name='avr-objcopy')
def load(self): if not os.path.exists(self.dump_filepath): return with open(self.dump_filepath, "rb") as f: try: self.update(pickle.load(f)) except: print colorize("Environment dump exists (%s), but failed to load" % self.dump_filepath, "yellow")
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ino', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command ) and x != Command commands = [ cls(e) for _, cls in inspect.getmembers(ino.commands, is_command) ] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: run_anywhere = "init clean list-models serial" in_project_dir = os.path.isdir(e.src_dir) if not in_project_dir and current_command not in run_anywhere: raise Abort("No project found in this directory.") e.process_args(args) if current_command not in run_anywhere: # For valid projects create .build & lib if not os.path.isdir(e.build_dir): os.makedirs(e.build_dir) if not os.path.isdir(e.lib_dir): os.makedirs(e.lib_dir) with open('lib/.holder', 'w') as f: f.write("") args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()
def load(self): if not os.path.exists(self.dump_filepath): return with open(self.dump_filepath, 'rb') as f: try: self.update(pickle.load(f)) except: print colorize('Environment dump exists (%s), but failed to load' % self.dump_filepath, 'yellow')
def format_available_options(items, head_width, head_color='cyan', default=None, default_mark="[DEFAULT]", default_mark_color='red'): from ino.filters import colorize default_mark = colorize(default_mark + ' ', default_mark_color) lines = ['%s: %s%s' % (colorize('%%%ds' % head_width % key, head_color), default_mark if key == default else '', val) for key, val in items] return '\n'.join(lines)
def copy_header_files(self): build_src_dir = os.path.join(self.e.build_dir, self.e.src_dir) if not os.path.exists(build_src_dir): os.makedirs(build_src_dir) files = glob.iglob(os.path.join(self.e.src_dir, "*.h")) for file in files: if os.path.isfile(file): print colorize("Scanning header files", 'cyan') print colorize("%s" % (file), 'yellow') shutil.copy(file, build_src_dir)
def format_available_options( items, head_width, head_color="cyan", default=None, default_mark="[DEFAULT]", default_mark_color="red" ): from ino.filters import colorize default_mark = colorize(default_mark + " ", default_mark_color) lines = [ "%s: %s%s" % (colorize("%%%ds" % head_width % key, head_color), default_mark if key == default else "", val) for key, val in items ] return "\n".join(lines)
def copy_header_files(self): build_src_dir = os.path.join(self.e.build_dir, self.e.src_dir) if not os.path.exists (build_src_dir): os.makedirs(build_src_dir) files = glob.iglob(os.path.join(self.e.src_dir, "*.h")) for file in files: if os.path.isfile(file): print colorize("Scanning header files", 'cyan') print colorize("%s" % (file), 'yellow') shutil.copy(file, build_src_dir)
def _find(self, key, items, places, human_name, join, multi, warn_only=False): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found and warn_only is False """ if key in self: return self[key] human_name = human_name or key # expand env variables in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(p) for p in places) print 'Searching for', human_name, '...', results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p if not multi: print colorize(result, 'green') self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print colorize('found multiple: %s' % formatted_results, 'green') else: print colorize(results[0], 'green') self[key] = results return results if warn_only: self[key] = None print colorize('FAILED (warn only)', 'yellow') else: print colorize('FAILED', 'red') raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))
def guess_serial_port(self): print 'Guessing serial port ...', ports = self.list_serial_ports() if len(ports) > 0: result = ports[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort("No device matching was found")
def guess_serial_port(self): print 'Guessing serial port ...', ports = self.list_serial_ports() if ports: result = ports[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort("No device matching following was found: %s" % (''.join(['\n - ' + p for p in self.serial_port_patterns()])))
def guess_serial_port(self): print 'Guessing serial port ...', ports = self.list_serial_ports() if len(ports)>0: result = ports[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort("No device matching was found")
def arduino_lib_version(self): self.find_arduino_file("version.txt", ["lib"], human_name="Arduino lib version file (version.txt)") if "arduino_lib_version" not in self: with open(self["version.txt"]) as f: print "Detecting Arduino software version ... ", v_string = f.read().strip() v = Version.parse(v_string) self["arduino_lib_version"] = v print colorize("%s (%s)" % (v, v_string), "green") return self["arduino_lib_version"]
def arduino_lib_version(self): self.find_arduino_file('version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self: with open(self['version.txt']) as f: print 'Detecting Arduino software version ... ', v_string = f.read().strip() v = Version.parse(v_string) self['arduino_lib_version'] = v print colorize("%s (%s)" % (v, v_string), 'green') return self['arduino_lib_version']
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ino', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command) and x != Command commands = [cls(e) for _, cls in inspect.getmembers(ino.commands, is_command)] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: run_anywhere = "init clean list-models serial" in_project_dir = os.path.isdir(e.src_dir) if not in_project_dir and current_command not in run_anywhere: raise Abort("No project found in this directory.") e.process_args(args) if current_command not in run_anywhere: # For valid projects create .build & lib if not os.path.isdir(e.build_dir): os.makedirs(e.build_dir) if not os.path.isdir(e.lib_dir): os.makedirs(e.lib_dir) with open('lib/.holder', 'w') as f: f.write("") args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()
def guess_serial_port(self): print "Guessing serial port ...", ports = self.list_serial_ports() if ports: result = ports[0] print colorize(result, "yellow") return result print colorize("FAILED", "red") raise Abort( "No device matching following was found: %s" % ("".join(["\n - " + p for p in self.serial_port_patterns()])) )
def guess_serial_port(self, serial_port=None): if serial_port is None: print('Guessing serial port ...', end='') else: print('Check serial port %s ...' % serial_port, end='') ports = self.list_serial_ports(serial_port) if ports: result = ports[0] print(colorize(ports[0][1], 'yellow')) return result print(colorize('FAILED', 'red')) return None
def guess_serial_port(self, serial_port=None): if serial_port is None: print 'Guessing serial port ...', else: print 'Check serial port %s ...' % serial_port, ports = self.list_serial_ports(serial_port) if ports: result = ports[0] print colorize(ports[0][1], 'yellow') return result print colorize('FAILED', 'red') return None
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ino', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command ) and x != Command commands = [ cls(e) for _, cls in inspect.getmembers(ino.commands, is_command) ] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: e.process_args(args) if current_command not in 'clean init' and not os.path.isdir( e.build_dir): os.makedirs(e.build_dir) args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()
def discover(self): self.e.find_arduino_file( 'version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self.e: with open(self.e['version.txt']) as f: print 'Detecting Arduino software version ... ', v_string = f.read().strip() v = Version.parse(v_string) self.e['arduino_lib_version'] = v print colorize("%s (%s)" % (v, v_string), 'green') self.e.find_arduino_dir('arduino_core_dir', [ 'hardware', 'arduino', 'cores', 'arduino' ], ['Arduino.h'] if self.e.arduino_lib_version.major else 'WProgram.h', 'Arduino core library') self.e.find_arduino_dir('arduino_libraries_dir', ['libraries'], human_name='Arduino standard libraries') if self.e.arduino_lib_version.major: self.e.find_arduino_dir('arduino_variants_dir', ['hardware', 'arduino', 'variants'], human_name='Arduino variants directory') toolset = [ ('cc', 'avr-gcc'), ('cxx', 'avr-g++'), ('ar', 'avr-ar'), ('objcopy', 'avr-objcopy'), ] # Linux has system-wide avr gcc toolset # other platforms are bundled with it as a part of Arduino Software system_wide = platform.system() == 'Linux' for tool_key, tool_binary in toolset: if system_wide: self.e.find_tool(tool_key, [tool_binary], human_name=tool_binary) else: self.e.find_arduino_tool(tool_key, ['hardware', 'tools', 'avr', 'bin'], items=[tool_binary], human_name=tool_binary)
def load(self): if not os.path.exists(self.dump_filepath): return with open(self.dump_filepath, 'rb') as f: try: self.update(pickle.load(f)) except: print(colorize('Environment dump exists (%s), but failed to load' % self.dump_filepath, 'yellow'))
def _find(self, key, items, places, human_name, join, multi): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found. """ if key in self: return self[key] human_name = human_name or key # expand env variables in `places` and split on colons places = itertools.chain.from_iterable( os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(p) for p in places) print 'Searching for', human_name, '...', results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p if not multi: print colorize(result, 'green') self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print colorize('found multiple: %s' % formatted_results, 'green') else: print colorize(results[0], 'green') self[key] = results return results print colorize('FAILED', 'red') raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))
def discover(self): self.e.find_arduino_file('version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self.e: with open(self.e['version.txt']) as f: print 'Detecting Arduino software version ... ', v_string = f.read().strip() v = Version.parse(v_string) self.e['arduino_lib_version'] = v print colorize("%s (%s)" % (v, v_string), 'green') self.e.find_arduino_dir('arduino_core_dir', ['hardware', 'arduino', 'cores', 'arduino'], ['Arduino.h'] if self.e.arduino_lib_version.major else 'WProgram.h', 'Arduino core library') self.e.find_arduino_dir('arduino_libraries_dir', ['libraries'], human_name='Arduino standard libraries') if self.e.arduino_lib_version.major: self.e.find_arduino_dir('arduino_variants_dir', ['hardware', 'arduino', 'variants'], human_name='Arduino variants directory') toolset = [ ('cc', 'avr-gcc'), ('cxx', 'avr-g++'), ('ar', 'avr-ar'), ('objcopy', 'avr-objcopy'), ] # Linux has system-wide avr gcc toolset # other platforms are bundled with it as a part of Arduino Software system_wide = platform.system() == 'Linux' for tool_key, tool_binary in toolset: if system_wide: self.e.find_tool(tool_key, [tool_binary], human_name=tool_binary) else: self.e.find_arduino_tool(tool_key, ['hardware', 'tools', 'avr', 'bin'], items=[tool_binary], human_name=tool_binary)
def guess_serial_port(self): from glob import glob print "Guessing serial port ...", system = platform.system() if system == "Linux": patterns = ["/dev/ttyACM*", "/dev/ttyUSB*"] elif system == "Darwin": patterns = ["/dev/tty.usbmodem*", "/dev/tty.usbserial*"] for p in patterns: matches = glob(p) if not matches: continue result = matches[0] print colorize(result, "yellow") return result print colorize("FAILED", "red") raise Abort("No device matching following was found: %s" % ("".join(["\n - " + p for p in patterns])))
def guess_serial_port(self): from glob import glob print 'Guessing serial port ...', system = platform.system() if system == 'Linux': patterns = ['/dev/ttyACM*', '/dev/ttyUSB*'] elif system == 'Darwin': patterns = ['/dev/tty.usbmodem*', '/dev/tty.usbserial*'] for p in patterns: matches = glob(p) if not matches: continue result = matches[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort("No device matching following was found: %s" % (''.join(['\n - ' + p for p in patterns])))
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ino-cocoduino', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command) and x != Command commands = [cls(e) for _, cls in inspect.getmembers(ino.commands, is_command)] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: e.process_args(args) if current_command not in 'clean' and not os.path.isdir(e.build_dir): os.mkdir(e.build_dir) args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()
def _find(self, key, items, places, human_name, join): if key in self: return self[key] human_name = human_name or key # expand env variables in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) print 'Searching for', human_name, '...', for p in places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p print colorize(result, 'green') self[key] = result return result print colorize('FAILED', 'red') raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))
def _find(self, key, items, places, human_name, join, multi): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found. """ if key in self: return self[key] human_name = human_name or key # make sure search on current directy first #places.insert(0,'.') # expand env variables in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(os.path.abspath(p)) for p in places) print 'Searching for', human_name, '...', test_func = os.path.isfile if join else os.path.exists results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p #KHAI: Convert to relative path for compatible with # windows and Linux system result = os.path.abspath(result) result = os.path.relpath(result, os.getcwd()) #KHAI added for convert window path to Linux path for # make.exe work in window if platform.system() == "Windows": result = result.split('\\') result = '/'.join(result) if not multi: print colorize(result, 'green') self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print colorize('found multiple: %s' % formatted_results, 'green') else: print colorize(results[0], 'green') self[key] = results return results print colorize('FAILED', 'red') raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))
def sanitize_dirs(environment): if not os.path.isdir(environment.src_dir): raise Abort("Sources directory '%s' not exists!" % environment.src_dir) if not os.path.isdir(environment.build_dir): os.makedirs(environment.build_dir) if not os.path.isdir(environment.lib_dir): os.makedirs(environment.lib_dir) open('lib/.holder', 'w').close() if __name__ == "__main__": cmd_name, args = build_args(environment) try: environment.process_args(args) if cmd_name in ("preprocess", "build", "upload", "build-tests"): sanitize_dirs(environment) args.func(args) except Abort as exc: print(colorize(str(exc), 'red')) sys.exit(1) except KeyboardInterrupt: print('Terminated by user') finally: environment.dump()
def _find(self, key, items, places, human_name, join, multi): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found. """ if key in self: return self[key] human_name = human_name or key or 'Null' # make sure search on current directy first #places.insert(0,'.') # expand env variables in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(os.path.abspath(p)) for p in places) print('Searching for', human_name, '...', end='') test_func = os.path.isfile if join else os.path.exists results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p #KHAI: Convert to relative path for compatible with # windows and Linux system result = os.path.abspath(result) result = os.path.relpath(result, os.getcwd()) #KHAI added for convert window path to Linux path for # make.exe work in window if platform.system() == "Windows": result = result.split('\\') result = '/'.join(result) if not multi: print(colorize(result, 'green')) self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print(colorize('found multiple: %s' % formatted_results, 'green')) else: print(colorize(results[0], 'green')) self[key] = results return results print(colorize('FAILED', 'red')) raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places])))