Пример #1
0
 def _upgrade(self, spec, old_file_text):
     in_file = six.StringIO(old_file_text)
     out_file = six.StringIO()
     upgrader = ast_edits.ASTCodeUpgrader(spec)
     count, report, errors = (upgrader.process_opened_file(
         "test.py", in_file, "test_out.py", out_file))
     return (count, report, errors), out_file.getvalue()
Пример #2
0
 def _upgrade(self, old_file_text):
     in_file = six.StringIO(old_file_text)
     out_file = six.StringIO()
     upgrader = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
     count, report, errors = (upgrader.process_opened_file(
         "test.py", in_file, "test_out.py", out_file))
     return count, report, errors, out_file.getvalue()
Пример #3
0
 def testInplace(self):
     """Check to make sure we don't have a file system race."""
     temp_file = tempfile.NamedTemporaryFile("w", delete=False)
     original = "tf.conj(a)\n"
     upgraded = "tf.math.conj(a)\n"
     temp_file.write(original)
     temp_file.close()
     upgrader = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
     upgrader.process_file(temp_file.name, temp_file.name)
     self.assertAllEqual(open(temp_file.name).read(), upgraded)
     os.unlink(temp_file.name)
Пример #4
0
    def testUpgradeInplaceWithSymlink(self):
        upgrade_dir = os.path.join(self.get_temp_dir(), "foo")
        os.mkdir(upgrade_dir)
        file_a = os.path.join(upgrade_dir, "a.py")
        file_b = os.path.join(upgrade_dir, "b.py")

        with open(file_a, "a") as f:
            f.write("import foo as f")
        os.symlink(file_a, file_b)

        upgrader = ast_edits.ASTCodeUpgrader(RenameImports())
        upgrader.process_tree_inplace(upgrade_dir)

        self.assertTrue(os.path.islink(file_b))
        self.assertEqual(file_a, os.readlink(file_b))
        with open(file_a, "r") as f:
            self.assertEqual("import bar as f", f.read())
Пример #5
0
    def testUpgradeCopyWithSymlink(self):
        upgrade_dir = os.path.join(self.get_temp_dir(), "foo")
        output_dir = os.path.join(self.get_temp_dir(), "bar")
        os.mkdir(upgrade_dir)
        file_a = os.path.join(upgrade_dir, "a.py")
        file_b = os.path.join(upgrade_dir, "b.py")

        with open(file_a, "a") as f:
            f.write("import foo as f")
        os.symlink(file_a, file_b)

        upgrader = ast_edits.ASTCodeUpgrader(RenameImports())
        upgrader.process_tree(upgrade_dir, output_dir, copy_other_files=True)

        new_file_a = os.path.join(output_dir, "a.py")
        new_file_b = os.path.join(output_dir, "b.py")
        self.assertTrue(os.path.islink(new_file_b))
        self.assertEqual(new_file_a, os.readlink(new_file_b))
        with open(new_file_a, "r") as f:
            self.assertEqual("import bar as f", f.read())
Пример #6
0
    def testUpgradeInPlaceWithSymlinkInDifferentDir(self):
        upgrade_dir = os.path.join(self.get_temp_dir(), "foo")
        other_dir = os.path.join(self.get_temp_dir(), "bar")
        os.mkdir(upgrade_dir)
        os.mkdir(other_dir)
        file_c = os.path.join(other_dir, "c.py")
        file_d = os.path.join(upgrade_dir, "d.py")

        with open(file_c, "a") as f:
            f.write("import foo as f")
        os.symlink(file_c, file_d)

        upgrader = ast_edits.ASTCodeUpgrader(RenameImports())
        upgrader.process_tree_inplace(upgrade_dir)

        self.assertTrue(os.path.islink(file_d))
        self.assertEqual(file_c, os.readlink(file_d))
        # File pointed to by symlink is in a different directory.
        # Therefore, it should not be upgraded.
        with open(file_c, "r") as f:
            self.assertEqual("import foo as f", f.read())
Пример #7
0
    def testUpgradeCopyWithSymlinkInDifferentDir(self):
        if os.name == "nt":
            self.skipTest("os.symlink doesn't work uniformly on Windows.")

        upgrade_dir = os.path.join(self.get_temp_dir(), "foo")
        other_dir = os.path.join(self.get_temp_dir(), "bar")
        output_dir = os.path.join(self.get_temp_dir(), "baz")
        os.mkdir(upgrade_dir)
        os.mkdir(other_dir)
        file_a = os.path.join(other_dir, "a.py")
        file_b = os.path.join(upgrade_dir, "b.py")

        with open(file_a, "a") as f:
            f.write("import foo as f")
        os.symlink(file_a, file_b)

        upgrader = ast_edits.ASTCodeUpgrader(RenameImports())
        upgrader.process_tree(upgrade_dir, output_dir, copy_other_files=True)

        new_file_b = os.path.join(output_dir, "b.py")
        self.assertTrue(os.path.islink(new_file_b))
        self.assertEqual(file_a, os.readlink(new_file_b))
        with open(file_a, "r") as f:
            self.assertEqual("import foo as f", f.read())
Пример #8
0
      "--copyotherfiles",
      dest="copy_other_files",
      help=("If converting a whole tree of files, whether to "
            "copy the other files."),
      type=bool,
      default=False)
  parser.add_argument(
      "--reportfile",
      dest="report_filename",
      help=("The name of the file where the report log is "
            "stored."
            "(default: %(default)s)"),
      default="report.txt")
  args = parser.parse_args()

  upgrade = ast_edits.ASTCodeUpgrader(TFAPIChangeSpec())
  report_text = None
  report_filename = args.report_filename
  files_processed = 0
  if args.input_file:
    if not args.output_file:
      raise ValueError(
          "--outfile=<output file> argument is required when converting a "
          "single file.")
    files_processed, report_text, errors = upgrade.process_file(
        args.input_file, args.output_file)
    files_processed = 1
  elif args.input_tree:
    if not args.output_tree:
      raise ValueError(
          "--outtree=<output directory> argument is required when converting a "
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument("--inplace",
                        dest="in_place",
                        help=("If converting a set of files, whether to "
                              "allow the conversion to be performed on the "
                              "input files."),
                        action="store_true")
    parser.add_argument("--no_import_rename",
                        dest="no_import_rename",
                        help=("Not to rename import to compat.v2 explicitly."),
                        action="store_true")
    parser.add_argument(
        "--no_upgrade_compat_v1_import",
        dest="no_upgrade_compat_v1_import",
        help=(
            "If specified, don't upgrade explicit imports of "
            "`tensorflow.compat.v1 as tf` to the v2 APIs. Otherwise, "
            "explicit imports of  the form `tensorflow.compat.v1 as tf` will "
            "be upgraded."),
        action="store_true")
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    parser.add_argument(
        "--mode",
        dest="mode",
        choices=[_DEFAULT_MODE, _SAFETY_MODE],
        help=("Upgrade script mode. Supported modes:\n"
              "%s: Perform only straightforward conversions to upgrade to "
              "2.0. In more difficult cases, switch to use compat.v1.\n"
              "%s: Keep 1.* code intact and import compat.v1 "
              "module." % (_DEFAULT_MODE, _SAFETY_MODE)),
        default=_DEFAULT_MODE)
    parser.add_argument(
        "--print_all",
        dest="print_all",
        help="Print full log to stdout instead of just printing errors",
        action="store_true")
    args = parser.parse_args()

    if args.mode == _SAFETY_MODE:
        change_spec = tf_upgrade_v2_safety.TFAPIChangeSpec()
    else:
        if args.no_import_rename:
            change_spec = tf_upgrade_v2.TFAPIChangeSpec(
                import_rename=False,
                upgrade_compat_v1_import=not args.no_upgrade_compat_v1_import)
        else:
            change_spec = tf_upgrade_v2.TFAPIChangeSpec(
                import_rename=_IMPORT_RENAME_DEFAULT,
                upgrade_compat_v1_import=not args.no_upgrade_compat_v1_import)
    upgrade = ast_edits.ASTCodeUpgrader(change_spec)

    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.in_place and not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        if args.in_place and args.output_file:
            raise ValueError(
                "--outfile argument is invalid when converting in place")
        output_file = args.input_file if args.in_place else args.output_file
        files_processed, report_text, errors = process_file(
            args.input_file, output_file, upgrade)
        errors = {args.input_file: errors}
        files_processed = 1
    elif args.input_tree:
        if not args.in_place and not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        if args.in_place and args.output_tree:
            raise ValueError(
                "--outtree argument is invalid when converting in place")
        output_tree = args.input_tree if args.in_place else args.output_tree
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, output_tree, args.copy_other_files)
    else:
        parser.print_help()
    if report_text:
        num_errors = 0
        report = []
        for f in errors:
            if errors[f]:
                num_errors += len(errors[f])
                report.append(six.ensure_str("-" * 80) + "\n")
                report.append("File: %s\n" % f)
                report.append(six.ensure_str("-" * 80) + "\n")
                report.append("\n".join(errors[f]) + "\n")

        report = ("TensorFlow 2.0 Upgrade Script\n"
                  "-----------------------------\n"
                  "Converted %d files\n" % files_processed +
                  "Detected %d issues that require attention" % num_errors +
                  "\n" + six.ensure_str("-" * 80) + "\n") + "".join(report)
        detailed_report_header = six.ensure_str("=" * 80) + "\n"
        detailed_report_header += "Detailed log follows:\n\n"
        detailed_report_header += six.ensure_str("=" * 80) + "\n"

        with open(report_filename, "w") as report_file:
            report_file.write(report)
            report_file.write(detailed_report_header)
            report_file.write(six.ensure_str(report_text))

        if args.print_all:
            print(report)
            print(detailed_report_header)
            print(report_text)
        else:
            print(report)
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #10
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument(
        "--inplace",
        dest="in_place",
        help=("If converting a whole tree of files, whether to "
              "allow the conversion to be performed on the "
              "files in the input tree."),
        type=bool,
        default=False)
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        files_processed, report_text, errors = upgrade.process_file(
            args.input_file, args.output_file)
        files_processed = 1
    elif args.input_tree:
        if not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, args.output_tree, args.copy_other_files,
            args.in_place)
    else:
        parser.print_help()
    if report_text:
        open(report_filename, "w").write(report_text)
        print("TensorFlow 2.0 Upgrade Script")
        print("-----------------------------")
        print("Converted %d files\n" % files_processed)
        print("Detected %d errors that require attention" % len(errors))
        print("-" * 80)
        print("\n".join(errors))
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #11
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument("--inplace",
                        dest="in_place",
                        help=("If converting a set of files, whether to "
                              "allow the conversion to be performed on the "
                              "input files."),
                        action="store_true")
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.in_place and not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        if args.in_place and args.output_file:
            raise ValueError(
                "--outfile argument is invalid when when converting in place")
        output_file = args.input_file if args.in_place else args.output_file
        files_processed, report_text, errors = process_file(
            args.input_file, output_file, upgrade)
        errors = {args.input_file: errors}
        files_processed = 1
    elif args.input_tree:
        if not args.in_place and not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        if args.in_place and args.output_tree:
            raise ValueError(
                "--outtree argument is invalid when when converting in place")
        output_tree = args.input_tree if args.in_place else args.output_tree
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, output_tree, args.copy_other_files)
    else:
        parser.print_help()
    if report_text:
        num_errors = 0
        report = []
        for f in errors:
            if errors[f]:
                num_errors += len(errors[f])
                report.append("-" * 80 + "\n")
                report.append("File: %s\n" % f)
                report.append("-" * 80 + "\n")
                report.append("\n".join(errors[f]) + "\n")

        report = ("TensorFlow 2.0 Upgrade Script\n"
                  "-----------------------------\n"
                  "Converted %d files\n" % files_processed +
                  "Detected %d issues that require attention" % num_errors +
                  "\n" + "-" * 80 + "\n") + "".join(report)
        with open(report_filename, "w") as report_file:
            report_file.write(report)
            report_file.write("=" * 80 + "\n")
            report_file.write("Detailed log follows:\n\n")
            report_file.write("=" * 80 + "\n")
            report_file.write(report_text)

        print(report)
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #12
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument("--inplace",
                        dest="in_place",
                        help=("If converting a set of files, whether to "
                              "allow the conversion to be performed on the "
                              "input files."),
                        action="store_true")
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    parser.add_argument(
        "--mode",
        dest="mode",
        choices=[_DEFAULT_MODE, _SAFETY_MODE],
        help=(
            "Upgrade script mode. Supported modes:\n"
            "%s: Perform only straightforward conversions to upgrade to "
            "2.0. In more difficult cases, switch to use compat.v1.\n"
            "%s: Keep 1.* code intact and import compat.v1 "
            "module. Note: safety mode is under development and not available "
            "yet." % (_DEFAULT_MODE, _SAFETY_MODE)),
        default=_DEFAULT_MODE)
    args = parser.parse_args()

    if args.mode == _SAFETY_MODE:
        change_spec = tf_upgrade_v2_safety.TFAPIChangeSpec()
        sys.stderr.write("%s mode is not fully implemented yet." %
                         _SAFETY_MODE)
    else:
        change_spec = tf_upgrade_v2.TFAPIChangeSpec()
    upgrade = ast_edits.ASTCodeUpgrader(change_spec)

    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.in_place and not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        if args.in_place and args.output_file:
            raise ValueError(
                "--outfile argument is invalid when when converting in place")
        output_file = args.input_file if args.in_place else args.output_file
        files_processed, report_text, errors = process_file(
            args.input_file, output_file, upgrade)
        errors = {args.input_file: errors}
        files_processed = 1
    elif args.input_tree:
        if not args.in_place and not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        if args.in_place and args.output_tree:
            raise ValueError(
                "--outtree argument is invalid when when converting in place")
        output_tree = args.input_tree if args.in_place else args.output_tree
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, output_tree, args.copy_other_files)
    else:
        parser.print_help()
    if report_text:
        num_errors = 0
        report = []
        for f in errors:
            if errors[f]:
                num_errors += len(errors[f])
                report.append("-" * 80 + "\n")
                report.append("File: %s\n" % f)
                report.append("-" * 80 + "\n")
                report.append("\n".join(errors[f]) + "\n")

        report = ("TensorFlow 2.0 Upgrade Script\n"
                  "-----------------------------\n"
                  "Converted %d files\n" % files_processed +
                  "Detected %d issues that require attention" % num_errors +
                  "\n" + "-" * 80 + "\n") + "".join(report)
        with open(report_filename, "w") as report_file:
            report_file.write(report)
            report_file.write("=" * 80 + "\n")
            report_file.write("Detailed log follows:\n\n")
            report_file.write("=" * 80 + "\n")
            report_file.write(report_text)

        print(report)
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #13
0
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=False)
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        files_processed, report_text, errors = upgrade.process_file(
            args.input_file, args.output_file)
        files_processed = 1
    elif args.input_tree:
        if not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "