66structure, or make assumptions about the passed arguments or calls' outcomes.
77"""
88
9+ from __future__ import unicode_literals
10+
11+ import io
912import os
13+ import posixpath
1014import re
1115import subprocess
1216import sys
@@ -106,13 +110,13 @@ def get_patch(repo, commit_hash):
106110 '--' # Explicitly tell Git `commit_hash` is a revision, not a path.
107111 ]
108112
109- return subprocess .check_output (args )
113+ return subprocess .check_output (args ). decode ( 'utf-8' )
110114
111115
112116def get_head_commit (repo ):
113117 args = ['git' , '-C' , repo , 'rev-parse' , 'HEAD' ]
114118
115- return subprocess .check_output (args ).strip ()
119+ return subprocess .check_output (args ).decode ( 'utf-8' ). strip ()
116120
117121
118122def update_ref (repo , ref , newvalue ):
@@ -154,7 +158,7 @@ def get_upstream_head(repo):
154158 '--verify' ,
155159 'refs/patches/upstream-head' ,
156160 ]
157- return subprocess .check_output (args ).strip ()
161+ return subprocess .check_output (args ).decode ( 'utf-8' ). strip ()
158162
159163def get_commit_count (repo , commit_range ):
160164 args = [
@@ -165,7 +169,7 @@ def get_commit_count(repo, commit_range):
165169 '--count' ,
166170 commit_range
167171 ]
168- return int (subprocess .check_output (args ).strip ())
172+ return int (subprocess .check_output (args ).decode ( 'utf-8' ). strip ())
169173
170174def guess_base_commit (repo ):
171175 """Guess which commit the patches might be based on"""
@@ -181,7 +185,7 @@ def guess_base_commit(repo):
181185 'describe' ,
182186 '--tags' ,
183187 ]
184- return subprocess .check_output (args ).rsplit ('-' , 2 )[0 :2 ]
188+ return subprocess .check_output (args ).decode ( 'utf-8' ). rsplit ('-' , 2 )[0 :2 ]
185189
186190
187191def format_patch (repo , since ):
@@ -190,7 +194,11 @@ def format_patch(repo, since):
190194 '-C' ,
191195 repo ,
192196 '-c' ,
193- 'core.attributesfile=' + os .path .join (os .path .dirname (os .path .realpath (__file__ )), 'electron.gitattributes' ),
197+ 'core.attributesfile='
198+ + os .path .join (
199+ os .path .dirname (os .path .realpath (__file__ )),
200+ 'electron.gitattributes' ,
201+ ),
194202 # Ensure it is not possible to match anything
195203 # Disabled for now as we have consistent chunk headers
196204 # '-c',
@@ -215,7 +223,7 @@ def format_patch(repo, since):
215223 '--full-index' ,
216224 since
217225 ]
218- return subprocess .check_output (args )
226+ return subprocess .check_output (args ). decode ( 'utf-8' )
219227
220228
221229def split_patches (patch_data ):
@@ -252,9 +260,13 @@ def remove_patch_filename(patch):
252260 force_keep_next_line = False
253261 for i , l in enumerate (patch ):
254262 is_patchfilename = l .startswith ('Patch-Filename: ' )
255- next_is_patchfilename = i < len (patch ) - 1 and patch [i + 1 ].startswith ('Patch-Filename: ' )
256- if not force_keep_next_line and (is_patchfilename or (next_is_patchfilename and len (l .rstrip ()) == 0 )):
257- pass # drop this line
263+ next_is_patchfilename = i < len (patch ) - 1 and patch [i + 1 ].startswith (
264+ 'Patch-Filename: '
265+ )
266+ if not force_keep_next_line and (
267+ is_patchfilename or (next_is_patchfilename and len (l .rstrip ()) == 0 )
268+ ):
269+ pass # drop this line
258270 else :
259271 yield l
260272 force_keep_next_line = l .startswith ('Subject: ' )
@@ -263,7 +275,9 @@ def remove_patch_filename(patch):
263275def export_patches (repo , out_dir , patch_range = None , dry_run = False ):
264276 if patch_range is None :
265277 patch_range , num_patches = guess_base_commit (repo )
266- sys .stderr .write ("Exporting {} patches since {}\n " .format (num_patches , patch_range ))
278+ sys .stderr .write (
279+ "Exporting {} patches since {}\n " .format (num_patches , patch_range )
280+ )
267281 patch_data = format_patch (repo , patch_range )
268282 patches = split_patches (patch_data )
269283
@@ -274,31 +288,46 @@ def export_patches(repo, out_dir, patch_range=None, dry_run=False):
274288
275289 if dry_run :
276290 # If we're doing a dry run, iterate through each patch and see if the newly
277- # exported patch differs from what exists. Report number of mismatched patches
278- # and fail if there's more than one.
291+ # exported patch differs from what exists. Report number of mismatched
292+ # patches and fail if there's more than one.
279293 patch_count = 0
280294 for patch in patches :
281295 filename = get_file_name (patch )
282- filepath = os .path .join (out_dir , filename )
283- existing_patch = open (filepath , 'r' ).read ()
284- formatted_patch = '\n ' .join (remove_patch_filename (patch )).rstrip ('\n ' ) + '\n '
296+ filepath = posixpath .join (out_dir , filename )
297+ existing_patch = io .open (filepath , 'r' , encoding = 'utf-8' ).read ()
298+ formatted_patch = (
299+ '\n ' .join (remove_patch_filename (patch )).rstrip ('\n ' ) + '\n '
300+ )
285301 if formatted_patch != existing_patch :
286302 patch_count += 1
287303 if patch_count > 0 :
288- sys .stderr .write ("Patches in {} not up to date: {} patches need update\n " .format (out_dir , patch_count ))
304+ sys .stderr .write (
305+ "Patches in {} not up to date: {} patches need update\n " .format (
306+ out_dir , patch_count
307+ )
308+ )
289309 exit (1 )
290310 else :
291311 # Remove old patches so that deleted commits are correctly reflected in the
292312 # patch files (as a removed file)
293313 for p in os .listdir (out_dir ):
294314 if p .endswith ('.patch' ):
295- os .remove (os .path .join (out_dir , p ))
296- with open (os .path .join (out_dir , '.patches' ), 'w' ) as pl :
315+ os .remove (posixpath .join (out_dir , p ))
316+ with io .open (
317+ posixpath .join (out_dir , '.patches' ),
318+ 'w' ,
319+ newline = '\n ' ,
320+ encoding = 'utf-8' ,
321+ ) as pl :
297322 for patch in patches :
298323 filename = get_file_name (patch )
299- file_path = os .path .join (out_dir , filename )
300- formatted_patch = '\n ' .join (remove_patch_filename (patch )).rstrip ('\n ' ) + '\n '
301- with open (file_path , 'w' ) as f :
324+ file_path = posixpath .join (out_dir , filename )
325+ formatted_patch = (
326+ '\n ' .join (remove_patch_filename (patch )).rstrip ('\n ' ) + '\n '
327+ )
328+ with io .open (
329+ file_path , 'w' , newline = '\n ' , encoding = 'utf-8'
330+ ) as f :
302331 f .write (formatted_patch )
303332 pl .write (filename + '\n ' )
304333
0 commit comments