]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/update.py
[MainStreaming] Add extractor (#2180)
[yt-dlp.git] / yt_dlp / update.py
index e4b1280be6a2e6519b479833502b8369c2c349c9..f3448568a51fce261b2a7ddce58f7ed655fc1cf8 100644 (file)
@@ -10,7 +10,7 @@
 from zipimport import zipimporter
 
 from .compat import compat_realpath
-from .utils import encode_compat_str, Popen
+from .utils import encode_compat_str, Popen, write_string
 
 from .version import __version__
 
@@ -33,10 +33,11 @@ def rsa_verify(message, signature, key):
 
 def detect_variant():
     if hasattr(sys, 'frozen'):
+        prefix = 'mac' if sys.platform == 'darwin' else 'win'
         if getattr(sys, '_MEIPASS', None):
             if sys._MEIPASS == os.path.dirname(sys.executable):
-                return 'dir'
-            return 'exe'
+                return f'{prefix}_dir'
+            return f'{prefix}_exe'
         return 'py2exe'
     elif isinstance(globals().get('__loader__'), zipimporter):
         return 'zip'
@@ -46,10 +47,12 @@ def detect_variant():
 
 
 _NON_UPDATEABLE_REASONS = {
-    'exe': None,
+    'win_exe': None,
     'zip': None,
-    'dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release',
-    'py2exe': 'There is no official release for py2exe executable; Build it again with the latest source code',
+    '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',
     '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',
 }
@@ -99,26 +102,28 @@ def version_tuple(version_str):
         return tuple(map(int, version_str.split('.')))
 
     version_id = version_info['tag_name']
+    ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
     if version_tuple(__version__) >= version_tuple(version_id):
         ydl.to_screen(f'yt-dlp is up to date ({__version__})')
         return
 
     err = is_non_updateable()
     if err:
-        ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
         return report_error(err, True)
 
     # sys.executable is set to the full pathname of the exe-file for py2exe
     # though symlinks are not followed so that we need to do this manually
     # with help of realpath
     filename = compat_realpath(sys.executable if hasattr(sys, 'frozen') else sys.argv[0])
-    ydl.to_screen(f'Current version {__version__}; Build Hash {calc_sha256sum(filename)}')
+    ydl.to_screen(f'Current Build Hash {calc_sha256sum(filename)}')
     ydl.to_screen(f'Updating to version {version_id} ...')
 
     version_labels = {
         'zip_3': '',
-        'exe_64': '.exe',
-        'exe_32': '_x86.exe',
+        '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):
@@ -139,9 +144,9 @@ def get_sha256sum(bin_or_exe, version):
         return report_permission_error(filename)
 
     # PyInstaller
-    if hasattr(sys, 'frozen'):
-        exe = filename
-        directory = os.path.dirname(exe)
+    variant = detect_variant()
+    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:
@@ -152,7 +157,7 @@ def get_sha256sum(bin_or_exe, version):
 
         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)
@@ -161,48 +166,46 @@ def get_sha256sum(bin_or_exe, version):
         except (IOError, OSError):
             return report_network_error('download latest version')
 
-        if not os.access(exe + '.new', os.W_OK):
-            return report_permission_error(f'{exe}.new')
         try:
-            with open(exe + '.new', 'wb') as outf:
+            with open(filename + '.new', 'wb') as outf:
                 outf.write(newcontent)
         except (IOError, OSError):
-            return report_unable('write the new version')
+            return report_permission_error(f'{filename}.new')
 
-        expected_sum = get_sha256sum('exe', arch)
+        expected_sum = get_sha256sum(variant, arch)
         if not expected_sum:
             ydl.report_warning('no hash information found for the release')
-        elif calc_sha256sum(exe + '.new') != expected_sum:
+        elif calc_sha256sum(filename + '.new') != expected_sum:
             report_network_error('verify the new executable')
             try:
-                os.remove(exe + '.new')
+                os.remove(filename + '.new')
             except OSError:
                 return report_unable('remove corrupt download')
 
         try:
-            os.rename(exe, exe + '.old')
+            os.rename(filename, filename + '.old')
         except (IOError, OSError):
             return report_unable('move current version')
         try:
-            os.rename(exe + '.new', exe)
+            os.rename(filename + '.new', filename)
         except (IOError, OSError):
             report_unable('overwrite current version')
-            os.rename(exe + '.old', exe)
+            os.rename(filename + '.old', filename)
             return
         try:
             # Continues to run in the background
             Popen(
-                'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % exe,
+                'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename,
                 shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
             ydl.to_screen('Updated yt-dlp to version %s' % version_id)
             return True  # Exit app
         except OSError:
             report_unable('delete the old version')
 
-    # Zip unix package
-    elif isinstance(globals().get('__loader__'), zipimporter):
+    elif variant in ('zip', 'mac_exe'):
+        pack_type = '3' if variant == 'zip' else '64'
         try:
-            url = get_bin_info('zip', '3').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)
@@ -211,11 +214,11 @@ def get_sha256sum(bin_or_exe, version):
         except (IOError, OSError):
             return report_network_error('download the latest version')
 
-        expected_sum = get_sha256sum('zip', '3')
+        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:
-            return report_network_error('verify the new zip')
+            return report_network_error('verify the new package')
 
         try:
             with open(filename, 'wb') as outf:
@@ -223,7 +226,10 @@ def get_sha256sum(bin_or_exe, version):
         except (IOError, OSError):
             return report_unable('overwrite current version')
 
-    ydl.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id)
+        ydl.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id)
+        return
+
+    assert False, f'Unhandled variant: {variant}'
 
 
 '''  # UNUSED
@@ -244,14 +250,14 @@ def print_notes(to_screen, versions, fromVersion=__version__):
 '''
 
 
+# Deprecated
 def update_self(to_screen, verbose, opener):
-    ''' Exists for backward compatibility '''
 
     printfn = to_screen
 
-    printfn(
-        'WARNING: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
-        'Use "yt_dlp.update.run_update(ydl)" instead')
+    write_string(
+        'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
+        'Use "yt_dlp.update.run_update(ydl)" instead\n')
 
     class FakeYDL():
         _opener = opener