Best Python code snippet using avocado_python
run_tool.py
Source:run_tool.py  
1#!/usr/bin/env python2# Copyright (c) 2013 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5"""Wrapper script to help run clang tools across Chromium code.6How to use run_tool.py:7If you want to run a clang tool across all Chromium code:8run_tool.py <tool> <path/to/compiledb>9If you want to include all files mentioned in the compilation database10(this will also include generated files, unlike the previous command):11run_tool.py <tool> <path/to/compiledb> --all12If you want to run the clang tool across only chrome/browser and13content/browser:14run_tool.py <tool> <path/to/compiledb> chrome/browser content/browser15Please see docs/clang_tool_refactoring.md for more information, which documents16the entire automated refactoring flow in Chromium.17Why use run_tool.py (instead of running a clang tool directly):18The clang tool implementation doesn't take advantage of multiple cores, and if19it fails mysteriously in the middle, all the generated replacements will be20lost. Additionally, if the work is simply sharded across multiple cores by21running multiple RefactoringTools, problems arise when they attempt to rewrite a22file at the same time.23run_tool.py will241) run multiple instances of clang tool in parallel252) gather stdout from clang tool invocations263) "atomically" forward #2 to stdout27Output of run_tool.py can be piped into extract_edits.py and then into28apply_edits.py. These tools will extract individual edits and apply them to the29source files. These tools assume the clang tool emits the edits in the30following format:31    ...32    ==== BEGIN EDITS ====33    r:::<file path>:::<offset>:::<length>:::<replacement text>34    r:::<file path>:::<offset>:::<length>:::<replacement text>35    ...etc...36    ==== END EDITS ====37    ...38extract_edits.py extracts only lines between BEGIN/END EDITS markers39apply_edits.py reads edit lines from stdin and applies the edits40"""41import argparse42import functools43import multiprocessing44import os45import os.path46import re47import subprocess48import sys49script_dir = os.path.dirname(os.path.realpath(__file__))50tool_dir = os.path.abspath(os.path.join(script_dir, '../pylib'))51sys.path.insert(0, tool_dir)52from clang import compile_db53def _GetFilesFromGit(paths=None):54  """Gets the list of files in the git repository.55  Args:56    paths: Prefix filter for the returned paths. May contain multiple entries.57  """58  args = []59  if sys.platform == 'win32':60    args.append('git.bat')61  else:62    args.append('git')63  args.append('ls-files')64  if paths:65    args.extend(paths)66  command = subprocess.Popen(args, stdout=subprocess.PIPE)67  output, _ = command.communicate()68  return [os.path.realpath(p) for p in output.splitlines()]69def _GetFilesFromCompileDB(build_directory):70  """ Gets the list of files mentioned in the compilation database.71  Args:72    build_directory: Directory that contains the compile database.73  """74  return [os.path.join(entry['directory'], entry['file'])75          for entry in compile_db.Read(build_directory)]76def _ExecuteTool(toolname, tool_args, build_directory, filename):77  """Executes the clang tool.78  This is defined outside the class so it can be pickled for the multiprocessing79  module.80  Args:81    toolname: Name of the clang tool to execute.82    tool_args: Arguments to be passed to the clang tool. Can be None.83    build_directory: Directory that contains the compile database.84    filename: The file to run the clang tool over.85  Returns:86    A dictionary that must contain the key "status" and a boolean value87    associated with it.88    If status is True, then the generated output is stored with the key89    "stdout_text" in the dictionary.90    Otherwise, the filename and the output from stderr are associated with the91    keys "filename" and "stderr_text" respectively.92  """93  args = [toolname, '-p', build_directory, filename]94  if (tool_args):95    args.extend(tool_args)96  command = subprocess.Popen(97      args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)98  stdout_text, stderr_text = command.communicate()99  stderr_text = re.sub(100      r"^warning: .*'linker' input unused \[-Wunused-command-line-argument\]\n",101      "", stderr_text, flags=re.MULTILINE)102  if command.returncode != 0:103    return {'status': False, 'filename': filename, 'stderr_text': stderr_text}104  else:105    return {'status': True, 'filename': filename, 'stdout_text': stdout_text,106            'stderr_text': stderr_text}107class _CompilerDispatcher(object):108  """Multiprocessing controller for running clang tools in parallel."""109  def __init__(self, toolname, tool_args, build_directory, filenames):110    """Initializer method.111    Args:112      toolname: Path to the tool to execute.113      tool_args: Arguments to be passed to the tool. Can be None.114      build_directory: Directory that contains the compile database.115      filenames: The files to run the tool over.116    """117    self.__toolname = toolname118    self.__tool_args = tool_args119    self.__build_directory = build_directory120    self.__filenames = filenames121    self.__success_count = 0122    self.__failed_count = 0123  @property124  def failed_count(self):125    return self.__failed_count126  def Run(self):127    """Does the grunt work."""128    pool = multiprocessing.Pool()129    result_iterator = pool.imap_unordered(130        functools.partial(_ExecuteTool, self.__toolname, self.__tool_args,131                          self.__build_directory),132                          self.__filenames)133    for result in result_iterator:134      self.__ProcessResult(result)135    sys.stderr.write('\n')136  def __ProcessResult(self, result):137    """Handles result processing.138    Args:139      result: The result dictionary returned by _ExecuteTool.140    """141    if result['status']:142      self.__success_count += 1143      sys.stdout.write(result['stdout_text'])144      sys.stderr.write(result['stderr_text'])145    else:146      self.__failed_count += 1147      sys.stderr.write('\nFailed to process %s\n' % result['filename'])148      sys.stderr.write(result['stderr_text'])149      sys.stderr.write('\n')150    done_count = self.__success_count + self.__failed_count151    percentage = (float(done_count) / len(self.__filenames)) * 100152    sys.stderr.write(153        'Processed %d files with %s tool (%d failures) [%.2f%%]\r' %154        (done_count, self.__toolname, self.__failed_count, percentage))155def main():156  parser = argparse.ArgumentParser()157  parser.add_argument('tool', help='clang tool to run')158  parser.add_argument('--all', action='store_true')159  parser.add_argument(160      '--generate-compdb',161      action='store_true',162      help='regenerate the compile database before running the tool')163  parser.add_argument(164      'compile_database',165      help='path to the directory that contains the compile database')166  parser.add_argument(167      'path_filter',168      nargs='*',169      help='optional paths to filter what files the tool is run on')170  parser.add_argument(171      '--tool-args', nargs='*',172      help='optional arguments passed to the tool')173  args = parser.parse_args()174  os.environ['PATH'] = '%s%s%s' % (175      os.path.abspath(os.path.join(176          os.path.dirname(__file__),177          '../../../third_party/llvm-build/Release+Asserts/bin')),178      os.pathsep,179      os.environ['PATH'])180  if args.generate_compdb:181    compile_db.GenerateWithNinja(args.compile_database)182  if args.all:183    source_filenames = set(_GetFilesFromCompileDB(args.compile_database))184  else:185    git_filenames = set(_GetFilesFromGit(args.path_filter))186    # Filter out files that aren't C/C++/Obj-C/Obj-C++.187    extensions = frozenset(('.c', '.cc', '.cpp', '.m', '.mm'))188    source_filenames = [f189                        for f in git_filenames190                        if os.path.splitext(f)[1] in extensions]191  dispatcher = _CompilerDispatcher(args.tool, args.tool_args,192                                   args.compile_database,193                                   source_filenames)194  dispatcher.Run()195  return -dispatcher.failed_count196if __name__ == '__main__':...test_io.py
Source:test_io.py  
1# -*- coding: utf-8 -*-2from __future__ import absolute_import, division, print_function, unicode_literals3from conda.common.io import attach_stderr_handler, captured, CaptureTarget4from io import StringIO5from logging import DEBUG, NOTSET, WARN, getLogger6import sys7def test_captured():8    stdout_text = "stdout text"9    stderr_text = "stderr text"10    def print_captured(*args, **kwargs):11        with captured(*args, **kwargs) as c:12            print(stdout_text, end="")13            print(stderr_text, end="", file=sys.stderr)14        return c15    c = print_captured()16    assert c.stdout == stdout_text17    assert c.stderr == stderr_text18    c = print_captured(CaptureTarget.STRING, CaptureTarget.STRING)19    assert c.stdout == stdout_text20    assert c.stderr == stderr_text21    c = print_captured(stderr=CaptureTarget.STDOUT)22    assert c.stdout == stdout_text + stderr_text23    assert c.stderr is None24    caller_stdout = StringIO()25    caller_stderr = StringIO()26    c = print_captured(caller_stdout, caller_stderr)27    assert c.stdout is caller_stdout28    assert c.stderr is caller_stderr29    assert caller_stdout.getvalue() == stdout_text30    assert caller_stderr.getvalue() == stderr_text31    with captured() as outer_c:32        inner_c = print_captured(None, None)33    assert inner_c.stdout is None34    assert inner_c.stderr is None35    assert outer_c.stdout == stdout_text36    assert outer_c.stderr == stderr_text37def test_attach_stderr_handler():38    name = 'abbacadabba'39    logr = getLogger(name)40    assert len(logr.handlers) == 041    assert logr.level is NOTSET42    debug_message = "debug message 1329-485"43    with captured() as c:44        attach_stderr_handler(WARN, name)45        logr.warn('test message')46        logr.debug(debug_message)47    assert len(logr.handlers) == 148    assert logr.handlers[0].name == 'stderr'49    assert logr.handlers[0].level is NOTSET50    assert logr.level is WARN51    assert c.stdout == ''52    assert 'test message' in c.stderr53    assert debug_message not in c.stderr54    # round two, with debug55    with captured() as c:56        attach_stderr_handler(DEBUG, name)57        logr.warn('test message')58        logr.debug(debug_message)59        logr.info('info message')60    assert len(logr.handlers) == 161    assert logr.handlers[0].name == 'stderr'62    assert logr.handlers[0].level is NOTSET63    assert logr.level is DEBUG64    assert c.stdout == ''65    assert 'test message' in c.stderr...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
