[update] Implement --update-to repo

Authored by: Grub4K, pukkandan
This commit is contained in:
Simon Sawicki
2023-05-20 21:21:32 +02:00
parent d2e84d5eb0
commit 665472a7de
4 changed files with 41 additions and 23 deletions

View File

@@ -939,7 +939,7 @@ def _real_main(argv=None):
ydl.cache.remove()
try:
updater = Updater(ydl, opts.update_self if isinstance(opts.update_self, str) else None)
updater = Updater(ydl, opts.update_self)
if opts.update_self and updater.update() and actual_use:
if updater.cmd:
return updater.restart()

View File

@@ -323,7 +323,7 @@ def create_parser():
help='Print program version and exit')
general.add_option(
'-U', '--update',
action='store_true', dest='update_self',
action='store_const', dest='update_self', const=CHANNEL,
help=format_field(
is_non_updateable(), None, 'Check if updates are available. %s',
default=f'Update this program to the latest {CHANNEL} version'))
@@ -335,9 +335,9 @@ def create_parser():
'--update-to',
action='store', dest='update_self', metavar='[CHANNEL]@[TAG]',
help=(
'Upgrade/downgrade to a specific version. CHANNEL and TAG defaults to '
f'"{CHANNEL}" and "latest" respectively if omitted; See "UPDATE" for details. '
f'Supported channels: {", ".join(UPDATE_SOURCES)}'))
'Upgrade/downgrade to a specific version. CHANNEL can be a repository as well. '
f'CHANNEL and TAG default to "{CHANNEL.partition("@")[0]}" and "latest" respectively if omitted; '
f'See "UPDATE" for details. Supported channels: {", ".join(UPDATE_SOURCES)}'))
general.add_option(
'-i', '--ignore-errors',
action='store_true', dest='ignoreerrors',

View File

@@ -129,27 +129,36 @@ class Updater:
self.ydl = ydl
self.target_channel, sep, self.target_tag = (target or CHANNEL).rpartition('@')
if not sep and self.target_tag in UPDATE_SOURCES: # stable => stable@latest
self.target_channel, self.target_tag = self.target_tag, None
# 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
self.target_channel = CHANNEL.partition('@')[0]
if not self.target_tag:
self.target_tag, self._exact = 'latest', False
self.target_tag = 'latest'
self._exact = False
elif self.target_tag != 'latest':
self.target_tag = f'tags/{self.target_tag}'
@property
def _target_repo(self):
try:
return UPDATE_SOURCES[self.target_channel]
except KeyError:
return self._report_error(
f'Invalid update channel {self.target_channel!r} requested. '
f'Valid channels are {", ".join(UPDATE_SOURCES)}', True)
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.restart = self._blocked_restart
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 channel != self.target_channel:
if self._exact and channel != self.target_channel:
return False
if _VERSION_RE.fullmatch(f'{a}.{b}'):
@@ -372,6 +381,12 @@ class Updater:
_, _, returncode = Popen.run(self.cmd)
return returncode
def _blocked_restart(self):
self._report_error(
'Automatically restarting into custom builds is disabled for security reasons. '
'Restart yt-dlp to use the updated version', expected=True)
return self.ydl._download_retcode
def run_update(ydl):
"""Update the program file with the latest version from the repository