update.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. from __future__ import annotations
  2. import atexit
  3. import contextlib
  4. import functools
  5. import hashlib
  6. import json
  7. import os
  8. import platform
  9. import re
  10. import subprocess
  11. import sys
  12. from dataclasses import dataclass
  13. from zipimport import zipimporter
  14. from .networking import Request
  15. from .networking.exceptions import HTTPError, network_exceptions
  16. from .utils import (
  17. NO_DEFAULT,
  18. Popen,
  19. deprecation_warning,
  20. format_field,
  21. remove_end,
  22. shell_quote,
  23. system_identifier,
  24. version_tuple,
  25. )
  26. from .version import (
  27. CHANNEL,
  28. ORIGIN,
  29. RELEASE_GIT_HEAD,
  30. UPDATE_HINT,
  31. VARIANT,
  32. __version__,
  33. )
  34. UPDATE_SOURCES = {
  35. 'stable': 'yt-dlp/yt-dlp',
  36. 'nightly': 'yt-dlp/yt-dlp-nightly-builds',
  37. 'master': 'yt-dlp/yt-dlp-master-builds',
  38. }
  39. REPOSITORY = UPDATE_SOURCES['stable']
  40. _INVERSE_UPDATE_SOURCES = {value: key for key, value in UPDATE_SOURCES.items()}
  41. _VERSION_RE = re.compile(r'(\d+\.)*\d+')
  42. _HASH_PATTERN = r'[\da-f]{40}'
  43. _COMMIT_RE = re.compile(rf'Generated from: https://(?:[^/?#]+/){{3}}commit/(?P<hash>{_HASH_PATTERN})')
  44. API_BASE_URL = 'https://api.github.com/repos'
  45. # Backwards compatibility variables for the current channel
  46. API_URL = f'{API_BASE_URL}/{REPOSITORY}/releases'
  47. @functools.cache
  48. def _get_variant_and_executable_path():
  49. """@returns (variant, executable_path)"""
  50. if getattr(sys, 'frozen', False):
  51. path = sys.executable
  52. if not hasattr(sys, '_MEIPASS'):
  53. return 'py2exe', path
  54. elif sys._MEIPASS == os.path.dirname(path):
  55. return f'{sys.platform}_dir', path
  56. elif sys.platform == 'darwin':
  57. machine = '_legacy' if version_tuple(platform.mac_ver()[0]) < (10, 15) else ''
  58. else:
  59. machine = f'_{platform.machine().lower()}'
  60. is_64bits = sys.maxsize > 2**32
  61. # Ref: https://en.wikipedia.org/wiki/Uname#Examples
  62. if machine[1:] in ('x86', 'x86_64', 'amd64', 'i386', 'i686'):
  63. machine = '_x86' if not is_64bits else ''
  64. # platform.machine() on 32-bit raspbian OS may return 'aarch64', so check "64-bitness"
  65. # See: https://github.com/yt-dlp/yt-dlp/issues/11813
  66. elif machine[1:] == 'aarch64' and not is_64bits:
  67. machine = '_armv7l'
  68. # sys.executable returns a /tmp/ path for staticx builds (linux_static)
  69. # Ref: https://staticx.readthedocs.io/en/latest/usage.html#run-time-information
  70. if static_exe_path := os.getenv('STATICX_PROG_PATH'):
  71. path = static_exe_path
  72. return f'{remove_end(sys.platform, "32")}{machine}_exe', path
  73. path = os.path.dirname(__file__)
  74. if isinstance(__loader__, zipimporter):
  75. return 'zip', os.path.join(path, '..')
  76. elif (os.path.basename(sys.argv[0]) in ('__main__.py', '-m')
  77. and os.path.exists(os.path.join(path, '../.git/HEAD'))):
  78. return 'source', path
  79. return 'unknown', path
  80. def detect_variant():
  81. return VARIANT or _get_variant_and_executable_path()[0]
  82. @functools.cache
  83. def current_git_head():
  84. if detect_variant() != 'source':
  85. return
  86. with contextlib.suppress(Exception):
  87. stdout, _, _ = Popen.run(
  88. ['git', 'rev-parse', '--short', 'HEAD'],
  89. text=True, cwd=os.path.dirname(os.path.abspath(__file__)),
  90. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  91. if re.fullmatch('[0-9a-f]+', stdout.strip()):
  92. return stdout.strip()
  93. _FILE_SUFFIXES = {
  94. 'zip': '',
  95. 'win_exe': '.exe',
  96. 'win_x86_exe': '_x86.exe',
  97. 'darwin_exe': '_macos',
  98. 'darwin_legacy_exe': '_macos_legacy',
  99. 'linux_exe': '_linux',
  100. 'linux_aarch64_exe': '_linux_aarch64',
  101. 'linux_armv7l_exe': '_linux_armv7l',
  102. }
  103. _NON_UPDATEABLE_REASONS = {
  104. **{variant: None for variant in _FILE_SUFFIXES}, # Updatable
  105. **{variant: f'Auto-update is not supported for unpackaged {name} executable; Re-download the latest release'
  106. for variant, name in {'win32_dir': 'Windows', 'darwin_dir': 'MacOS', 'linux_dir': 'Linux'}.items()},
  107. 'py2exe': 'py2exe is no longer supported by yt-dlp; This executable cannot be updated',
  108. 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
  109. 'unknown': 'You installed yt-dlp from a manual build or with a package manager; Use that to update',
  110. 'other': 'You are using an unofficial build of yt-dlp; Build the executable again',
  111. }
  112. def is_non_updateable():
  113. if UPDATE_HINT:
  114. return UPDATE_HINT
  115. return _NON_UPDATEABLE_REASONS.get(
  116. detect_variant(), _NON_UPDATEABLE_REASONS['unknown' if VARIANT else 'other'])
  117. def _get_binary_name():
  118. return format_field(_FILE_SUFFIXES, detect_variant(), template='yt-dlp%s', ignore=None, default=None)
  119. def _get_system_deprecation():
  120. MIN_SUPPORTED, MIN_RECOMMENDED = (3, 9), (3, 9)
  121. if sys.version_info > MIN_RECOMMENDED:
  122. return None
  123. major, minor = sys.version_info[:2]
  124. PYTHON_MSG = f'Please update to Python {".".join(map(str, MIN_RECOMMENDED))} or above'
  125. if sys.version_info < MIN_SUPPORTED:
  126. return f'Python version {major}.{minor} is no longer supported! {PYTHON_MSG}'
  127. return f'Support for Python version {major}.{minor} has been deprecated. {PYTHON_MSG}'
  128. def _sha256_file(path):
  129. h = hashlib.sha256()
  130. mv = memoryview(bytearray(128 * 1024))
  131. with open(os.path.realpath(path), 'rb', buffering=0) as f:
  132. for n in iter(lambda: f.readinto(mv), 0):
  133. h.update(mv[:n])
  134. return h.hexdigest()
  135. def _make_label(origin, tag, version=None):
  136. if '/' in origin:
  137. channel = _INVERSE_UPDATE_SOURCES.get(origin, origin)
  138. else:
  139. channel = origin
  140. label = f'{channel}@{tag}'
  141. if version and version != tag:
  142. label += f' build {version}'
  143. if channel != origin:
  144. label += f' from {origin}'
  145. return label
  146. @dataclass
  147. class UpdateInfo:
  148. """
  149. Update target information
  150. Can be created by `query_update()` or manually.
  151. Attributes:
  152. tag The release tag that will be updated to. If from query_update,
  153. the value is after API resolution and update spec processing.
  154. The only property that is required.
  155. version The actual numeric version (if available) of the binary to be updated to,
  156. after API resolution and update spec processing. (default: None)
  157. requested_version Numeric version of the binary being requested (if available),
  158. after API resolution only. (default: None)
  159. commit Commit hash (if available) of the binary to be updated to,
  160. after API resolution and update spec processing. (default: None)
  161. This value will only match the RELEASE_GIT_HEAD of prerelease builds.
  162. binary_name Filename of the binary to be updated to. (default: current binary name)
  163. checksum Expected checksum (if available) of the binary to be
  164. updated to. (default: None)
  165. """
  166. tag: str
  167. version: str | None = None
  168. requested_version: str | None = None
  169. commit: str | None = None
  170. binary_name: str | None = _get_binary_name() # noqa: RUF009: Always returns the same value
  171. checksum: str | None = None
  172. class Updater:
  173. # XXX: use class variables to simplify testing
  174. _channel = CHANNEL
  175. _origin = ORIGIN
  176. _update_sources = UPDATE_SOURCES
  177. def __init__(self, ydl, target: str | None = None):
  178. self.ydl = ydl
  179. # For backwards compat, target needs to be treated as if it could be None
  180. self.requested_channel, sep, self.requested_tag = (target or self._channel).rpartition('@')
  181. # Check if requested_tag is actually the requested repo/channel
  182. if not sep and ('/' in self.requested_tag or self.requested_tag in self._update_sources):
  183. self.requested_channel = self.requested_tag
  184. self.requested_tag: str = None # type: ignore (we set it later)
  185. elif not self.requested_channel:
  186. # User did not specify a channel, so we are requesting the default channel
  187. self.requested_channel = self._channel.partition('@')[0]
  188. # --update should not be treated as an exact tag request even if CHANNEL has a @tag
  189. self._exact = bool(target) and target != self._channel
  190. if not self.requested_tag:
  191. # User did not specify a tag, so we request 'latest' and track that no exact tag was passed
  192. self.requested_tag = 'latest'
  193. self._exact = False
  194. if '/' in self.requested_channel:
  195. # requested_channel is actually a repository
  196. self.requested_repo = self.requested_channel
  197. if not self.requested_repo.startswith('yt-dlp/') and self.requested_repo != self._origin:
  198. self.ydl.report_warning(
  199. f'You are switching to an {self.ydl._format_err("unofficial", "red")} executable '
  200. f'from {self.ydl._format_err(self.requested_repo, self.ydl.Styles.EMPHASIS)}. '
  201. f'Run {self.ydl._format_err("at your own risk", "light red")}')
  202. self._block_restart('Automatically restarting into custom builds is disabled for security reasons')
  203. else:
  204. # Check if requested_channel resolves to a known repository or else raise
  205. self.requested_repo = self._update_sources.get(self.requested_channel)
  206. if not self.requested_repo:
  207. self._report_error(
  208. f'Invalid update channel {self.requested_channel!r} requested. '
  209. f'Valid channels are {", ".join(self._update_sources)}', True)
  210. self._identifier = f'{detect_variant()} {system_identifier()}'
  211. @property
  212. def current_version(self):
  213. """Current version"""
  214. return __version__
  215. @property
  216. def current_commit(self):
  217. """Current commit hash"""
  218. return RELEASE_GIT_HEAD
  219. def _download_asset(self, name, tag=None):
  220. if not tag:
  221. tag = self.requested_tag
  222. path = 'latest/download' if tag == 'latest' else f'download/{tag}'
  223. url = f'https://github.com/{self.requested_repo}/releases/{path}/{name}'
  224. self.ydl.write_debug(f'Downloading {name} from {url}')
  225. return self.ydl.urlopen(url).read()
  226. def _call_api(self, tag):
  227. tag = f'tags/{tag}' if tag != 'latest' else tag
  228. url = f'{API_BASE_URL}/{self.requested_repo}/releases/{tag}'
  229. self.ydl.write_debug(f'Fetching release info: {url}')
  230. return json.loads(self.ydl.urlopen(Request(url, headers={
  231. 'Accept': 'application/vnd.github+json',
  232. 'User-Agent': 'yt-dlp',
  233. 'X-GitHub-Api-Version': '2022-11-28',
  234. })).read().decode())
  235. def _get_version_info(self, tag: str) -> tuple[str | None, str | None]:
  236. if _VERSION_RE.fullmatch(tag):
  237. return tag, None
  238. api_info = self._call_api(tag)
  239. if tag == 'latest':
  240. requested_version = api_info['tag_name']
  241. else:
  242. match = re.search(rf'\s+(?P<version>{_VERSION_RE.pattern})$', api_info.get('name', ''))
  243. requested_version = match.group('version') if match else None
  244. if re.fullmatch(_HASH_PATTERN, api_info.get('target_commitish', '')):
  245. target_commitish = api_info['target_commitish']
  246. else:
  247. match = _COMMIT_RE.match(api_info.get('body', ''))
  248. target_commitish = match.group('hash') if match else None
  249. if not (requested_version or target_commitish):
  250. self._report_error('One of either version or commit hash must be available on the release', expected=True)
  251. return requested_version, target_commitish
  252. def _download_update_spec(self, source_tags):
  253. for tag in source_tags:
  254. try:
  255. return self._download_asset('_update_spec', tag=tag).decode()
  256. except network_exceptions as error:
  257. if isinstance(error, HTTPError) and error.status == 404:
  258. continue
  259. self._report_network_error(f'fetch update spec: {error}')
  260. return None
  261. self._report_error(
  262. f'The requested tag {self.requested_tag} does not exist for {self.requested_repo}', True)
  263. return None
  264. def _process_update_spec(self, lockfile: str, resolved_tag: str):
  265. lines = lockfile.splitlines()
  266. is_version2 = any(line.startswith('lockV2 ') for line in lines)
  267. for line in lines:
  268. if is_version2:
  269. if not line.startswith(f'lockV2 {self.requested_repo} '):
  270. continue
  271. _, _, tag, pattern = line.split(' ', 3)
  272. else:
  273. if not line.startswith('lock '):
  274. continue
  275. _, tag, pattern = line.split(' ', 2)
  276. if re.match(pattern, self._identifier):
  277. if _VERSION_RE.fullmatch(tag):
  278. if not self._exact:
  279. return tag
  280. elif self._version_compare(tag, resolved_tag):
  281. return resolved_tag
  282. elif tag != resolved_tag:
  283. continue
  284. self._report_error(
  285. f'yt-dlp cannot be updated to {resolved_tag} since you are on an older Python version '
  286. 'or your operating system is not compatible with the requested build', True)
  287. return None
  288. return resolved_tag
  289. def _version_compare(self, a: str, b: str):
  290. """
  291. Compare two version strings
  292. This function SHOULD NOT be called if self._exact == True
  293. """
  294. if _VERSION_RE.fullmatch(f'{a}.{b}'):
  295. return version_tuple(a) >= version_tuple(b)
  296. return a == b
  297. def query_update(self, *, _output=False) -> UpdateInfo | None:
  298. """Fetches info about the available update
  299. @returns An `UpdateInfo` if there is an update available, else None
  300. """
  301. if not self.requested_repo:
  302. self._report_error('No target repository could be determined from input')
  303. return None
  304. try:
  305. requested_version, target_commitish = self._get_version_info(self.requested_tag)
  306. except network_exceptions as e:
  307. self._report_network_error(f'obtain version info ({e})', delim='; Please try again later or')
  308. return None
  309. if self._exact and self._origin != self.requested_repo:
  310. has_update = True
  311. elif requested_version:
  312. if self._exact:
  313. has_update = self.current_version != requested_version
  314. else:
  315. has_update = not self._version_compare(self.current_version, requested_version)
  316. elif target_commitish:
  317. has_update = target_commitish != self.current_commit
  318. else:
  319. has_update = False
  320. resolved_tag = requested_version if self.requested_tag == 'latest' else self.requested_tag
  321. current_label = _make_label(self._origin, self._channel.partition('@')[2] or self.current_version, self.current_version)
  322. requested_label = _make_label(self.requested_repo, resolved_tag, requested_version)
  323. latest_or_requested = f'{"Latest" if self.requested_tag == "latest" else "Requested"} version: {requested_label}'
  324. if not has_update:
  325. if _output:
  326. self.ydl.to_screen(f'{latest_or_requested}\nyt-dlp is up to date ({current_label})')
  327. return None
  328. update_spec = self._download_update_spec(('latest', None) if requested_version else (None,))
  329. if not update_spec:
  330. return None
  331. # `result_` prefixed vars == post-_process_update_spec() values
  332. result_tag = self._process_update_spec(update_spec, resolved_tag)
  333. if not result_tag or result_tag == self.current_version:
  334. return None
  335. elif result_tag == resolved_tag:
  336. result_version = requested_version
  337. elif _VERSION_RE.fullmatch(result_tag):
  338. result_version = result_tag
  339. else: # actual version being updated to is unknown
  340. result_version = None
  341. checksum = None
  342. # Non-updateable variants can get update_info but need to skip checksum
  343. if not is_non_updateable():
  344. try:
  345. hashes = self._download_asset('SHA2-256SUMS', result_tag)
  346. except network_exceptions as error:
  347. if not isinstance(error, HTTPError) or error.status != 404:
  348. self._report_network_error(f'fetch checksums: {error}')
  349. return None
  350. self.ydl.report_warning('No hash information found for the release, skipping verification')
  351. else:
  352. for ln in hashes.decode().splitlines():
  353. if ln.endswith(_get_binary_name()):
  354. checksum = ln.split()[0]
  355. break
  356. if not checksum:
  357. self.ydl.report_warning('The hash could not be found in the checksum file, skipping verification')
  358. if _output:
  359. update_label = _make_label(self.requested_repo, result_tag, result_version)
  360. self.ydl.to_screen(
  361. f'Current version: {current_label}\n{latest_or_requested}'
  362. + (f'\nUpgradable to: {update_label}' if update_label != requested_label else ''))
  363. return UpdateInfo(
  364. tag=result_tag,
  365. version=result_version,
  366. requested_version=requested_version,
  367. commit=target_commitish if result_tag == resolved_tag else None,
  368. checksum=checksum)
  369. def update(self, update_info=NO_DEFAULT):
  370. """Update yt-dlp executable to the latest version
  371. @param update_info `UpdateInfo | None` as returned by query_update()
  372. """
  373. if update_info is NO_DEFAULT:
  374. update_info = self.query_update(_output=True)
  375. if not update_info:
  376. return False
  377. err = is_non_updateable()
  378. if err:
  379. self._report_error(err, True)
  380. return False
  381. self.ydl.to_screen(f'Current Build Hash: {_sha256_file(self.filename)}')
  382. update_label = _make_label(self.requested_repo, update_info.tag, update_info.version)
  383. self.ydl.to_screen(f'Updating to {update_label} ...')
  384. directory = os.path.dirname(self.filename)
  385. if not os.access(self.filename, os.W_OK):
  386. return self._report_permission_error(self.filename)
  387. elif not os.access(directory, os.W_OK):
  388. return self._report_permission_error(directory)
  389. new_filename, old_filename = f'{self.filename}.new', f'{self.filename}.old'
  390. if detect_variant() == 'zip': # Can be replaced in-place
  391. new_filename, old_filename = self.filename, None
  392. try:
  393. if os.path.exists(old_filename or ''):
  394. os.remove(old_filename)
  395. except OSError:
  396. return self._report_error('Unable to remove the old version')
  397. try:
  398. newcontent = self._download_asset(update_info.binary_name, update_info.tag)
  399. except network_exceptions as e:
  400. if isinstance(e, HTTPError) and e.status == 404:
  401. return self._report_error(
  402. f'The requested tag {self.requested_repo}@{update_info.tag} does not exist', True)
  403. return self._report_network_error(f'fetch updates: {e}', tag=update_info.tag)
  404. if not update_info.checksum:
  405. self._block_restart('Automatically restarting into unverified builds is disabled for security reasons')
  406. elif hashlib.sha256(newcontent).hexdigest() != update_info.checksum:
  407. return self._report_network_error('verify the new executable', tag=update_info.tag)
  408. try:
  409. with open(new_filename, 'wb') as outf:
  410. outf.write(newcontent)
  411. except OSError:
  412. return self._report_permission_error(new_filename)
  413. if old_filename:
  414. mask = os.stat(self.filename).st_mode
  415. try:
  416. os.rename(self.filename, old_filename)
  417. except OSError:
  418. return self._report_error('Unable to move current version')
  419. try:
  420. os.rename(new_filename, self.filename)
  421. except OSError:
  422. self._report_error('Unable to overwrite current version')
  423. return os.rename(old_filename, self.filename)
  424. variant = detect_variant()
  425. if variant.startswith('win'):
  426. atexit.register(Popen, f'ping 127.0.0.1 -n 5 -w 1000 & del /F "{old_filename}"',
  427. shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  428. elif old_filename:
  429. try:
  430. os.remove(old_filename)
  431. except OSError:
  432. self._report_error('Unable to remove the old version')
  433. try:
  434. os.chmod(self.filename, mask)
  435. except OSError:
  436. return self._report_error(
  437. f'Unable to set permissions. Run: sudo chmod a+rx {shell_quote(self.filename)}')
  438. self.ydl.to_screen(f'Updated yt-dlp to {update_label}')
  439. return True
  440. @functools.cached_property
  441. def filename(self):
  442. """Filename of the executable"""
  443. return os.path.realpath(_get_variant_and_executable_path()[1])
  444. @functools.cached_property
  445. def cmd(self):
  446. """The command-line to run the executable, if known"""
  447. argv = None
  448. # There is no sys.orig_argv in py < 3.10. Also, it can be [] when frozen
  449. if getattr(sys, 'orig_argv', None):
  450. argv = sys.orig_argv
  451. elif getattr(sys, 'frozen', False):
  452. argv = sys.argv
  453. # linux_static exe's argv[0] will be /tmp/staticx-NNNN/yt-dlp_linux if we don't fixup here
  454. if argv and os.getenv('STATICX_PROG_PATH'):
  455. argv = [self.filename, *argv[1:]]
  456. return argv
  457. def restart(self):
  458. """Restart the executable"""
  459. assert self.cmd, 'Must be frozen or Py >= 3.10'
  460. self.ydl.write_debug(f'Restarting: {shell_quote(self.cmd)}')
  461. _, _, returncode = Popen.run(self.cmd)
  462. return returncode
  463. def _block_restart(self, msg):
  464. def wrapper():
  465. self._report_error(f'{msg}. Restart yt-dlp to use the updated version', expected=True)
  466. return self.ydl._download_retcode
  467. self.restart = wrapper
  468. def _report_error(self, msg, expected=False):
  469. self.ydl.report_error(msg, tb=False if expected else None)
  470. self.ydl._download_retcode = 100
  471. def _report_permission_error(self, file):
  472. self._report_error(f'Unable to write to {file}; try running as administrator', True)
  473. def _report_network_error(self, action, delim=';', tag=None):
  474. if not tag:
  475. tag = self.requested_tag
  476. path = tag if tag == 'latest' else f'tag/{tag}'
  477. self._report_error(
  478. f'Unable to {action}{delim} visit '
  479. f'https://github.com/{self.requested_repo}/releases/{path}', True)
  480. def run_update(ydl):
  481. """Update the program file with the latest version from the repository
  482. @returns Whether there was a successful update (No update = False)
  483. """
  484. deprecation_warning(
  485. '"yt_dlp.update.run_update(ydl)" is deprecated and may be removed in a future version. '
  486. 'Use "yt_dlp.update.Updater(ydl).update()" instead')
  487. return Updater(ydl).update()
  488. __all__ = ['Updater']