Skip to content

Commit

Permalink
Merge pull request #71 from hyrulelinks/master
Browse files Browse the repository at this point in the history
Update macos-build-script.sh
  • Loading branch information
TransparentLC authored Apr 8, 2024
2 parents 1697c43 + 0b4dc87 commit 41d4cb6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 41 deletions.
144 changes: 106 additions & 38 deletions macos-build-script.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Ask user whether to use mirror_head
read -p "Do you want to use mirror_head? (y/n): " use_mirror
if [ "$use_mirror" = "y" ]; then
mirror_head="https://mirror.ghproxy.com/"
else
mirror_head=""
fi

# Clone repo
echo "INFO: Clone repo."
git clone https://github.com/TransparentLC/realesrgan-gui.git
repo_url="https://github.com/TransparentLC/realesrgan-gui.git"
git clone $mirror_head$repo_url
cd realesrgan-gui

# Create and activate Python virtual environment
echo "INFO: 🚀 Checking current Python version..."
echo "INFO: 🚧 Checking current Python version..."
python_version=$(python3 -V 2>&1 | cut -d" " -f2 | cut -d"." -f1-2)

if ! which python3 >/dev/null 2>&1; then
Expand All @@ -15,65 +24,124 @@ else
echo "INFO: The 'python3' command found."
if [ "$python_version" == "3.12" ]; then
echo "INFO: ✅ The current Python version is 3.12"
echo "INFO: 🚀 Creating Python 3.12 virtual enviroment..."
echo "INFO: 🚧 Creating Python 3.12 virtual enviroment..."
python3 -m venv venv
echo "INFO: 🚀 Activating Python virtual enviroment..."
echo "INFO: 🚧 Activating Python virtual enviroment..."
source venv/bin/activate

else
echo "ERROR: ⛔️ The current Python version is $python_version but 3.12 is required."
echo "INFO: 🚀 Installling Python package 'virtualenv'..."
echo "INFO: 🚧 Installling Python package 'virtualenv'..."
pip3 install virtualenv
echo "INFO: 🚀 Creating Python 3.12 virtual enviroment..."
echo "INFO: 🚧 Creating Python 3.12 virtual enviroment..."
virtualenv -p python3.12 venv
echo "INFO: 🚀 Activating Python virtual enviroment..."
echo "INFO: 🚧 Activating Python virtual enviroment..."
source venv/bin/activate
fi
fi

# Download required files
echo "INFO: 🚀 Downloading realesrgan-ncnn-vulkan executable and models..."
base_url="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0"
source_file="realesrgan-ncnn-vulkan-20220424-macos.zip"
target_file="realesrgan-ncnn-vulkan"
model_folder="models"
# Download extra files
# Function to download and process release asset
download_asset() {
repo=$1
asset_keyword=$2
unzip_target=$3
output_folder=$4

if command -v wget &> /dev/null; then
echo "INFO: Using wget..."
wget -q --show-progress "$base_url/$source_file" -O "$source_file"
else
echo "INFO: wget not available, using curl..."
curl -L "$base_url/$source_file" -o "$source_file"
fi
# GitHub API endpoint to retrieve releases
api_url="https://api.github.com/repos/$repo/releases/latest"

# Make a GET request to the GitHub API to retrieve the latest release
response=$(curl -s "$api_url")

# Check if repo is xinntao/Real-ESRGAN
if [ "$repo" = "xinntao/Real-ESRGAN" ]; then
asset_url="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesrgan-ncnn-vulkan-20220424-macos.zip"
zip_file=$(basename "$asset_url")
folder_name="models"
else
# Parse the response to get the latest release asset URL
asset_url=$(echo "$response" | grep -o "https://.*$asset_keyword*.zip" | head -n 1)
zip_file=$(basename "$asset_url")
folder_name=$(basename -s .zip "$zip_file")
fi

unzip -j "$source_file" "$target_file" -d "."
unzip -j "$source_file" "$model_folder/*" -d "$model_folder"
rm -rf "$source_file"
# Download the latest release asset
if [ -n "$asset_url" ]; then
echo "INFO: 🚧 Downloading $zip_file..."
curl -LO "$mirror_head$asset_url"
echo "INFO: ✅ Download $zip_file complete."

# Thin fat files to single architecture
arch=$(uname -m)
# Unzip and process the file
echo "INFO: 🚧 Extracting and processing files..."
unzip -j "$zip_file" "$folder_name/$unzip_target" -d "$output_folder"

echo "INFO: System architecture is $arch."
echo "INFO: Extracting architecture specific libraries..."
# Perform additional actions based on the repo
if [ "$repo" = "upscayl/upscayl-ncnn" ]; then
# Thin file and set permissions
if [ -f "upscayl-bin" ]; then
# Thin file
echo "INFO: 🚧 Thin fat files to single architecture..."
arch=$(uname -m)
echo "INFO: 💬 System architecture is $arch."
echo "INFO: 🚧 Extracting architecture specific libraries..."
if [ "$arch" = "arm64" ]; then
ditto --arch arm64 "$target_file" "$target_file_temp"
else
ditto --arch x86_64 "$target_file" "$target_file_temp"
fi
rm -rf "$target_file"
mv "$target_file_temp" "$target_file"
# Add execute permission
echo "INFO: 🚧 Add execute permission to realesrgan-ncnn-vulkan..."
chmod u+x realesrgan-ncnn-vulkan
fi
elif [ "$repo" = "nihui/realsr-ncnn-vulkan" ]; then
# Rename RealSR models
if [ -f "models/x4.param" ] && [ -f "models/x4.bin" ]; then
echo "INFO: 🚧 Rename RealSR models..."
mv models/x4.param models/realsr-x4-realworld.param
mv models/x4.bin models/realsr-x4-realworld.bin
fi
elif [ "$repo" = "xinntao/Real-ESRGAN" ]; then
# Models to remove
models_to_remove=(
"realesr-animevideov3-x2"
"realesr-animevideov3-x3"
)
# Remove models
for model in "${models_to_remove[@]}"; do
param_file="models/$model.param"
bin_file="models/$model.bin"
if [ -f "$param_file" ] && [ -f "$bin_file" ]; then
echo "INFO: 🚧 Removing $model models..."
rm -rf "$param_file" "$bin_file"
fi
done
fi
echo "INFO: ✅ Processing complete."
else
echo "ERROR: ⛔️ No release asset found for $repo."
fi
}

if [ "$arch" = "arm64" ]; then
ditto --arch arm64 "$target_file" "temp_file"
else
ditto --arch x86_64 "$target_file" "temp_file"
fi
# Download and process assets for upscayl-ncnn
download_asset "upscayl/upscayl-ncnn" "macos" "upscayl-bin" "."

# Download and process assets for realsr-ncnn-vulkan
download_asset "nihui/realsr-ncnn-vulkan" "macos" "models-DF2K/*" "models"

rm -rf "$target_file"
mv "temp_file" "$target_file"
chmod u+x "$target_file"
# Download and process assets for realesrgan-ncnn-vulkan
download_asset "xinntao/Real-ESRGAN" "macos" "*" "models"

# Install dependencies
echo "INFO: 🚀 Installing requirements..."
echo "INFO: 🚧 Installing requirements..."
pip3 install -r requirements.txt
echo "INFO: 🚀 Installing Python package 'pyinstaller'..."
echo "INFO: 🚧 Installing Python package 'pyinstaller'..."
pip install pyinstaller

# Build macOS app
echo "INFO: 🚀 Packaging macOS app..."
echo "INFO: 🚧 Packaging macOS app..."
sudo pyinstaller realesrgan-gui-macos.spec

# Copy built app to Download directory
Expand Down
12 changes: 9 additions & 3 deletions realesrgan-gui-macos.spec
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import subprocess
commit_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()
version = "0.2.5." + commit_hash

# 判断是否存在 upscayl-bin 文件
if os.path.exists('upscayl-bin'):
# 存在 upscayl-bin,只需要打包 upscayl-bin
bin = [('upscayl-bin', '.')]
else:
# 不存在 upscayl-bin,打包 realesrgan-ncnn-vulkan
bin = [('realesrgan-ncnn-vulkan', '.')]

# PyInstaller分析脚本
# 指定主入口文件
a = Analysis(
['main.py'],
# 需要打包的二进制
binaries=[
('realesrgan-ncnn-vulkan', '.'),
],
binaries=bin,
# 需要打包的数据文件
datas=[
('models', 'models'),
Expand Down

0 comments on commit 41d4cb6

Please sign in to comment.