Reland "Migrate //PRESUBMIT*.py to Python3."

This reverts commit abee50a262a41ae335bb74b6093d8610349118d3.

Reason for revert: Underlying fix to
tools/translation/helper/git_helper has landed.

Original change's description:
> Revert "Migrate //PRESUBMIT*.py to Python3."
>
> This reverts commit 8b59f97def70a451cb491d0bdd1819ba4840deda.
>
> Reason for revert:
> Breaks uploading CLs which change localized strings. See
> https://bugs.chromium.org/p/chromium/issues/detail?id=1209392.
>
> Original change's description:
> > Migrate //PRESUBMIT*.py to Python3.
> >
> > This CL migrates the root-level PRESUBMIT checks, their associated
> > tests, and and one other file that got pulled along for the ride)
> > to run under Python3 instead of Python2.
> >
> > Bug: 816629
> > Change-Id: I75f3edb34ca72458432ba54f3afa022e027f8eb9
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2877897
> > Commit-Queue: Dirk Pranke <[email protected]>
> > Reviewed-by: Andrew Grieve <[email protected]>
> > Cr-Commit-Position: refs/heads/master@{#883002}
>
> Bug: 816629
> Fixed: 1209392
> Change-Id: Icf0c084f1db3d20ca76600eb67cc3f6e81b3c9f9
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2899404
> Auto-Submit: Kyle Horimoto <[email protected]>
> Commit-Queue: Rubber Stamper <[email protected]>
> Bot-Commit: Rubber Stamper <[email protected]>
> Owners-Override: Yutaka Hirano <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#883359}

Bug: 816629
Change-Id: Ie70748076c06e3ebcacd18e3d69f105d94a5e5ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2901242
Bot-Commit: Rubber Stamper <[email protected]>
Commit-Queue: Dirk Pranke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#884028}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index ecec493..032995cb 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -9,6 +9,10 @@
 """
 PRESUBMIT_VERSION = '2.0.0'
 
+# This line is 'magic' in that git-cl looks for it to decide whether to
+# use Python3 instead of Python2 when running the code in this file.
+USE_PYTHON3 = True
+
 _EXCLUDED_PATHS = (
     # Generated file.
     (r"^components[\\/]variations[\\/]proto[\\/]devtools[\\/]"
@@ -1929,7 +1933,7 @@
     for f in input_api.AffectedFiles():
       # checkperms.py file/directory arguments must be relative to the
       # repository.
-      file_list.write(f.LocalPath() + '\n')
+      file_list.write((f.LocalPath() + '\n').encode('utf8'))
     file_list.close()
     args += ['--file-list', file_list.name]
     try:
@@ -2142,7 +2146,7 @@
       if rule.startswith('+') or rule.startswith('!')
   ])
   for _, rules in parsed_deps.get('specific_include_rules',
-                                              {}).iteritems():
+                                              {}).items():
     add_rules.update([
         rule[1:] for rule in rules
         if rule.startswith('+') or rule.startswith('!')
@@ -2171,12 +2175,7 @@
       'Str': str,
   }
 
-  # TODO(crbug.com/1207012): We need to strip the BOM because it isn't
-  # legal in Python source files. We can remove this check once the CL
-  # that actually removes the BOM from //third_party/crashpad/DEPS lands.
-  if contents.startswith(u'\ufeff'):
-      contents = contents[1:]
-  exec contents in global_scope, local_scope
+  exec(contents, global_scope, local_scope)
   return local_scope
 
 
@@ -2907,9 +2906,9 @@
 
   # Go through the OWNERS files to check, filtering out rules that are already
   # present in that OWNERS file.
-  for owners_file, patterns in to_check.iteritems():
+  for owners_file, patterns in to_check.items():
     try:
-      with file(owners_file) as f:
+      with open(owners_file) as f:
         lines = set(f.read().splitlines())
         for entry in patterns.values():
           entry['rules'] = [rule for rule in entry['rules'] if rule not in lines
@@ -2920,10 +2919,10 @@
 
   # All the remaining lines weren't found in OWNERS files, so emit an error.
   errors = []
-  for owners_file, patterns in to_check.iteritems():
+  for owners_file, patterns in to_check.items():
     missing_lines = []
     files = []
-    for _, entry in patterns.iteritems():
+    for _, entry in patterns.items():
       missing_lines.extend(entry['rules'])
       files.extend(['  %s' % f.LocalPath() for f in entry['files']])
     if missing_lines:
@@ -3739,7 +3738,7 @@
     return []
 
   error_descriptions = []
-  for file_path, bad_lines in bad_files.iteritems():
+  for file_path, bad_lines in bad_files.items():
     error_description = file_path
     for line in bad_lines:
       error_description += '\n    ' + line
@@ -4290,9 +4289,16 @@
         # The PRESUBMIT.py file (and the directory containing it) might
         # have been affected by being moved or removed, so only try to
         # run the tests if they still exist.
+        use_python3 = False
+        with open(f.LocalPath()) as fp:
+            use_python3 = any(line.startswith('USE_PYTHON3 = True')
+                              for line in fp.readlines())
+
         results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
             input_api, output_api, full_path,
-            files_to_check=[r'^PRESUBMIT_test\.py$']))
+            files_to_check=[r'^PRESUBMIT_test\.py$'],
+            run_on_python2=not use_python3,
+            run_on_python3=use_python3))
   return results
 
 
@@ -5032,18 +5038,18 @@
     if file_path.endswith('.grdp'):
       if f.OldContents():
         old_id_to_msg_map = grd_helper.GetGrdpMessagesFromString(
-          unicode('\n'.join(f.OldContents())))
+          '\n'.join(f.OldContents()))
       if f.NewContents():
         new_id_to_msg_map = grd_helper.GetGrdpMessagesFromString(
-          unicode('\n'.join(f.NewContents())))
+          '\n'.join(f.NewContents()))
     else:
       file_dir = input_api.os_path.dirname(file_path) or '.'
       if f.OldContents():
         old_id_to_msg_map = grd_helper.GetGrdMessages(
-          StringIO(unicode('\n'.join(f.OldContents()))), file_dir)
+          StringIO('\n'.join(f.OldContents())), file_dir)
       if f.NewContents():
         new_id_to_msg_map = grd_helper.GetGrdMessages(
-          StringIO(unicode('\n'.join(f.NewContents()))), file_dir)
+          StringIO('\n'.join(f.NewContents())), file_dir)
 
     grd_name, ext = input_api.os_path.splitext(
         input_api.os_path.basename(file_path))
@@ -5161,7 +5167,7 @@
   # ui/webui/resoucres/tools/generate_grd.py.
   ignore_path = input_api.os_path.join(
       'ui', 'webui', 'resources', 'tools', 'tests')
-  grd_files = filter(lambda p: ignore_path not in p, grd_files)
+  grd_files = [p for p in grd_files if ignore_path not in p]
 
   try:
     translation_helper.get_translatable_grds(repo_root, grd_files,