def check_output(*args, **kwargs): """ return output from the sub-process """ #silent subprocess execution closeNULL = 0 import subprocess as sp try: from subprocess import DEVNULL closeNULL = 0 except ImportError: DEVNULL = open(os.devnull, 'wb') closeNULL = 1 #execute command in subprocess process = sp.Popen(stdout=sp.PIPE, stderr=DEVNULL, *args, **kwargs) output, unused_err = process.communicate() retcode = process.poll() #close the process if closeNULL: DEVNULL.close() #if error occured raise error if retcode: cmd = kwargs.get("args") if cmd is None: cmd = args[0] error = sp.CalledProcessError(retcode, cmd) error.output = output raise error return output
def snmp_set_via_cli(oid, value, type): """ Sets an SNMP variable using the snmpset command. :param oid: the OID to update :param value: the new value to set the OID to :param type: a single character type as required by the snmpset command (i: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS o: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING, b: BITS U: unsigned int64, I: signed int64, F: float, D: double) """ DEVNULL = open(devnull, "w") process = Popen( "snmpset -v2c -c public localhost:11161 {} {} {}".format( oid, type, '"{}"'.format(value) if type == "s" else value), stdout=DEVNULL, stderr=DEVNULL, shell=True, ) process.communicate() if process.returncode != 0: raise SNMPSetCLIError("failed to set {0} to {1} (type {2})".format( oid, value, type)) DEVNULL.close()
def check_output(*popenargs, **kwargs): closeNULL = 0 try: from subprocess import DEVNULL closeNULL = 0 except ImportError: import os DEVNULL = open(os.devnull, 'wb') closeNULL = 1 process = sp.Popen(stdout=sp.PIPE, stderr=DEVNULL, *popenargs, **kwargs) output, unused_err = process.communicate() retcode = process.poll() if closeNULL: DEVNULL.close() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] error = sp.CalledProcessError(retcode, cmd) error.output = output raise error return output
def createLatex(packageName): initialPath = os.getcwd() packageTestPath = 'platform_test_' + packageName latexPath = os.path.join(documentPath, packageTestPath, 'latex') os.chdir(latexPath) try: from subprocess import DEVNULL except ImportError: DEVNULL = open(os.devnull, 'wb') subprocess.call(['make'], stdout=DEVNULL, stderr=DEVNULL) DEVNULL.close() shutil.copy('refman.pdf', '../' + packageName + '.pdf') os.chdir(initialPath)
def createLatex( packageName ): initialPath = os.getcwd() packageTestPath = 'platform_test_' + packageName latexPath = os.path.join( documentPath, packageTestPath , 'latex' ) os.chdir( latexPath ) try: from subprocess import DEVNULL except ImportError: DEVNULL = open(os.devnull, 'wb') subprocess.call( ['make'], stdout = DEVNULL, stderr = DEVNULL ) DEVNULL.close() shutil.copy( 'refman.pdf', '../' + packageName + '.pdf') os.chdir( initialPath )
def check_output(*popenargs, **kwargs): closeNULL = 0 try: from subprocess import DEVNULL closeNULL = 0 except ImportError: import os try: DEVNULL = open(os.devnull, 'wb') except FileNotFoundError: DEVNULL = open('null', 'wb') closeNULL = 1 startupinfo = sp.STARTUPINFO() startupinfo.dwFlags = sp.CREATE_NEW_CONSOLE | sp.STARTF_USESHOWWINDOW startupinfo.wShowWindow = sp.SW_HIDE try: process = sp.Popen(stdout=sp.PIPE, stderr=DEVNULL, *popenargs, **kwargs, startupinfo=startupinfo) except FileNotFoundError: DEVNULL = open('null', 'wb') closeNULL = 1 process = sp.Popen(stdout=sp.PIPE, stderr=DEVNULL, *popenargs, **kwargs, startupinfo=startupinfo) output, unused_err = process.communicate() retcode = process.poll() if closeNULL: DEVNULL.close() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] error = sp.CalledProcessError(retcode, cmd) error.output = output raise error return output
def createDoxy(packagePath, packageName, packageTransformedName, testPkg): # Create temp configuration doxyConf = './doxy_conf_platform_tests' tempConf = doxyConf + packageName shutil.copy(doxyConf, tempConf) # General params projectName = "\"RAPP Platform Tests - " + packageTransformedName + " \"" if testPkg: packageTestPath = packagePath else: packageTestPath = os.path.join(packagePath, 'tests') outputFile = './platform_test_' + packageName # Doxygen Configuration params doxInput = 'INPUT = ' + packageTestPath doxPatterns = 'FILE_PATTERNS = ' doxProjectName = 'PROJECT_NAME = ' + projectName doxOutput = 'OUTPUT_DIRECTORY = ' + outputFile # Temp configuration modification doxyFD = open(tempConf, 'a') doxyFD.write(doxInput + '\n') doxyFD.write(doxPatterns + '\n') doxyFD.write(doxProjectName + '\n') doxyFD.write(doxOutput + '\n') doxyFD.close() try: from subprocess import DEVNULL except ImportError: DEVNULL = open(os.devnull, 'wb') subprocess.call(['doxygen', tempConf], stdout=DEVNULL, stderr=DEVNULL) DEVNULL.close() os.remove(tempConf) shutil.move(outputFile, documentPath)
def createDoxy( packagePath, packageName, packageTransformedName, testPkg): # Create temp configuration doxyConf = './doxy_conf_platform_tests' tempConf = doxyConf + packageName shutil.copy( doxyConf, tempConf ) # General params projectName = "\"RAPP Platform Tests - " + packageTransformedName + " \"" if testPkg: packageTestPath = packagePath else: packageTestPath = os.path.join( packagePath, 'tests' ) outputFile = './platform_test_' + packageName # Doxygen Configuration params doxInput = 'INPUT = ' + packageTestPath doxPatterns = 'FILE_PATTERNS = ' doxProjectName = 'PROJECT_NAME = ' + projectName doxOutput = 'OUTPUT_DIRECTORY = ' + outputFile # Temp configuration modification doxyFD = open( tempConf, 'a' ) doxyFD.write( doxInput + '\n' ) doxyFD.write( doxPatterns + '\n' ) doxyFD.write( doxProjectName + '\n' ) doxyFD.write( doxOutput + '\n' ) doxyFD.close() try: from subprocess import DEVNULL except ImportError: DEVNULL = open(os.devnull, 'wb') subprocess.call( ['doxygen', tempConf], stdout = DEVNULL, stderr = DEVNULL ) DEVNULL.close() os.remove( tempConf ) shutil.move( outputFile, documentPath )
def __call__(self, request=None): """ Executes either man page for LMIShell or prints pydoc help for an object. """ if request is None: try: # Python 3+ from subprocess import DEVNULL as devnull devnull_close = lambda: None except ImportError: devnull = open(os.devnull, "wb") devnull_close = lambda: devnull.close() rcode = subprocess.call(["man", "lmishell"], stderr=devnull) devnull_close() if rcode > 0: sys.stderr.write("Man page for lmishell can not be found, ") sys.stderr.write("refer to official documentation.\n\n") sys.stderr.write("Available at: http://www.openlmi.org\n") else: import pydoc return pydoc.help(request)
def local(command, **kwargs): "preprocesses given `command` and options before executing it locally using Python's `subprocess.Popen`" base_kwargs = { "use_shell": True, "combine_stderr": True, "capture": False, "timeout": None, "quiet": False, "warn_only": False, # https://github.com/mathiasertl/fabric/blob/master/fabric/state.py#L301-L305 "abort_exception": RuntimeError, } global_kwargs, user_kwargs, final_kwargs = handle(base_kwargs, kwargs) # TODO: once py2 support has been dropped, move this back to file head devnull_opened = False try: from subprocess import DEVNULL # py3 except ImportError: devnull_opened = True DEVNULL = open(os.devnull, "wb") if final_kwargs["capture"]: if final_kwargs["combine_stderr"]: out_stream = subprocess.PIPE err_stream = subprocess.STDOUT else: out_stream = subprocess.PIPE err_stream = subprocess.PIPE else: if final_kwargs["quiet"]: # we're not capturing and we've been told to be quiet # send everything to /dev/null out_stream = DEVNULL err_stream = DEVNULL else: out_stream = None err_stream = None if not final_kwargs["use_shell"] and not isinstance(command, list): raise ValueError("when shell=False, given command *must* be a list") if final_kwargs["use_shell"]: command = shell_wrap_command(command) proc = subprocess.Popen( command, shell=final_kwargs["use_shell"], stdout=out_stream, stderr=err_stream ) if final_kwargs["timeout"]: timer = Timer(final_kwargs["timeout"], proc.kill) try: timer.start() # proximity matters stdout, stderr = proc.communicate() finally: timer.cancel() else: stdout, stderr = proc.communicate() # https://github.com/mathiasertl/fabric/blob/master/fabric/operations.py#L1240-L1244 result = { "return_code": proc.returncode, "failed": proc.returncode != 0, "succeeded": proc.returncode == 0, "command": command, "stdout": (stdout or b"").decode("utf-8").splitlines(), "stderr": (stderr or b"").decode("utf-8").splitlines(), } if devnull_opened: DEVNULL.close() if result["succeeded"]: return result err_msg = "local() encountered an error (return code %s) while executing %r" % ( result["return_code"], command, ) if final_kwargs["warn_only"]: LOG.warning(err_msg) return result abort_exc_klass = final_kwargs["abort_exception"] exc = abort_exc_klass(err_msg) setattr(exc, "result", result) raise exc