+ if old_filename:
+ mask = os.stat(self.filename).st_mode
+ try:
+ os.rename(self.filename, old_filename)
+ except OSError:
+ return self._report_error('Unable to move current version')
+
+ try:
+ os.rename(new_filename, self.filename)
+ except OSError:
+ self._report_error('Unable to overwrite current version')
+ return os.rename(old_filename, self.filename)
+
+ 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:
+ try:
+ os.remove(old_filename)
+ except OSError:
+ self._report_error('Unable to remove the old version')
+
+ try:
+ os.chmod(self.filename, mask)
+ except OSError:
+ 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 {update_label}')
+ return True
+
+ @functools.cached_property
+ def filename(self):
+ """Filename of the executable"""
+ return compat_realpath(_get_variant_and_executable_path()[1])
+
+ @functools.cached_property
+ def cmd(self):
+ """The command-line to run the executable, if known"""
+ # 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 getattr(sys, 'frozen', False):
+ return sys.argv
+
+ def restart(self):
+ """Restart the executable"""
+ assert self.cmd, 'Must be frozen or Py >= 3.10'
+ self.ydl.write_debug(f'Restarting: {shell_quote(self.cmd)}')
+ _, _, 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 _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=';', tag=None):
+ if not tag:
+ tag = self.requested_tag
+ self._report_error(
+ f'Unable to {action}{delim} visit https://github.com/{self.requested_repo}/releases/'
+ + tag if tag == "latest" else f"tag/{tag}", True)
+
+ # XXX: Everything below this line in this class is deprecated / for compat only
+ @property
+ def _target_tag(self):
+ """Deprecated; requested tag with 'tags/' prepended when necessary for API calls"""
+ return f'tags/{self.requested_tag}' if self.requested_tag != 'latest' else self.requested_tag
+
+ def _check_update(self):
+ """Deprecated; report whether there is an update available"""
+ return bool(self.query_update(_output=True))
+
+ def __getattr__(self, attribute: str):
+ """Compat getter function for deprecated attributes"""
+ deprecated_props_map = {
+ 'check_update': '_check_update',
+ 'target_tag': '_target_tag',
+ 'target_channel': 'requested_channel',
+ }
+ update_info_props_map = {
+ 'has_update': '_has_update',
+ 'new_version': 'version',
+ 'latest_version': 'requested_version',
+ 'release_name': 'binary_name',
+ 'release_hash': 'checksum',
+ }
+
+ if attribute not in deprecated_props_map and attribute not in update_info_props_map:
+ raise AttributeError(f'{type(self).__name__!r} object has no attribute {attribute!r}')
+
+ msg = f'{type(self).__name__}.{attribute} is deprecated and will be removed in a future version'
+ if attribute in deprecated_props_map:
+ source_name = deprecated_props_map[attribute]
+ if not source_name.startswith('_'):
+ msg += f'. Please use {source_name!r} instead'
+ source = self
+ mapping = deprecated_props_map
+
+ else: # attribute in update_info_props_map
+ msg += '. Please call query_update() instead'
+ source = self.query_update()
+ if source is None:
+ source = UpdateInfo('', None, None, None)
+ source._has_update = False
+ mapping = update_info_props_map
+
+ deprecation_warning(msg)
+ for target_name, source_name in mapping.items():
+ value = getattr(source, source_name)
+ setattr(self, target_name, value)
+
+ return getattr(self, attribute)