Repository: electron/electron
Pull request: build: fix python util verbose mode logic

Backport of #44431

See that PR for details.


Notes: none

Changed files:
filename: .github/actions/build-electron/action.yml
patch: @@ -63,7 +63,7 @@ runs:
       run: |
         cd src
         electron/script/copy-debug-symbols.py --target-cpu="${{ inputs.target-arch }}" --out-dir=out/Default/debug --compress
-        electron/script/strip-binaries.py --target-cpu="${{ inputs.target-arch }}"
+        electron/script/strip-binaries.py --target-cpu="${{ inputs.target-arch }}" --verbose
         electron/script/add-debug-link.py --target-cpu="${{ inputs.target-arch }}" --debug-dir=out/Default/debug
     - name: Build Electron dist.zip ${{ inputs.step-suffix }}
       shell: bash

filename: script/lib/config.py
patch: @@ -28,16 +28,17 @@ def get_target_arch():
     return 'x64'
   return arch
 
-
-def enable_verbose_mode():
+def set_verbose_mode(mode):
   print('Running in verbose mode')
   global verbose_mode
-  verbose_mode = True
-
+  verbose_mode = mode
 
 def is_verbose_mode():
   return verbose_mode
 
+def verbose_mode_print(output):
+  if verbose_mode:
+    print(output)
 
 def get_zip_name(name, version, suffix=''):
   arch = get_target_arch()

filename: script/lib/util.py
patch: @@ -11,9 +11,7 @@
 from urllib.request import urlopen
 import zipfile
 
-# from lib.config import is_verbose_mode
-def is_verbose_mode():
-  return False
+from lib.config import verbose_mode_print
 
 ELECTRON_DIR = os.path.abspath(
   os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
@@ -112,13 +110,11 @@ def safe_mkdir(path):
 def execute(argv, env=None, cwd=None):
   if env is None:
     env = os.environ
-  if is_verbose_mode():
-    print(' '.join(argv))
+  verbose_mode_print(' '.join(argv))
   try:
     output = subprocess.check_output(argv, stderr=subprocess.STDOUT,
                                      env=env, cwd=cwd)
-    if is_verbose_mode():
-      print(output)
+    verbose_mode_print(output.decode('utf-8').strip())
     return output
   except subprocess.CalledProcessError as e:
     print(e.output)

filename: script/release/uploaders/upload.py
patch: @@ -16,8 +16,9 @@
 
 from zipfile import ZipFile
 from lib.config import PLATFORM, get_target_arch, \
-                       get_zip_name, enable_verbose_mode, \
-                       is_verbose_mode, get_platform_key
+                       get_zip_name, set_verbose_mode, \
+                       is_verbose_mode, get_platform_key, \
+                       verbose_mode_print
 from lib.util import get_electron_branding, execute, get_electron_version, \
                      store_artifact, get_electron_exec, get_out_dir, \
                      SRC_DIR, ELECTRON_DIR, TS_NODE
@@ -45,8 +46,7 @@
 
 def main():
   args = parse_args()
-  if args.verbose:
-    enable_verbose_mode()
+  set_verbose_mode(args.verbose)
   if args.upload_to_storage:
     utcnow = datetime.datetime.now(datetime.UTC)
     args.upload_timestamp = utcnow.strftime('%Y%m%d')
@@ -391,9 +391,8 @@ def get_release(version):
   release_env = os.environ.copy()
   release_env['NODE_NO_WARNINGS'] = '1'
   release_info = execute([TS_NODE, script_path, version], release_env)
-  if is_verbose_mode():
-    print(f'Release info for version: {version}:\n')
-    print(release_info)
+  verbose_mode_print(f'Release info for version: {version}:\n')
+  verbose_mode_print(release_info)
   release = json.loads(release_info)
   return release
 

filename: script/strip-binaries.py
patch: @@ -4,11 +4,26 @@
 import os
 import sys
 
-from lib.config import enable_verbose_mode
+from lib.config import set_verbose_mode, is_verbose_mode, verbose_mode_print
 from lib.util import execute, get_linux_binaries, get_out_dir
 
+def get_size(path):
+  size = os.path.getsize(path)
+  units = ["bytes", "KB", "MB", "GB"]
+  for unit in units:
+    if size < 1024:
+      return f"{size:.2f} {unit}"
+    size /= 1024
+  raise ValueError("File size is too large to be processed")
+
 def strip_binaries(directory, target_cpu):
+  if not os.path.isdir(directory):
+    verbose_mode_print('Directory ' + directory + ' does not exist.')
+    return
+
+  verbose_mode_print('Stripping binaries in ' + directory)
   for binary in get_linux_binaries():
+    verbose_mode_print('\nStripping ' + binary)
     binary_path = os.path.join(directory, binary)
     if os.path.isfile(binary_path):
       strip_binary(binary_path, target_cpu)
@@ -20,14 +35,23 @@ def strip_binary(binary_path, target_cpu):
     strip = 'aarch64-linux-gnu-strip'
   else:
     strip = 'strip'
-  execute([
-    strip, '--discard-all', '--strip-debug', '--preserve-dates',
-    binary_path])
+  
+  strip_args = [strip,
+                '--discard-all',
+                '--strip-debug',
+                '--preserve-dates',
+                binary_path]
+  if (is_verbose_mode()):
+    strip_args.insert(1, '--verbose')
+  verbose_mode_print('Binary size before stripping: ' + 
+                     str(get_size(binary_path)))
+  execute(strip_args)
+  verbose_mode_print('Binary size after stripping: ' +
+                     str(get_size(binary_path)))
 
 def main():
   args = parse_args()
-  if args.verbose:
-    enable_verbose_mode()
+  set_verbose_mode(args.verbose)
   if args.file:
     strip_binary(args.file, args.target_cpu)
   else:
@@ -43,6 +67,7 @@ def parse_args():
                       help='Path to a specific file to strip.',
                       required=False)
   parser.add_argument('-v', '--verbose',
+                      default=False,
                       action='store_true',
                       help='Prints the output of the subprocesses')
   parser.add_argument('--target-cpu',