예제 #1
0
def run(args):
  if (len(args) == 0):
    folders = ["."]
  else:
    folders = args
  from libtbx.path import walk_source_tree
  mod_count_total = 0
  mod_file_count = 0
  for folder in folders:
    for path in walk_source_tree(folder):
      if (not path.endswith(".py")): continue
      txt = open(path).read()
      mod_lines = []
      mod_count = 0
      for line in txt.splitlines():
        ls = line.strip()
        if (    ls.startswith("except")
            and ls[6:].strip().startswith(":")
            and not ls.endswith(" # intentional")):
          line = line.replace("except", "except Exception", 1)
          mod_count += 1
        mod_lines.append(line)
      if (mod_count != 0):
        print >> open(path, "w"), "\n".join(mod_lines)
        mod_count_total += mod_count
        mod_file_count += 1
  print "Number of modifications: %d in %d files" % (
    mod_count_total, mod_file_count)
예제 #2
0
def run(args, command_name="libtbx.list_files"):
    if (len(args) == 0): args = ["."]
    command_line = (option_parser(
        usage="%s [options] path ..." % command_name,
        description="Recursively lists all files,"
        " excluding CVS and .svn directories and .pyc files.").option(
            "-t",
            "--text",
            action="store_true",
            default=False,
            help="list text files only").option(
                "-b",
                "--binary",
                action="store_true",
                default=False,
                help="list binary files only").option(
                    "-q",
                    "--quote",
                    action="store_true",
                    default=False,
                    help="quote file names")).process(args=args)
    paths = command_line.args
    co = command_line.options
    text = co.text
    binary = co.binary
    quote = co.quote
    if (not (text or binary)):
        binary = True
        text = True
    if (len(paths) == 0): paths = ["."]
    for path in paths:
        if (not os.path.exists(path)):
            print("No such file or directory:", path, file=sys.stderr)
        elif (os.path.isfile(path)):
            show_status(path=path, text=text, binary=binary, quote=quote)
        else:
            for file_path in walk_source_tree(top=path):
                show_status(path=file_path,
                            text=text,
                            binary=binary,
                            quote=quote)
예제 #3
0
def run(args, command_name="libtbx.list_files"):
  if (len(args) == 0): args = ["."]
  command_line = (option_parser(
    usage="%s [options] path ..." % command_name,
    description="Recursively lists all files,"
      " excluding CVS and .svn directories and .pyc files.")
    .option("-t", "--text",
      action="store_true",
      default=False,
      help="list text files only")
    .option("-b", "--binary",
      action="store_true",
      default=False,
      help="list binary files only")
    .option("-q", "--quote",
      action="store_true",
      default=False,
      help="quote file names")
  ).process(args=args)
  paths = command_line.args
  co = command_line.options
  text = co.text
  binary = co.binary
  quote = co.quote
  if (not (text or binary)):
    binary = True
    text = True
  if (len(paths) == 0): paths = ["."]
  for path in paths:
    if (not os.path.exists(path)):
      print >> sys.stderr, "No such file or directory:", path
    elif (os.path.isfile(path)):
      show_status(path=path, text=text, binary=binary, quote=quote)
    else:
      for file_path in walk_source_tree(top=path):
        show_status(path=file_path, text=text, binary=binary, quote=quote)
예제 #4
0
def run(args, command_name="libtbx.find_files"):
    if (len(args) == 0): args = ["--help"]
    command_line = (
        option_parser(
            usage="%s [options] pattern ..." % command_name,
            description="Recursively finds all files matching patterns,\n"
            "excluding CVS and .svn directories and .pyc files.").option(
                "-t",
                "--top",
                action="append",
                type="string",
                metavar="PATH",
                help="top-level directory where search starts"
                " (default is current working directory)").
        option("-g",
               "--grep",
               action="append",
               type="string",
               metavar="PATTERN",
               help="find regular expression pattern in each file (multiple"
               " -g/--grep options can be given)").option(
                   "-i",
                   "--ignore_case",
                   action="store_true",
                   default=False,
                   help="with -g/--grep: case-insensitive match").
        option(
            "-f",
            "--file_names_only",
            action="store_true",
            default=False,
            help="with -g/--grep: show file names only, not the matching lines"
        ).option("-q",
                 "--quote",
                 action="store_true",
                 default=False,
                 help="quote file names")).process(args=args)
    fn_patterns = command_line.args
    co = command_line.options
    grep_flags = 0
    if (co.ignore_case):
        grep_flags |= re.IGNORECASE
    if (len(fn_patterns) == 0):
        fn_patterns = ["*"]
    tops = co.top
    if (tops is None):
        tops = ["."]
    for top in tops:
        if (not os.path.isdir(top)):
            raise Sorry("Not a directory: %s" % show_string(top))
        for file_path in walk_source_tree(top=top):
            file_name = os.path.basename(file_path)
            for fn_pattern in fn_patterns:
                if (fnmatch(file_name, fn_pattern)):
                    if (co.quote): fp = show_string(file_path)
                    else: fp = file_path
                    if (co.grep is None):
                        print(fp)
                    else:
                        is_binary_file = co.file_names_only
                        for line in read_lines_if_possible(
                                file_path=file_path):
                            if (not is_binary_file):
                                is_binary_file = "\0" in line

                            def line_matches_all_grep_patterns():
                                for grep_pattern in co.grep:
                                    if (re.search(pattern=grep_pattern,
                                                  string=line,
                                                  flags=grep_flags) is None):
                                        return False
                                return True

                            if (line_matches_all_grep_patterns()):
                                if (co.file_names_only):
                                    print(fp)
                                    break
                                elif (is_binary_file):
                                    print("%s: match in binary file" % fp)
                                    break
                                else:
                                    print("%s: %s" % (fp, line))
예제 #5
0
파일: find_files.py 프로젝트: hickerson/bbn
def run(args, command_name="libtbx.find_files"):
    if len(args) == 0:
        args = ["--help"]
    command_line = (
        option_parser(
            usage="%s [options] pattern ..." % command_name,
            description="Recursively finds all files matching patterns,\n"
            "excluding CVS and .svn directories and .pyc files.",
        )
        .option(
            "-t",
            "--top",
            action="append",
            type="string",
            metavar="PATH",
            help="top-level directory where search starts" " (default is current working directory)",
        )
        .option(
            "-g",
            "--grep",
            action="append",
            type="string",
            metavar="PATTERN",
            help="find regular expression pattern in each file (multiple" " -g/--grep options can be given)",
        )
        .option(
            "-i", "--ignore_case", action="store_true", default=False, help="with -g/--grep: case-insensitive match"
        )
        .option(
            "-f",
            "--file_names_only",
            action="store_true",
            default=False,
            help="with -g/--grep: show file names only, not the matching lines",
        )
        .option("-q", "--quote", action="store_true", default=False, help="quote file names")
    ).process(args=args)
    fn_patterns = command_line.args
    co = command_line.options
    grep_flags = 0
    if co.ignore_case:
        grep_flags |= re.IGNORECASE
    if len(fn_patterns) == 0:
        fn_patterns = ["*"]
    tops = co.top
    if tops is None:
        tops = ["."]
    for top in tops:
        if not os.path.isdir(top):
            raise Sorry("Not a directory: %s" % show_string(top))
        for file_path in walk_source_tree(top=top):
            file_name = os.path.basename(file_path)
            for fn_pattern in fn_patterns:
                if fnmatch(file_name, fn_pattern):
                    if co.quote:
                        fp = show_string(file_path)
                    else:
                        fp = file_path
                    if co.grep is None:
                        print fp
                    else:
                        is_binary_file = co.file_names_only
                        for line in read_lines_if_possible(file_path=file_path):
                            if not is_binary_file:
                                is_binary_file = "\0" in line

                            def line_matches_all_grep_patterns():
                                for grep_pattern in co.grep:
                                    if re.search(pattern=grep_pattern, string=line, flags=grep_flags) is None:
                                        return False
                                return True

                            if line_matches_all_grep_patterns():
                                if co.file_names_only:
                                    print fp
                                    break
                                elif is_binary_file:
                                    print "%s: match in binary file" % fp
                                    break
                                else:
                                    print "%s: %s" % (fp, line)