1+ #!/usr/bin/env python3
12# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
23# for details. All rights reserved. Use of this source code is governed by a
34# BSD-style license that can be found in the LICENSE file.
1011import imp
1112import os
1213import os .path
14+ from typing import Callable
1315import scm
1416import subprocess
15- import tempfile
1617import platform
1718
19+ USE_PYTHON3 = True
20+
21+
1822def is_cpp_file (path ):
1923 return path .endswith ('.cc' ) or path .endswith ('.h' )
2024
@@ -68,7 +72,7 @@ def _CheckFormat(input_api,
6872 identification ,
6973 extension ,
7074 windows ,
71- hasFormatErrors ,
75+ hasFormatErrors : Callable [[ str , str ], bool ] ,
7276 should_skip = lambda path : False ):
7377 local_root = input_api .change .RepositoryRoot ()
7478 upstream = input_api .change ._upstream
@@ -105,7 +109,6 @@ def _CheckFormat(input_api,
105109
106110def _CheckDartFormat (input_api , output_api ):
107111 local_root = input_api .change .RepositoryRoot ()
108- upstream = input_api .change ._upstream
109112 utils = imp .load_source ('utils' ,
110113 os .path .join (local_root , 'tools' , 'utils.py' ))
111114
@@ -119,7 +122,7 @@ def _CheckDartFormat(input_api, output_api):
119122 print ('WARNING: dart not found: %s' % (dart ))
120123 return []
121124
122- def HasFormatErrors (filename = None , contents = None ):
125+ def HasFormatErrors (filename : str = None , contents : str = None ):
123126 # Don't look for formatting errors in multitests. Since those are very
124127 # sensitive to whitespace, many cannot be formatted with dartfmt without
125128 # breaking them.
@@ -135,11 +138,12 @@ def HasFormatErrors(filename=None, contents=None):
135138 '--set-exit-if-changed' ,
136139 '--output=none' ,
137140 '--summary=none' ,
138- filename ,
139141 ]
140-
141- process = subprocess .Popen (
142- args , stdout = subprocess .PIPE , stdin = subprocess .PIPE )
142+ if contents :
143+ process = subprocess .run (args , input = contents , text = True )
144+ else :
145+ args .append (filename )
146+ process = subprocess .run (args )
143147
144148 # Check for exit code 1 explicitly to distinguish it from a syntax error
145149 # in the file (exit code 65). The repo contains many Dart files that are
@@ -158,16 +162,15 @@ def HasFormatErrors(filename=None, contents=None):
158162 output_api .PresubmitError (
159163 'File output does not match dartfmt.\n '
160164 'Fix these issues with:\n '
161- '%s -w %s%s' % ( prebuilt_dartfmt , lineSep ,
162- lineSep .join (unformatted_files )))
165+ '%s format %s%s' %
166+ ( dart , lineSep , lineSep .join (unformatted_files )))
163167 ]
164168
165169 return []
166170
167171
168172def _CheckStatusFiles (input_api , output_api ):
169173 local_root = input_api .change .RepositoryRoot ()
170- upstream = input_api .change ._upstream
171174 utils = imp .load_source ('utils' ,
172175 os .path .join (local_root , 'tools' , 'utils.py' ))
173176
@@ -188,9 +191,7 @@ def _CheckStatusFiles(input_api, output_api):
188191
189192 def HasFormatErrors (filename = None , contents = None ):
190193 args = [dart , lint ] + (['-t' ] if contents else [filename ])
191- process = subprocess .Popen (
192- args , stdout = subprocess .PIPE , stdin = subprocess .PIPE )
193- process .communicate (input = contents )
194+ process = subprocess .run (args , input = contents , text = True )
194195 return process .returncode != 0
195196
196197 def should_skip (path ):
@@ -230,12 +231,8 @@ def _CheckPackageConfigUpToDate(input_api, output_api):
230231 dart = utils .CheckedInSdkExecutable ()
231232 generate = os .path .join (local_root , 'tools' , 'generate_package_config.dart' )
232233 cmd = [dart , generate , '--check' ]
233- pipe = subprocess .Popen (cmd ,
234- stdout = subprocess .PIPE ,
235- stderr = subprocess .PIPE ,
236- shell = utils .IsWindows ())
237- output = pipe .communicate ()
238- if pipe .returncode != 0 :
234+ result = subprocess .run (cmd , shell = utils .IsWindows ())
235+ if result .returncode != 0 :
239236 return [
240237 output_api .PresubmitError (
241238 'File .dart_tool/package_config.json is out of date.\n '
@@ -255,7 +252,7 @@ def _CheckValidHostsInDEPS(input_api, output_api):
255252 try :
256253 input_api .subprocess .check_output (['gclient' , 'verify' ])
257254 return []
258- except input_api .subprocess .CalledProcessError , error :
255+ except input_api .subprocess .CalledProcessError as error :
259256 return [
260257 output_api .PresubmitError (
261258 'DEPS file must have only dependencies from allowed hosts.' ,
0 commit comments