def run(self, argv, name=None, target=None): options = self.options(argv) name = options.name[0] validate_name(name, self.template_type) # Check that the name cannot be imported. try: import_module(name) except ImportError: pass else: raise lux.CommandError("%r conflicts with the name of an existing " "Python module and cannot be used as a " "%s name.\nPlease try another name." % (name, self.template_type)) # # if some directory is given, make sure it's nicely expanded if target is None: top_dir = path.join(os.getcwd(), name) try: os.makedirs(top_dir) except OSError as e: if e.errno == errno.EEXIST: message = "'%s' already exists" % top_dir else: message = e raise lux.CommandError(message) else: top_dir = path.abspath(path.expanduser(target)) if not path.exists(top_dir): raise lux.CommandError("Destination directory '%s' does not " "exist, please create it first." % top_dir) self.build(name, top_dir) self.write('%s "%s" created' % (self.template_type, name))
def validate_name(name, app_or_project): # If it's not a valid directory name. if not re.search(r'^[_a-zA-Z]\w*$', name): # Provide a smart error message, depending on the error. if not re.search(r'^[_a-zA-Z]', name): message = 'make sure the name begins with a letter or underscore' else: message = 'use only numbers, letters and underscores' raise lux.CommandError("%r is not a valid %s name. Please %s." % (name, app_or_project, message))
def run(self, options): self.template_dir = path.join(path.dirname(__file__), 'templates') if options.template_list: for name in os.listdir(self.template_dir): dir = os.path.join(self.template_dir, name) if os.path.isdir(dir) and not name.startswith('_'): self.write(name) else: template = options.template template_dir = path.join(self.template_dir, template) if not os.path.isdir(template_dir): raise lux.CommandError('Unknown template project "%s"' % template) if not options.luxname: raise lux.CommandError("A project name is required") name = options.luxname validate_name(name) self.target = path.join(os.getcwd(), '%s-project' % name) if path.exists(self.target): raise lux.CommandError("%r conflicts with an existing path" % self.target) # Check that the name cannot be imported. try: import_module(name) except ImportError: pass else: raise lux.CommandError("%r conflicts with the name of an " "existing Python module and cannot be " "used as a %s name.\n" "Please try another name." % (name, self.template_type)) # # if some directory is given, make sure it's nicely expanded try: os.makedirs(self.target) except OSError as e: raise lux.CommandError(str(e)) self.build(options.template, name) self.write('Project "%s" created' % name)
def flash_write(self, page_no, data): """ Write a chunk of data to flash memory at `addr` """ assert len(data) <= 1024, ValueError("Invalid length, must be <=1016") result, crc = self.command(self.BL_CMD_WRITE, data, index=page_no, get_crc=True) if result != data: if result[1:5] == crc: raise lux.CommandError( "Flash write command returned error code {}".format( result[0])) else: raise lux.ValidationError( "Validate after write failed: {}, page #{}".format( len(data), page_no + 1))
def flash_device(dev, lux_address, binary, start_address): CHUNK_SIZE = 512 data = binary.read() print "Loaded binary file. Len: {} (0x{:04x})".format(len(data), len(data)) print "Start address: ", hex(start_address) print "End address: ", hex(start_address + len(data)) print "Checking for bootloader..." dev.trigger_bootloader(lux_address) print "Success!" print "Invalidating app..." dev.invalidate_app() dev.flash_baseaddr(start_address) PAGE_SIZE = 1024 total_page_count = (len(data) + PAGE_SIZE - 1) / PAGE_SIZE index = 0 while data: page, data = data[:PAGE_SIZE], data[PAGE_SIZE:] print "Writing flash {}/{} ({} bytes)".format(index + 1, total_page_count, len(page)) retry = 5 for i in range(retry): try: dev.flash_erase(start_address + PAGE_SIZE * index) dev.get_id() dev.flash_write(index, page) break except lux.CommandError as e: print e time.sleep(5) else: raise lux.CommandError("Unable to flash page #{}".format(index + 1)) time.sleep(1) index += 1 print "Done writing; resetting" dev.reset() print "Checking response..." new_device = lux.Device(dev.bus, lux_address) print new_device.get_id()