Python os.devnull() Examples

The following are code examples for showing how to use os.devnull(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: code   Author: ActiveState   File: recipe-580672.py    (MIT License) View Source Project 7 votes vote down vote up
def test_err_in_fun(self):
            # Test that the original signal this process was hit with
            # is not returned in case fun raise an exception. Instead,
            # we're supposed to see retsig = 1.
            ret = pyrun(textwrap.dedent(
                """
                import os, signal, imp, sys
                mod = imp.load_source("mod", r"{}")

                def foo():
                    sys.stderr = os.devnull
                    1 / 0

                sig = signal.SIGTERM if os.name == 'posix' else \
                    signal.CTRL_C_EVENT
                mod.register_exit_fun(foo)
                os.kill(os.getpid(), sig)
                """.format(os.path.abspath(__file__), TESTFN)
            ))
            if POSIX:
                self.assertEqual(ret, 1)
                assert ret != signal.SIGTERM, strfsig(ret) 
Example 2
Project: wib   Author: chengsoonong   File: cli.py    (GNU General Public License v3.0) View Source Project 7 votes vote down vote up
def call(self, args, devnull=False):
        """Call other processes.
        args - list of command args
        devnull - whether to pipe stdout to /dev/null (or equivalent)
        """
        if self.debug:
            click.echo(subprocess.list2cmdline(args))
            click.confirm('Continue?', default=True, abort=True)
        try:
            kwargs = {}
            if devnull:
                # Pipe to /dev/null (or equivalent).
                kwargs['stderr'] = subprocess.STDOUT
                kwargs['stdout'] = self.FNULL
            ret_code = subprocess.call(args, **kwargs)
        except subprocess.CalledProcessError:
            return False
        return ret_code 
Example 3
Project: alfred-mpd   Author: deanishe   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 4
Project: games_nebula   Author: yancharkin   File: launcher_wine.py    (license) View Source Project 6 votes vote down vote up
def win64_available(self):

        wine_bin, \
        wineserver_bin, \
        wine_lib = self.get_wine_bin_path()

        dev_null = open(os.devnull, 'w')
        try:
            proc = subprocess.Popen([wine_bin + '64'], stdout=dev_null, \
                stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
            dev_null.close()
            stdoutdata, stderrdata = proc.communicate()
            if proc.returncode == 1:
                self.combobox_winearch.set_visible(True)
                return True
            else:
                self.combobox_winearch.set_visible(False)
                self.winearch = 'win32'
                return False
        except:
            self.combobox_winearch.set_visible(False)
            self.winearch = 'win32'
            return False 
Example 5
Project: ipwb   Author: oduwsdl   File: util.py    (MIT License) View Source Project 6 votes vote down vote up
def isDaemonAlive(hostAndPort="{0}:{1}".format(IPFSAPI_IP, IPFSAPI_PORT)):
    """Ensure that the IPFS daemon is running via HTTP before proceeding"""
    client = ipfsapi.Client(IPFSAPI_IP, IPFSAPI_PORT)

    try:
        # OSError if ipfs not installed, redundant of below
        # subprocess.call(['ipfs', '--version'], stdout=open(devnull, 'wb'))

        # ConnectionError/AttributeError if IPFS daemon not running
        client.id()
        return True
    except (ConnectionError, exceptions.AttributeError):
        logError("Daemon is not running at http://" + hostAndPort)
        return False
    except OSError:
        logError("IPFS is likely not installed. "
                 "See https://ipfs.io/docs/install/")
        sys.exit()
    except:
        logError('Unknown error in retrieving daemon status')
        logError(sys.exc_info()[0]) 
Example 6
Project: DeepSea   Author: SUSE   File: ui_iscsi.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def interfaces(self, wrapped=True):
        """
        Parse grains for all network interfaces on igw roles.  Possibly
        select public interface.
        """
        _stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

        igws = self.local.cmd("[email protected]:igw", 'grains.get', ['ipv4'], expr_form="compound")
        sys.stdout = _stdout
        if wrapped:
            data = []
            for igw in igws:
                for addr in igws[igw]:
                    if addr == '127.0.0.1':
                        continue
                    data.append({'node': igw, 'addr': addr})
            return data
        else:
            for igw in igws:
                igws[igw].remove('127.0.0.1')

            return igws 
Example 7
Project: DeepSea   Author: SUSE   File: cephprocesses.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def restart_required(role=None, cluster='ceph'):
    """
    Complement Runner call to cephprocesses.restart_required_lsof
    """
    assert role
    _stdout = sys.stdout
    sys.stdout = open(os.devnull, 'w')

    local = salt.client.LocalClient()

    role_search = "[email protected]:{} and [email protected]:{}".format(cluster, role)
    restart = local.cmd(role_search,
                        'cephprocesses.restart_required_lsof',
                        ["role={}".format(role)],
                        expr_form="compound")

    sys.stdout = _stdout
    return restart 
Example 8
Project: DeepSea   Author: SUSE   File: cephprocesses.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def _status(search, roles, quiet):
    """
    Return a structure of roles with module results
    """
    # When search matches no minions, salt prints to stdout.  Suppress stdout.
    _stdout = sys.stdout
    sys.stdout = open(os.devnull, 'w')

    status = {}
    local = salt.client.LocalClient()

    for role in roles:
        role_search = search + " and [email protected]:{}".format(role)
        status[role] = local.cmd(role_search,
                                 'cephprocesses.check',
                                 kwarg={'roles': [role]},
                                 quiet=quiet,
                                 expr_form="compound")

    sys.stdout = _stdout
    log.debug(pprint.pformat(status))
    return status 
Example 9
Project: DeepSea   Author: SUSE   File: deepsea_minions.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def _query(self):
        """
        Returns the value of deepsea_minions
        """
        # When search matches no minions, salt prints to stdout.
        # Suppress stdout.
        _stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

        # Relying on side effect - pylint: disable=unused-variable
        ret = self.local.cmd('*', 'saltutil.pillar_refresh')
        minions = self.local.cmd('*', 'pillar.get', ['deepsea_minions'],
                                 expr_form="compound")
        sys.stdout = _stdout
        for minion in minions:
            if minions[minion]:
                return minions[minion]

        log.error("deepsea_minions is not set")
        return [] 
Example 10
Project: DeepSea   Author: SUSE   File: deepsea_minions.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def _matches(self):
        """
        Returns the list of matched minions
        """
        if self.deepsea_minions:
            # When search matches no minions, salt prints to stdout.
            # Suppress stdout.
            _stdout = sys.stdout
            sys.stdout = open(os.devnull, 'w')
            result = self.local.cmd(self.deepsea_minions,
                                    'pillar.get',
                                    ['id'],
                                    expr_form="compound")
            sys.stdout = _stdout
            return result.keys()
        return [] 
Example 11
Project: Gank-Alfred-Workflow   Author: hujiaweibujidao   File: notify.py    (MIT License) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 12
Project: Gank-Alfred-Workflow   Author: hujiaweibujidao   File: notify.py    (MIT License) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 13
Project: code   Author: ActiveState   File: recipe-580672.py    (MIT License) View Source Project 6 votes vote down vote up
def test_exception(self):
            # Make sure handler fun is called on exception.
            ret = pyrun(textwrap.dedent(
                """
                import os, imp, sys
                mod = imp.load_source("mod", r"{}")

                def foo():
                    with open(r"{}", "ab") as f:
                        f.write(b"1")

                mod.register_exit_fun(foo)
                sys.stderr = os.devnull
                1 / 0
                """.format(os.path.abspath(__file__), TESTFN)
            ))
            self.assertEqual(ret, 1)
            with open(TESTFN, "rb") as f:
                self.assertEqual(f.read(), b"1") 
Example 14
Project: code   Author: ActiveState   File: recipe-580672.py    (MIT License) View Source Project 6 votes vote down vote up
def test_kinterrupt(self):
            # Simulates CTRL + C and make sure the exit function is called.
            ret = pyrun(textwrap.dedent(
                """
                import os, imp, sys
                mod = imp.load_source("mod", r"{}")

                def foo():
                    with open(r"{}", "ab") as f:
                        f.write(b"1")

                mod.register_exit_fun(foo)
                sys.stderr = os.devnull
                raise KeyboardInterrupt
                """.format(os.path.abspath(__file__), TESTFN)
            ))
            self.assertEqual(ret, 1)
            with open(TESTFN, "rb") as f:
                self.assertEqual(f.read(), b"1") 
Example 15
Project: PyCIRCLeanMail   Author: CIRCL   File: helpers.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def __init__(self, raw_email, debug=False):
        '''
            Setup the base options of the copy/convert setup
        '''
        self.raw_email = raw_email
        self.log_processing = StringIO()
        self.log_content = StringIO()
        self.tree(self.raw_email)

        twiggy_out = outputs.StreamOutput(formats.shell_format, stream=self.log_processing)
        emitters['*'] = filters.Emitter(levels.DEBUG, True, twiggy_out)

        self.log_name = log.name('files')

        self.cur_attachment = None

        self.debug = debug
        if self.debug:
            if not os.path.exists('debug_logs'):
                os.makedirs('debug_logs')
            self.log_debug_err = os.path.join('debug_logs', 'debug_stderr.log')
            self.log_debug_out = os.path.join('debug_logs', 'debug_stdout.log')
        else:
            self.log_debug_err = os.devnull
            self.log_debug_out = os.devnull 
Example 16
Project: mobot   Author: JokerQyou   File: updater.py    (license) View Source Project 6 votes vote down vote up
def _check_ssl_cert(self, cert, key):
        # Check SSL-Certificate with openssl, if possible
        try:
            exit_code = subprocess.call(
                ["openssl", "x509", "-text", "-noout", "-in", cert],
                stdout=open(os.devnull, 'wb'),
                stderr=subprocess.STDOUT)
        except OSError:
            exit_code = 0
        if exit_code is 0:
            try:
                self.httpd.socket = ssl.wrap_socket(
                    self.httpd.socket, certfile=cert, keyfile=key, server_side=True)
            except ssl.SSLError as error:
                self.logger.exception('Failed to init SSL socket')
                raise TelegramError(str(error))
        else:
            raise TelegramError('SSL Certificate invalid') 
Example 17
Project: workflows.kyoyue   Author: wizyoung   File: notify.py    (MIT License) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 18
Project: alphy   Author: maximepeschard   File: notify.py    (MIT License) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 19
Project: pheweb   Author: statgen   File: load_utils.py    (MIT License) View Source Project 6 votes vote down vote up
def run_script(script):
    script = 'set -euo pipefail\n' + script
    try:
        with open(os.devnull) as devnull:
            # is this the right way to block stdin?
            data = subprocess.check_output(['bash', '-c', script], stderr=subprocess.STDOUT, stdin=devnull)
        status = 0
    except subprocess.CalledProcessError as ex:
        data = ex.output
        status = ex.returncode
    data = data.decode('utf8')
    if status != 0:
        raise PheWebError(
            'FAILED with status {}\n'.format(status) +
            'output was:\n' +
            data)
    return data 
Example 20
Project: hostapd-mana   Author: adde88   File: capture.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, targetfd, tmpfile=None):
        self.targetfd = targetfd
        try:
            self.targetfd_save = os.dup(self.targetfd)
        except OSError:
            self.start = lambda: None
            self.done = lambda: None
        else:
            if targetfd == 0:
                assert not tmpfile, "cannot set tmpfile with stdin"
                tmpfile = open(os.devnull, "r")
                self.syscapture = SysCapture(targetfd)
            else:
                if tmpfile is None:
                    f = TemporaryFile()
                    with f:
                        tmpfile = safe_text_dupfile(f, mode="wb+")
                if targetfd in patchsysdict:
                    self.syscapture = SysCapture(targetfd, tmpfile)
                else:
                    self.syscapture = NoCapture()
            self.tmpfile = tmpfile
            self.tmpfile_fd = tmpfile.fileno() 
Example 21
Project: d-tailor   Author: jcg   File: Functions.py    (license) View Source Project 6 votes vote down vote up
def analyze_structure(seq,filename,ensemble=False):
    
        
    chdir(project_dir)

    system("echo '" + str(seq) + "' > " + filename + ".seq")        
    fnull = open(devnull, 'w')   #this line is necessary to omit output generated by UNAFOLD
    if ensemble:         
        call("./3rdParty/unafold/UNAFold.pl -n RNA " + filename + ".seq", shell = True, stdout = fnull, stderr = fnull)     #code is necessary to omit output generated by UNAFOLD
    else:
        call("./3rdParty/unafold/hybrid-ss-min -n RNA " + filename + ".seq", shell = True, stdout = fnull, stderr = fnull)     #code is necessary to omit output generated by UNAFOLD
                
    if os.path.isfile(filename+".ct"):        
        system("mv %s*.ct tmp/structures/" % filename)
        # remove tmp files
    
    system("rm %s*" % filename)
    #system("mv " + filename + "* tmp/unafold_files/")
    fnull.close()             
 
    return 1 
Example 22
Project: dartqc   Author: esteinig   File: DartModules.py    (license) View Source Project 6 votes vote down vote up
def _run_cdhit(self, fasta_path, identity=0.95, word_size=5, description_length=0, cdhit_path=None):

        """ Run CDHIT-EST for sequences, install with sudo apt install cd-hit on Ubuntu """

        self.messages.get_cdhit_message(identity)

        if cdhit_path is None:
            cdhit_path = "cd-hit-est"

        file_name = self.project + "_IdentityClusters_" + str(identity)

        out_file = os.path.join(self.tmp_path, file_name)
        cluster_path = os.path.join(self.tmp_path, file_name + '.clstr')

        with open(os.devnull, "w") as devnull:
            call([cdhit_path, "-i", fasta_path, "-o", out_file, "-c", str(identity), "-n", str(word_size),
                  "-d", str(description_length)], stdout=devnull)

        return cluster_path 
Example 23
Project: polichombr   Author: ANSSI-FR   File: sample.py    (license) View Source Project 6 votes vote down vote up
def disassemble_it(filename, address=None):
    """
        Wrapper for the ruby disassembler script.
    """
    FNULL = open(os.devnull, 'w')

    if address is not None:
        outfile = filename + "_disass_" + hex(address)
    else:
        outfile = filename + "_disass_None"
    args = ['ruby', 'analysis_tools/disassfunc.rb',
            "-graph", "-svg", "-o", outfile, filename]
    if address is not None:
        args.append(hex(address))
    proc = Popen(args, stdin=FNULL, stdout=FNULL, stderr=FNULL)
    proc.wait()
    FNULL.close()
    app.logger.debug("Disassembly just finished!")
    return outfile 
Example 24
Project: polichombr   Author: ANSSI-FR   File: task_analyzeitrb.py    (license) View Source Project 6 votes vote down vote up
def analyze_it(self):
        """
        Wrapper for the ruby analysis script.
        Executes and get results from files.
        """
        FNULL = open(os.devnull, 'w')
        args = ['ruby', 'analysis_tools/AnalyzeIt.rb', self.storage_file]
        proc = Popen(args, stdin=FNULL, stdout=FNULL, stderr=FNULL)
        proc.wait()
        FNULL.close()

        # TEXT report, just UTF-8 decode/parsing
        fname = self.storage_file + '.txt'
        if os.path.exists(fname):
            data = open(fname, 'rb').read()
            self.txt_report = re.sub(r'[^\x00-\x7F]', '', data).decode('utf-8')

        return True 
Example 25
Project: q2-vsearch   Author: qiime2   File: test_cluster_features.py    (license) View Source Project 6 votes vote down vote up
def test_no_clustering(self):
        with redirected_stdio(stderr=os.devnull):
            obs_table, obs_sequences = cluster_features_de_novo(
                sequences=self.input_sequences, table=self.input_table,
                perc_identity=1.0)
        # order of identifiers is important for biom.Table equality
        obs_table = \
            obs_table.sort_order(self.input_table.ids(axis='observation'),
                                 axis='observation')
        self.assertEqual(obs_table, self.input_table)

        obs_seqs = _read_seqs(obs_sequences)

        # sequences are reverse-sorted by abundance in output
        exp_seqs = [self.input_sequences_list[0], self.input_sequences_list[3],
                    self.input_sequences_list[2], self.input_sequences_list[1]]
        self.assertEqual(obs_seqs, exp_seqs) 
Example 26
Project: q2-vsearch   Author: qiime2   File: test_cluster_features.py    (license) View Source Project 6 votes vote down vote up
def test_99_percent_clustering(self):
        exp_table = biom.Table(np.array([[104, 106, 109],
                                         [1, 1, 2],
                                         [7, 8, 9]]),
                               ['feature1', 'feature2',
                                'feature4'],
                               ['sample1', 'sample2', 'sample3'])

        with redirected_stdio(stderr=os.devnull):
            obs_table, obs_sequences = cluster_features_de_novo(
                sequences=self.input_sequences, table=self.input_table,
                perc_identity=0.99)
        # order of identifiers is important for biom.Table equality
        obs_table = \
            obs_table.sort_order(exp_table.ids(axis='observation'),
                                 axis='observation')
        self.assertEqual(obs_table, exp_table)

        # sequences are reverse-sorted by abundance in output
        obs_seqs = _read_seqs(obs_sequences)
        exp_seqs = [self.input_sequences_list[0], self.input_sequences_list[3],
                    self.input_sequences_list[1]]
        self.assertEqual(obs_seqs, exp_seqs) 
Example 27
Project: q2-vsearch   Author: qiime2   File: test_cluster_features.py    (license) View Source Project 6 votes vote down vote up
def test_97_percent_clustering_feature1_most_abundant(self):
        exp_table = biom.Table(np.array([[111, 114, 118],
                                         [1, 1, 2]]),
                               ['feature1', 'feature2'],
                               ['sample1', 'sample2', 'sample3'])

        with redirected_stdio(stderr=os.devnull):
            obs_table, obs_sequences = cluster_features_de_novo(
                sequences=self.input_sequences, table=self.input_table,
                perc_identity=0.97)
        # order of identifiers is important for biom.Table equality
        obs_table = \
            obs_table.sort_order(exp_table.ids(axis='observation'),
                                 axis='observation')
        self.assertEqual(obs_table, exp_table)

        # sequences are reverse-sorted by abundance in output
        obs_seqs = _read_seqs(obs_sequences)
        exp_seqs = [self.input_sequences_list[0], self.input_sequences_list[1]]
        self.assertEqual(obs_seqs, exp_seqs) 
Example 28
Project: q2-vsearch   Author: qiime2   File: test_chimera.py    (license) View Source Project 6 votes vote down vote up
def test_uchime_denovo(self):
        with redirected_stdio(stderr=os.devnull):
            chime, nonchime, stats = uchime_denovo(
                sequences=self.input_sequences, table=self.input_table)

        obs_chime = _read_seqs(chime)
        exp_chime = [self.input_sequences_list[3]]
        self.assertEqual(obs_chime, exp_chime)

        # sequences are reverse-sorted by abundance in output
        obs_nonchime = _read_seqs(nonchime)
        exp_nonchime = [self.input_sequences_list[0],
                        self.input_sequences_list[1],
                        self.input_sequences_list[2]]
        self.assertEqual(obs_nonchime, exp_nonchime)

        with stats.open() as stats_fh:
            stats_text = stats_fh.read()
        self.assertTrue('feature1' in stats_text)
        self.assertTrue('feature2' in stats_text)
        self.assertTrue('feature3' in stats_text)
        self.assertTrue('feature4' in stats_text)
        stats_lines = [e for e in stats_text.split('\n')
                       if len(e) > 0]
        self.assertEqual(len(stats_lines), 4) 
Example 29
Project: q2-vsearch   Author: qiime2   File: test_join_pairs.py    (license) View Source Project 6 votes vote down vote up
def test_join_pairs_all_samples_w_no_joined_seqs(self):
        # minmergelen is set very high here, resulting in no sequences
        # being joined across the three samples.
        with redirected_stdio(stderr=os.devnull):
            obs = join_pairs(self.input_seqs, minmergelen=500)

        # manifest is as expected
        self._test_manifest(obs)

        # expected number of fastq files are created
        output_fastqs = list(obs.sequences.iter_views(FastqGzFormat))
        self.assertEqual(len(output_fastqs), 3)

        for fastq_name, fastq_path in output_fastqs:
            with redirected_stdio(stderr=os.devnull):
                seqs = skbio.io.read(str(fastq_path), format='fastq',
                                     compression='gzip', constructor=skbio.DNA)
            seqs = list(seqs)
            seq_lengths = np.asarray([len(s) for s in seqs])

            self.assertEqual(len(seq_lengths), 0) 
Example 30
Project: azure-cli-shell   Author: Azure   File: argfinder.py    (license) View Source Project 6 votes vote down vote up
def get_parsed_args(self, comp_words):
        """ gets the parsed args from a patched parser """
        active_parsers = self._patch_argument_parser()

        parsed_args = argparse.Namespace()

        self.completing = True
        if USING_PYTHON2:
            # Python 2 argparse only properly works with byte strings.
            comp_words = [ensure_bytes(word) for word in comp_words]

        try:
            stderr = sys.stderr
            sys.stderr = io.open(os.devnull, "w")

            active_parsers[0].parse_known_args(comp_words, namespace=parsed_args)

            sys.stderr.close()
            sys.stderr = stderr
        except BaseException:
            pass

        self.completing = False
        return parsed_args 
Example 31
Project: GoToMeetingTools   Author: plongitudes   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 32
Project: behelper   Author: istommao   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 33
Project: cloudml-samples   Author: GoogleCloudPlatform   File: pipeline.py    (license) View Source Project 6 votes vote down vote up
def get_cloud_project():
  cmd = [
      'gcloud', '-q', 'config', 'list', 'project',
      '--format=value(core.project)'
  ]
  with open(os.devnull, 'w') as dev_null:
    try:
      res = subprocess.check_output(cmd, stderr=dev_null).strip()
      if not res:
        raise Exception('--cloud specified but no Google Cloud Platform '
                        'project found.\n'
                        'Please specify your project name with the --project '
                        'flag or set a default project: '
                        'gcloud config set project YOUR_PROJECT_NAME')
      return res
    except OSError as e:
      if e.errno == errno.ENOENT:
        raise Exception('gcloud is not installed. The Google Cloud SDK is '
                        'necessary to communicate with the Cloud ML service. '
                        'Please install and set up gcloud.')
      raise 
Example 34
Project: radar   Author: amoose136   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 35
Project: ParlAI   Author: facebookresearch   File: train.py    (license) View Source Project 6 votes vote down vote up
def validate(opt, agent):
    old_datatype = agent.opt['datatype']
    agent.opt['datatype'] = 'valid'

    opt = deepcopy(opt)
    opt['datatype'] = 'valid'
    opt['terminate'] = True
    opt['batchsize'] = 1

    old_stdout = sys.stdout
    sys.stdout = open(os.devnull, 'w')
    valid_world = create_task(opt, agent)
    sys.stdout = old_stdout

    for _ in valid_world:
        valid_world.parley()

    stats = valid_world.report()
    agent.opt['datatype'] = old_datatype
    return stats 
Example 36
Project: pytzdata   Author: sdispater   File: make.py    (license) View Source Project 6 votes vote down vote up
def build(self):
        self.line('[<comment>Building tzdata</>]')
        dest_path = os.path.join(self.path, 'tz')

        # Getting VERSION
        with open(os.path.join(dest_path, 'version')) as f:
            version = f.read().strip()

        self.write('<comment>Building</> version <fg=cyan>{}</>'.format(version))
        os.chdir(dest_path)

        with open(os.devnull, 'w') as temp:
            subprocess.call(
                ['make', 'TOPDIR={}'.format(dest_path), 'install'],
                stdout=temp,
                stderr=temp
            )

        self.overwrite('<info>Built</> version <fg=cyan>{}</>'.format(version))
        self.line('') 
Example 37
Project: alfred-workflows   Author: arthurhammer   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 38
Project: alfred-zebra   Author: r0x73   File: notify.py    (license) View Source Project 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using `sips`.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if `sips` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', b'{0}'.format(size), b'{0}'.format(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with {0}'.format(retcode)) 
Example 39
Project: nojs   Author: chrisdickinson   File: package.py    (license) View Source Project 6 votes vote down vote up
def TeeCmd(cmd, logfile, fail_hard=True):
  """Runs cmd and writes the output to both stdout and logfile."""
  # Reading from PIPE can deadlock if one buffer is full but we wait on a
  # different one.  To work around this, pipe the subprocess's stderr to
  # its stdout buffer and don't give it a stdin.
  # shell=True is required in cmd.exe since depot_tools has an svn.bat, and
  # bat files only work with shell=True set.
  proc = subprocess.Popen(cmd, bufsize=1, shell=sys.platform == 'win32',
                          stdin=open(os.devnull), stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT)
  for line in iter(proc.stdout.readline,''):
    Tee(line, logfile)
    if proc.poll() is not None:
      break
  exit_code = proc.wait()
  if exit_code != 0 and fail_hard:
    print 'Failed:', cmd
    sys.exit(1) 
Example 40
Project: nojs   Author: chrisdickinson   File: package.py    (license) View Source Project 6 votes vote down vote up
def TeeCmd(cmd, logfile, fail_hard=True):
  """Runs cmd and writes the output to both stdout and logfile."""
  # Reading from PIPE can deadlock if one buffer is full but we wait on a
  # different one.  To work around this, pipe the subprocess's stderr to
  # its stdout buffer and don't give it a stdin.
  # shell=True is required in cmd.exe since depot_tools has an svn.bat, and
  # bat files only work with shell=True set.
  proc = subprocess.Popen(cmd, bufsize=1, shell=sys.platform == 'win32',
                          stdin=open(os.devnull), stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT)
  for line in iter(proc.stdout.readline,''):
    Tee(line, logfile)
    if proc.poll() is not None:
      break
  exit_code = proc.wait()
  if exit_code != 0 and fail_hard:
    print 'Failed:', cmd
    sys.exit(1) 
Example 41
Project: swiftbackmeup   Author: redhat-cip   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def run(self, with_intermediate_file=False, cwd=None):
        """Method to run the backup command where it applies."""

        command = self.build_dump_command()

        if with_intermediate_file:
            try:
                backup_file_f = open('%s/%s' % (self.output_directory,
                                                self.backup_file), 'w')
            except IOError as exc:
                raise

            p = subprocess.Popen(command.split(), stdout=backup_file_f,
                                 env=self.env, cwd=cwd)
            p.wait()
            backup_file_f.flush()
        else:
            FNULL = open(os.devnull, 'w')
            p = subprocess.Popen(command.split(), env=self.env, cwd=cwd,
                                 stdout=FNULL, stderr=subprocess.STDOUT) 
Example 42
Project: swiftbackmeup   Author: redhat-cip   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def restore(self, backup_filename,with_intermediate_file=False):
        """Method to restore the backup."""

        self.store.get(self.swift_container, backup_filename,
                       self.output_directory)
        command = self.build_restore_command(backup_filename)


        if with_intermediate_file:
            file_path = '%s/%s' % (self.output_directory, backup_filename)
            backup_file_content = open(file_path, 'r').read()

            p = subprocess.Popen(command.split(), stdin=subprocess.PIPE)
            p.communicate(backup_file_content)
        else:
            FNULL = open(os.devnull, 'w')
            p = subprocess.Popen(command.split(), stdout=FNULL,
                                 stderr=subprocess.STDOUT)
            p.wait()

        if self.clean_local_copy:
            self._clean_local_copy(backup_filename) 
Example 43
Project: Vanquish   Author: frizb   File: Vanquish2.py    (license) View Source Project 6 votes vote down vote up
def execute_command(self, command):
        Logger.verbose("[email protected]:/# " + command)
        Logger.debug("execute_command() - Starting: - " + command)
        command_start_time = time.time()
        with open(self.active_commands, 'w') as active_command_report_file:
            active_command_report_file.write("Last Update: " + str(datetime.now()) + "\n")
            active_command_report_file.write(pformat(self.thread_pool_commands, indent=4, width=1))
        self.thread_pool_commands.append(command)
        process = Popen(command, shell=True, stdin=PIPE, stderr=self.command_error_log, stdout=self.devnull)
        process.stdin.close()
        # FIXME: Process wait is causing the application to hang in some fringe cases - need to find a better way
        if process.wait() != 0:
            Logger.debug("execute_command() - ERRORS EXECUTING:  - " + command)
            self.thread_pool_errors.append(command)
        Logger.debug("execute_command() - COMPLETED! - " + command)
        self.thread_pool_commands.remove(command)
        if self.args.benchmarking:
            with open(self.active_commands, 'w') as active_command_report_file:
                active_command_report_file.write("Last Update: "+ str(datetime.now())+"\n")
                active_command_report_file.write(pformat(self.thread_pool_commands, indent=4, width=1))
            self.benchmarking_csv.write(
                time.strftime('%H:%M:%S', time.gmtime(time.time() - command_start_time)) + "," + command.replace(",",
                                                                                                                 " ") + "\n") 
Example 44
Project: Dallinger   Author: Dallinger   File: data.py    (license) View Source Project 6 votes vote down vote up
def dump_database(id):
    """Dump the database to a temporary directory."""

    tmp_dir = tempfile.mkdtemp()
    current_dir = os.getcwd()
    os.chdir(tmp_dir)

    FNULL = open(os.devnull, 'w')
    heroku_app = HerokuApp(dallinger_uid=id, output=FNULL)
    heroku_app.backup_capture()
    heroku_app.backup_download()

    for filename in os.listdir(tmp_dir):
        if filename.startswith("latest.dump"):
            os.rename(filename, "database.dump")

    os.chdir(current_dir)

    return os.path.join(tmp_dir, "database.dump") 
Example 45
Project: TCP-IP   Author: JackZ0   File: log_test.py    (license) View Source Project 6 votes vote down vote up
def setUp(self):
        super(PostArgParseSetupTest, self).setUp()
        self.config.debug = False
        self.config.max_log_backups = 1000
        self.config.quiet = False
        self.config.verbose_count = constants.CLI_DEFAULTS['verbose_count']
        self.devnull = open(os.devnull, 'w')

        from certbot.log import ColoredStreamHandler
        self.stream_handler = ColoredStreamHandler(six.StringIO())
        from certbot.log import MemoryHandler, TempHandler
        self.temp_handler = TempHandler()
        self.temp_path = self.temp_handler.path
        self.memory_handler = MemoryHandler(self.temp_handler)
        self.root_logger = mock.MagicMock(
            handlers=[self.memory_handler, self.stream_handler]) 
Example 46
Project: jtc   Author: jwilk-archive   File: test_examples.py    (license) View Source Project 6 votes vote down vote up
def _test_good(self, filename):
        base_name, _ = os.path.splitext(filename)
        input_filename = base_name + '.input'
        if not os.path.exists(input_filename):
            input_filename = os.devnull
        output_filename = base_name + '.output'
        rc, stderr = self._compile(filename)
        stderr = stderr.decode()
        assert_multi_line_equal(stderr, '')
        assert_equal(rc, 0)
        with open(input_filename, 'rb') as input_file:
            child = ipc.Popen(self.runner + [self.executable],
                stdin=input_file,
                stdout=ipc.PIPE,
                stderr=ipc.PIPE
            )
            stdout = child.stdout.read()
            stderr = child.stderr.read()
            rc = child.wait()
        stderr = stderr.decode()
        assert_multi_line_equal(stderr, '')
        assert_equal(rc, 0)
        with open(output_filename, 'rb') as output_file:
            expected_stdout = output_file.read()
        assert_equal(expected_stdout, stdout) 
Example 47
Project: ISB-CGC-pipelines   Author: isb-cgc   File: utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def getJobLogs(args, config):  # TODO: reimplement
		pipelineDbUtils = PipelineDbUtils(config)

		jobInfo = pipelineDbUtils.getJobInfo(select=["stdout_log", "stderr_log", "gcs_log_path"],
		                                     where={"job_id": args.jobId})

		with open(os.devnull, 'w') as fnull:
			if args.stdout:
				try:
					stdoutLogFile = subprocess.check_output(
						["gsutil", "cat", os.path.join(jobInfo[0].gcs_log_path, jobInfo[0].stdout_log)], stderr=fnull)
				except subprocess.CalledProcessError as e:
					print "ERROR: couldn't get the stdout log : {reason}".format(reason=e)
					exit(-1)

				print "STDOUT:\n"
				print stdoutLogFile
				print "---------\n"

			if args.stderr:
				try:
					stderrLogFile = subprocess.check_output(
						["gsutil", "-q", "cat", os.path.join(jobInfo[0].gcs_log_path, jobInfo[0].stderr_log)],
						stderr=fnull)
				except subprocess.CalledProcessError as e:
					print "ERROR: couldn't get the stderr log : {reason}".format(reason=e)
					exit(-1)

				print "STDERR:\n"
				print stderrLogFile
				print "---------\n"

		pipelineDbUtils.closeConnection() 
Example 48
Project: charm-plumgrid-gateway   Author: openstack   File: pg_gw_utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def interface_exists(interface):
    '''
    Checks if interface exists on node.
    '''
    try:
        subprocess.check_call(['ip', 'link', 'show', interface],
                              stdout=open(os.devnull, 'w'),
                              stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError:
        return False
    return True 
Example 49
Project: python-   Author: secondtonone1   File: index.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, url=None):
        """
        Initialise an instance.

        :param url: The URL of the index. If not specified, the URL for PyPI is
                    used.
        """
        self.url = url or DEFAULT_INDEX
        self.read_configuration()
        scheme, netloc, path, params, query, frag = urlparse(self.url)
        if params or query or frag or scheme not in ('http', 'https'):
            raise DistlibException('invalid repository: %s' % self.url)
        self.password_handler = None
        self.ssl_verifier = None
        self.gpg = None
        self.gpg_home = None
        self.rpc_proxy = None
        with open(os.devnull, 'w') as sink:
            # Use gpg by default rather than gpg2, as gpg2 insists on
            # prompting for passwords
            for s in ('gpg', 'gpg2'):
                try:
                    rc = subprocess.check_call([s, '--version'], stdout=sink,
                                               stderr=sink)
                    if rc == 0:
                        self.gpg = s
                        break
                except OSError:
                    pass 
Example 50
Project: python-   Author: secondtonone1   File: baseparser.py    (license) View Source Project 5 votes vote down vote up
def get_config_files(self):
        # the files returned by this method will be parsed in order with the
        # first files listed being overridden by later files in standard
        # ConfigParser fashion
        config_file = os.environ.get('PIP_CONFIG_FILE', False)
        if config_file == os.devnull:
            return []

        # at the base we have any site-wide configuration
        files = list(site_config_files)

        # per-user configuration next
        if not self.isolated:
            if config_file and os.path.exists(config_file):
                files.append(config_file)
            else:
                # This is the legacy config file, we consider it to be a lower
                # priority than the new file location.
                files.append(legacy_config_file)

                # This is the new config file, we consider it to be a higher
                # priority than the legacy file.
                files.append(
                    os.path.join(
                        appdirs.user_config_dir("pip"),
                        config_basename,
                    )
                )

        # finally virtualenv configuration first trumping others
        if running_under_virtualenv():
            venv_config_file = os.path.join(
                sys.prefix,
                config_basename,
            )
            if os.path.exists(venv_config_file):
                files.append(venv_config_file)

        return files