Esempio n. 1
0
 def test_fixer_loading_helpers(self):
     contents = ["explicit", "first", "last", "parrot", "preorder"]
     non_prefixed = refactor.get_all_fix_names("myfixes")
     prefixed = refactor.get_all_fix_names("myfixes", False)
     full_names = refactor.get_fixers_from_package("myfixes")
     self.assertEqual(prefixed, ["fix_" + name for name in contents])
     self.assertEqual(non_prefixed, contents)
     self.assertEqual(full_names, ["myfixes.fix_" + name for name in contents])
Esempio n. 2
0
 def test_fixer_loading_helpers(self):
     contents = ["explicit", "first", "last", "parrot", "preorder"]
     non_prefixed = refactor.get_all_fix_names("myfixes")
     prefixed = refactor.get_all_fix_names("myfixes", False)
     full_names = refactor.get_fixers_from_package("myfixes")
     self.assertEqual(prefixed, ["fix_" + name for name in contents])
     self.assertEqual(non_prefixed, contents)
     self.assertEqual(full_names,
                      ["myfixes.fix_" + name for name in contents])
Esempio n. 3
0
 def test_fixer_loading_helpers(self):
     contents = ['explicit', 'first', 'last', 'parrot', 'preorder']
     non_prefixed = refactor.get_all_fix_names('myfixes')
     prefixed = refactor.get_all_fix_names('myfixes', False)
     full_names = refactor.get_fixers_from_package('myfixes')
     self.assertEqual(prefixed, [('fix_' + name) for name in contents])
     self.assertEqual(non_prefixed, contents)
     self.assertEqual(full_names,
                      [('myfixes.fix_' + name) for name in contents])
Esempio n. 4
0
    def execute(self, finder):  # pragma: PY2
        fixers = get_all_fix_names(FIXER_PKG)
        for disabled in self.config['disabled']:
            if disabled in fixers:
                fixers.remove(disabled)

        tool = TidyPyRefactoringTool(fixers)

        for filepath in finder.files(self.config['filters']):
            tool.refactor([filepath])

        return tool.get_issues()
Esempio n. 5
0
def main(fixer_pkg, args=None):
    """Main program.

    Args:
        fixer_pkg: the name of a package where the fixers are located.
        args: optional; a list of command line arguments. If omitted,
              sys.argv[1:] is used.

    Returns a suggested exit status (0, 1, 2).
    """
    # Set up option parser
    parser = optparse.OptionParser(usage="orange2to25 [options] file|dir ...")
    parser.add_option("-d",
                      "--doctests_only",
                      action="store_true",
                      help="Fix up doctests only")
    parser.add_option("-f",
                      "--fix",
                      action="append",
                      default=[],
                      help="Each FIX specifies a transformation; default: all")
    parser.add_option("-j",
                      "--processes",
                      action="store",
                      default=1,
                      type="int",
                      help="Run orange2to25 concurrently")
    parser.add_option("-x",
                      "--nofix",
                      action="append",
                      default=[],
                      help="Prevent a fixer from being run.")
    parser.add_option("-l",
                      "--list-fixes",
                      action="store_true",
                      help="List available transformations")
    #    parser.add_option("-p", "--print-function", action="store_true",
    #                      help="Modify the grammar so that print() is a function")
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      help="More verbose logging")
    parser.add_option("--no-diffs",
                      action="store_true",
                      help="Don't show diffs of the refactoring")
    parser.add_option("-w",
                      "--write",
                      action="store_true",
                      help="Write back modified files")
    parser.add_option("-n",
                      "--nobackups",
                      action="store_true",
                      default=False,
                      help="Don't write backups for modified files.")
    parser.add_option(
        "-a",
        "--aggressive",
        action="store_true",
        default=False,
        help=
        "Aggressive name replace (without a module qualifier, e.g. replace ExampleTable with even if not preceded by orange.* )"
    )
    parser.add_option(
        "-o",
        "--outfile",
        default="",
        help=
        "Output filename (if -w present and a single file on the command line)"
    )

    # Parse command line arguments
    refactor_stdin = False
    flags = {}
    options, args = parser.parse_args(args)
    if not options.write and options.no_diffs:
        warn(
            "not writing files and not printing diffs; that's not very useful")
    if not options.write and options.nobackups:
        parser.error("Can't use -n without -w")
    if options.list_fixes:
        print "Available transformations for the -f/--fix option:"
        for fixname in refactor.get_all_fix_names(fixer_pkg):
            print fixname
        if not args:
            return 0
    if not args:
        print >> sys.stderr, "At least one file or directory argument required."
        print >> sys.stderr, "Use --help to show usage."
        return 2
    if "-" in args:
        refactor_stdin = True
        if options.write:
            print >> sys.stderr, "Can't write to stdin."
            return 2


#    if options.print_function:
#        flags["print_function"] = True

# Set up logging handler
    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(name)s: %(message)s', level=level)

    # Initialize the refactoring tool
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
    # Remove aggressive fixes if not requested
    if not options.aggressive:
        avail_fixes = set(
            [fix for fix in avail_fixes if not fix.endswith("_aggressive")])

    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
    explicit = set()
    if options.fix:
        all_present = False
        for fix in options.fix:
            if fix == "all":
                all_present = True
            else:
                explicit.add(fixer_pkg + ".fix_" + fix)
        requested = avail_fixes.union(explicit) if all_present else explicit
    else:
        requested = avail_fixes.union(explicit)
    fixer_names = requested.difference(unwanted_fixes)
    rt = MyStdRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
                              options.nobackups, not options.no_diffs)
    if options.outfile:
        if len(args) > 2:
            print >> sys.stderr, "-o, --outfile used but multiple script arguments"
        elif not os.path.isfile(args[-1]):
            print >> sys.stderr, "-o, --outfile used but argumnet is a directory"
        else:
            rt.outputfilename = options.outfile

    # Refactor all files and directories passed as arguments
    if not rt.errors:
        if refactor_stdin:
            rt.refactor_stdin()
        else:
            try:
                rt.refactor(args[1:], options.write, options.doctests_only,
                            options.processes)
            except refactor.MultiprocessingUnsupported:
                assert options.processes > 1
                print >> sys.stderr, "Sorry, -j isn't " \
                    "supported on this platform."
                return 1
        rt.summarize()

    # Return error status (0 if rt.errors is zero)
    return int(bool(rt.errors))
Esempio n. 6
0
def main(fixer_pkg, args=None):
    """Main program.

    Args:
        fixer_pkg: the name of a package where the fixers are located.
        args: optional; a list of command line arguments. If omitted,
              sys.argv[1:] is used.

    Returns a suggested exit status (0, 1, 2).
    """
    # Set up option parser
    parser = optparse.OptionParser(usage="3to2 [options] file|dir ...")
    parser.add_option("-d",
                      "--doctests_only",
                      action="store_true",
                      help="Fix up doctests only")
    parser.add_option("-f",
                      "--fix",
                      action="append",
                      default=[],
                      help="Each FIX specifies a transformation; default: all")
    parser.add_option("-j",
                      "--processes",
                      action="store",
                      default=1,
                      type="int",
                      help="Run 3to2 concurrently")
    parser.add_option("-x",
                      "--nofix",
                      action="append",
                      default=[],
                      help="Prevent a fixer from being run.")
    parser.add_option("-l",
                      "--list-fixes",
                      action="store_true",
                      help="List available transformations (fixes/fix_*.py)")
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      help="More verbose logging")
    parser.add_option("-w",
                      "--write",
                      action="store_true",
                      help="Write back modified files")
    parser.add_option("-n",
                      "--nobackups",
                      action="store_true",
                      default=False,
                      help="Don't write backups for modified files.")
    parser.add_option("--no-diffs",
                      action="store_true",
                      help="Don't show diffs of the refactoring")

    # Parse command line arguments
    refactor_stdin = False
    options, args = parser.parse_args(args)
    if not options.write and options.no_diffs:
        warn(
            "not writing files and not printing diffs; that's not very useful")
    if not options.write and options.nobackups:
        parser.error("Can't use -n without -w")
    if options.list_fixes:
        print("Available transformations for the -f/--fix option:")
        for fixname in refactor.get_all_fix_names(fixer_pkg):
            print(fixname)
        if not args:
            return 0
    if not args:
        print("At least one file or directory argument required.",
              file=sys.stderr)
        print("Use --help to show usage.", file=sys.stderr)
        return 2
    if "-" in args:
        refactor_stdin = True
        if options.write:
            print("Can't write to stdin.", file=sys.stderr)
            return 2

    # Set up logging handler
    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(name)s: %(message)s', level=level)

    # Initialize the refactoring tool
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
    explicit = set()
    if options.fix:
        all_present = False
        for fix in options.fix:
            if fix == "all":
                all_present = True
            else:
                explicit.add(fixer_pkg + ".fix_" + fix)
        requested = avail_fixes.union(explicit) if all_present else explicit
    else:
        requested = avail_fixes.union(explicit)
    fixer_names = requested.difference(unwanted_fixes)
    rt = StdoutRefactoringTool(sorted(fixer_names), None, sorted(explicit),
                               options.nobackups, not options.no_diffs)

    # Refactor all files and directories passed as arguments
    if not rt.errors:
        if refactor_stdin:
            rt.refactor_stdin()
        else:
            try:
                rt.refactor(args, options.write, options.doctests_only,
                            options.processes)
            except refactor.MultiprocessingUnsupported:
                assert options.processes > 1
                print("Sorry, -j isn't supported on this platform.",
                      file=sys.stderr)
                return 1
        rt.summarize()

    # Return error status (0 if rt.errors is zero)
    return int(bool(rt.errors))
Esempio n. 7
0
def main(fixer_pkg, args=None):
    """Main program.

	Args:
		fixer_pkg: the name of a package where the fixers are located.
		args: optional; a list of command line arguments. If omitted,
			  sys.argv[1:] is used.

	Returns a suggested exit status (0, 1, 2).
	"""
    # Set up option parser
    parser = optparse.OptionParser(usage="2to3 [options] file|dir ...")
    parser.add_option("-d",
                      "--doctests_only",
                      action="store_true",
                      help="Fix up doctests only")
    parser.add_option("-f",
                      "--fix",
                      action="append",
                      default=[],
                      help="Each FIX specifies a transformation; default: all")
    parser.add_option("-j",
                      "--processes",
                      action="store",
                      default=1,
                      type="int",
                      help="Run 2to3 concurrently")
    parser.add_option("-x",
                      "--nofix",
                      action="append",
                      default=[],
                      help="Prevent a transformation from being run")
    parser.add_option("-l",
                      "--list-fixes",
                      action="store_true",
                      help="List available transformations")
    parser.add_option("-p",
                      "--print-function",
                      action="store_true",
                      help="Modify the grammar so that print() is a function")
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      help="More verbose logging")
    """
	parser.add_option("--no-diffs", action="store_true",
					  help="Don't show diffs of the refactoring")
	"""
    parser.add_option("-w",
                      "--write",
                      action="store_true",
                      help="Write back modified files")
    parser.add_option("-n",
                      "--nobackups",
                      action="store_true",
                      default=False,
                      help="Don't write backups for modified files")
    parser.add_option("-o",
                      "--output-dir",
                      action="store",
                      type="str",
                      default="",
                      help="Put output files in this directory "
                      "instead of overwriting the input files.	Requires -n.")
    parser.add_option("-W",
                      "--write-unchanged-files",
                      action="store_true",
                      help="Also write files even if no changes were required"
                      " (useful with --output-dir); implies -w.")
    parser.add_option("--add-suffix",
                      action="store",
                      type="str",
                      default="",
                      help="Append this string to all output filenames."
                      " Requires -n if non-empty.  "
                      "ex: --add-suffix='3' will generate .py3 files.")

    # Parse command line arguments
    refactor_stdin = False
    flags = {}
    options, args = parser.parse_args(args)
    if options.write_unchanged_files:
        flags["write_unchanged_files"] = True
        if not options.write:
            warn("--write-unchanged-files/-W implies -w.")
        options.write = True
    # If we allowed these, the original files would be renamed to backup names
    # but not replaced.
    if options.output_dir and not options.nobackups:
        parser.error("Can't use --output-dir/-o without -n.")
    if options.add_suffix and not options.nobackups:
        parser.error("Can't use --add-suffix without -n.")

    if not options.write:  #and options.no_diffs:
        warn(
            "not writing files and not printing diffs; that's not very useful")
    if not options.write and options.nobackups:
        parser.error("Can't use -n without -w")
    if options.list_fixes:
        print("Available transformations for the -f/--fix option:")
        for fixname in refactor.get_all_fix_names(fixer_pkg):
            print(fixname)
        if not args:
            return 0
    if not args:
        print("At least one file or directory argument required.",
              file=sys.stderr)
        print("Use --help to show usage.", file=sys.stderr)
        return 2
    if "-" in args:
        refactor_stdin = True
        if options.write:
            print("Can't write to stdin.", file=sys.stderr)
            return 2
    if options.print_function:
        flags["print_function"] = True

    # Set up logging handler
    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(name)s: %(message)s', level=level)
    logger = logging.getLogger('lib2to3.main')

    # Initialize the refactoring tool
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
    explicit = set()
    if options.fix:
        all_present = False
        for fix in options.fix:
            if fix == "all":
                all_present = True
            else:
                explicit.add(fixer_pkg + ".fix_" + fix)
        requested = avail_fixes.union(explicit) if all_present else explicit
    else:
        requested = avail_fixes.union(explicit)
    fixer_names = requested.difference(unwanted_fixes)
    input_base_dir = os.path.commonprefix(args)
    if (input_base_dir and not input_base_dir.endswith(os.sep)
            and not os.path.isdir(input_base_dir)):
        # One or more similar names were passed, their directory is the base.
        # os.path.commonprefix() is ignorant of path elements, this corrects
        # for that weird API.
        input_base_dir = os.path.dirname(input_base_dir)
    if options.output_dir:
        input_base_dir = input_base_dir.rstrip(os.sep)
        logger.info('Output in %r will mirror the input directory %r layout.',
                    options.output_dir, input_base_dir)
    rt = DIYRefactoringTool(
        sorted(fixer_names),
        flags,
        sorted(explicit),
        options.nobackups,
        False,  #not options.no_diffs,
        input_base_dir=input_base_dir,
        output_dir=options.output_dir,
        append_suffix=options.add_suffix)

    # Refactor all files and directories passed as arguments
    if not rt.errors:
        if refactor_stdin:
            rt.refactor_stdin()
        else:
            try:
                rt.refactor(args, options.write, options.doctests_only,
                            options.processes)
            except refactor.MultiprocessingUnsupported:
                assert options.processes > 1
                print("Sorry, -j isn't supported on this platform.",
                      file=sys.stderr)
                return 1
        rt.summarize()

    # Return error status (0 if rt.errors is zero)
    return int(bool(rt.errors))
Esempio n. 8
0
def main(fixer_pkg, args=None):
    """Main program.

    Args:
        fixer_pkg: the name of a package where the fixers are located.
        args: optional; a list of command line arguments. If omitted,
              sys.argv[1:] is used.

    Returns a suggested exit status (0, 1, 2).
    """
    # Set up option parser
    parser = optparse.OptionParser(usage="orange2to25 [options] file|dir ...")
    parser.add_option("-d", "--doctests_only", action="store_true",
                      help="Fix up doctests only")
    parser.add_option("-f", "--fix", action="append", default=[],
                      help="Each FIX specifies a transformation; default: all")
    parser.add_option("-j", "--processes", action="store", default=1,
                      type="int", help="Run orange2to25 concurrently")
    parser.add_option("-x", "--nofix", action="append", default=[],
                      help="Prevent a fixer from being run.")
    parser.add_option("-l", "--list-fixes", action="store_true",
                      help="List available transformations")
#    parser.add_option("-p", "--print-function", action="store_true",
#                      help="Modify the grammar so that print() is a function")
    parser.add_option("-v", "--verbose", action="store_true",
                      help="More verbose logging")
    parser.add_option("--no-diffs", action="store_true",
                      help="Don't show diffs of the refactoring")
    parser.add_option("-w", "--write", action="store_true",
                      help="Write back modified files")
    parser.add_option("-n", "--nobackups", action="store_true", default=False,
                      help="Don't write backups for modified files.")
    parser.add_option("-a", "--aggressive", action="store_true", default=False,
                      help="Aggressive name replace (without a module qualifier, e.g. replace ExampleTable with even if not preceded by orange.* )")
    parser.add_option("-o", "--outfile", default = "",
                      help="Output filename (if -w present and a single file on the command line)")

    # Parse command line arguments
    refactor_stdin = False
    flags = {}
    options, args = parser.parse_args(args)
    if not options.write and options.no_diffs:
        warn("not writing files and not printing diffs; that's not very useful")
    if not options.write and options.nobackups:
        parser.error("Can't use -n without -w")
    if options.list_fixes:
        print "Available transformations for the -f/--fix option:"
        for fixname in refactor.get_all_fix_names(fixer_pkg):
            print fixname
        if not args:
            return 0
    if not args:
        print >> sys.stderr, "At least one file or directory argument required."
        print >> sys.stderr, "Use --help to show usage."
        return 2
    if "-" in args:
        refactor_stdin = True
        if options.write:
            print >> sys.stderr, "Can't write to stdin."
            return 2
        
#    if options.print_function:
#        flags["print_function"] = True

    # Set up logging handler
    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(name)s: %(message)s', level=level)

    # Initialize the refactoring tool
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
    # Remove aggressive fixes if not requested
    if not options.aggressive:
        avail_fixes = set([fix for fix in avail_fixes if not fix.endswith("_aggressive")])
        
    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
    explicit = set()
    if options.fix:
        all_present = False
        for fix in options.fix:
            if fix == "all":
                all_present = True
            else:
                explicit.add(fixer_pkg + ".fix_" + fix)
        requested = avail_fixes.union(explicit) if all_present else explicit
    else:
        requested = avail_fixes.union(explicit)
    fixer_names = requested.difference(unwanted_fixes)
    rt = MyStdRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
                               options.nobackups, not options.no_diffs)
    if options.outfile:
        if len(args) > 2:
            print >> sys.stderr, "-o, --outfile used but multiple script arguments"
        elif not os.path.isfile(args[-1]):
            print >> sys.stderr, "-o, --outfile used but argumnet is a directory"
        else:
            rt.outputfilename = options.outfile
        
    # Refactor all files and directories passed as arguments
    if not rt.errors:
        if refactor_stdin:
            rt.refactor_stdin()
        else:
            try:
                rt.refactor(args[1:], options.write, options.doctests_only,
                            options.processes)
            except refactor.MultiprocessingUnsupported:
                assert options.processes > 1
                print >> sys.stderr, "Sorry, -j isn't " \
                    "supported on this platform."
                return 1
        rt.summarize()

    # Return error status (0 if rt.errors is zero)
    return int(bool(rt.errors))
Esempio n. 9
0
 def get_all_codes(cls):
     return [(fixer, fixer) for fixer in get_all_fix_names(FIXER_PKG)]
Esempio n. 10
0
def main(fixer_pkg, args=None):
    """Main program.

    Args:
        fixer_pkg: the name of a package where the fixers are located.
        args: optional; a list of command line arguments. If omitted,
              sys.argv[1:] is used.

    Returns a suggested exit status (0, 1, 2).
    """
    # Set up option parser
    parser = optparse.OptionParser(usage="3to2 [options] file|dir ...")
    parser.add_option("-d", u"--doctests_only", action=u"store_true",
                      help="Fix up doctests only")
    parser.add_option("-f", u"--fix", action=u"append", default=[],
                      help="Each FIX specifies a transformation; default: all")
    parser.add_option("-j", u"--processes", action=u"store", default=1,
                      type="int", help=u"Run 3to2 concurrently")
    parser.add_option("-x", u"--nofix", action=u"append", default=[],
                      help="Prevent a fixer from being run.")
    parser.add_option("-l", u"--list-fixes", action=u"store_true",
                      help="List available transformations (fixes/fix_*.py)")
    parser.add_option("-v", u"--verbose", action=u"store_true",
                      help="More verbose logging")
    parser.add_option("-w", u"--write", action=u"store_true",
                      help="Write back modified files")
    parser.add_option("-n", u"--nobackups", action=u"store_true", default=False,
                      help="Don't write backups for modified files.")
    parser.add_option("--no-diffs", action=u"store_true",
                      help="Don't show diffs of the refactoring")

    # Parse command line arguments
    refactor_stdin = False
    options, args = parser.parse_args(args)
    if not options.write and options.no_diffs:
        warn("not writing files and not printing diffs; that's not very useful")
    if not options.write and options.nobackups:
        parser.error("Can't use -n without -w")
    if options.list_fixes:
        print "Available transformations for the -f/--fix option:"
        for fixname in refactor.get_all_fix_names(fixer_pkg):
            print fixname
        if not args:
            return 0
    if not args:
        print >>sys.stderr, "At least one file or directory argument required."
        print >>sys.stderr, "Use --help to show usage."
        return 2
    if "-" in args:
        refactor_stdin = True
        if options.write:
            print >>sys.stderr, "Can't write to stdin."
            return 2

    # Set up logging handler
    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format=u'%(name)s: %(message)s', level=level)

    # Initialize the refactoring tool
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
    explicit = set()
    if options.fix:
        all_present = False
        for fix in options.fix:
            if fix == "all":
                all_present = True
            else:
                explicit.add(fixer_pkg + ".fix_" + fix)
        requested = avail_fixes.union(explicit) if all_present else explicit
    else:
        requested = avail_fixes.union(explicit)
    fixer_names = requested.difference(unwanted_fixes)
    rt = StdoutRefactoringTool(sorted(fixer_names), None, sorted(explicit),
                               options.nobackups, not options.no_diffs)

    # Refactor all files and directories passed as arguments
    if not rt.errors:
        if refactor_stdin:
            rt.refactor_stdin()
        else:
            try:
                rt.refactor(args, options.write, options.doctests_only,
                            options.processes)
            except refactor.MultiprocessingUnsupported:
                assert options.processes > 1
                print >>sys.stderr, "Sorry, -j isn't supported on this platform."
                return 1
        rt.summarize()

    # Return error status (0 if rt.errors is zero)
    return int(bool(rt.errors))