]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/update.py
[extractor/youtube] Process `post_live` over 2 hours
[yt-dlp.git] / yt_dlp / update.py
index fc96f298505377b38aae0bbd0fcedc795bf1e6f4..4790075eb631980c2ccd87632d6b405219752340 100644 (file)
@@ -1,4 +1,5 @@
 import atexit
+import contextlib
 import hashlib
 import json
 import os
@@ -6,6 +7,7 @@
 import re
 import subprocess
 import sys
+import urllib.error
 from zipimport import zipimporter
 
 from .compat import functools  # isort: split
 from .utils import (
     Popen,
     cached_method,
+    deprecation_warning,
+    network_exceptions,
+    remove_end,
+    remove_start,
+    sanitized_Request,
     shell_quote,
     system_identifier,
-    traverse_obj,
     version_tuple,
 )
-from .version import UPDATE_HINT, VARIANT, __version__
+from .version import CHANNEL, UPDATE_HINT, VARIANT, __version__
 
-REPOSITORY = 'yt-dlp/yt-dlp'
-API_URL = f'https://api.github.com/repos/{REPOSITORY}/releases'
+UPDATE_SOURCES = {
+    'stable': 'yt-dlp/yt-dlp',
+    'nightly': 'yt-dlp/yt-dlp-nightly-builds',
+}
+REPOSITORY = UPDATE_SOURCES['stable']
+
+_VERSION_RE = re.compile(r'(\d+\.)*\d+')
+
+API_BASE_URL = 'https://api.github.com/repos'
+
+# Backwards compatibility variables for the current channel
+API_URL = f'{API_BASE_URL}/{REPOSITORY}/releases'
 
 
 @functools.cache
 def _get_variant_and_executable_path():
     """@returns (variant, executable_path)"""
-    if hasattr(sys, 'frozen'):
+    if getattr(sys, 'frozen', False):
         path = sys.executable
         if not hasattr(sys, '_MEIPASS'):
             return 'py2exe', path
-        if sys._MEIPASS == os.path.dirname(path):
+        elif sys._MEIPASS == os.path.dirname(path):
             return f'{sys.platform}_dir', path
-        if sys.platform == 'darwin' and version_tuple(platform.mac_ver()[0]) < (10, 15):
-            return 'darwin_legacy_exe', path
-        return f'{sys.platform}_exe', path
+        elif sys.platform == 'darwin':
+            machine = '_legacy' if version_tuple(platform.mac_ver()[0]) < (10, 15) else ''
+        else:
+            machine = f'_{platform.machine().lower()}'
+            # Ref: https://en.wikipedia.org/wiki/Uname#Examples
+            if machine[1:] in ('x86', 'x86_64', 'amd64', 'i386', 'i686'):
+                machine = '_x86' if platform.architecture()[0][:2] == '32' else ''
+        return f'{remove_end(sys.platform, "32")}{machine}_exe', path
 
     path = os.path.dirname(__file__)
     if isinstance(__loader__, zipimporter):
@@ -50,13 +71,29 @@ def detect_variant():
     return VARIANT or _get_variant_and_executable_path()[0]
 
 
+@functools.cache
+def current_git_head():
+    if detect_variant() != 'source':
+        return
+    with contextlib.suppress(Exception):
+        stdout, _, _ = Popen.run(
+            ['git', 'rev-parse', '--short', 'HEAD'],
+            text=True, cwd=os.path.dirname(os.path.abspath(__file__)),
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        if re.fullmatch('[0-9a-f]+', stdout.strip()):
+            return stdout.strip()
+
+
 _FILE_SUFFIXES = {
     'zip': '',
     'py2exe': '_min.exe',
-    'win32_exe': '.exe',
+    'win_exe': '.exe',
+    'win_x86_exe': '_x86.exe',
     'darwin_exe': '_macos',
     'darwin_legacy_exe': '_macos_legacy',
     'linux_exe': '_linux',
+    'linux_aarch64_exe': '_linux_aarch64',
+    'linux_armv7l_exe': '_linux_armv7l',
 }
 
 _NON_UPDATEABLE_REASONS = {
@@ -86,49 +123,108 @@ def _sha256_file(path):
 
 
 class Updater:
-    def __init__(self, ydl):
+    _exact = True
+
+    def __init__(self, ydl, target=None):
         self.ydl = ydl
 
+        self.target_channel, sep, self.target_tag = (target or CHANNEL).rpartition('@')
+        # stable => stable@latest
+        if not sep and ('/' in self.target_tag or self.target_tag in UPDATE_SOURCES):
+            self.target_channel = self.target_tag
+            self.target_tag = None
+        elif not self.target_channel:
+            self.target_channel = CHANNEL.partition('@')[0]
+
+        if not self.target_tag:
+            self.target_tag = 'latest'
+            self._exact = False
+        elif self.target_tag != 'latest':
+            self.target_tag = f'tags/{self.target_tag}'
+
+        if '/' in self.target_channel:
+            self._target_repo = self.target_channel
+            if self.target_channel not in (CHANNEL, *UPDATE_SOURCES.values()):
+                self.ydl.report_warning(
+                    f'You are switching to an {self.ydl._format_err("unofficial", "red")} executable '
+                    f'from {self.ydl._format_err(self._target_repo, self.ydl.Styles.EMPHASIS)}. '
+                    f'Run {self.ydl._format_err("at your own risk", "light red")}')
+                self._block_restart('Automatically restarting into custom builds is disabled for security reasons')
+        else:
+            self._target_repo = UPDATE_SOURCES.get(self.target_channel)
+            if not self._target_repo:
+                self._report_error(
+                    f'Invalid update channel {self.target_channel!r} requested. '
+                    f'Valid channels are {", ".join(UPDATE_SOURCES)}', True)
+
+    def _version_compare(self, a, b, channel=CHANNEL):
+        if self._exact and channel != self.target_channel:
+            return False
+
+        if _VERSION_RE.fullmatch(f'{a}.{b}'):
+            a, b = version_tuple(a), version_tuple(b)
+            return a == b if self._exact else a >= b
+        return a == b
+
     @functools.cached_property
     def _tag(self):
-        if version_tuple(__version__) >= version_tuple(self.latest_version):
-            return 'latest'
+        if self._version_compare(self.current_version, self.latest_version):
+            return self.target_tag
 
-        identifier = f'{detect_variant()} {system_identifier()}'
+        identifier = f'{detect_variant()} {self.target_channel} {system_identifier()}'
         for line in self._download('_update_spec', 'latest').decode().splitlines():
             if not line.startswith('lock '):
                 continue
             _, tag, pattern = line.split(' ', 2)
             if re.match(pattern, identifier):
-                return f'tags/{tag}'
-        return 'latest'
+                if not self._exact:
+                    return f'tags/{tag}'
+                elif self.target_tag == 'latest' or not self._version_compare(
+                        tag, self.target_tag[5:], channel=self.target_channel):
+                    self._report_error(
+                        f'yt-dlp cannot be updated above {tag} since you are on an older Python version', True)
+                    return f'tags/{self.current_version}'
+        return self.target_tag
 
     @cached_method
     def _get_version_info(self, tag):
-        self.ydl.write_debug(f'Fetching release info: {API_URL}/{tag}')
-        return json.loads(self.ydl.urlopen(f'{API_URL}/{tag}').read().decode())
+        url = f'{API_BASE_URL}/{self._target_repo}/releases/{tag}'
+        self.ydl.write_debug(f'Fetching release info: {url}')
+        return json.loads(self.ydl.urlopen(sanitized_Request(url, headers={
+            'Accept': 'application/vnd.github+json',
+            'User-Agent': 'yt-dlp',
+            'X-GitHub-Api-Version': '2022-11-28',
+        })).read().decode())
 
     @property
     def current_version(self):
         """Current version"""
         return __version__
 
+    @staticmethod
+    def _label(channel, tag):
+        """Label for a given channel and tag"""
+        return f'{channel}@{remove_start(tag, "tags/")}'
+
+    def _get_actual_tag(self, tag):
+        if tag.startswith('tags/'):
+            return tag[5:]
+        return self._get_version_info(tag)['tag_name']
+
     @property
     def new_version(self):
         """Version of the latest release we can update to"""
-        if self._tag.startswith('tags/'):
-            return self._tag[5:]
-        return self._get_version_info(self._tag)['tag_name']
+        return self._get_actual_tag(self._tag)
 
     @property
     def latest_version(self):
-        """Version of the latest release"""
-        return self._get_version_info('latest')['tag_name']
+        """Version of the target release"""
+        return self._get_actual_tag(self.target_tag)
 
     @property
     def has_update(self):
         """Whether there is an update available"""
-        return version_tuple(__version__) < version_tuple(self.new_version)
+        return not self._version_compare(self.current_version, self.new_version)
 
     @functools.cached_property
     def filename(self):
@@ -136,20 +232,15 @@ def filename(self):
         return compat_realpath(_get_variant_and_executable_path()[1])
 
     def _download(self, name, tag):
-        url = traverse_obj(self._get_version_info(tag), (
-            'assets', lambda _, v: v['name'] == name, 'browser_download_url'), get_all=False)
-        if not url:
-            raise Exception('Unable to find download URL')
+        slug = 'latest/download' if tag == 'latest' else f'download/{tag[5:]}'
+        url = f'https://github.com/{self._target_repo}/releases/{slug}/{name}'
         self.ydl.write_debug(f'Downloading {name} from {url}')
         return self.ydl.urlopen(url).read()
 
     @functools.cached_property
     def release_name(self):
         """The release filename"""
-        label = _FILE_SUFFIXES[detect_variant()]
-        if label and platform.architecture()[0][:2] == '32':
-            label = f'_x86{label}'
-        return f'yt-dlp{label}'
+        return f'yt-dlp{_FILE_SUFFIXES[detect_variant()]}'
 
     @functools.cached_property
     def release_hash(self):
@@ -159,29 +250,38 @@ def release_hash(self):
 
     def _report_error(self, msg, expected=False):
         self.ydl.report_error(msg, tb=False if expected else None)
+        self.ydl._download_retcode = 100
 
     def _report_permission_error(self, file):
         self._report_error(f'Unable to write to {file}; Try running as administrator', True)
 
     def _report_network_error(self, action, delim=';'):
-        self._report_error(f'Unable to {action}{delim} Visit  https://github.com/{REPOSITORY}/releases/latest', True)
+        self._report_error(
+            f'Unable to {action}{delim} visit  '
+            f'https://github.com/{self._target_repo}/releases/{self.target_tag.replace("tags/", "tag/")}', True)
 
     def check_update(self):
         """Report whether there is an update available"""
+        if not self._target_repo:
+            return False
         try:
-            self.ydl.to_screen(
-                f'Latest version: {self.latest_version}, Current version: {self.current_version}')
-            if not self.has_update:
-                if self._tag == 'latest':
-                    return self.ydl.to_screen(f'yt-dlp is up to date ({__version__})')
-                return self.ydl.report_warning(
-                    'yt-dlp cannot be updated any further since you are on an older Python version')
-        except Exception:
-            return self._report_network_error('obtain version info', delim='; Please try again later or')
+            self.ydl.to_screen((
+                f'Available version: {self._label(self.target_channel, self.latest_version)}, ' if self.target_tag == 'latest' else ''
+            ) + f'Current version: {self._label(CHANNEL, self.current_version)}')
+        except network_exceptions as e:
+            return self._report_network_error(f'obtain version info ({e})', delim='; Please try again later or')
 
         if not is_non_updateable():
-            self.ydl.to_screen(f'Current Build Hash {_sha256_file(self.filename)}')
-        return True
+            self.ydl.to_screen(f'Current Build Hash: {_sha256_file(self.filename)}')
+
+        if self.has_update:
+            return True
+
+        if self.target_tag == self._tag:
+            self.ydl.to_screen(f'yt-dlp is up to date ({self._label(CHANNEL, self.current_version)})')
+        elif not self._exact:
+            self.ydl.report_warning('yt-dlp cannot be updated any further since you are on an older Python version')
+        return False
 
     def update(self):
         """Update yt-dlp executable to the latest version"""
@@ -190,7 +290,11 @@ def update(self):
         err = is_non_updateable()
         if err:
             return self._report_error(err, True)
-        self.ydl.to_screen(f'Updating to version {self.new_version} ...')
+        self.ydl.to_screen(f'Updating to {self._label(self.target_channel, self.new_version)} ...')
+        if (_VERSION_RE.fullmatch(self.target_tag[5:])
+                and version_tuple(self.target_tag[5:]) < (2023, 3, 2)):
+            self.ydl.report_warning('You are downgrading to a version without --update-to')
+            self._block_restart('Cannot automatically restart to a version without --update-to')
 
         directory = os.path.dirname(self.filename)
         if not os.access(self.filename, os.W_OK):
@@ -210,10 +314,11 @@ def update(self):
 
         try:
             newcontent = self._download(self.release_name, self._tag)
-        except OSError:
-            return self._report_network_error('download latest version')
-        except Exception:
-            return self._report_network_error('fetch updates')
+        except network_exceptions as e:
+            if isinstance(e, urllib.error.HTTPError) and e.code == 404:
+                return self._report_error(
+                    f'The requested tag {self._label(self.target_channel, self.target_tag)} does not exist', True)
+            return self._report_network_error(f'fetch updates: {e}')
 
         try:
             expected_hash = self.release_hash
@@ -242,7 +347,8 @@ def update(self):
                 self._report_error('Unable to overwrite current version')
                 return os.rename(old_filename, self.filename)
 
-        if detect_variant() in ('win32_exe', 'py2exe'):
+        variant = detect_variant()
+        if variant.startswith('win') or variant == 'py2exe':
             atexit.register(Popen, f'ping 127.0.0.1 -n 5 -w 1000 & del /F "{old_filename}"',
                             shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
         elif old_filename:
@@ -257,7 +363,7 @@ def update(self):
                 return self._report_error(
                     f'Unable to set permissions. Run: sudo chmod a+rx {compat_shlex_quote(self.filename)}')
 
-        self.ydl.to_screen(f'Updated yt-dlp to version {self.new_version}')
+        self.ydl.to_screen(f'Updated yt-dlp to {self._label(self.target_channel, self.new_version)}')
         return True
 
     @functools.cached_property
@@ -266,7 +372,7 @@ def cmd(self):
         # There is no sys.orig_argv in py < 3.10. Also, it can be [] when frozen
         if getattr(sys, 'orig_argv', None):
             return sys.orig_argv
-        elif hasattr(sys, 'frozen'):
+        elif getattr(sys, 'frozen', False):
             return sys.argv
 
     def restart(self):
@@ -276,6 +382,12 @@ def restart(self):
         _, _, returncode = Popen.run(self.cmd)
         return returncode
 
+    def _block_restart(self, msg):
+        def wrapper():
+            self._report_error(f'{msg}. Restart yt-dlp to use the updated version', expected=True)
+            return self.ydl._download_retcode
+        self.restart = wrapper
+
 
 def run_update(ydl):
     """Update the program file with the latest version from the repository
@@ -288,11 +400,8 @@ def run_update(ydl):
 def update_self(to_screen, verbose, opener):
     import traceback
 
-    from .utils import write_string
-
-    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')
+    deprecation_warning(f'"{__name__}.update_self" is deprecated and may be removed '
+                        f'in a future version. Use "{__name__}.run_update(ydl)" instead')
 
     printfn = to_screen
 
@@ -326,3 +435,6 @@ def urlopen(self, url):
             return opener.open(url)
 
     return run_update(FakeYDL())
+
+
+__all__ = ['Updater']