Python sys.stderr() Examples

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

You may also check out all available functions/classes of the module sys , or try the search function .

Example 1
Project: mlbv   Author: kmac   File: util.py    License: GNU General Public License v3.0 6 votes vote down vote up
def init_logging(log_file=None, append=False, console_loglevel=logging.INFO):
    """Set up logging to file and console."""
    if log_file is not None:
        if append:
            filemode_val = 'a'
        else:
            filemode_val = 'w'
        logging.basicConfig(level=logging.DEBUG,
                            format="%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s",
                            # datefmt='%m-%d %H:%M',
                            filename=log_file,
                            filemode=filemode_val)
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler()
    console.setLevel(console_loglevel)
    # set a format which is simpler for console use
    formatter = logging.Formatter("%(message)s")
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger('').addHandler(console)
    global LOG
    LOG = logging.getLogger(__name__) 
Example 2
Project: aegea   Author: kislyuk   File: test.py    License: Apache License 2.0 6 votes vote down vote up
def call(self, cmd, **kwargs):
        print('Running "{}"'.format(cmd), file=sys.stderr)
        expect = kwargs.pop("expect", [dict(return_codes=[os.EX_OK], stdout=None, stderr=None)])
        process = subprocess.Popen(cmd, stdin=kwargs.get("stdin", subprocess.PIPE), stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, **kwargs)
        out, err = process.communicate()
        return_code = process.poll()
        out = out.decode(sys.stdin.encoding)
        err = err.decode(sys.stdin.encoding)

        def match(return_code, out, err, expected):
            exit_ok = return_code in expected["return_codes"]
            stdout_ok = re.search(expected.get("stdout") or "", out)
            stderr_ok = re.search(expected.get("stderr") or "", err)
            return exit_ok and stdout_ok and stderr_ok
        if not any(match(return_code, out, err, exp) for exp in expect):
            print(err)
            e = subprocess.CalledProcessError(return_code, cmd, output=out)
            e.stdout, e.stderr = out, err
            raise e
        return self.SubprocessResult(out, err, return_code) 
Example 3
Project: arm_now   Author: nongiach   File: arm_now.py    License: MIT License 6 votes vote down vote up
def check_dependencies_or_exit():
    dependencies = [
            which("e2cp",
                ubuntu="apt-get install e2tools",
                arch="yaourt -S e2tools",
                darwin="brew install e2tools gettext e2fsprogs\nbrew unlink e2fsprogs && brew link e2fsprogs -f"),
            which("qemu-system-arm",
                  ubuntu="apt-get install qemu",
                  kali="apt-get install qemu-system",
                  arch="pacman -S qemu-arch-extra",
                  darwin="brew install qemu"),
            which("unzip",
                ubuntu="apt-get install unzip",
                arch="pacman -S unzip",
                darwin="brew install unzip")
            ]
    if not all(dependencies):
        print("requirements missing, plz install them", file=sys.stderr)
        sys.exit(1) 
Example 4
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: notify.py    License: MIT License 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 5
Project: cherrypy   Author: cherrypy   File: _cplogging.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, appid=None, logger_root='cherrypy'):
        self.logger_root = logger_root
        self.appid = appid
        if appid is None:
            self.error_log = logging.getLogger('%s.error' % logger_root)
            self.access_log = logging.getLogger('%s.access' % logger_root)
        else:
            self.error_log = logging.getLogger(
                '%s.error.%s' % (logger_root, appid))
            self.access_log = logging.getLogger(
                '%s.access.%s' % (logger_root, appid))
        self.error_log.setLevel(logging.INFO)
        self.access_log.setLevel(logging.INFO)

        # Silence the no-handlers "warning" (stderr write!) in stdlib logging
        self.error_log.addHandler(NullHandler())
        self.access_log.addHandler(NullHandler())

        cherrypy.engine.subscribe('graceful', self.reopen_files) 
Example 6
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: train_models.py    License: Apache License 2.0 6 votes vote down vote up
def create_model_directory(args):


    # create necessary directories
    if os.path.isdir("models_{name}".format(name=args.name)):
        print("Directory models_{name} already exists, old files will be overwritten".format(name=args.name), file=sys.stderr)
        
    else:
        os.mkdir("models_{name}".format(name=args.name))
        os.mkdir("models_{name}/Data".format(name=args.name))
        os.mkdir("models_{name}/Tokenizer".format(name=args.name))

    # copy necessary files
    if args.embeddings: # embeddings
        copyfile(args.embeddings, "models_{name}/Data/embeddings.vectors".format(name=args.name))
    copyfile("{config}/pipelines.yaml".format(config=args.config_directory), "models_{name}/pipelines.yaml".format(name=args.name))
    process_morpho(args) # train/dev files for tagger/parser
    process_config(args) # configs for tagger/parser 
Example 7
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: output_mod.py    License: Apache License 2.0 6 votes vote down vote up
def launch(args,q_in,q_out):
    start=time.time()
    total_parsed_trees=0
    total_parsed_tokens=0
    next_report=start+10.0 #report every 10sec at most
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            print("Output exiting",file=sys.stderr,flush=True)
            return
        total_parsed_trees+=sum(1 for line in txt.split("\n") if line.startswith("1\t"))
        total_parsed_tokens+=sum(1 for line in txt.split("\n") if re.match(token_regex, line))
        if total_parsed_trees>0 and time.time()>next_report:
            time_spent=time.time()-start
            print("Runtime: {}:{} [m:s]  Parsed: {} [trees], {} [tokens]  Speed: {} [trees/sec]  {} [sec/tree] {} [tokens/sec]".format(int(time_spent)//60,int(time_spent)%60,total_parsed_trees,total_parsed_tokens, total_parsed_trees/time_spent,time_spent/total_parsed_trees, total_parsed_tokens/time_spent) ,file=sys.stderr,flush=True)
            next_report=time.time()+10
        print(txt,end="",flush=True) 
Example 8
Project: CAMISIM   Author: CAMI-challenge   File: loggingwrapper.py    License: Apache License 2.0 6 votes vote down vote up
def add_log_stream(self, stream=sys.stderr, level=logging.INFO):
		"""
		Add a stream where messages are outputted to.

		@param stream: stderr/stdout or a file stream
		@type stream: file | FileIO | StringIO
		@param level: minimum level of messages to be logged
		@type level: int | long

		@return: None
		@rtype: None
		"""
		assert self.is_stream(stream)
		# assert isinstance(stream, (file, io.FileIO))
		assert level in self._levelNames

		err_handler = logging.StreamHandler(stream)
		err_handler.setFormatter(self.message_formatter)
		err_handler.setLevel(level)
		self._logger.addHandler(err_handler) 
Example 9
Project: CAMISIM   Author: CAMI-challenge   File: taxonomynode.py    License: Apache License 2.0 6 votes vote down vote up
def active_parent_nodes_consistency(oCurrNode):
		""" active_parent_nodes_consistency(oCurrNode)
			oCurrNode ... class Node
			------------------------------------------------------------------------
			Checks, if the parent node of an ncbi taxonomy node has at least one active
			child node. Every parent node of the ncbi taxonomy node that has no active
			child nodes will be inactivated.
			Returns the last node that has a parent with at least one active child.
		"""
		try:
			bCheckActiveChildNodes = False
			oChildrenNodes = oCurrNode.parent.children
			for oChildNode in oChildrenNodes:
				bCheckActiveChildNodes = bCheckActiveChildNodes or oChildNode.node_active

			if not bCheckActiveChildNodes:
				oCurrNode.parent.node_active = False
				oCurrNode = TaxonomyNode.active_parent_nodes_consistency(oCurrNode.parent)

		except Exception as e:
			print >> sys.stderr, str(e)
			# logging.error(str(e))

		return oCurrNode 
Example 10
Project: indras_net   Author: gcallah   File: test_basic.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)

# make sure to run test file from root directory! 
Example 11
Project: indras_net   Author: gcallah   File: test_coop.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 12
Project: indras_net   Author: gcallah   File: test_hiv.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 13
Project: indras_net   Author: gcallah   File: test_fmarket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 14
Project: indras_net   Author: gcallah   File: test_party.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 15
Project: indras_net   Author: gcallah   File: test_wolfsheep.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 16
Project: indras_net   Author: gcallah   File: test_grid.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 17
Project: indras_net   Author: gcallah   File: test_gridang.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 18
Project: indras_net   Author: gcallah   File: test_fashion.py    License: GNU General Public License v3.0 5 votes vote down vote up
def announce(name):
    present = date.today()
    print("Running " + name + " at " + str(present), file=sys.stderr)


# make sure to run test file from root directory! 
Example 19
Project: indras_net   Author: gcallah   File: test_models.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_models(self):
        for name, env in self.models.items():
            print("Testing " + name + "...", file=sys.stderr)
            self.assertTrue(env.runN(2) > 0) 
Example 20
Project: svviz   Author: svviz   File: export.py    License: MIT License 5 votes vote down vote up
def checkWebkitToPDF():
    try:
        subprocess.check_call("webkitToPDF", stderr=subprocess.PIPE, shell=True)
        return True
    except subprocess.CalledProcessError:
        return False 
Example 21
Project: svviz   Author: svviz   File: export.py    License: MIT License 5 votes vote down vote up
def _convertSVG_webkitToPDF(inpath, outpath, outformat):
    if outformat.lower() != "pdf":
        return None

    try:
        cmd = "webkitToPDF {} {}".format(inpath, outpath)
        subprocess.check_call(cmd, shell=True)#, stderr=subprocess.PIPE)
    except subprocess.CalledProcessError:
        return None

    return open(outpath, "rb").read() 
Example 22
Project: svviz   Author: svviz   File: export.py    License: MIT License 5 votes vote down vote up
def _convertSVG_rsvg_convert(inpath, outpath, outformat):
    options = ""
    outformat = outformat.lower()
    if outformat == "png":
        options = "-a --background-color white"

    try:
        subprocess.check_call("rsvg-convert -f {} {} -o {} {}".format(outformat, options, outpath, inpath), shell=True)
    except subprocess.CalledProcessError as e:
        print("EXPORT ERROR:", str(e))

    return open(outpath, "rb").read()


# def test():
#     base = """  <svg><rect x="10" y="10" height="100" width="100" style="stroke:#ffff00; stroke-width:3; fill: #0000ff"/><text x="25" y="25" fill="blue">{}</text></svg>"""
#     svgs = [base.format("track {}".format(i)) for i in range(5)]

#     tc = TrackCompositor(200, 600)
#     for i, svg in enumerate(svgs):
#         tc.addTrack(svg, i, viewbox="0 0 110 110")

#     outf = open("temp.svg", "w")
#     outf.write(tc.render())
#     outf.flush()
#     outf.close()

#     pdfPath = convertSVGToPDF("temp.svg")
#     subprocess.check_call("open {}".format(pdfPath), shell=True)

# if __name__ == '__main__':
#     test()

#     import sys
#     print(canConvertSVGToPDF(), file=sys.stderr) 
Example 23
Project: svviz   Author: svviz   File: multiprocessor.py    License: MIT License 5 votes vote down vote up
def finish(self):
        text = [" "]
        if len(self.name) > 0:
            text.append(self.name)
        text.append("[completed] time elapsed: {}".format(formatTime(time.time()-self.t0)))
        text = " ".join(text)
        text = text.ljust(self.term_width)
        
        sys.stderr.write(text+"\n") 
Example 24
Project: svviz   Author: svviz   File: multiprocessor.py    License: MIT License 5 votes vote down vote up
def redraw(self):
        if self.isatty or (time.time()-self.lastRedraw) > 30:
            overallTotal = sum(x[1] for x in list(self.barsToProgress.values()))
            overallCompleted = sum(x[0] for x in list(self.barsToProgress.values()))
            
            numBars = len(self.barsToProgress)+1
            
            barWidth = (self.term_width-40-len(self.name)) // numBars - 1

            if self.status == "+":
                self.status = " "
            else:
                self.status = "+"
                
            text = [" ", self.status]
            if len(self.name) > 0:
                text.append(self.name)

            text.append(self._getBar("total", overallCompleted, overallTotal, 25))

            text.append("left:%s"%self.timeRemaining)

            if barWidth >= 6:
                for barid in sorted(self.barsToProgress):
                    text.append(self._getBar(barid, self.barsToProgress[barid][0], self.barsToProgress[barid][1], barWidth))
            else:
                text.append("[processes=%d]"%len(self.barsToProgress))
                
            endmarker = "\n"
            if self.isatty:
                endmarker = "\r"

            sys.stderr.write(" ".join(text)+endmarker)

            self.lastRedraw = time.time() 
Example 25
Project: clikit   Author: sdispater   File: error_output_stream.py    License: MIT License 5 votes vote down vote up
def __init__(self):  # type: () -> None
        super(ErrorOutputStream, self).__init__(sys.stderr) 
Example 26
Project: vergeml   Author: mme   File: display.py    License: MIT License 5 votes vote down vote up
def __init__(self,
                 highlight_color=36,
                 table_style='default',
                 progress_style='default', 
                 is_interactive=True,
                 stdout=sys.stdout, 
                 stderr=sys.stderr):
        self.stdout = stdout
        self.stderr = stderr
        self.highlight_color = highlight_color
        self.table_style = table_style
        self.progress_style = progress_style
        self.is_interactive = is_interactive
        self.cursor_hidden = False
        self.quiet = False 
Example 27
Project: vergeml   Author: mme   File: ls.py    License: MIT License 5 votes vote down vote up
def __call__(self, args, env):

        # Parse and partition into normal and comparison args.
        args, cargs = _parse_args(args, env)

        # When trainings dir does not exist, print an error and exit
        if not os.path.exists(env.get('trainings-dir')):
            print("No trainings found.", file=sys.stderr)
            return

        info, hyper = _find_trained_models(args, env)

        theader, tdata, left_align = _format_table(args, cargs, info, hyper)

        _output_table(args['output'], theader, tdata, left_align) 
Example 28
Project: vergeml   Author: mme   File: ls.py    License: MIT License 5 votes vote down vote up
def _output_table(output, theader, tdata, left_align):

    if not tdata:
        print("No matching trained models found.", file=sys.stderr)

    if output == 'table':
        if not tdata:
            return
        tdata.insert(0, theader)
        print(DISPLAY.table(tdata, left_align=left_align).getvalue(fit=True))

    elif output == 'json':
        res = []
        for row in tdata:
            res.append(dict(zip(theader, row)))
        print(json.dumps(res))

    elif output == 'csv':
        buffer = io.StringIO()

        writer = csv.writer(buffer)
        writer.writerow(theader)
        for row in tdata:
            writer.writerow(row)
        val = buffer.getvalue()
        val = val.replace('\r', '')
        print(val.strip()) 
Example 29
Project: vergeml   Author: mme   File: libraries.py    License: MIT License 5 votes vote down vote up
def version():
        stderr = sys.stderr
        sys.stderr = open(os.devnull, 'w')
        import keras # pylint: disable=E0401
        sys.stderr = stderr
        return keras.__version__ 
Example 30
Project: aegea   Author: kislyuk   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def main(args=None):
    parsed_args = parser.parse_args(args=args)
    logger.setLevel(parsed_args.log_level)
    has_attrs = (getattr(parsed_args, "sort_by", None) and getattr(parsed_args, "columns", None))
    if has_attrs and parsed_args.sort_by not in parsed_args.columns:
        parsed_args.columns.append(parsed_args.sort_by)
    try:
        result = parsed_args.entry_point(parsed_args)
    except Exception as e:
        if isinstance(e, NoRegionError):
            msg = "The AWS CLI is not configured."
            msg += " Please configure it using instructions at"
            msg += " http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html"
            exit(msg)
        elif logger.level < logging.ERROR:
            raise
        else:
            err_msg = traceback.format_exc()
            try:
                err_log_filename = os.path.join(config.user_config_dir, "error.log")
                with open(err_log_filename, "ab") as fh:
                    print(datetime.datetime.now().isoformat(), file=fh)
                    print(err_msg, file=fh)
                exit("{}: {}. See {} for error details.".format(e.__class__.__name__, e, err_log_filename))
            except Exception:
                print(err_msg, file=sys.stderr)
                exit(os.EX_SOFTWARE)
    if isinstance(result, SystemExit):
        raise result
    elif result is not None:
        if isinstance(result, dict) and "ResponseMetadata" in result:
            del result["ResponseMetadata"]
        print(json.dumps(result, indent=2, default=str)) 
Example 31
Project: aegea   Author: kislyuk   File: ssh.py    License: Apache License 2.0 5 votes vote down vote up
def check_output(self, command, input_data=None, stderr=sys.stderr):
        logger.debug('Running "%s"', command)
        ssh_stdin, ssh_stdout, ssh_stderr = self.exec_command(command)
        if input_data is not None:
            ssh_stdin.write(input_data)
        exit_code = ssh_stdout.channel.recv_exit_status()
        stderr.write(ssh_stderr.read().decode("utf-8"))
        if exit_code != os.EX_OK:
            raise Exception('Error while running "{}": {}'.format(command, errno.errorcode.get(exit_code)))
        return ssh_stdout.read().decode("utf-8") 
Example 32
Project: aegea   Author: kislyuk   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def get_pricing_data(service_code, filters=None, max_cache_age_days=30):
    from ... import config

    if filters is None:
        filters = [("location", region_name(clients.ec2.meta.region_name))]

    get_products_args = dict(ServiceCode=service_code,
                             Filters=[dict(Type="TERM_MATCH", Field=k, Value=v) for k, v in filters])
    cache_key = hashlib.sha256(json.dumps(get_products_args, sort_keys=True).encode()).hexdigest()[:32]
    service_code_filename = os.path.join(config.user_config_dir, "pricing_cache_{}.json.gz".format(cache_key))
    try:
        cache_date = datetime.fromtimestamp(os.path.getmtime(service_code_filename))
        if cache_date < datetime.now() - timedelta(days=max_cache_age_days):
            raise Exception("Cache is too old, discard")
        with gzip.open(service_code_filename) as gz_fh:
            with io.BufferedReader(gz_fh) as buf_fh:
                pricing_data = json.loads(buf_fh.read().decode())
    except Exception:
        logger.info("Fetching pricing data for %s", service_code)
        client = boto3.client("pricing", region_name="us-east-1")
        pricing_data = [json.loads(p) for p in paginate(client.get_paginator("get_products"), **get_products_args)]
        try:
            with gzip.open(service_code_filename, "w") as fh:
                fh.write(json.dumps(pricing_data).encode())
        except Exception as e:
            print(e, file=sys.stderr)
    return pricing_data 
Example 33
Project: aegea   Author: kislyuk   File: test.py    License: Apache License 2.0 5 votes vote down vote up
def test_deploy_utils(self):
        deploy_utils_bindir = os.path.join(pkg_root, "aegea", "rootfs.skel", "usr", "bin")
        for script in glob.glob(deploy_utils_bindir + "/aegea*"):
            self.call([script, "--help"], expect=[dict(return_codes=[0, 1])])
        for script in "aegea-deploy-pilot", "aegea-git-ssh-helper":
            self.call(os.path.join(deploy_utils_bindir, script),
                      expect=[dict(return_codes=[2], stderr="(required|too few)")]) 
Example 34
Project: aegea   Author: kislyuk   File: test.py    License: Apache License 2.0 5 votes vote down vote up
def test_secrets(self):
        unauthorized_ok = [dict(return_codes=[os.EX_OK]),
                           dict(return_codes=[1, os.EX_SOFTWARE], stderr="(AccessDenied|NoSuchKey)")]
        secret_name = "test_secret_{}".format(int(time.time()))
        self.call("{s}=test aegea secrets put {s} --iam-role aegea.launch".format(s=secret_name),
                  shell=True, expect=unauthorized_ok)
        self.call("aegea secrets put {s} --generate-ssh-key --iam-role aegea.launch".format(s=secret_name),
                  shell=True, expect=unauthorized_ok)
        self.call("aegea secrets ls", shell=True, expect=unauthorized_ok)
        self.call("aegea secrets ls --json", shell=True, expect=unauthorized_ok)
        self.call("aegea secrets get {s} --iam-role aegea.launch".format(s=secret_name), shell=True,
                  expect=unauthorized_ok)
        self.call("aegea secrets delete {s} --iam-role aegea.launch".format(s=secret_name), shell=True,
                  expect=unauthorized_ok) 
Example 35
Project: arm_now   Author: nongiach   File: download.py    License: MIT License 5 votes vote down vote up
def get_link_filetype(link):
    if ".cpio" in link or ".ext2" in link or "rootfs" in link:
        return "rootfs"
    elif "dtb" in link:
        return "dtb"
    elif "Image" in link or "vmlinux" in link or "linux.bin" in link:
        return "kernel"
    print("ERROR: I don't know this kind of file {}".format(link), file=sys.stderr)
    # os.kill(0, 9)
    return None 
Example 36
Project: arm_now   Author: nongiach   File: download.py    License: MIT License 5 votes vote down vote up
def download_image(arch, *, dest, real_source):
    if real_source:
        kernel, dtb, rootfs = scrawl_kernel(arch)
        if kernel is None or rootfs is None:
            pred("ERROR: couldn't download files for this arch", file=sys.stderr)
            sys.exit(1)
        download(kernel, dest + Config.kernel, Config.DOWNLOAD_CACHE_DIR)
        if dtb:
            download(dtb, dest + Config.dtb, Config.DOWNLOAD_CACHE_DIR)
        download(rootfs, dest + Config.rootfs, Config.DOWNLOAD_CACHE_DIR)
    else:
        download_from_github(arch)
        check_call("tar xf ~/.config/arm_now/templates/{}.tar.xz {}".format(arch, dest), shell=True)
    with open(dest + "/arch", "w") as F:
        F.write(arch) 
Example 37
Project: aospy   Author: spencerahill   File: versioneer.py    License: Apache License 2.0 5 votes vote down vote up
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
                env=None):
    """Call the given command(s)."""
    assert isinstance(commands, list)
    p = None
    for c in commands:
        try:
            dispcmd = str([c] + args)
            # remember shell=False, so use git.cmd on windows, not just git
            p = subprocess.Popen([c] + args, cwd=cwd, env=env,
                                 stdout=subprocess.PIPE,
                                 stderr=(subprocess.PIPE if hide_stderr
                                         else None))
            break
        except EnvironmentError:
            e = sys.exc_info()[1]
            if e.errno == errno.ENOENT:
                continue
            if verbose:
                print("unable to run %s" % dispcmd)
                print(e)
            return None, None
    else:
        if verbose:
            print("unable to find command, tried %s" % (commands,))
        return None, None
    stdout = p.communicate()[0].strip()
    if sys.version_info[0] >= 3:
        stdout = stdout.decode()
    if p.returncode != 0:
        if verbose:
            print("unable to run %s (error)" % dispcmd)
            print("stdout was %s" % stdout)
        return None, p.returncode
    return stdout, p.returncode 
Example 38
Project: ALF   Author: blackberry   File: WinDBGTrace.py    License: Apache License 2.0 5 votes vote down vote up
def info(msg, *args):
        print >> sys.stderr, msg % args 
Example 39
Project: ALF   Author: blackberry   File: WinDBGTrace.py    License: Apache License 2.0 5 votes vote down vote up
def debug(msg, *args):
        if LOG_DEBUG:
            print >> sys.stderr, "DEBUG|" + msg % args 
Example 40
Project: ALF   Author: blackberry   File: WinDBGMemoryLimit.py    License: Apache License 2.0 5 votes vote down vote up
def info(msg, *args):
        print >> sys.stderr, msg % args 
Example 41
Project: ALF   Author: blackberry   File: WinDBGMemoryLimit.py    License: Apache License 2.0 5 votes vote down vote up
def debug(msg, *args):
        if LOG_DEBUG:
            print >> sys.stderr, "DEBUG|" + msg % args 
Example 42
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    License: MIT License 5 votes vote down vote up
def _call_security(self, action, service, account, *args):
        """Call ``security`` CLI program that provides access to keychains.

        May raise `PasswordNotFound`, `PasswordExists` or `KeychainError`
        exceptions (the first two are subclasses of `KeychainError`).

        :param action: The ``security`` action to call, e.g.
                           ``add-generic-password``
        :type action: ``unicode``
        :param service: Name of the service.
        :type service: ``unicode``
        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param password: the password to secure
        :type password: ``unicode``
        :param *args: list of command line arguments to be passed to
                      ``security``
        :type *args: `list` or `tuple`
        :returns: ``(retcode, output)``. ``retcode`` is an `int`, ``output`` a
                  ``unicode`` string.
        :rtype: `tuple` (`int`, ``unicode``)

        """
        cmd = ['security', action, '-s', service, '-a', account] + list(args)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, _ = p.communicate()
        if p.returncode == 44:  # password does not exist
            raise PasswordNotFound()
        elif p.returncode == 45:  # password already exists
            raise PasswordExists()
        elif p.returncode > 0:
            err = KeychainError('Unknown Keychain error : %s' % stdout)
            err.retcode = p.returncode
            raise err
        return stdout.strip().decode('utf-8') 
Example 43
Project: xrft   Author: xgcm   File: versioneer.py    License: MIT License 5 votes vote down vote up
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
                env=None):
    """Call the given command(s)."""
    assert isinstance(commands, list)
    p = None
    for c in commands:
        try:
            dispcmd = str([c] + args)
            # remember shell=False, so use git.cmd on windows, not just git
            p = subprocess.Popen([c] + args, cwd=cwd, env=env,
                                 stdout=subprocess.PIPE,
                                 stderr=(subprocess.PIPE if hide_stderr
                                         else None))
            break
        except EnvironmentError:
            e = sys.exc_info()[1]
            if e.errno == errno.ENOENT:
                continue
            if verbose:
                print("unable to run %s" % dispcmd)
                print(e)
            return None, None
    else:
        if verbose:
            print("unable to find command, tried %s" % (commands,))
        return None, None
    stdout = p.communicate()[0].strip()
    if sys.version_info[0] >= 3:
        stdout = stdout.decode()
    if p.returncode != 0:
        if verbose:
            print("unable to run %s (error)" % dispcmd)
            print("stdout was %s" % stdout)
        return None, p.returncode
    return stdout, p.returncode 
Example 44
Project: cherrypy   Author: cherrypy   File: _cplogging.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _set_screen_handler(self, log, enable, stream=None):
        h = self._get_builtin_handler(log, 'screen')
        if enable:
            if not h:
                if stream is None:
                    stream = sys.stderr
                h = logging.StreamHandler(stream)
                h.setFormatter(logfmt)
                h._cpbuiltin = 'screen'
                log.addHandler(h)
        elif h:
            log.handlers.remove(h) 
Example 45
Project: cherrypy   Author: cherrypy   File: _cplogging.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def screen(self):
        """Turn stderr/stdout logging on or off.

        If you set this to True, it'll add the appropriate StreamHandler for
        you. If you set it to False, it will remove the handler.
        """
        h = self._get_builtin_handler
        has_h = h(self.error_log, 'screen') or h(self.access_log, 'screen')
        return bool(has_h) 
Example 46
Project: cherrypy   Author: cherrypy   File: _cplogging.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def screen(self, newvalue):
        self._set_screen_handler(self.error_log, newvalue, stream=sys.stderr)
        self._set_screen_handler(self.access_log, newvalue, stream=sys.stdout)

    # -------------------------- File handlers -------------------------- # 
Example 47
Project: zmirror   Author: aploium   File: initialise.py    License: MIT License 5 votes vote down vote up
def init(autoreset=False, convert=None, strip=None, wrap=True):

    if not wrap and any([autoreset, convert, strip]):
        raise ValueError('wrap=False conflicts with any other arg=True')

    global wrapped_stdout, wrapped_stderr
    global orig_stdout, orig_stderr

    orig_stdout = sys.stdout
    orig_stderr = sys.stderr

    if sys.stdout is None:
        wrapped_stdout = None
    else:
        sys.stdout = wrapped_stdout = \
            wrap_stream(orig_stdout, convert, strip, autoreset, wrap)
    if sys.stderr is None:
        wrapped_stderr = None
    else:
        sys.stderr = wrapped_stderr = \
            wrap_stream(orig_stderr, convert, strip, autoreset, wrap)

    global atexit_done
    if not atexit_done:
        atexit.register(reset_all)
        atexit_done = True 
Example 48
Project: zmirror   Author: aploium   File: initialise.py    License: MIT License 5 votes vote down vote up
def deinit():
    if orig_stdout is not None:
        sys.stdout = orig_stdout
    if orig_stderr is not None:
        sys.stderr = orig_stderr 
Example 49
Project: zmirror   Author: aploium   File: initialise.py    License: MIT License 5 votes vote down vote up
def reinit():
    if wrapped_stdout is not None:
        sys.stdout = wrapped_stdout
    if wrapped_stderr is not None:
        sys.stderr = wrapped_stderr 
Example 50
Project: zmirror   Author: aploium   File: ansitowin32.py    License: MIT License 5 votes vote down vote up
def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
        # The wrapped stream (normally sys.stdout or sys.stderr)
        self.wrapped = wrapped

        # should we reset colors to defaults after every .write()
        self.autoreset = autoreset

        # create the proxy wrapping our output stream
        self.stream = StreamWrapper(wrapped, self)

        on_windows = os.name == 'nt'
        # We test if the WinAPI works, because even if we are on Windows
        # we may be using a terminal that doesn't support the WinAPI
        # (e.g. Cygwin Terminal). In this case it's up to the terminal
        # to support the ANSI codes.
        conversion_supported = on_windows and winapi_test()

        # should we strip ANSI sequences from our output?
        if strip is None:
            strip = conversion_supported or (not wrapped.closed and not is_a_tty(wrapped))
        self.strip = strip

        # should we should convert ANSI sequences into win32 calls?
        if convert is None:
            convert = conversion_supported and not wrapped.closed and is_a_tty(wrapped)
        self.convert = convert

        # dict of ansi codes to win32 functions and parameters
        self.win32_calls = self.get_win32_calls()

        # are we wrapping stderr?
        self.on_stderr = self.wrapped is sys.stderr 
Example 51
Project: disentangling_conditional_gans   Author: zalandoresearch   File: misc.py    License: MIT License 5 votes vote down vote up
def save_image_grid(images, filename, drange=[0,1], grid_size=None):
    convert_to_pil_image(create_image_grid(images, grid_size), drange).save(filename)

#----------------------------------------------------------------------------
# Logging of stdout and stderr to a file. 
Example 52
Project: disentangling_conditional_gans   Author: zalandoresearch   File: misc.py    License: MIT License 5 votes vote down vote up
def init_output_logging():
    global output_logger
    if output_logger is None:
        output_logger = OutputLogger()
        sys.stdout = TeeOutputStream([sys.stdout, output_logger], autoflush=True)
        sys.stderr = TeeOutputStream([sys.stderr, output_logger], autoflush=True) 
Example 53
Project: LPHK   Author: nimaid   File: logger.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, file_in):
            self._file = file_in
            self._stderr = sys.stderr
            sys.stderr = self 
Example 54
Project: LPHK   Author: nimaid   File: logger.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __del__(self):
            sys.stderr = self._stderr 
Example 55
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: parser_lib.py    License: Apache License 2.0 5 votes vote down vote up
def txt_to_conllu(sentences):
  """Just a quick dummy to turn list of whitespace-tokenized sentences into conllu"""
  buff=io.StringIO()
  for s in sentences:
    for idx,wrd in enumerate(s.split()):
      print(idx+1,wrd,"_","_","_","_","_","_","_","_",sep="\t",file=buff)
    print(file=buff)
  print("ENTERS PARSER",repr(buff.getvalue()),file=sys.stderr)
  return buff.getvalue() 
Example 56
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: train_models.py    License: Apache License 2.0 5 votes vote down vote up
def train_all(args):

    # tokenizer --- TODO

    # Tagger
    if args.tagger:
        print("Training a tagger", file=sys.stderr)
        status = os.system("python3 {workdir}/../Parser-v2/main.py --save_dir models_{name}/Tagger train --config_file models_{name}/tagger.cfg".format(workdir=thisdir, name=args.name))
        if status != 0:
            print("Tagger status:", status, "Training failed.", file=sys.stderr)
            sys.exit()

    # Parser
    if args.parser:
        print("Training a parser")
        status = os.system("python3 {workdir}/../Parser-v2/main.py --save_dir models_{name}/Parser train --config_file models_{name}/parser.cfg".format(workdir=thisdir, name=args.name))
        if status != 0:
            print("Parser status:", status, "Training failed.", file=sys.stderr)
            sys.exit()

    # Lemmatizer
    if args.lemmatizer == True:
        print("Training a lemmatizer")
        status = os.system("python3 {workdir}/../universal-lemmatizer/train_lemmatizer.py --treebank default --config models_{name}/lemmatizer.yaml".format(workdir=thisdir, name=args.name))
        if status != 0:
            print("Lemmatizer status:", status, "Training failed.", file=sys.stderr)
            sys.exit()

        copy_lemmatizer(args) # copy the latest lemmatizer under correct name

        status = os.system("cat {train} | python3 {workdir}/../build_lemma_cache.py > models_{name}/Lemmatizer/lemma_cache.tsv".format(train=args.train_file, workdir=thisdir, name=args.name)) # build lemma cache
        if status != 0:
            print("Lemma cache status:", status, "Training failed.", file=sys.stderr)
            sys.exit()

    print("Training done", file=sys.stderr) 
Example 57
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: marian_lemmatizer_mod.py    License: Apache License 2.0 5 votes vote down vote up
def parse_text(self,conllu):
        submitted=set()
        to_submit=[]
        submitted_tokdata=[]
        sents=list(prepare_data.read_conllu(conllu.split("\n")))
        ws = create_connection("ws://localhost:{}/translate".format(self.args.marian_port))
        for comments,sent in sents:
            for cols in sent:
                token_data=(cols[FORM],cols[UPOS],cols[XPOS],cols[FEATS])
                if token_data not in self.cache and token_data not in self.local_cache and token_data not in submitted:
                    lem_in=prepare_data.transform_token(cols)[0]
                    to_submit.append(lem_in)
                    submitted_tokdata.append(token_data)
                    submitted.add(token_data)
        
        ws.send("\n".join(to_submit))
        lemmatized=ws.recv()
        #print("LEMMATIZED",lemmatized,file=sys.stderr,flush=True)
        for token_data,lemma in zip(submitted_tokdata,lemmatized.strip().split("\n")):
            self.local_cache[token_data]=prepare_data.detransform_string(lemma)
        result=[]
        for comments,sent in sents:
            for c in comments:
                result.append(c)
            for cols in sent:
                token_data=(cols[FORM],cols[UPOS],cols[XPOS],cols[FEATS])
                if token_data in self.cache:
                    lemma=self.cache[token_data]
                elif token_data in self.local_cache:
                    lemma=self.local_cache[token_data]
                else:
                    assert False
                if not lemma.strip():
                    lemma="_"
                cols[LEMMA]=lemma
                result.append("\t".join(cols))
            result.append("")
        result.append("")
        return "\n".join(result) 
Example 58
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: lemmatizer_mod.py    License: Apache License 2.0 5 votes vote down vote up
def launch(args,q_in,q_out):

    if args.lemma_cache!="" or args.no_xpos==True:
        sys.exit("ERROR! Calling lemmatizer with outdated parameters, you probably need to update your pipeline configs.\nPlease, download updated models with 'python fetch_models.py treebank_code'\nEXITING")

    lemmatizer=LemmatizerWrapper(args)
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            print("Lemmatizer exiting",file=sys.stderr,flush=True)
            return
        q_out.put((jobid,lemmatizer.parse_text(txt))) 
Example 59
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: lemma_cache_mod.py    License: Apache License 2.0 5 votes vote down vote up
def lemmatize_batch(self, conllu_batch):

        output_lines=[]
        lemmatized=0
        token_counter=0
        filled=0
        for (comm, sent) in read_conllu(conllu_batch.split("\n")):
            for c in comm:
                output_lines.append(c)
            for cols in sent:
                if "-" in cols[ID]: # multiword token line, not supposed to be analysed
                    output_lines.append("\t".join(t for t in cols))
                    continue
                token_counter+=1
                if cols[LEMMA]!="_": # already filled in for example by another module, do not process
                    output_lines.append("\t".join(t for t in cols))
                    filled+=1
                    continue
                token_data=(cols[FORM],cols[UPOS],cols[XPOS],cols[FEAT])
                if token_data in self.cache:
                    plemma=self.cache[token_data]
                    if plemma.strip()=="":
                        plemma="_" # make sure not to output empty lemma
                    cols[LEMMA]=plemma
                    output_lines.append("\t".join(t for t in cols))
                    lemmatized+=1
                    continue
                if self.lemmatize_url_and_email==False and self.is_url_or_email(cols[FORM]): # simple copy
                    cols[LEMMA]=cols[FORM]
                    output_lines.append("\t".join(t for t in cols))
                    lemmatized+=1
                    continue

                # lemma not in cache, pass empty lemma for next module
                output_lines.append("\t".join(t for t in cols))
            output_lines.append("")
        print(" >>> {}/{} lemmas already filled before lemma cache module".format(filled,token_counter),file=sys.stderr)
        print(" >>> {}/{} lemmatized with lemma cache".format(lemmatized,token_counter),file=sys.stderr)
        return "\n".join(output_lines)+"\n" 
Example 60
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: lemma_cache_mod.py    License: Apache License 2.0 5 votes vote down vote up
def launch(args,q_in,q_out):
    lemma_cache=LemmaCacheWrapper(args)
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            print("Lemma cache exiting",file=sys.stderr,flush=True)
            return
        q_out.put((jobid,lemma_cache.parse_text(txt)))