def _check_directory(directory): """Raise exception if directory does not exist.""" if directory is not None: if not exists(directory): raise CommandError( "Cannot run command - directory {0} does not exist".format( directory)) if not isdir(directory): raise CommandError( "Cannot run command - specified directory {0} is not a directory." .format(directory))
def __getattr__(self, name): # Make tab autocompletion work in IPython if name in [ "bin_directory", "_added_commands", ]: return self.__dict__[name] if name == "__methods__": return None if name == "trait_names" or name == "_getAttributeNames": return list( set(self._added_commands.keys()) | set(self._directory_commands().keys())) # If command is in _added_commands use that first if name in self._added_commands: return self._added_commands[name] dir_cmds = self._directory_commands() if name in dir_cmds.keys(): return dir_cmds[name] raise CommandError( "Command {0} not found in {1} or added commands: {2}".format( name, abspath(self.bin_directory), str(self._added_commands)))
def __setattr__(self, name, value): if name in ["bin_directory", "_added_commands"]: self.__dict__[name] = value return if type(value) != Command: raise CommandError( "Command {0} must be of type commandlib.Command".format( value.__repr__())) self._added_commands[name] = value
def run(self): """Run command and wait until it finishes.""" _check_directory(self.directory) with DirectoryContextManager(self.directory): process = subprocess.Popen(self.arguments, shell=self._shell, env=self.env) _, _ = process.communicate() returncode = process.returncode if returncode != 0 and not self._ignore_errors: raise CommandError( '"{0}" failed (err code {1})'.format(self.__repr__(), returncode) )
def __getattr__(self, name): commands = {} for filename in listdir(self._directory): absfilename = join(self._directory, filename) if isfile(absfilename) and access(absfilename, os.X_OK): object_name = filename.replace(".", "_").replace("-", "_") commands[object_name] = Command(absfilename).with_path(self._directory) if name == "__methods__": return list(commands.keys()) if name in commands: return commands[name] else: raise CommandError("'{0}' not found in '{1}'".format(name, self._directory))
def __init__(self, directory): """ Initialize a group of commands in the directory specified. Each of those commands will also be run with the directory added to the PATH environment variable. Commands can also be added to the object at any time. """ directory = abspath(str(directory)) if not exists(directory): raise CommandError( "Can't create CommandPath object: {0} does not exist.".format(directory) ) self._directory = directory
def __init__(self, bin_directory=None): """ Initialize a group of commands. If bin_directory is specified, a dynamically updated command group from the list of commands in the specified directory will be created. Each of those commands will also be run with the directory added to the PATH environment variable. Commands can also be added to the object at any time. """ if bin_directory is not None: bin_directory = abspath(str(bin_directory)) if not exists(bin_directory): raise CommandError( "Can't create Commands object: {0} does not exist.".format( bin_directory)) self.bin_directory = bin_directory self._added_commands = {}
def _type_check_command(command): """Raise exception if non-Command object passed.""" if type(command) != Command: raise CommandError("Command must be of type commandlib.Command")
def run(self): _check_directory(self._command.directory) previous_directory = getcwd() if ( self._from_handle is None and self._from_string is None and self._from_filename is None ): stdin = None else: if self._from_string: stdin = PIPE if self._from_handle: stdin = self._from_handle if self._from_filename: stdin = open(self._from_filename, "r") if self._stdout_to_handle is None and self._stdout_to_filename is None: stdout = None else: if self._stdout_to_handle: stdout = self._stdout_to_handle if self._stdout_to_filename: stdout = open(self._stdout_to_filename, "w") if self._stderr_to_handle is None: stderr = PIPE else: if self._stderr_to_handle: stderr = self._stderr_to_handle if self._command.directory is not None: chdir(self._command.directory) try: process = Popen( self._command.arguments, stdout=stdout, stderr=stderr, stdin=stdin, shell=self._command._shell, env=self._command.env, ) if self._from_string: process.stdin.write(self._from_string.encode("utf8")) _, _ = process.communicate() returncode = process.returncode finally: if self._from_filename: stdin.close() if self._stdout_to_filename: stdout.close() chdir(previous_directory) if returncode != 0 and not self._command._ignore_errors: raise CommandError( '"{0}" failed (err code {1})'.format(self.__repr__(), returncode) )