def kmeans_gen(self, l=10): for _ in range(l): builder = CommandBuilder(self) cmd = builder.kmeans_random_command() assert cmd is not None # print(cmd) self.take_command(cmd)
def __init__(self): self.started = False self.matlab = None self.quarc = True self.external = True self.build = True self.external = self.quarc & self.external self.build = self.quarc & self.build self.commandBuilder = CommandBuilder()
def __init__(self, drawer, exception_handler): super().__init__(drawer) self.regex_pattern = r'(^[a-zA-Z]\b)\s+?(-?\b\d+\.?\d?\b)?\s*?([#|//].*)?$' self.__output_log = [] self.exception_handler = exception_handler try: self.command_builder = CommandBuilder() except Exception as e: self.exception_handler.display_and_exit(e)
def rand_gen(self, l=10): for _ in range(l): # self.tree_.dump_tree() builder = CommandBuilder(self) cmd = builder.random_command() assert cmd is not None # print(cmd) self.take_command(cmd)
def env(self): command = CommandBuilder('docker-machine', 'env') if self.local: command.append('-u') else: command.append(self.name) return command.run()
def __init__(self, name=False, url=False): if not name and url: name = CommandBuilder('bash', '-c', 'docker-machine ls | tail -n +2 | grep "%s" ' '| sed "s/\s.*//"' % url).run() self.local = not name if self.local: name = '127.0.0.1' self.name = name
def logs(self, tail=100): if self.name: return self.base_command().append( 'logs', '--follow', '--tail', str(int(tail)), self.name).run(replaceForeground=True) else: return CommandBuilder( 'bash', '-c', """ set -m trap 'kill $(jobs -p)' 2 count=0 for c in $(docker ps --format "{{.Names}}"); do color="$(echo "$c" | md5 | cut -c1-4)" color=$((31 + ((16#${color}) % 7) )) docker logs -f $c | sed "s/^/\033[1;${color}m$c | \033[0;00m/" & count+=1 done wait """).run(replaceForeground=True)
def ip(self): return CommandBuilder('docker-machine', 'ip', self.name).run()
def create(self, driver, **config): command = CommandBuilder('docker-machine', 'create') command.append('--driver', driver) if config.get('swarm_token') is not None: command.append('--swarm') command.append('--swarm-discovery', 'token://{token}'.format(config.get('swarm_token'))) if config.get('swarm_master') is not None: command.append('--swarm-master') if config.get('registry_mirror') is not None: ip = DockerMachine(config.get('registry_mirror')).ip() if not ip: printe('IP for the registry machine could not be determined. ' 'Does that machine have an IP?', terminate=True) command.append('--engine-registry-mirror', 'http://%s:5000' % ip) if config.get('experimental') is not None: command.append('--engine-install-url', 'https://experimental.docker.com') if config.get('neighbor_machine') is not None \ and not config.get('multihost_networking') is not None: printe('Neighbor machine was provided but multihost networking ' 'was not enabled explicitly. Multihost networking must be ' 'enabled if neighboring machine is to be used.', terminate=2) if config.get('multihost_networking') is not None: command.append('--engine-opt', 'default-network=overlay:multihost') command.append('--engine-label', 'com.docker.network.driver.overlay.' 'bind_interface=eth0') if config.get('neighbor_machine') is not None: command.append('--engine-label', 'com.docker.network.driver.overlay.' 'neighbor_ip={ip}'.format( DockerMachine( config.get('neighbor_machine')).ip())) if config.get('consul') is not None: if isinstance(config.get('consul'), str): consul_name = config.get('consul') else: consul_name = 'consul' command.append('--engine-opt', 'kv-store=consul:{ip}:8500'.format( DockerMachine(consul_name).ip())) command.append(self.name) return command.run()
def list(): return CommandBuilder('docker-machine', 'ls').run(replaceForeground=True)
def stop(self): if self.local: printe("Machine name not provided: Won't try to stop local.") return CommandBuilder('docker-machine', 'stop', self.name).run()
def remove(self): if self.local: printe("Machine name not provided: Cannot remove a local Docker " "instance.") return CommandBuilder('docker-machine', 'rm', self.name).run()
def config(self): if self.local: return False return CommandBuilder('docker-machine', 'config', self.name).run().split()
class TigrParser(AbstractParser): def __init__(self, drawer, exception_handler): super().__init__(drawer) self.regex_pattern = r'(^[a-zA-Z]\b)\s+?(-?\b\d+\.?\d?\b)?\s*?([#|//].*)?$' self.__output_log = [] self.exception_handler = exception_handler try: self.command_builder = CommandBuilder() except Exception as e: self.exception_handler.display_and_exit(e) @property def output_log(self): # readonly return self.__output_log def parse(self, raw_source): if type( raw_source ) == str: # defensively handles edge case where a single command was passed as a string raw_source = [raw_source] for line_number in range(0, len(raw_source) - 1): current_line = self.__prepare_line(raw_source[line_number]) if self.__is_line_blank(current_line): continue try: command, data = self.__parse_line(current_line) prepared_command = self.command_builder.prepare_command( command, data) output = prepared_command.execute(self.drawer) self._log_drawer_output(output) except Exception as e: self.exception_handler.display_and_exit( e, line_number=line_number, line=current_line) def __prepare_line(self, line): return line.strip() def __is_line_blank(self, line): return not line def __parse_line(self, line): match = re.findall(self.regex_pattern, line) if match: groups = match[0] command = groups[0].upper() if groups[1]: data = int(round(float(groups[1]))) """ Parser accepts decimals but silently rounds them in the background - all numbers passed are stored as integers""" else: data = None return command, data else: raise SyntaxError(f"Invalid Syntax") def _log_drawer_output(self, output): self.__output_log.append(output)
def ssh(self): if self.local: printe("Machine name not provided: Won't try to ssh to local.") return CommandBuilder('docker-machine', 'ssh', self.name).run(replaceForeground=True)
def base_command(self): return CommandBuilder('docker')
class MatlabConnector(object): def __init__(self): self.started = False self.matlab = None self.quarc = True self.external = True self.build = True self.external = self.quarc & self.external self.build = self.quarc & self.build self.commandBuilder = CommandBuilder() def connect(self): if self.matlab == None: names = matlab.engine.find_matlab() if not names: # self.matlab = matlab.engine.start_matlab() # use matlab.engine.start_matlab('-desktop') for debug try: future = matlab.engine.start_matlab( async=True) # use async=True in old versions except: future = matlab.engine.start_matlab( background=True) # use background=True for async self.matlab = future.result() print("Opened new MATLAB session") else: # self.matlab = matlab.engine.connect_matlab(names[0]) # TODO: Control which shared session connect to? try: future = matlab.engine.connect_matlab(names[0], async=True) except: future = matlab.engine.connect_matlab(names[0], background=True) self.matlab = future.result() print("Connected to existing MATLAB session: " + names[0]) def stop(self): self._stop(self.model['name']) def disconnect(self): self._stop(self.model['name']) self._close_system(self.model['name']) self._exitThis() def set(self, variables, values): if isinstance(variables, list): size = len(variables) for i in range(size): try: name, value = variables[i], values[i] tomodelworkspace = self.commandBuilder.to_model_workspace( name, value) try: self.matlab.eval(tomodelworkspace, async=True, nargout=0) except: self.matlab.eval(tomodelworkspace, background=True, nargout=0) except: pass else: try: name, value = variables, values tomodelworkspace = self.commandBuilder.to_model_workspace( name, value) try: self.matlab.eval(tomodelworkspace, async=True, nargout=0) except: self.matlab.eval(tomodelworkspace, background=True, nargout=0) except: pass self._update(self.model['name']) def get(self, variables): future_value = {} variables_names = [] variables_values = [] if self.external: data = self.qtcpc.read(variables) for name in variables: try: variables_names.append(name) variables_values.append(data[name]) except: pass else: for name in variables: try: if name == "SimulationTime": try: future_value[name] = self.matlab.get_param( self.model['name'], name, background=True) except: future_value[name] = self.matlab.get_param( self.model['name'], name, async=True) else: try: future_value[name] = self.matlab.eval( name + '.InputPort(1).Data', background=True) except: future_value[name] = self.matlab.eval( name + '.InputPort(1).Data', async=True) except: pass for name in variables: try: variables_names.append(name) if name == "SimulationTime": variables_values.append(future_value[name].result()) else: variables_values.append(future_value[name].result()) except: pass return np.array([variables_names, variables_values]).tolist() def open(self, path): """" Open a Simulink model """ try: dirname = os.path.dirname(path) filename = os.path.basename(path) modelname = os.path.splitext(filename)[0] self.model = { 'path': dirname, 'file': filename, 'name': modelname, } version = self.matlab.version('-release') if version >= '2018b': self.matlab.eval('Simulink.sdi.setAutoArchiveMode(false);', nargout=0) self.matlab.eval('Simulink.sdi.setArchiveRunLimit(0);', nargout=0) self._load(self.model) self._settime(self.model['name']) self._getmodelworkspace(self.model['name']) self._buildandconnect(self.model['name']) # for quarc only except: print("Could not open simulink model " + path) def close_system(self): self._close_system(self.model['name']) def exit(self): self._exitThis() def _load(self, model): version = self.matlab.version('-release') self.matlab.cd(model['path']) autosavedfile = model['file'] + '.autosave' cachedfile = model['file'] + 'c' if version >= '2017b': if self.matlab.isfile(autosavedfile): self.matlab.delete(autosavedfile, nargout=0) if self.matlab.isfile(cachedfile): self.matlab.delete(cachedfile, nargout=0) else: if self.matlab.exist(autosavedfile, 'file') == 2: self.matlab.delete(autosavedfile, nargout=0) if self.matlab.exist(cachedfile, 'file') == 2: self.matlab.delete(cachedfile, nargout=0) if version >= '2018b': if not self.matlab.slreportgen.utils.isModelLoaded(model['name']): self.matlab.load_system(model['file']) else: self.matlab.load_system(model['file']) print("Loaded Simulink model") def _getmodelworkspace(self, model): getmodelworkspace = self.commandBuilder.get_model_workspace(model) self.matlab.eval(getmodelworkspace, nargout=0) def _settime(self, model): self.matlab.set_param(model, "StartTime", "0", nargout=0) self.matlab.set_param(model, "StopTime", "inf", nargout=0) self.matlab.set_param(model, 'SolverType', 'Fixed-step', nargout=0) if not self.external: self.matlab.set_param(model, 'FixedStep', '0.06', nargout=0) def start(self): if self.quarc: if self.external: self.matlab.qc_set_simulation_mode(self.model['name'], 'external', nargout=0) else: self.matlab.qc_set_simulation_mode(self.model['name'], 'normal', nargout=0) self.matlab.qc_start_model(self.model['name'], async=True, nargout=0) else: try: self.matlab.qc_set_simulation_mode(self.model['name'], 'normal', nargout=0) except: pass self.matlab.set_param(self.model['name'], "SimulationCommand", "start", async=True, nargout=0) self.started = True def creatertos(self, readables): if self.external: self.qtcpc = QuarcTCPClient(18000) self.qtcpc.config( ['double'] * len(readables)) # TODO: Add support for other data types else: for readable in readables: if readable != "SimulationTime": try: self.matlab.eval(readable + "=get_param('" + self.model['name'] + "/" + readable + "', 'RuntimeObject');", async=True, nargout=0) except: self.matlab.eval(readable + "=get_param('" + self.model['name'] + "/" + readable + "', 'RuntimeObject');", background=True, nargout=0) def _pause(self, model): self.matlab.set_param(model, "SimulationCommand", "pause", nargout=0) def ispaused(self): return self.matlab.get_param(self.model['name'], "SimulationStatus") def _buildandconnect(self, model): self.matlab.set_param(model, 'AccelVerboseBuild', 'off', nargout=0) self.matlab.save_system(model, nargout=0) try: if self.external: if self.build: self.matlab.qc_build_model(model, nargout=0) print("Quarc model has been built") if not self.matlab.qc_is_model_loaded(model): self.matlab.qc_download_model(model, nargout=0) print("Quarc model downloaded in target") self.matlab.qc_load_model(model, nargout=0) print("Quarc model loaded in target") self.matlab.qc_connect_model(model, nargout=0) print("Simulink model connected to quarc target") except: pass def _update(self, model): if self.external: try: self.matlab.qc_update_model(model, async=True, nargout=0) except: self.matlab.qc_update_model(model, background=True, nargout=0) else: try: self.matlab.set_param(model, "SimulationCommand", "update", async=True, nargout=0) except: self.matlab.set_param(model, "SimulationCommand", "update", background=True, nargout=0) def resume(self): try: self.matlab.set_param(self.model['name'], "SimulationCommand", "continue", async=True, nargout=0) except: self.matlab.set_param(self.model['name'], "SimulationCommand", "continue", background=True, nargout=0) def _stop(self, model): if self.external: self.qtcpc.close() self.matlab.qc_stop_model(model) self.matlab.qc_connect_model(model, nargout=0) else: self.matlab.set_param(model, "SimulationCommand", "stop", nargout=0) self.started = False def _close_system(self, model): self.matlab.close_system(model) def _exitThis(self): self.qtcpc.close() self.matlab.quit()