Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[infra] Refactored some sh files with python #841

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions infra/portable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

- This directory contains files and scripts to make portable *.zip* packages of Thorium for Linux and Windows.
- Run a script with `--help` to see usage.
- For `make_portable_win.py`, make sure
[7-zip](https://www.7-zip.org/download.html) is installed on the device and
the installation directory of 7-zip needs to be placed in the environment
variable.
100 changes: 100 additions & 0 deletions infra/portable/make_portable_win.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright (c) 2024 Alex313031 and gz83.

# This file is the equivalent of make_portable_win.py.


import os
import shutil
import subprocess
import sys
import time
import zipfile


def fail(msg):
print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
sys.exit(111)


def try_run(command):
result = subprocess.run(command, shell=True)
if result.returncode != 0:
fail(f"Failed {command}")


def display_help():
print("\nScript to make a portable Thorium .zip for Windows.\n")
print("\nPlease place the thorium_mini_installer.exe file in this directory before running.\n")

if '--help' in sys.argv:
display_help()
sys.exit(0)


def copy_installer():
cr_src_dir = os.getenv('CR_DIR', r'C:/src/chromium/src')
src_path = os.path.normpath(os.path.join(cr_src_dir, 'out', 'thorium', 'thorium_mini_installer.exe'))
dest_path = os.path.normpath(os.path.join(os.getcwd(), 'thorium_mini_installer.exe'))

if not os.path.exists(src_path):
fail(f"thorium_mini_installer.exe not found at {src_path}")

print(f"Copying thorium_mini_installer.exe from {src_path} to {dest_path}...\n")
shutil.copy(src_path, dest_path)


def extract_and_copy_files():
# Extract and copy files
os.makedirs('./temp/USER_DATA', exist_ok=True)
try_run('7z x thorium_mini_installer.exe')
try_run('7z x chrome.7z')
shutil.move('Chrome-bin', './temp/BIN')
shutil.copy('./README.win', './temp/README.txt')
shutil.copy('./THORIUM.BAT', './temp/')
shutil.copy('./THORIUM_SHELL.BAT', './temp/')

def zip_files():
# Create zip archive
with zipfile.ZipFile('thorium_portable.zip', 'w', zipfile.ZIP_DEFLATED) as zf:
for root, _, files in os.walk('./temp'):
for file in files:
file_path = os.path.join(root, file)
zf.write(file_path, os.path.relpath(file_path, './temp'))

def clean_up():
# Cleanup extracted files
try:
os.remove('chrome.7z')
shutil.rmtree('temp')
except FileNotFoundError:
pass

def main():

print("\nNOTE: You must place the thorium .exe file in this directory before running.")
print(" AND you must have 7-Zip installed and in your PATH.")
print(" Make sure to rename the .zip properly as per the release instructions.")
print(" AND make sure to edit the THORIUM_SHELL.BAT to match the version number of this release.\n")

copy_installer()

input("Press Enter to continue or Ctrl + C to abort.")

print("Extracting & Copying files from thorium .exe file...\n")
time.sleep(2)

extract_and_copy_files()

print("\nZipping up...\n")
zip_files()

print("\nCleaning up...\n")
time.sleep(2)

clean_up()

print("\nDone! Zip is at ./thorium_portable.zip")
print("Remember to rename it with the version before distributing it.\n")

if __name__ == "__main__":
main()
6 changes: 2 additions & 4 deletions win_scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<img src="https://github.com/Alex313031/thorium/blob/main/logos/NEW/win/mini_installer/thorium_mini_installer_86.png">

This directory contains batch files for building Thorium natively on Windows.
This directory contains python files for building Thorium natively on Windows.

The "bin" subdirectory contains useful "aliases" for cmd.exe. One can copy this folder somewhere or leave it where it is and then add it to your PATH.

Also recommended is Clink > https://mridgers.github.io/clink/
Before running the Python files in this directory, please strictly follow the instructions in [BUILDING_WIN](https://github.com/Alex313031/thorium/blob/main/docs/BUILDING_WIN.md).
48 changes: 48 additions & 0 deletions win_scripts/build_win.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (c) 2024 Alex313031 and gz83.

# This file is the equivalent of build_win.sh in the parent directory.

import os
import subprocess
import sys

# Error handling functions
def fail(msg):
# Print error message and exit
print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
sys.exit(111)


def try_run(command):
if subprocess.call(command, shell=True) != 0:
fail(f"Failed {command}")


# Help function
def display_help():
print("\nScript to build Thorium for Windows.\n")
print("Usage: python win_scripts\build_win.py # (where # is number of jobs)\n")

if '--help' in sys.argv:
display_help()
sys.exit(0)


# Set chromium/src dir from Windows environment variable
cr_src_dir = os.getenv('CR_DIR', r'C:/src/chromium/src')

print("\nBuilding Thorium for Windows\n")

# Change directory and run build commands
os.chdir(cr_src_dir)
# Determine the number of threads to use
jobs = sys.argv[1] if len(sys.argv) > 1 else str(os.cpu_count())

try_run(f'autoninja -C out/thorium thorium_all -j{jobs}')

# Move the installer
installer_src = os.path.normpath(os.path.join(cr_src_dir, 'out', 'thorium', 'mini_installer.exe'))
installer_dest = os.path.normpath(os.path.join(cr_src_dir, 'out', 'thorium', 'thorium_mini_installer.exe'))
os.rename(installer_src, installer_dest)

print(f"Build Completed. Installer at '{installer_dest}'")
64 changes: 64 additions & 0 deletions win_scripts/clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2024 Alex313031 and gz83.

"""
This file is the equivalent of clean.sh in the parent directory, but it directly
deletes //out/thorium and unneeded PGO files.
"""

import os
import shutil
import sys
import subprocess

def fail(msg):
print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
sys.exit(111)


def try_run(command):
try:
subprocess.run(command, shell=True, check=True)
except subprocess.CalledProcessError:
fail(f"Failed {command}")


def clean_files(directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
try:
os.remove(file_path)
print(f"Removed: {file_path}")
except Exception as e:
fail(f"Failed to remove {file_path}: {e}")


def delete_directory(directory):
if os.path.exists(directory):
try:
shutil.rmtree(directory)
print(f"Removed directory: {directory}")
except Exception as e:
fail(f"Failed to remove directory {directory}: {e}")


def display_help():
print("\nScript to remove unneeded artifacts\n")

if '--help' in sys.argv:
display_help()
sys.exit(0)


# Set chromium/src dir from Windows environment variable
cr_src_dir = os.getenv('CR_DIR', r'C:/src/chromium/src')

print("\nCleaning up unneeded artifacts\n")

profiles_dir = os.path.normpath(os.path.join(cr_src_dir, "chrome", "build", "pgo_profiles"))
clean_files(profiles_dir)

thorium_dir = os.path.normpath(os.path.join(cr_src_dir, "out", "thorium"))
delete_directory(thorium_dir)

print("\nDone cleaning artifacts\n")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions win_scripts/reset_depot_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright (c) 2024 Alex313031, midzer and gz83.

"""
This file is the equivalent of reset_depot_tools.py in the parent folder, but we
do not need to deal with the .vpython_cipd_cache.
This file may prompt "Access denied" and other prompts during the operation, but
in fact it seems that the files we need to delete have been deleted.
"""

# TODO(gz83): Suppress false positives during operation?

import os
import shutil
import subprocess
import sys


def fail(msg):
print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
sys.exit(111)


def try_run(command):
try:
subprocess.run(command, shell=True, check=True)
except subprocess.CalledProcessError:
fail(f"Failed {command}")


def remove(item_path):
if os.path.exists(item_path):
if os.path.isdir(item_path):
try:
# Try to unlock and delete the directory
unlock_and_delete(item_path)
print(f"removed '{item_path}'")
except PermissionError as e:
print(f"Failed to remove '{item_path}': {e}")
else:
os.remove(item_path)
print(f"removed '{item_path}'")


def unlock_and_delete(path):
"""Attempts to unlock and delete a directory using cmd commands."""
if sys.platform == "win32":
# Use the Windows command line tools to unlock the directory
try:
# Use the command 'del' to delete all files recursively
subprocess.run(f'del /S /Q "{path}\\*"', shell=True, check=True)
# Use the command 'rmdir' to delete the directory
subprocess.run(f'rmdir /S /Q "{path}"', shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"Failed to unlock and delete directory '{path}' via CMD: {e}")
raise PermissionError(f"Failed to unlock and delete directory '{path}' via CMD: {e}")
else:
# For other platforms, just use shutil.rmtree
shutil.rmtree(path)


def display_help():
print("\nScript to reset depot_tools on Windows.\n")
print("This will remove depot_tools, .gsutil, and .vpython-root")
print("from your disk, and then re-clone depot_tools.")
print("\n")

if '--help' in sys.argv:
display_help()
sys.exit(0)


depot_tools_dir = os.getenv('DEPOT_TOOLS_DIR', r"C:\src\depot_tools")
gsutil_dir = os.path.expandvars(os.getenv('GSUTIL_DIR', r'%USERPROFILE%\.gsutil'))
vpython_root_dir = os.path.expandvars(os.getenv('VPYTHON_ROOT_DIR', r'%LOCALAPPDATA%\.vpython-root'))

print("\nRemoving depot_tools, etc\n")

remove(depot_tools_dir)
remove(gsutil_dir)
remove(vpython_root_dir)

print("\nRe-clone depot_tools\n")

os.chdir(os.path.dirname(depot_tools_dir))
try_run(f"git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git")

print(f"\nCompleted. You can now use the depot_tools installed at: {depot_tools_dir}\n")
print("\nYou can now run trunk.py\n")
Loading