From 386cdfdb5b9ff90c7e7b716e9db6ccdd776feb77 Mon Sep 17 00:00:00 2001
From: pukkandan <pukkandan.ytdlp@gmail.com>
Date: Thu, 21 Oct 2021 18:26:56 +0530
Subject: [PATCH] [build] Release windows exe built with py2exe Closes: #855
 Related: #661, #705, #890, #1024, #1160

---
 .github/workflows/build.yml | 27 ++++++++++++++++++++++++++-
 README.md                   |  1 +
 setup.py                    |  2 +-
 yt_dlp/update.py            | 22 +++++++++++-----------
 4 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 9bcdc4f94c..b2da4063b9 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -211,6 +211,8 @@ jobs:
     outputs:
       sha256_win: ${{ steps.sha256_win.outputs.sha256_win }}
       sha512_win: ${{ steps.sha512_win.outputs.sha512_win }}
+      sha256_py2exe: ${{ steps.sha256_py2exe.outputs.sha256_py2exe }}
+      sha512_py2exe: ${{ steps.sha512_py2exe.outputs.sha512_py2exe }}
       sha256_win_zip: ${{ steps.sha256_win_zip.outputs.sha256_win_zip }}
       sha512_win_zip: ${{ steps.sha512_win_zip.outputs.sha512_win_zip }}
 
@@ -224,7 +226,7 @@ jobs:
     - name: Install Requirements
       # Custom pyinstaller built with https://github.com/yt-dlp/pyinstaller-builds
       run: |
-          python -m pip install --upgrade pip setuptools wheel
+          python -m pip install --upgrade pip setuptools wheel py2exe
           pip install "https://yt-dlp.github.io/Pyinstaller-Builds/x86_64/pyinstaller-4.5.1-py3-none-any.whl" mutagen pycryptodomex websockets
     - name: Bump version
       id: bump_version
@@ -275,6 +277,25 @@ jobs:
       id: sha512_win_zip
       run: echo "::set-output name=sha512_win_zip::$((Get-FileHash dist\yt-dlp_win.zip -Algorithm SHA512).Hash.ToLower())"
 
+    - name: Run py2exe Script
+      run: python setup.py py2exe
+    - name: Upload yt-dlp_min.exe Windows binary
+      id: upload-release-windows-py2exe
+      uses: actions/upload-release-asset@v1
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      with:
+        upload_url: ${{ needs.build_unix.outputs.upload_url }}
+        asset_path: ./dist/yt-dlp.exe
+        asset_name: yt-dlp_min.exe
+        asset_content_type: application/vnd.microsoft.portable-executable
+    - name: Get SHA2-256SUMS for yt-dlp_min.exe
+      id: sha256_py2exe
+      run: echo "::set-output name=sha256_py2exe::$((Get-FileHash dist\yt-dlp.exe -Algorithm SHA256).Hash.ToLower())"
+    - name: Get SHA2-512SUMS for yt-dlp_min.exe
+      id: sha512_py2exe
+      run: echo "::set-output name=sha512_py2exe::$((Get-FileHash dist\yt-dlp.exe -Algorithm SHA512).Hash.ToLower())"
+
   build_windows32:
     runs-on: windows-latest
     needs: build_unix
@@ -330,6 +351,7 @@ jobs:
     - name: Make SHA2-256SUMS file
       env:
         SHA256_WIN: ${{ needs.build_windows.outputs.sha256_win }}
+        SHA256_PY2EXE: ${{ needs.build_windows.outputs.sha256_py2exe }}
         SHA256_WIN_ZIP: ${{ needs.build_windows.outputs.sha256_win_zip }}
         SHA256_WIN32: ${{ needs.build_windows32.outputs.sha256_win32 }}
         SHA256_MACOS: ${{ needs.build_macos.outputs.sha256_macos }}
@@ -338,6 +360,7 @@ jobs:
         SHA256_TAR: ${{ needs.build_unix.outputs.sha256_tar }}
       run: |
         echo "${{ env.SHA256_WIN }}  yt-dlp.exe" >> SHA2-256SUMS
+        echo "${{ env.SHA256_PY2EXE }}  yt-dlp_min.exe" >> SHA2-256SUMS
         echo "${{ env.SHA256_WIN32 }}  yt-dlp_x86.exe" >> SHA2-256SUMS
         echo "${{ env.SHA256_MACOS }}  yt-dlp_macos" >> SHA2-256SUMS
         echo "${{ env.SHA256_MACOS_ZIP }}  yt-dlp_macos.zip" >> SHA2-256SUMS
@@ -357,6 +380,7 @@ jobs:
     - name: Make SHA2-512SUMS file
       env:
         SHA512_WIN: ${{ needs.build_windows.outputs.sha512_win }}
+        SHA512_PY2EXE: ${{ needs.build_windows.outputs.sha512_py2exe }}
         SHA512_WIN_ZIP: ${{ needs.build_windows.outputs.sha512_win_zip }}
         SHA512_WIN32: ${{ needs.build_windows32.outputs.sha512_win32 }}
         SHA512_MACOS: ${{ needs.build_macos.outputs.sha512_macos }}
@@ -365,6 +389,7 @@ jobs:
         SHA512_TAR: ${{ needs.build_unix.outputs.sha512_tar }}
       run: |
         echo "${{ env.SHA512_WIN }}  yt-dlp.exe" >> SHA2-512SUMS
+        echo "${{ env.SHA512_PY2EXE }}  yt-dlp_min.exe" >> SHA2-512SUMS
         echo "${{ env.SHA512_WIN32 }}  yt-dlp_x86.exe" >> SHA2-512SUMS
         echo "${{ env.SHA512_MACOS }}  yt-dlp_macos" >> SHA2-512SUMS
         echo "${{ env.SHA512_MACOS_ZIP }}  yt-dlp_macos.zip" >> SHA2-512SUMS
diff --git a/README.md b/README.md
index edd7d298af..25dd290020 100644
--- a/README.md
+++ b/README.md
@@ -201,6 +201,7 @@ File|Description
 [yt-dlp_win.zip](https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_win.zip)|Unpackaged windows executable
 [yt-dlp_macos](https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos)|MacOS standalone executable
 [yt-dlp_macos.zip](https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos.zip)|Unpackaged MacOS executable
+[yt-dlp_min.exe](https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_x86.exe)|Windows standalone x64 binary built with `py2exe`. Does not contain `pycryptodomex`, needs VC++14
 [yt-dlp.tar.gz](https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.tar.gz)|Source tarball. Also contains manpages, completions, etc
 [SHA2-512SUMS](https://github.com/yt-dlp/yt-dlp/releases/latest/download/SHA2-512SUMS)|GNU-style SHA512 sums
 [SHA2-256SUMS](https://github.com/yt-dlp/yt-dlp/releases/latest/download/SHA2-256SUMS)|GNU-style SHA256 sums
diff --git a/setup.py b/setup.py
index fbd2be0aeb..e1c585be4a 100644
--- a/setup.py
+++ b/setup.py
@@ -29,7 +29,7 @@ REQUIREMENTS = ['mutagen', 'pycryptodomex', 'websockets']
 if sys.argv[1:2] == ['py2exe']:
     import py2exe
     warnings.warn(
-        'Building with py2exe is not officially supported. '
+        'py2exe builds do not support pycryptodomex and needs VC++14 to run. '
         'The recommended way is to use "pyinst.py" to build using pyinstaller')
     params = {
         'console': [{
diff --git a/yt_dlp/update.py b/yt_dlp/update.py
index 127b2cbc84..e880cbd8dc 100644
--- a/yt_dlp/update.py
+++ b/yt_dlp/update.py
@@ -50,9 +50,9 @@ _NON_UPDATEABLE_REASONS = {
     'win_exe': None,
     'zip': None,
     'mac_exe': None,
+    'py2exe': None,
     'win_dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release',
     'mac_dir': 'Auto-update is not supported for unpackaged MacOS executable; Re-download the latest release',
-    'py2exe': 'There is no official release for py2exe executable; Build it again with the latest source code',
     'source': 'You cannot update when running from source code; Use git to pull the latest changes',
     'unknown': 'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball; Use that to update',
 }
@@ -120,9 +120,10 @@ def run_update(ydl):
 
     version_labels = {
         'zip_3': '',
-        'exe_64': '.exe',
-        'exe_32': '_x86.exe',
-        'mac_64': '_macos',
+        'win_exe_64': '.exe',
+        'py2exe_64': '_min.exe',
+        'win_exe_32': '_x86.exe',
+        'mac_exe_64': '_macos',
     }
 
     def get_bin_info(bin_or_exe, version):
@@ -144,9 +145,8 @@ def run_update(ydl):
 
     # PyInstaller
     variant = detect_variant()
-    if variant == 'win_exe':
-        exe = filename
-        directory = os.path.dirname(exe)
+    if variant in ('win_exe', 'py2exe'):
+        directory = os.path.dirname(filename)
         if not os.access(directory, os.W_OK):
             return report_permission_error(directory)
         try:
@@ -157,7 +157,7 @@ def run_update(ydl):
 
         try:
             arch = platform.architecture()[0][:2]
-            url = get_bin_info('exe', arch).get('browser_download_url')
+            url = get_bin_info(variant, arch).get('browser_download_url')
             if not url:
                 return report_network_error('fetch updates')
             urlh = ydl._opener.open(url)
@@ -203,9 +203,9 @@ def run_update(ydl):
             report_unable('delete the old version')
 
     elif variant in ('zip', 'mac_exe'):
-        pack_type = ('mac', '64') if variant == 'mac_exe' else ('zip', '3')
+        pack_type = '3' if variant == 'zip' else '64'
         try:
-            url = get_bin_info(*pack_type).get('browser_download_url')
+            url = get_bin_info(variant, pack_type).get('browser_download_url')
             if not url:
                 return report_network_error('fetch updates')
             urlh = ydl._opener.open(url)
@@ -214,7 +214,7 @@ def run_update(ydl):
         except (IOError, OSError):
             return report_network_error('download the latest version')
 
-        expected_sum = get_sha256sum(*pack_type)
+        expected_sum = get_sha256sum(variant, pack_type)
         if not expected_sum:
             ydl.report_warning('no hash information found for the release')
         elif hashlib.sha256(newcontent).hexdigest() != expected_sum: