Python subprocess.DEVNULL() Examples

The following are code examples for showing how to use subprocess.DEVNULL(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: raveberry   Author: raveberry   File: youtube.py    GNU Lesser General Public License v3.0 8 votes vote down vote up
def fetch(self):
        # check if file was already downloaded and only download if necessary
        location = song_utils.path_from_id(self.info_dict['id'])
        if location is not None:
            return location

        with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
            ydl.download([self.target])

        location = song_utils.path_from_id(self.info_dict['id'])
        base = os.path.splitext(location)[0]
        thumbnail = base + '.jpg'
        try:
            os.remove(thumbnail)
        except FileNotFoundError:
            self.musiq.base.logger.info('tried to delete ' + thumbnail + ' but does not exist')

        try:
            # tag the file with replaygain to perform volume normalization
            subprocess.call(['aacgain', '-q', '-c', location], stdout=subprocess.DEVNULL)
        except OSError as e:
            if e.errno == errno.ENOENT:
                # the aacgain package was not found. Skip normalization
                pass
            else:
                raise
                
        return location 
Example 2
Project: raveberry   Author: raveberry   File: downloader.py    GNU Lesser General Public License v3.0 8 votes vote down vote up
def fetch(self, target):
        # check if file was already downloaded and only download if necessary
        location = song_utils.path_from_id(self.info_dict['id'])
        if location is not None:
            return location

        with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
            ydl.download([target])

        location = song_utils.path_from_id(self.info_dict['id'])
        base = os.path.splitext(location)[0]
        thumbnail = base + '.jpg'
        try:
            os.remove(thumbnail)
        except FileNotFoundError:
            self.musiq.base.logger.info('tried to delete ' + thumbnail + ' but does not exist')

        try:
            # tag the file with replaygain to perform volume normalization
            subprocess.call(['aacgain', '-q', '-c', location], stdout=subprocess.DEVNULL)
        except OSError as e:
            if e.errno == errno.ENOENT:
                # the aacgain package was not found. Skip normalization
                pass
            else:
                raise
                
        return location 
Example 3
Project: lichess-bot   Author: ShailChoksi   File: engine_wrapper.py    GNU Affero General Public License v3.0 7 votes vote down vote up
def __init__(self, board, commands, options, silence_stderr=False):
        commands = commands[0] if len(commands) == 1 else commands
        self.go_commands = options.get("go_commands", {})

        self.engine = chess.uci.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
        self.engine.uci()

        if options:
            self.engine.setoption(options)

        self.engine.setoption({
            "UCI_Variant": type(board).uci_variant,
            "UCI_Chess960": board.chess960
        })
        self.engine.position(board)

        info_handler = chess.uci.InfoHandler()
        self.engine.info_handlers.append(info_handler) 
Example 4
Project: pydockenv   Author: se7entyse7en   File: commander.py    Apache License 2.0 6 votes vote down vote up
def source(self, cmd, env=None):
        env = self._prepare_env(env)

        proc = subprocess.Popen('env', stdout=subprocess.PIPE, shell=True,
                                env=env)
        initial_env = self._get_env(proc.stdout)
        proc.communicate()

        command = f"bash -c 'PYDOCKENV_DEBUG=1 source {self._bin_path} {cmd}'"
        proc = subprocess.Popen(command, stdout=subprocess.DEVNULL,
                                stderr=subprocess.PIPE, shell=True, env=env)
        post_env = self._get_env(proc.stderr)
        proc.communicate()

        env_diff = {}
        for k in set().union(initial_env.keys(), post_env.keys()):
            initial_value, post_value = initial_env.get(k), post_env.get(k)
            if initial_value != post_value:
                env_diff[k] = (initial_value, post_value)

        return env_diff 
Example 5
Project: ups-utils   Author: Ricks-Lab   File: UPSmodule.py    GNU General Public License v3.0 6 votes vote down vote up
def check_snmp(self, tups=None):
        """ check if the IP address for the target UPS or active UPS if target is None, responds to snmp command.
        :param tups:  The target ups dictionary from list or None.
        :type tups: dict
        :return:  True if the given IP address responds, else False
        :rtype: bool
        """
        if not tups:
            tups = self.active_ups
        cmd_str = 'snmpget -v2c -c {} {} {}'.format(tups['snmp_community'], tups['ups_IP'], 'iso.3.6.1.2.1.1.1.0')

        try:
            snmp_output = subprocess.check_output(shlex.split(cmd_str), shell=False,
                                                  stderr=subprocess.DEVNULL).decode().split('\n')
            if env.ut_const.DEBUG: print(snmp_output)
        except:
            return False
        return True 
Example 6
Project: lichess-bot   Author: ShailChoksi   File: engine_wrapper.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, board, commands, options=None, silence_stderr=False):
        commands = commands[0] if len(commands) == 1 else commands
        self.engine = chess.xboard.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)

        self.engine.xboard()

        if board.chess960:
            self.engine.send_variant("fischerandom")
        elif type(board).uci_variant != "chess":
            self.engine.send_variant(type(board).uci_variant)

        if options:
            self._handle_options(options)

        self.engine.setboard(board)

        post_handler = chess.xboard.PostHandler()
        self.engine.post_handlers.append(post_handler) 
Example 7
Project: singer-tools   Author: singer-io   File: check_tap.py    Apache License 2.0 6 votes vote down vote up
def run_and_summarize(tap, config, state=None, debug=False):
    cmd = [tap, '--config', config]
    if state:
        cmd += ['--state', state]
    print('Running command {}'.format(' '.join(cmd)))

    stderr = None if debug else subprocess.DEVNULL
    tap = Popen(cmd,
                stdout=subprocess.PIPE,
                stderr=stderr,
                bufsize=1,
                universal_newlines=True)
    summarizer = StdoutReader(tap)
    summarizer.start()
    returncode = tap.wait()
    if returncode != 0:
        print('ERROR: tap exited with status {}'.format(returncode))
        exit(1)

    return summarizer.summary 
Example 8
Project: latex-completion-data   Author: latex-lsp   File: tex.py    MIT License 6 votes vote down vote up
def compile(code, fmt=Format.LATEX, timeout=10, pdf=False):
    tmpdir = TemporaryDirectory()
    (Path(tmpdir.name) / 'code.tex').write_text(code)

    flags = ['-interaction=batchmode', '-shell-escape']
    if pdf:
        flags.append('-output-format=pdf')
    try:
        subprocess.run([fmt.value, *flags, 'code.tex'], cwd=tmpdir.name, timeout=timeout,
                       stdout=DEVNULL, stderr=DEVNULL)
        return CompilationResult(tmpdir)
    except TimeoutExpired as error:
        try:
            tmpdir.cleanup()
        except OSError:
            pass

        raise error 
Example 9
Project: ssrspeed_backup   Author: mazhenting   File: client_shadowsocksr.py    GNU General Public License v3.0 6 votes vote down vote up
def startClient(self,config = {}):
		if (self._process == None):
			if (self._checkPlatform() == "Windows"):
				self.__winConf()
			#	sys.exit(0)
				self._process = subprocess.Popen(["./clients/shadowsocksr-libev/ssr-local.exe"])
			elif(self._checkPlatform() == "Linux" or self._checkPlatform() == "MacOS"):
				self._config = config
				self._config["server_port"] = int(self._config["server_port"])
				with open("./config.json","w+",encoding="utf-8") as f:
					f.write(json.dumps(self._config))
					f.close()
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["python3","./clients/shadowsocksr/shadowsocks/local.py","-c","%s/config.json" % os.getcwd()])
				else:
					self._process = subprocess.Popen(["python3","./clients/shadowsocksr/shadowsocks/local.py","-c","%s/config.json" % os.getcwd()],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
				logger.info("Starting ShadowsocksR-Python with server %s:%d" % (config["server"],config["server_port"]))
			else:
				logger.error("Your system does not supported.Please contact developer.")
				sys.exit(1)
		#	print(self.__process.returncode) 
Example 10
Project: ssrspeed_backup   Author: mazhenting   File: client_shadowsocks.py    GNU General Public License v3.0 6 votes vote down vote up
def startClient(self,config={},testing=False):
		self._config = config
	#	self._config["server_port"] = int(self._config["server_port"])
		with open("./config.json","w+",encoding="utf-8") as f:
			f.write(json.dumps(self._config))
			f.close()
		if (self._process == None):
			if (self._checkPlatform() == "Windows"):
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["./clients/shadowsocks-libev/ss-local.exe","-c","{}/config.json".format(os.getcwd()),"-v"])
				else:
					self._process = subprocess.Popen(["./clients/shadowsocks-libev/ss-local.exe","-c","{}/config.json".format(os.getcwd())],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
				logger.info("Starting Shadowsocks-libev with server %s:%d" % (config["server"],config["server_port"]))
			elif(self._checkPlatform() == "Linux" or self._checkPlatform() == "MacOS"):
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["ss-local","-v","-c","%s/config.json" % os.getcwd()])
				else:
					self._process = subprocess.Popen(["ss-local","-c","%s/config.json" % os.getcwd()],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
				logger.info("Starting Shadowsocks-libev with server %s:%d" % (config["server"],config["server_port"]))
			else:
				logger.critical("Your system does not supported.Please contact developer.")
				sys.exit(1) 
Example 11
Project: ssrspeed_backup   Author: mazhenting   File: client_shadowsocks.py    GNU General Public License v3.0 6 votes vote down vote up
def startClient(self,config={},testing=False):
		if (self._process == None):
			if (self._checkPlatform() == "Windows"):
				if (not testing):
					self.__winConf()
			#	sys.exit(0)
				self._process = subprocess.Popen(["./clients/shadowsocks-win/Shadowsocks.exe"])
			elif(self._checkPlatform() == "Linux" or self._checkPlatform() == "MacOS"):
				self._config = config
				self._config["server_port"] = int(self._config["server_port"])
				with open("./config.json","w+",encoding="utf-8") as f:
					f.write(json.dumps(self._config))
					f.close()
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["ss-local","-v","-c","%s/config.json" % os.getcwd()])
				else:
					self._process = subprocess.Popen(["ss-local","-c","%s/config.json" % os.getcwd()],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
				logger.info("Starting Shadowsocks-libev with server %s:%d" % (config["server"],config["server_port"]))
			else:
				logger.critical("Your system does not supported.Please contact developer.")
				sys.exit(1) 
Example 12
Project: transcode2H264   Author: amaurypm   File: transcode2H264.py    GNU General Public License v3.0 6 votes vote down vote up
def __get_input_data(self):
        if os.path.isfile(self.__in_filename):
            #cmd_in_file,cmd_output_error_file=os.popen4("ffmpeg -i \"%s\"" % self.__in_filename) # Depecrated in Python3
            #for line in cmd_output_error_file:   
            cproc = subprocess.run(["ffmpeg", "-i", self.__in_filename], stdout = subprocess.DEVNULL, stderr = subprocess.PIPE, universal_newlines = True)
            for line in cproc.stderr.split('\n'):
                line=line.strip()
                if ('Video' in line) and ('Stream' in line):
                    if 'ansi' not in line:
                        self.__in_ok=True
                    
                if ('Audio' in line) and ('Stream' in line):
                    if '(' in line.split(':')[1]:
                        self.__avlang = line.split(':')[1].split('(')[1].strip(')')
                        if "unk" in self.__avlang.lower() or "und" in self.__avlang.lower():
                            self.__avlang = None
                    
                if 'Duration' in line:
                    duration_string=line.split(',')[0].split()[1]
                    if not 'N/A' in duration_string:
                        self.__in_duration=dstring2dint(duration_string) 
Example 13
Project: transcode2H264   Author: amaurypm   File: transcode2H264.py    GNU General Public License v3.0 6 votes vote down vote up
def __get_crop_data(self):
        crop_data=None
        crop_list=[]
        tmp_output_filename=os.path.splitext(self.__in_filename)[0] + '_tmp_' + random_string(10)  +'_autocrop.mkv'
        input_duration=self.__in_duration
        if input_duration:
            ss_list=[input_duration/x for x in random.sample(range(1,100),5)] # Cheacking autocrop in 5 random sites in the video.
            for ss in ss_list:
                #cmd_in_file,cmd_output_error_file=os.popen4("ffmpeg -ss %d -i \"%s\" -t 1 -filter cropdetect -y \"%s\"" % (ss,self.__in_filename,tmp_output_filename))
                cproc = subprocess.run(["ffmpeg", "-ss", str(ss), "-i", self.__in_filename, "-t", "1", "-filter", "cropdetect", "-y", tmp_output_filename], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, universal_newlines=True)
                for line in cproc.stderr.split('\n'):
                    line=line.strip()
                    if ('cropdetect' in line) and ('=' in line):
                        crop_list.append(line.split('=')[1])
                        
            os.remove(tmp_output_filename)            
            crop_set=set(crop_list)
            crop_mode_cont=0
            for crop in crop_set:
                if crop_list.count(crop) > crop_mode_cont:
                    crop_data=crop
                    crop_mode_cont=crop_list.count(crop)
            
        sys.stdout.write('{}\n'.format(crop_data))
        self.__crop_data=crop_data 
Example 14
Project: NiujiaoDebugger   Author: MrSrc   File: test_program.py    GNU General Public License v3.0 6 votes vote down vote up
def testSelectedTestNamesFunctionalTest(self):
        def run_unittest(args):
            p = subprocess.Popen([sys.executable, '-m', 'unittest'] + args,
                stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
            with p:
                _, stderr = p.communicate()
            return stderr.decode()

        t = '_test_warnings'
        self.assertIn('Ran 7 tests', run_unittest([t]))
        self.assertIn('Ran 7 tests', run_unittest(['-k', 'TestWarnings', t]))
        self.assertIn('Ran 7 tests', run_unittest(['discover', '-p', '*_test*', '-k', 'TestWarnings']))
        self.assertIn('Ran 2 tests', run_unittest(['-k', 'f', t]))
        self.assertIn('Ran 7 tests', run_unittest(['-k', 't', t]))
        self.assertIn('Ran 3 tests', run_unittest(['-k', '*t', t]))
        self.assertIn('Ran 7 tests', run_unittest(['-k', '*test_warnings.*Warning*', t]))
        self.assertIn('Ran 1 test', run_unittest(['-k', '*test_warnings.*warning*', t])) 
Example 15
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def _get_soname(f):
            if not f:
                return None

            try:
                proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f),
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.DEVNULL)
            except OSError:  # E.g. command not found
                return None
            with proc:
                data = proc.stdout.read()
            res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data)
            if not res:
                return None
            return os.fsdecode(res.group(1)) 
Example 16
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def _get_soname(f):
            # assuming GNU binutils / ELF
            if not f:
                return None
            objdump = shutil.which('objdump')
            if not objdump:
                # objdump is not available, give up
                return None

            try:
                proc = subprocess.Popen((objdump, '-p', '-j', '.dynamic', f),
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.DEVNULL)
            except OSError:  # E.g. bad executable
                return None
            with proc:
                dump = proc.stdout.read()
            res = re.search(br'\sSONAME\s+([^\s]+)', dump)
            if not res:
                return None
            return os.fsdecode(res.group(1)) 
Example 17
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def find_library(name):
            ename = re.escape(name)
            expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
            expr = os.fsencode(expr)

            try:
                proc = subprocess.Popen(('/sbin/ldconfig', '-r'),
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.DEVNULL)
            except OSError:  # E.g. command not found
                data = b''
            else:
                with proc:
                    data = proc.stdout.read()

            res = re.findall(expr, data)
            if not res:
                return _get_soname(_findLib_gcc(name))
            res.sort(key=_num_version)
            return os.fsdecode(res[-1]) 
Example 18
Project: NiujiaoDebugger   Author: MrSrc   File: _aix.py    GNU General Public License v3.0 6 votes vote down vote up
def get_ld_headers(file):
    """
    Parse the header of the loader section of executable and archives
    This function calls /usr/bin/dump -H as a subprocess
    and returns a list of (ld_header, ld_header_info) tuples.
    """
    # get_ld_headers parsing:
    # 1. Find a line that starts with /, ./, or ../ - set as ld_header
    # 2. If "INDEX" in occurs in a following line - return ld_header
    # 3. get info (lines starting with [0-9])
    ldr_headers = []
    p = Popen(["/usr/bin/dump", f"-X{AIX_ABI}", "-H", file],
        universal_newlines=True, stdout=PIPE, stderr=DEVNULL)
    # be sure to read to the end-of-file - getting all entries
    while True:
        ld_header = get_ld_header(p)
        if ld_header:
            ldr_headers.append((ld_header, get_ld_header_info(p)))
        else:
            break
    p.stdout.close()
    p.wait()
    return ldr_headers 
Example 19
Project: NiujiaoDebugger   Author: MrSrc   File: test_subprocess.py    GNU General Public License v3.0 6 votes vote down vote up
def _test_bufsize_equal_one(self, line, expected, universal_newlines):
        # subprocess may deadlock with bufsize=1, see issue #21332
        with subprocess.Popen([sys.executable, "-c", "import sys;"
                               "sys.stdout.write(sys.stdin.readline());"
                               "sys.stdout.flush()"],
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.DEVNULL,
                              bufsize=1,
                              universal_newlines=universal_newlines) as p:
            p.stdin.write(line) # expect that it flushes the line in text mode
            os.close(p.stdin.fileno()) # close it without flushing the buffer
            read_line = p.stdout.readline()
            with support.SuppressCrashReport():
                try:
                    p.stdin.close()
                except OSError:
                    pass
            p.stdin = None
        self.assertEqual(p.returncode, 0)
        self.assertEqual(read_line, expected) 
Example 20
Project: NiujiaoDebugger   Author: MrSrc   File: test_shutil.py    GNU General Public License v3.0 6 votes vote down vote up
def test_zipfile_vs_zip(self):
        root_dir, base_dir = self._create_files()
        base_name = os.path.join(self.mkdtemp(), 'archive')
        archive = make_archive(base_name, 'zip', root_dir, base_dir)

        # check if ZIP file  was created
        self.assertEqual(archive, base_name + '.zip')
        self.assertTrue(os.path.isfile(archive))

        # now create another ZIP file using `zip`
        archive2 = os.path.join(root_dir, 'archive2.zip')
        zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
        subprocess.check_call(zip_cmd, cwd=root_dir,
                              stdout=subprocess.DEVNULL)

        self.assertTrue(os.path.isfile(archive2))
        # let's compare both ZIP files
        with zipfile.ZipFile(archive) as zf:
            names = zf.namelist()
        with zipfile.ZipFile(archive2) as zf:
            names2 = zf.namelist()
        self.assertEqual(sorted(names), sorted(names2)) 
Example 21
Project: NiujiaoDebugger   Author: MrSrc   File: test_cmd_line.py    GNU General Public License v3.0 6 votes vote down vote up
def test_isolatedmode(self):
        self.verify_valid_flag('-I')
        self.verify_valid_flag('-IEs')
        rc, out, err = assert_python_ok('-I', '-c',
            'from sys import flags as f; '
            'print(f.no_user_site, f.ignore_environment, f.isolated)',
            # dummyvar to prevent extraneous -E
            dummyvar="")
        self.assertEqual(out.strip(), b'1 1 1')
        with support.temp_cwd() as tmpdir:
            fake = os.path.join(tmpdir, "uuid.py")
            main = os.path.join(tmpdir, "main.py")
            with open(fake, "w") as f:
                f.write("raise RuntimeError('isolated mode test')\n")
            with open(main, "w") as f:
                f.write("import uuid\n")
                f.write("print('ok')\n")
            self.assertRaises(subprocess.CalledProcessError,
                              subprocess.check_output,
                              [sys.executable, main], cwd=tmpdir,
                              stderr=subprocess.DEVNULL)
            out = subprocess.check_output([sys.executable, "-I", main],
                                          cwd=tmpdir)
            self.assertEqual(out.strip(), b"ok") 
Example 22
Project: twiml-generator   Author: TwilioDevEd   File: twiml_code_generator.py    MIT License 5 votes vote down vote up
def format_code(self):
        if 'formatter' not in self.language_spec:
            return
        format_cmd = self.language_spec['formatter'].format(filepath=str(self.code_filepath))
        subprocess.run([format_cmd], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    # Constants for verify result 
Example 23
Project: docker-ipsec   Author: cbrichford   File: common.py    Apache License 2.0 5 votes vote down vote up
def ipsec(*args) -> IPSecResult:
    cmd_line = ['ipsec']
    cmd_line.extend(args)
        
    with tempfile.TemporaryFile() as output_file:
        status_code = subprocess.call(cmd_line, stdin=subprocess.DEVNULL, stdout=output_file, stderr=output_file)
        output_file.seek(0)
        output = output_file.read().decode("utf-8", "strict")
        return IPSecResult(status=status_code, output=output) 
Example 24
Project: wuy   Author: manatlan   File: wuy.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, url, size=None, chromeArgs=[]):
        self.__instance = None

        if sys.platform[:3] == "win":
            exe = find_chrome_win()
        elif sys.platform == "darwin":
            exe = find_chrome_mac()
        else:
            for i in ["chromium-browser", "chromium", "google-chrome", "chrome"]:
                try:
                    exe = webbrowser.get(i).name
                    break
                except webbrowser.Error:
                    exe = None

        if exe:
            args = [exe, "--app=" + url] + chromeArgs
            if size == FULLSCREEN:
                args.append("--start-fullscreen")
            if tempfile.gettempdir():
                args.append(
                    "--user-data-dir=%s"
                    % os.path.join(tempfile.gettempdir(), ".wuyapp")
                )
            # self.__instance = subprocess.Popen( args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) # make troubles on windows (freezd with noconsole don't start)
            self.__instance = subprocess.Popen(args)
        else:
            raise Exception("no browser") 
Example 25
Project: Possum   Author: brysontyrrell   File: pipenv_.py    MIT License 5 votes vote down vote up
def install_packages(self):
        p = subprocess.Popen(
            [self.pipenv_path, 'install'],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )
        p.communicate() 
Example 26
Project: Possum   Author: brysontyrrell   File: pipenv_.py    MIT License 5 votes vote down vote up
def remove_virtualenv(self):
        p = subprocess.Popen(
            [self.pipenv_path, '--rm'],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )
        p.communicate() 
Example 27
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 5 votes vote down vote up
def _getusb():
    summ = "\nUSB devices:"
    detail = detailheader("USB information")
    linuxroot = []
    hub = []
    usb = []
    total = []

    output = check_output("lsusb", stderr=DEVNULL).decode("utf-8").splitlines()

    for item in output:
        bus = item.split(':')[0].lower().replace("bus ", "").replace(" device ", ":")
        usbid = item.split('ID ')[1][:9]
        name = item.split('ID ')[1][10:]
        if "1d6b:000" in usbid.lower():
            linuxroot.append([usbid, name, bus])
        elif "hub" in name.lower():
            hub.append([usbid, name, bus])
        else:
            usb.append([usbid, name, bus])
        total.append([usbid, name, bus])

    summ += "\n |__{} devices\n |__{} hubs " \
            "(Linux root hubs included)\n".format(len(usb), len(linuxroot) + len(hub))
    helptext = "(device ID - name - bus:device)"
    detail += "{:^80}\n".format(helptext)

    detail += "Linux root hubs:\n"
    for item in linuxroot:
        detail += " |__{} - {} - {}\n".format(item[0], item[1], item[2])
    detail += "\nOther hubs:\n"
    for item in hub:
        detail += " |__{} - {} - {}\n".format(item[0], item[1], item[2])
    detail += "\nUSB devices:\n"
    for item in usb:
        detail += " |__{} - {} - {}\n".format(item[0], item[1], item[2])

    return summ, detail, total 
Example 28
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 5 votes vote down vote up
def _getzypper():
    packages = []
    output = check_output(["zypper", "packages", "--installed-only"],
                          stderr=DEVNULL).decode("utf-8").splitlines()
    for item in output:
        if item[0] == "i":
            name = item.split("|")[2].strip()
            version = item.split("|")[3].strip()
            if [name, version] not in packages:
                packages.append([name, version])
    return packages 
Example 29
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 5 votes vote down vote up
def _getyum():
    packages = []
    output = check_output(["yum", "list",
                           "installed", "--noplugins"],
                          stderr=DEVNULL).decode("utf-8").splitlines()[1:]
    output = " ".join(output)
    output = " ".join(output.split()).split(" ")

    for item in range(0, len(output), 3):
        packages.append([output[item], output[item+1]])

    return packages 
Example 30
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 5 votes vote down vote up
def _getpacman():
    packages = []
    output = check_output(["pacman", "-Q"], stderr=DEVNULL).decode("utf-8").splitlines()
    for item in output:
        packages.append(item.split(" "))
    return packages 
Example 31
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 5 votes vote down vote up
def _getdpkg():
    packages = []
    output = check_output(["dpkg-query", "-f",
                           "${binary:Package}\t${source:Version}\n",
                           "-W"], stderr=DEVNULL).decode("utf-8").splitlines()
    for item in output:
        name = item.split("\t")[0]
        if ":" in name:
            name = name.split(":")[0]
        version = item.split("\t")[1]
        if [name, version] not in packages:
            packages.append([name, version])
    return packages 
Example 32
Project: aridi   Author: dpgon   File: gathering0.py    GNU General Public License v3.0 5 votes vote down vote up
def nslookup(hostname):
        if re.fullmatch("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", hostname):
            return ip_address(hostname)

        if Precheck.checkcommand("nslookup"):
            ip = None
            try:
                output = check_output(["nslookup", hostname],
                                      stderr=DEVNULL).decode("utf-8").splitlines()
                for line in output:
                    line = " ".join(line.split()).split(" ")
                    try:
                        if line[0].startswith("Address") and \
                                re.fullmatch("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", line[1]):
                            ip = ip_address(line[1])
                    except:
                        pass
            except CalledProcessError as e:
                ip = None
            return ip

        if Precheck.checkcommand("dig"):
            ip = None
            try:
                output = check_output(["dig", hostname],
                                      stderr=DEVNULL).decode("utf-8").splitlines()
                for line in output:
                    line = " ".join(line.split()).split(" ")
                    try:
                        if line[0].startswith(hostname):
                            if line[-2] == "A" and \
                                    re.fullmatch("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", line[-1]):
                                ip = ip_address(line[-1])
                            elif line[-2] == "CNAME":
                                ip = Precheck.nslookup(line[-1])
                    except:
                        pass
            except CalledProcessError as e:
                ip = None
            return ip 
Example 33
Project: typeshed   Author: python   File: pytype_test.py    Apache License 2.0 5 votes vote down vote up
def can_run(exe: str, *, args: List[str]) -> bool:
    try:
        subprocess.run([exe] + args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    except OSError:
        return False
    else:
        return True 
Example 34
Project: imgcomp-cvpr   Author: fab-jul   File: other_codecs.py    GNU General Public License v3.0 5 votes vote down vote up
def _decode_webp_to_png(webp_p):
    png_p = webp_p.replace('.webp', '_as_png.png')
    subprocess.call([DWEBP, webp_p, '-o', png_p], stderr=subprocess.DEVNULL)
    return png_p 
Example 35
Project: imgcomp-cvpr   Author: fab-jul   File: other_codecs.py    GNU General Public License v3.0 5 votes vote down vote up
def check_if_programm_is_available(prg, name, env_name):
    try:
        subprocess.call([prg, '-v'],
                        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    except FileNotFoundError:
        print('*** Invalid path to {}: {}'.format(name, prg))
        print('Make sure {} is available in $PATH or at ${}.'.format(name, env_name))
        sys.exit(1) 
Example 36
Project: arpwitch   Author: verbnetworks   File: ArpWitch.py    Apache License 2.0 5 votes vote down vote up
def command_exec(self, command_line, as_user=None):
        self.Log.debug('ArpWitch.command_exec({}, {})'.format(command_line, as_user))
        if as_user is not None:
            command_line = 'sudo -u {} {}'.format(as_user, command_line)
        self.exec_subprocesses.append(subprocess.Popen(command_line,
                                                       shell=True,
                                                       stdout=subprocess.DEVNULL,
                                                       stderr=subprocess.STDOUT
                                                       )) 
Example 37
Project: jawfish   Author: war-and-code   File: support.py    MIT License 5 votes vote down vote up
def start(self):
        try:
            f = open(self.procfile, 'r')
        except OSError as e:
            warnings.warn('/proc not available for stats: {}'.format(e),
                          RuntimeWarning)
            sys.stderr.flush()
            return

        watchdog_script = findfile("memory_watchdog.py")
        self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script],
                                             stdin=f, stderr=subprocess.DEVNULL)
        f.close()
        self.started = True 
Example 38
Project: pyrex   Author: garmin   File: pyrex.py    Apache License 2.0 5 votes vote down vote up
def get_image_id(config, image):
    docker_args = [
        config["config"]["dockerpath"],
        "image",
        "inspect",
        image,
        "--format={{.Id}}",
    ]
    return (
        subprocess.check_output(docker_args, stderr=subprocess.DEVNULL)
        .decode("utf-8")
        .rstrip()
    ) 
Example 39
Project: dlp3-update   Author: arunpersaud   File: dlp3.py    GNU General Public License v3.0 5 votes vote down vote up
def my_cleanup(package: str):
    print("updating dlp3 checkout for", package)
    try:
        output = subprocess.check_output('cd {} && osc up'.
                                         format(os.path.join(dlp3_path, package)),
                                         stderr=subprocess.DEVNULL,
                                         shell=True)
    except:
        # package didn't exist yet, create a new checkout
        output = subprocess.check_output('cd {} && osc co {}'.
                                         format(dlp3_path, package),
                                         shell=True)
    print(output.decode('utf8')) 
Example 40
Project: blobfile   Author: cshesse   File: ops_test.py    The Unlicense 5 votes vote down vote up
def _write_contents(path, contents):
    if path.startswith("as://"):
        with tempfile.TemporaryDirectory() as tmpdir:
            assert isinstance(tmpdir, str)
            account, container, blob = azure.split_url(path)
            filepath = os.path.join(tmpdir, "tmp")
            with open(filepath, "wb") as f:
                f.write(contents)
            sp.run(
                [
                    "az",
                    "storage",
                    "blob",
                    "upload",
                    "--account-name",
                    account,
                    "--container-name",
                    container,
                    "--name",
                    blob,
                    "--file",
                    filepath,
                ],
                check=True,
                shell=platform.system() == "Windows",
                stdout=sp.DEVNULL,
                stderr=sp.DEVNULL,
            )
    else:
        with gfile.GFile(path, "wb") as f:
            f.write(contents) 
Example 41
Project: blobfile   Author: cshesse   File: ops_test.py    The Unlicense 5 votes vote down vote up
def _read_contents(path):
    if path.startswith("as://"):
        with tempfile.TemporaryDirectory() as tmpdir:
            assert isinstance(tmpdir, str)
            account, container, blob = azure.split_url(path)
            filepath = os.path.join(tmpdir, "tmp")
            sp.run(
                [
                    "az",
                    "storage",
                    "blob",
                    "download",
                    "--account-name",
                    account,
                    "--container-name",
                    container,
                    "--name",
                    blob,
                    "--file",
                    filepath,
                ],
                check=True,
                shell=platform.system() == "Windows",
                stdout=sp.DEVNULL,
                stderr=sp.DEVNULL,
            )
            with open(filepath, "rb") as f:
                return f.read()
    else:
        with gfile.GFile(path, "rb") as f:
            return f.read() 
Example 42
Project: Repobot   Author: Desgard   File: support.py    MIT License 5 votes vote down vote up
def start(self):
        try:
            f = open(self.procfile, 'r')
        except OSError as e:
            warnings.warn('/proc not available for stats: {0}'.format(e),
                          RuntimeWarning)
            sys.stderr.flush()
            return

        watchdog_script = findfile("memory_watchdog.py")
        self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script],
                                             stdin=f, stderr=subprocess.DEVNULL)
        f.close()
        self.started = True 
Example 43
Project: aladdin   Author: fivestars-os   File: helm.py    MIT License 5 votes vote down vote up
def release_exists(self, release_name):
        command = ['helm', 'status', release_name]
        ret_code = subprocess.run(command, stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL).returncode
        # If return code is 0, release exists
        if ret_code == 0:
            return True
        else:
            return False 
Example 44
Project: aladdin   Author: fivestars-os   File: git.py    MIT License 5 votes vote down vote up
def _get_hash_ls_remote(cls, ref, url):
        """
        This get the info from remote without having to download project/data
        :param ref:
        :param url:
        """
        try:
            output = subprocess.check_output(['git', 'ls-remote', url, ref],
                                             stderr=subprocess.DEVNULL).decode('utf-8').split()
            return output[0] if output else None
        except subprocess.CalledProcessError:
            return None 
Example 45
Project: kivy-smoothie-host   Author: wolfmanjm   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def _show_spindle_cam(self):
        if self.is_desktop in [0, 4]:
            self.sm.current = "spindle camera"
        else:
            # we run it as a separate program so it is in its own window
            subprocess.Popen(['python3', 'spindle_camera.py'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 
Example 46
Project: inmanta   Author: inmanta   File: env.py    Apache License 2.0 5 votes vote down vote up
def _get_installed_packages(cls, python_interpreter: str) -> Dict[str, str]:
        cmd = [python_interpreter, "-m", "pip", "list", "--format", "json"]
        try:
            output = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
        except CalledProcessError as e:
            LOGGER.error("%s: %s", cmd, e.output.decode())
            raise
        except Exception:
            LOGGER.error("%s: %s", cmd, output.decode())
            raise
        else:
            LOGGER.debug("%s: %s", cmd, output.decode())

        return {r["name"]: r["version"] for r in json.loads(output.decode())} 
Example 47
Project: inmanta   Author: inmanta   File: postgresproc.py    Apache License 2.0 5 votes vote down vote up
def start(self) -> bool:
        """
            Start DB.

            :return: `True` if instance has been started or `False` if it could not start.
        """
        if self.running():
            return True

        try:
            old_wc = os.getcwd()
            self._create_db_path()
            self._init_db()
            self._create_sockets_dir(self.db_path)

            os.chdir(self.db_path)
            args = [self.pg_ctl_bin, "start", "-D", ".", "-o", "-p " + str(self.port) + " -k " + "sockets", "-s"]
            process = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            process.communicate()
            return process.returncode == 0
        except Exception:
            LOGGER.exception("Failed to initialize the database.")
            return False
        finally:
            if old_wc is not None:
                os.chdir(old_wc) 
Example 48
Project: inmanta   Author: inmanta   File: postgresproc.py    Apache License 2.0 5 votes vote down vote up
def stop(self) -> None:
        if not self.running():
            return
        args = [self.pg_ctl_bin, "stop", "-D", self.db_path, "-m", "immediate", "-s"]
        process = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        process.communicate()
        if process.returncode != 0:
            raise Exception("Failed to stop embedded db.")
        if self._db_path_is_temporary:
            shutil.rmtree(self.db_path)
            self.db_path = None 
Example 49
Project: inmanta   Author: inmanta   File: postgresproc.py    Apache License 2.0 5 votes vote down vote up
def running(self) -> bool:
        if self.db_path is None:
            return False
        args = [self.pg_ctl_bin, "status", "-D", self.db_path, "-s"]
        process = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        process.communicate()
        return process.returncode == 0 
Example 50
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def clone(self, src: str, dest: str) -> None:
        env = os.environ.copy()
        env["GIT_ASKPASS"] = "true"
        subprocess.check_call(["git", "clone", src, dest], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env) 
Example 51
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def checkout_tag(self, repo: str, tag: str) -> None:
        subprocess.check_call(["git", "checkout", tag], cwd=repo, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 
Example 52
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def commit(self, repo: str, message: str, commit_all: bool, add: List[str] = []) -> None:
        for file in add:
            subprocess.check_call(["git", "add", file], cwd=repo, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if not commit_all:
            subprocess.check_call(
                ["git", "commit", "-m", message], cwd=repo, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
            )
        else:
            subprocess.check_call(
                ["git", "commit", "-a", "-m", message], cwd=repo, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
            ) 
Example 53
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def tag(self, repo: str, tag: str) -> None:
        subprocess.check_call(
            ["git", "tag", "-a", "-m", "auto tag by module tool", tag],
            cwd=repo,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        ) 
Example 54
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def pull(self, repo: str) -> str:
        return subprocess.check_output(["git", "pull"], cwd=repo, stderr=subprocess.DEVNULL).decode("utf-8") 
Example 55
Project: inmanta   Author: inmanta   File: module.py    Apache License 2.0 5 votes vote down vote up
def push(self, repo: str) -> str:
        return subprocess.check_output(
            ["git", "push", "--follow-tags", "--porcelain"], cwd=repo, stderr=subprocess.DEVNULL
        ).decode("utf-8") 
Example 56
Project: ssrspeed_backup   Author: mazhenting   File: client_v2ray.py    GNU General Public License v3.0 5 votes vote down vote up
def startClient(self,config={}):
		self._config = config
		with open("./config.json","w+",encoding="utf-8") as f:
			f.write(json.dumps(self._config))
			f.close()
		try:
			if (self._process == None):
				if (self._checkPlatform() == "Windows"):
					if (logger.level == logging.DEBUG):
						self._process = subprocess.Popen(["./clients/v2ray-core/v2ray.exe","--config","{}/config.json".format(os.getcwd())])
					else:
						self._process = subprocess.Popen(["./clients/v2ray-core/v2ray.exe","--config","{}/config.json".format(os.getcwd())],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
					logger.info("Starting v2ray-core with server %s:%d" % (config["server"],config["server_port"]))
				elif(self._checkPlatform() == "Linux" or self._checkPlatform() == "MacOS"):
					if (logger.level == logging.DEBUG):
						self._process = subprocess.Popen(["./clients/v2ray-core/v2ray","--config","%s/config.json" % os.getcwd()])
					else:
						self._process = subprocess.Popen(["./clients/v2ray-core/v2ray","--config","%s/config.json" % os.getcwd()],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
					logger.info("Starting v2ray-core with server %s:%d" % (config["server"],config["server_port"]))
				else:
					logger.critical("Your system does not supported.Please contact developer.")
					sys.exit(1)
		except FileNotFoundError:
			#logger.exception("")
			logger.error("V2Ray Core Not Found !")
			sys.exit(1) 
Example 57
Project: ssrspeed_backup   Author: mazhenting   File: client_shadowsocksr.py    GNU General Public License v3.0 5 votes vote down vote up
def startClient(self,config = {}):
		self._config = config
	#	self._config["server_port"] = int(self._config["server_port"])
		with open("./config.json","w+",encoding="utf-8") as f:
			f.write(json.dumps(self._config))
			f.close()
		if (self._process == None):
			if (self._checkPlatform() == "Windows"):
				if (self.useSsrCSharp):
					self.__ssrCSharpConf(config)
					self._process = subprocess.Popen(["./clients/shadowsocksr-win/shadowsocksr.exe"])
					logger.info("ShadowsocksR-C# started.")
					return
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["./clients/shadowsocksr-libev/ssr-local.exe","-c","{}/config.json".format(os.getcwd()),"-v"])
					logger.info("Starting ShadowsocksR-libev with server %s:%d" % (config["server"],config["server_port"]))
				else:
					self._process = subprocess.Popen(["./clients/shadowsocksr-libev/ssr-local.exe","-c","{}/config.json".format(os.getcwd())],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
					logger.info("Starting ShadowsocksR-libev with server %s:%d" % (config["server"],config["server_port"]))
			elif(self._checkPlatform() == "Linux" or self._checkPlatform() == "MacOS"):
				if (logger.level == logging.DEBUG):
					self._process = subprocess.Popen(["python3","./clients/shadowsocksr/shadowsocks/local.py","-v","-c","%s/config.json" % os.getcwd()])
				else:
					self._process = subprocess.Popen(["python3","./clients/shadowsocksr/shadowsocks/local.py","-c","%s/config.json" % os.getcwd()],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
				logger.info("Starting ShadowsocksR-Python with server %s:%d" % (config["server"],config["server_port"]))
			else:
				logger.error("Your system does not supported.Please contact developer.")
				sys.exit(1)
		#	print(self.__process.returncode) 
Example 58
Project: raveberry   Author: raveberry   File: settings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _check_internet(self):
        response = subprocess.call(['ping','-c','1','-W','3','1.1.1.1'], stdout=subprocess.DEVNULL)
        if response == 0:
            self.has_internet = True
        else:
            self.has_internet = False

    # settings can only be changed by admin 
Example 59
Project: pyzfscmds   Author: johnramsden   File: freebsd.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def zfs_module_loaded():
    try:
        subprocess.check_call(["sysctl", "vfs.zfs.version.spa"],
                              stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError:
        return False

    return True 
Example 60
Project: EMNLP2018-ArgMining-Morio   Author: EdoFrank   File: common.py    MIT License 5 votes vote down vote up
def run_procs_using_gpu(py_processes: List[str], available_gpus: List[int], max_proc_per_gpu: int):
    running_procs = []
    py_processes = copy.deepcopy(py_processes)
    while py_processes or running_procs:
        if py_processes and len(running_procs) < len(available_gpus) * max_proc_per_gpu:
            # Search available GPUs
            gpu_assign = None
            for gpu_cand in available_gpus:
                if len([True for rproc in running_procs if rproc['gpu'] == gpu_cand]) < max_proc_per_gpu:
                    gpu_assign = gpu_cand
                    break
            assert gpu_assign is not None
            # Run a process
            args = py_processes.pop(0)
            str_args = [arg for arg in 'python {0} --gpu {1}'.format(args, gpu_assign).split(' ')
                        if arg != '']
            proc = Popen(str_args, stdout=DEVNULL, stderr=DEVNULL)
            running_procs.append(
                {
                    'gpu': gpu_assign,
                    'proc': proc
                }
            )
            print_info('running (gpu:{0}, pid:{1}) {2}'.format(gpu_assign, proc.pid, args))
        for rproc in running_procs:
            retcode = rproc['proc'].poll()
            if retcode is not None:  # Process finished.
                running_procs.remove(rproc)
                print_ok('finished (gpu:{0}) {1}'.format(rproc['gpu'], rproc['proc']))
                break
            else:  # No process is done, wait a bit and check again.
                continue
        time.sleep(0.5)
    return 
Example 61
Project: mx   Author: rafi   File: tmux.py    MIT License 5 votes vote down vote up
def has_session(self, session_name):
        """
        Returns true if specified session currently exists

        :param session_name: The session name to match
        """
        try:
            cmds = ['tmux', 'has-session', '-t', session_name]
            # Compatibility code, python 2.x doesn't have subprocess.DEVNULL
            with open(os.devnull, 'wb') as DEVNULL:
                code = subprocess.check_call(cmds, stderr=DEVNULL)
        except subprocess.CalledProcessError as e:
            code = e.returncode
        return code == 0 
Example 62
Project: chromecastplay   Author: thotypous   File: chromecastplay.py    ISC License 5 votes vote down vote up
def to_webvtt(sub_file, video_file=None):
    encoding = None
    if sub_file:
        encoding = detect_encoding(sub_file)
    sub_transcoder = Popen(['ffmpeg',
                            '-y', '-nostdin'] +
                           (['-sub_charenc', encoding] if encoding else []) +
                           ['-i', sub_file or video_file,
                            '-map', 's?',
                            '-f', 'webvtt',
                            '-loglevel', 'error',
                            '-'],
                           stdout=PIPE,
                           stderr=DEVNULL)
    return sub_transcoder.stdout.read() 
Example 63
Project: plagFinder   Author: xoften   File: run_jplag.py    GNU General Public License v3.0 5 votes vote down vote up
def run_jplag(submission_directory):
    JPLAG_FILEPATH = os.path.join(os.path.dirname(__file__), "jplag.jar")

    #Runing Jplag as a subprocess with no output (stdout adn error = DEVNULL)
    subprocess.call(['java', '-jar', JPLAG_FILEPATH, '-l', 'java17', '-s',
                     submission_directory], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 
Example 64
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 5 votes vote down vote up
def _findLib_crle(name, is64):
            if not os.path.exists('/usr/bin/crle'):
                return None

            env = dict(os.environ)
            env['LC_ALL'] = 'C'

            if is64:
                args = ('/usr/bin/crle', '-64')
            else:
                args = ('/usr/bin/crle',)

            paths = None
            try:
                proc = subprocess.Popen(args,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.DEVNULL,
                                        env=env)
            except OSError:  # E.g. bad executable
                return None
            with proc:
                for line in proc.stdout:
                    line = line.strip()
                    if line.startswith(b'Default Library Path (ELF):'):
                        paths = os.fsdecode(line).split()[4]

            if not paths:
                return None

            for dir in paths.split(":"):
                libfile = os.path.join(dir, "lib%s.so" % name)
                if os.path.exists(libfile):
                    return libfile

            return None 
Example 65
Project: NiujiaoDebugger   Author: MrSrc   File: test_find.py    GNU General Public License v3.0 5 votes vote down vote up
def test_find_on_libpath(self):
        import subprocess
        import tempfile

        try:
            p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE,
                                 stderr=subprocess.DEVNULL)
            out, _ = p.communicate()
        except OSError:
            raise unittest.SkipTest('gcc, needed for test, not available')
        with tempfile.TemporaryDirectory() as d:
            # create an empty temporary file
            srcname = os.path.join(d, 'dummy.c')
            libname = 'py_ctypes_test_dummy'
            dstname = os.path.join(d, 'lib%s.so' % libname)
            with open(srcname, 'w') as f:
                pass
            self.assertTrue(os.path.exists(srcname))
            # compile the file to a shared library
            cmd = ['gcc', '-o', dstname, '--shared',
                   '-Wl,-soname,lib%s.so' % libname, srcname]
            out = subprocess.check_output(cmd)
            self.assertTrue(os.path.exists(dstname))
            # now check that the .so can't be found (since not in
            # LD_LIBRARY_PATH)
            self.assertIsNone(find_library(libname))
            # now add the location to LD_LIBRARY_PATH
            with test.support.EnvironmentVarGuard() as env:
                KEY = 'LD_LIBRARY_PATH'
                if KEY not in env:
                    v = d
                else:
                    v = '%s:%s' % (env[KEY], d)
                env.set(KEY, v)
                # now check that the .so can be found (since in
                # LD_LIBRARY_PATH)
                self.assertEqual(find_library(libname), 'lib%s.so' % libname) 
Example 66
Project: NiujiaoDebugger   Author: MrSrc   File: webbrowser.py    GNU General Public License v3.0 5 votes vote down vote up
def _invoke(self, args, remote, autoraise):
        raise_opt = []
        if remote and self.raise_opts:
            # use autoraise argument only for remote invocation
            autoraise = int(autoraise)
            opt = self.raise_opts[autoraise]
            if opt: raise_opt = [opt]

        cmdline = [self.name] + raise_opt + args

        if remote or self.background:
            inout = subprocess.DEVNULL
        else:
            # for TTY browsers, we need stdin/out
            inout = None
        p = subprocess.Popen(cmdline, close_fds=True, stdin=inout,
                             stdout=(self.redirect_stdout and inout or None),
                             stderr=inout, start_new_session=True)
        if remote:
            # wait at most five seconds. If the subprocess is not finished, the
            # remote invocation has (hopefully) started a new instance.
            try:
                rc = p.wait(5)
                # if remote call failed, open() will try direct invocation
                return not rc
            except subprocess.TimeoutExpired:
                return True
        elif self.background:
            if p.poll() is None:
                return True
            else:
                return False
        else:
            return not p.wait() 
Example 67
Project: NiujiaoDebugger   Author: MrSrc   File: test_support.py    GNU General Public License v3.0 5 votes vote down vote up
def check_options(self, args, func, expected=None):
        code = f'from test.support import {func}; print(repr({func}()))'
        cmd = [sys.executable, *args, '-c', code]
        env = {key: value for key, value in os.environ.items()
               if not key.startswith('PYTHON')}
        proc = subprocess.run(cmd,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.DEVNULL,
                              universal_newlines=True,
                              env=env)
        if expected is None:
            expected = args
        self.assertEqual(proc.stdout.rstrip(), repr(expected))
        self.assertEqual(proc.returncode, 0) 
Example 68
Project: NiujiaoDebugger   Author: MrSrc   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def start(self):
        try:
            f = open(self.procfile, 'r')
        except OSError as e:
            warnings.warn('/proc not available for stats: {}'.format(e),
                          RuntimeWarning)
            sys.stderr.flush()
            return

        watchdog_script = findfile("memory_watchdog.py")
        self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script],
                                             stdin=f, stderr=subprocess.DEVNULL)
        f.close()
        self.started = True 
Example 69
Project: NiujiaoDebugger   Author: MrSrc   File: test_subprocess.py    GNU General Public License v3.0 5 votes vote down vote up
def test_stdout_devnull(self):
        p = subprocess.Popen([sys.executable, "-c",
                              'for i in range(10240):'
                              'print("x" * 1024)'],
                              stdout=subprocess.DEVNULL)
        p.wait()
        self.assertEqual(p.stdout, None) 
Example 70
Project: NiujiaoDebugger   Author: MrSrc   File: test_subprocess.py    GNU General Public License v3.0 5 votes vote down vote up
def test_stderr_devnull(self):
        p = subprocess.Popen([sys.executable, "-c",
                              'import sys\n'
                              'for i in range(10240):'
                              'sys.stderr.write("x" * 1024)'],
                              stderr=subprocess.DEVNULL)
        p.wait()
        self.assertEqual(p.stderr, None) 
Example 71
Project: NiujiaoDebugger   Author: MrSrc   File: test_subprocess.py    GNU General Public License v3.0 5 votes vote down vote up
def test_stdin_devnull(self):
        p = subprocess.Popen([sys.executable, "-c",
                              'import sys;'
                              'sys.stdin.read(1)'],
                              stdin=subprocess.DEVNULL)
        p.wait()
        self.assertEqual(p.stdin, None) 
Example 72
Project: NiujiaoDebugger   Author: MrSrc   File: test_shutil.py    GNU General Public License v3.0 5 votes vote down vote up
def test_tarfile_vs_tar(self):
        root_dir, base_dir = self._create_files()
        base_name = os.path.join(self.mkdtemp(), 'archive')
        tarball = make_archive(base_name, 'gztar', root_dir, base_dir)

        # check if the compressed tarball was created
        self.assertEqual(tarball, base_name + '.tar.gz')
        self.assertTrue(os.path.isfile(tarball))

        # now create another tarball using `tar`
        tarball2 = os.path.join(root_dir, 'archive2.tar')
        tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir]
        subprocess.check_call(tar_cmd, cwd=root_dir,
                              stdout=subprocess.DEVNULL)

        self.assertTrue(os.path.isfile(tarball2))
        # let's compare both tarballs
        self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))

        # trying an uncompressed one
        tarball = make_archive(base_name, 'tar', root_dir, base_dir)
        self.assertEqual(tarball, base_name + '.tar')
        self.assertTrue(os.path.isfile(tarball))

        # now for a dry_run
        tarball = make_archive(base_name, 'tar', root_dir, base_dir,
                               dry_run=True)
        self.assertEqual(tarball, base_name + '.tar')
        self.assertTrue(os.path.isfile(tarball)) 
Example 73
Project: NiujiaoDebugger   Author: MrSrc   File: uuid.py    GNU General Public License v3.0 5 votes vote down vote up
def _popen(command, *args):
    import os, shutil, subprocess
    executable = shutil.which(command)
    if executable is None:
        path = os.pathsep.join(('/sbin', '/usr/sbin'))
        executable = shutil.which(command, path=path)
        if executable is None:
            return None
    # LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
    # on stderr (Note: we don't have an example where the words we search
    # for are actually localized, but in theory some system could do so.)
    env = dict(os.environ)
    env['LC_ALL'] = 'C'
    proc = subprocess.Popen((executable,) + args,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.DEVNULL,
                            env=env)
    return proc

# For MAC (a.k.a. IEEE 802, or EUI-48) addresses, the second least significant
# bit of the first octet signifies whether the MAC address is universally (0)
# or locally (1) administered.  Network cards from hardware manufacturers will
# always be universally administered to guarantee global uniqueness of the MAC
# address, but any particular machine may have other interfaces which are
# locally administered.  An example of the latter is the bridge interface to
# the Touch Bar on MacBook Pros.
#
# This bit works out to be the 42nd bit counting from 1 being the least
# significant, or 1<<41.  We'll prefer universally administered MAC addresses
# over locally administered ones since the former are globally unique, but
# we'll return the first of the latter found if that's all the machine has.
#
# See https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local 
Example 74
Project: aridi   Author: dpgon   File: gathering1.py    GNU General Public License v3.0 4 votes vote down vote up
def _getdisks(precheck):
    disks = []
    mounted = []
    unmounted = []
    repeated = []

    # check disks and parts with lsblk
    if precheck.checkcommand("lsblk"):
        outputlsblk = check_output(["lsblk", "-l"],
                                   stderr=DEVNULL).decode("utf-8").splitlines()[1:]
        for item in outputlsblk:
            spacefree = " ".join(item.split()).split(" ")
            name = spacefree[0]
            blktype = spacefree[5]
            size = spacefree[3]
            mountpoint = "".join(spacefree[6:])

            if name not in repeated:
                repeated.append(name)
                if blktype == "disk" or blktype == "dmraid":
                    disks.append([name, size])
                elif blktype == "part":
                    if mountpoint:
                        mounted.append([name, size, mountpoint, [], [], []])
                    else:
                        unmounted.append([name, size])

    # check findmnt output
    if precheck.checkcommand("findmnt"):
        outputfindmnt = check_output(["findmnt", "-l"],
                                     stderr=DEVNULL).decode("utf-8").splitlines()[1:]
        for item in outputfindmnt:
            spacefree = " ".join(item.split()).split(" ")
            target = spacefree[0]
            try:
                options = spacefree[3]
                fstype = spacefree[2]
            except IndexError:
                options = spacefree[2]
                fstype = spacefree[1]

            for part in range(len(mounted)):
                if mounted[part][2] == target:
                    mounted[part][3] = fstype
                    mounted[part][4] = options
                    try:
                        output = check_output(["df", mounted[part][2], "-h"],
                                              stderr=DEVNULL).decode("utf-8").splitlines()[1]
                    except CalledProcessError as e:
                        output = e.output.decode("utf-8").splitlines()[1]
                    mounted[part][5] = " ".join(output.split()).split(" ")[2]

    return disks, unmounted, mounted 
Example 75
Project: aridi   Author: dpgon   File: gathering2.py    GNU General Public License v3.0 4 votes vote down vote up
def _gethostnames(report, precheck):
    summ = '\nHostname and banner info:\n'
    detail = detailheader("Hostnames information")

    if precheck.shouldread('/etc/hostname'):
        report.hostname = open('/etc/hostname').read()
        summ += ' |__hostname: ' + report.hostname
        detail += detailfile('/etc/hostname')
        detail += '{}\n'.format(report.hostname)
    elif precheck.checkcommand("hostname"):
        report.hostname = check_output(["hostname"]).decode("utf-8")
        summ += ' |__hostname: ' + report.hostname
        detail += detailfile('hostname')
        detail += '{}\n'.format(report.hostname)
    else:
        report.hostname = gethostname()
        summ += ' |__hostname: ' + report.hostname
        detail += detailfile('hostname')
        detail += '{}\n'.format(report.hostname)

    if precheck.checkcommand("dnsdomainname"):
        try:
            report.domainname = check_output(["dnsdomainname"], stderr=DEVNULL).decode("utf-8")
            if report.domainname:
                summ += ' |__dnsdomainname: ' + report.domainname
                detail += detailfile('domainname')
                detail += '{}\n'.format(report.hostname)
        except:
            pass

    if precheck.checkcommand("hostid"):
        report.hostid = check_output(["hostid"]).decode("utf-8")
        summ += ' |__hostid: ' + report.hostid
        detail += detailfile('hostid')
        detail += '{}\n'.format(report.hostid)

    # Check banner files
    if precheck.shouldread('/etc/issue'):
        report.issue = open('/etc/issue').read()
        summ += ' |__You have an issue banner\n'
        detail += detailfile('/etc/issue')
        detail += '{}\n'.format(report.issue)

    if precheck.shouldread('/etc/issue.net'):
        report.issuenet = open('/etc/issue.net').read()
        summ += ' |__You have an issue.net banner\n'
        detail += detailfile('/etc/issue.net')
        detail += '{}\n'.format(report.issuenet)

    if precheck.shouldread('/etc/motd'):
        report.motd = open('/etc/motd').read()
        summ += ' |__You have an motd banner\n'
        detail += detailfile('/etc/motd')
        detail += '{}\n'.format(report.motd)

    return summ, detail 
Example 76
Project: limejudge   Author: infmagic2047   File: judge.py    MIT License 4 votes vote down vote up
def run(self):
        proc = subprocess.Popen(self.prog,
                                stderr=subprocess.DEVNULL,
                                preexec_fn=self.preexec,
                                cwd=self.cwd)

        tused = 0
        mused = 0

        while True:
            _, status, rusage = os.wait4(proc.pid, 0)
            tused = rusage.ru_utime

            if os.WIFEXITED(status) or os.WIFSIGNALED(status):
                if os.WIFEXITED(status) and not os.WEXITSTATUS(status):
                    result = JudgeResult.NORMAL
                else:
                    result = JudgeResult.RUNTIME_ERROR
                break

            # WIFSTOPPED
            if os.WSTOPSIG(status) == signal.SIGXCPU:
                proc.kill()
                result = JudgeResult.TIME_LIMIT_EXCEEDED
                break
            if os.WSTOPSIG(status) not in (signal.SIGTRAP,
                                           signal.SIGWINCH):
                proc.kill()
                result = JudgeResult.RUNTIME_ERROR
                break

            # Check time usage
            if tused > self.limits.time_limit:
                proc.kill()
                result = JudgeResult.TIME_LIMIT_EXCEEDED
                break

            # Check memory usage
            with open('/proc/{}/status'.format(proc.pid), 'r') as fp:
                for line in fp:
                    if line.startswith('VmPeak:'):
                        mused = int(line[7:-3]) * 1024
            if mused > self.limits.memory_limit:
                proc.kill()
                result = JudgeResult.MEMORY_LIMIT_EXCEEDED
                break

            try:
                ptrace.syscall(proc.pid, 0)
            except ProcessLookupError:
                # Child process dies
                pass

        resource_usage = ResourceUsage(time_usage=tused,
                                       memory_usage=mused)
        return result, resource_usage 
Example 77
Project: job_manager   Author: lnls-fac   File: pyjob_run.py    MIT License 4 votes vote down vote up
def get_new_jobs_and_submit(njobstoget):

    # If it still can run more jobs, I ask the server for new ones:
    if (njobstoget > 0) and MyConfigs.MoreJobs:
        ok, NewQueue = handle_request('GIME_JOBS', njobstoget)
        if not ok:
            return
        for k, v in NewQueue.items():
            # create temporary directory
            tempdir = '/'.join([TMPFLDR, FOLDERFORMAT.format(k)])
            os.mkdir(tempdir)
            # create files
            createfile(
                name='/'.join([tempdir, SUBMITSCRNAME.format(k)]),
                data=SUBMITSCR.format(v.execution_script_name, k, JOBDONE),
                stats=MyStats(st_mode=0o774))
            for name, info in v.execution_script.items():
                createfile(
                    name='/'.join([tempdir, name]),
                    data=info[1], stats=info[0])
            for name, info in v.input_files.items():
                createfile(
                    name='/'.join([tempdir, name]),
                    data=info[1], stats=info[0])
            for name, info in v.output_files.items():
                createfile(
                    name='/'.join([tempdir, name]),
                    data=info[1], stats=info[0])
            # submit job
            proc = psutil.Popen(
                '/'.join([tempdir, SUBMITSCRNAME.format(k)]),
                stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
                start_new_session=True, cwd=tempdir)
            # update queues
            v.status_key = 'r'
            proc.nice(MyConfigs.niceness)
            MyQueue.update({k: v})
            jobid2proc.update({k: proc})
            # create job file to be loaded later, if necessary:
            createfile(
                name='/'.join([tempdir, JOBFILE.format(proc.pid)]),
                data=repr(v)) 
Example 78
Project: inkscape-shortcut-manager   Author: gillescastel   File: vim.py    MIT License 4 votes vote down vote up
def open_vim(self, compile_latex):
    f = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.tex')

    f.write('$$')
    f.close()

    config['open_editor'](f.name)

    latex = ""
    with open(f.name, 'r') as g:
        latex = g.read().strip()

    os.remove(f.name)

    if latex != '$$':
        if not compile_latex:
            svg = f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <svg>
              <text
                 style="font-size:{config['font_size']}px; font-family:'{config['font']}';-inkscape-font-specification:'{config['font']}, Normal';fill:#000000;fill-opacity:1;stroke:none;"
                 xml:space="preserve"><tspan sodipodi:role="line" >{latex}</tspan></text>
            </svg> """
            copy(svg, target=TARGET)
        else:
            m = tempfile.NamedTemporaryFile(mode='w+', delete=False)
            m.write(config['latex_document'](latex))
            m.close()

            working_directory = tempfile.gettempdir()
            subprocess.run(
                ['pdflatex', m.name],
                cwd=working_directory,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL
            )

            subprocess.run(
                ['pdf2svg', f'{m.name}.pdf', f'{m.name}.svg'],
                cwd=working_directory
            )

            with open(f'{m.name}.svg') as svg:
                subprocess.run(
                    ['xclip', '-selection', 'c', '-target', TARGET],
                    stdin=svg
                )

        self.press('v', X.ControlMask)
    self.press('Escape') 
Example 79
Project: siphon-cli   Author: getsiphon   File: system.py    MIT License 4 votes vote down vote up
def background_process(cmd, time_estimate=60, spinner=False):
    """
    Run a given command in the background and display a progress bar.
    Provide an time estimate (in seconds) for the task to complete, and
    the bar will run at an appropriate rate. If it is finished before the
    time estimate, the bar will hurry up and reach the end. If the process
    is still running after the time estimate, the bar will wait at 90%% until
    the task is complete.

    Returns a process object when complete that can be inspected for
    the status code, errors etc.
    """
    try:
        p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.DEVNULL,
                             stderr=subprocess.PIPE)
        if spinner:
            spin_phases = itertools.cycle(['|', '/', '-', '\\'])
            while p.poll() is None:
                sys.stdout.write('\r%s' % next(spin_phases))
                time.sleep(0.05)
                sys.stdout.flush()
            sys.stdout.write('\rDone\n')
        else:
            progress = 0
            bar_width = 50  # Length in chars

            load_up_to = int(50 * 0.9)  # 90%
            sleep_time = time_estimate / bar_width
            while p.poll() is None:
                sys.stdout.write('\r[%s%s]' % ('=' * progress,
                                            ' ' * (bar_width - progress)))
                sys.stdout.flush()
                time.sleep(sleep_time)
                if progress < load_up_to:
                    progress += 1
            remainder = bar_width - progress
            for i in range(remainder):
                progress += 1
                sys.stdout.write('\r[%s%s]' % ('=' * progress,
                                               ' ' * (bar_width - progress)))
                sys.stdout.flush()
                time.sleep(0.1)
            sys.stdout.write('\n')
        return p

    except KeyboardInterrupt:
        p.kill()
        raise 
Example 80
Project: NiujiaoDebugger   Author: MrSrc   File: webbrowser.py    GNU General Public License v3.0 4 votes vote down vote up
def open(self, url, new=0, autoraise=True):
        # XXX Currently I know no way to prevent KFM from opening a new win.
        if new == 2:
            action = "newTab"
        else:
            action = "openURL"

        devnull = subprocess.DEVNULL

        try:
            p = subprocess.Popen(["kfmclient", action, url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull)
        except OSError:
            # fall through to next variant
            pass
        else:
            p.wait()
            # kfmclient's return code unfortunately has no meaning as it seems
            return True

        try:
            p = subprocess.Popen(["konqueror", "--silent", url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull,
                                 start_new_session=True)
        except OSError:
            # fall through to next variant
            pass
        else:
            if p.poll() is None:
                # Should be running now.
                return True

        try:
            p = subprocess.Popen(["kfm", "-d", url],
                                 close_fds=True, stdin=devnull,
                                 stdout=devnull, stderr=devnull,
                                 start_new_session=True)
        except OSError:
            return False
        else:
            return (p.poll() is None)