def _execute(self, q330): if self.action == 'read': if os.path.isfile(self.file): confirmation = 'M' first = True while confirmation.lower() not in ('y','n',''): if first: first = False else: print "Please enter 'y' or 'n'" confirmation = raw_input("%s: file exists. Do you wish to overwrite it? (y/N): " % os.path.abspath(self.file)) if confirmation.lower() != 'y': print "Clone operation aborted." return elif os.path.exists(self.file): print "%s: path exists. Please select a different path." % os.path.abspath(self.file) return profile = Profile.Profile() profile.verbosity = self.verbosity profile.tries = self.tries profile.subset = self.subset print "Reading configuration from Q330..." profile.readFromQ330(q330) #print "====================== PROFILE =========================" #print profile print "Writing configuration to file..." Profile.writeProfileToFile(profile, fileName=self.file) if self.verbosity > 1: print profile print "Done." elif self.action in ('write', 'edit'): if self.action == 'edit': print "Reading configuration from Q330..." profile = Profile.Profile() profile.readFromQ330(q330) else: print "Reading configuration from file..." profile = Profile.getProfileFromFile(self.file) profile.verbosity = self.verbosity profile.tries = self.tries profile.subset = self.subset profile.write_base_port = self.write_base_port profile.write_ethernet_ip_address = self.write_ethernet_ip_address profile.write_ethernet_mac_address = self.write_ethernet_mac_address profile.write_slave_processor_parameters = self.write_slave_processor_parameters profile.write_web_page = self.write_web_page # Modify the profile if supplemental configuration was supplied config_type,config_info = self.config supplement = {} if config_type == 'text': print "Parsing supplemental configuration from string." supplement = Config.raw_to_config(config_info) elif config_type == 'file': print "Reading supplemental configuration from file '%s'." % config_info raw = '' try: store = Store.StoreFile(config_info) raw = store.read() except IOError, e: raise QTools.QExMessage("Error parsing supplemental configuration: %s" % str(e)) supplement = Config.raw_to_config(raw) elif config_type == 'sqlite': # 'file?station' parts = config_info.rsplit('?', 1) if len(parts) != 2: raise QTools.QExArgs("Invalid specifier for SQLite database") file,station = parts if len(file) < 1: raise QTools.QExArgs("Invalid specifier for SQLite database") if len(station) < 1: raise QTools.QExArgs("Invalid specifier for SQLite database") if not os.path.exists(file): raise QTools.QExMessage("%s: path does not exist" % file) if not os.path.isfile(file): raise QTools.QExMessage("%s: path is not a regular file" % file) print "Acquiring supplemental configuration from SQLite database '%s'." % file raw = '' try: store = Store.StoreSQLite(file) raw = store.read("SELECT config FROM configurations WHERE station=? ORDER BY timestamp DESC LIMIT 1", (station,))[0][0] except Store.StoreError, e: raise QTools.QExMessage("Error retrieving SQLite supplemental configuration: %s" % str(e)) supplement = Config.raw_to_config(raw, b64=True)
def _run(self): QTools.assert_arg_count(self.args, 'gte', 1) arg_count = 3 if self.args[0] == 'indices': print "Section Indices:" for value in sorted(Profile.section_map.values()): index,_,description,_,_ = value print " % 3d : %s" % (index, description) return elif self.args[0] == 'list': QTools.assert_arg_count(self.args, 'eq', 2) config_file = self.args[1] print "Reading clone from %s ..." % os.path.abspath(config_file) profile = Profile.getProfileFromFile(config_file) profile.listFileContents() return elif self.args[0] == 'diff': QTools.assert_arg_count(self.args, 'eq', 3) file1 = self.args[1] file2 = self.args[2] print "Reading configuration from %s..." % file1, sys.stdout.flush() try: profile1 = Profile.getProfileFromFile(file1) except: print "Failed!" return print "Done." print "Reading configuration from %s..." % file2, sys.stdout.flush() try: profile2 = Profile.getProfileFromFile(file2) except: print "Failed!" return print "Done." profile1.diff(profile2) return if (len(self.args) > 1) and (self.args[1] == 'edit'): arg_count = 2 QTools.assert_arg_count(self.args, 'eq', arg_count) arg_device_id = self.args[0] try: int(arg_device_id) except: raise QTools.QExArgs("invalid type for argument 'device'") if not self.comm.set_from_file(arg_device_id, self.options.reg_file): raise QTools.QExArgs("invalid entry for Q330 #%d in configuration" % int(arg_device_id)) config_count = 0 config_type = '' config_subset = None mysql_password = '' if self.options.subset and len(self.options.subset): try: config_subset = QTools.expand_int_list_set(self.options.subset) except ValueError: raise QTools.QExArgs("invalid value(s) for option '--subset'") if self.options.config_file: config_info = self.options.config_file config_type = 'file' config_count += 1 if self.options.config_sqlite: if not Store.SQLITE: raise QTools.QExMessage("SQLite support is not included with this python install") config_info = self.options.config_sqlite config_type = 'sqlite' config_count += 1 if self.options.config_mysql: if not Store.MYSQL: raise QTools.QExMessage("MySQL support is not included with this python install") config_info = self.options.config_mysql config_type = 'mysql' config_count += 1 if self.options.config_text: config_info = self.options.config_text config_type = 'text' config_count += 1 if self.options.mysql_passwd: mysql_password = self.options.mysql_passwd try: action = QClone.QClone() action.action = self.args[1] if action.action != "edit": action.file = self.args[2] action.subset = config_subset action.verbosity = self.options.verbosity action.mysql_password = mysql_password if self.options.max_tries: action.tries = self.options.max_tries if self.options.force: action.confirm = False if config_count > 1: raise QTools.QExArgs("You may only select one of --config-[file|sqlite|mysql|text]") elif config_count == 1: action.config = (config_type, config_info) if self.options.write_base_port: action.write_base_port = True if self.options.write_ethernet_ip_address: action.write_ethernet_ip_address = True if self.options.write_ethernet_mac_address: action.write_ethernet_mac_address = True if self.options.write_slave_processor_parameters: action.write_slave_processor_parameters = True if self.options.write_web_page: action.write_web_page = True except TypeError, msg: raise QTools.QExArgs("invalid type for argument '%s'" % msg.__str__())