update.py 23 KB

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