logging_util.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. __package__ = 'archivebox'
  2. import re
  3. import os
  4. import sys
  5. import stat
  6. import time
  7. from math import log
  8. from multiprocessing import Process
  9. from pathlib import Path
  10. from datetime import datetime, timezone
  11. from dataclasses import dataclass
  12. from typing import Any, Optional, List, Dict, Union, Iterable, IO, TYPE_CHECKING
  13. if TYPE_CHECKING:
  14. from ..index.schema import Link, ArchiveResult
  15. from rich import print
  16. from rich.panel import Panel
  17. from rich_argparse import RichHelpFormatter
  18. from django.core.management.base import DjangoHelpFormatter
  19. from archivebox.config import CONSTANTS, DATA_DIR, VERSION
  20. from archivebox.config.common import SHELL_CONFIG
  21. from archivebox.misc.system import get_dir_size
  22. from archivebox.misc.util import enforce_types
  23. from archivebox.misc.logging import ANSI, stderr
  24. @dataclass
  25. class RuntimeStats:
  26. """mutable stats counter for logging archiving timing info to CLI output"""
  27. skipped: int = 0
  28. succeeded: int = 0
  29. failed: int = 0
  30. parse_start_ts: Optional[datetime] = None
  31. parse_end_ts: Optional[datetime] = None
  32. index_start_ts: Optional[datetime] = None
  33. index_end_ts: Optional[datetime] = None
  34. archiving_start_ts: Optional[datetime] = None
  35. archiving_end_ts: Optional[datetime] = None
  36. # globals are bad, mmkay
  37. _LAST_RUN_STATS = RuntimeStats()
  38. def debug_dict_summary(obj: Dict[Any, Any]) -> None:
  39. stderr(' '.join(f'{key}={str(val).ljust(6)}' for key, val in obj.items()))
  40. def get_fd_info(fd) -> Dict[str, Any]:
  41. NAME = fd.name[1:-1]
  42. FILENO = fd.fileno()
  43. MODE = os.fstat(FILENO).st_mode
  44. IS_TTY = hasattr(fd, 'isatty') and fd.isatty()
  45. IS_PIPE = stat.S_ISFIFO(MODE)
  46. IS_FILE = stat.S_ISREG(MODE)
  47. IS_TERMINAL = not (IS_PIPE or IS_FILE)
  48. IS_LINE_BUFFERED = fd.line_buffering
  49. IS_READABLE = fd.readable()
  50. return {
  51. 'NAME': NAME, 'FILENO': FILENO, 'MODE': MODE,
  52. 'IS_TTY': IS_TTY, 'IS_PIPE': IS_PIPE, 'IS_FILE': IS_FILE,
  53. 'IS_TERMINAL': IS_TERMINAL, 'IS_LINE_BUFFERED': IS_LINE_BUFFERED,
  54. 'IS_READABLE': IS_READABLE,
  55. }
  56. # # Log debug information about stdin, stdout, and stderr
  57. # sys.stdout.write('[>&1] this is python stdout\n')
  58. # sys.stderr.write('[>&2] this is python stderr\n')
  59. # debug_dict_summary(get_fd_info(sys.stdin))
  60. # debug_dict_summary(get_fd_info(sys.stdout))
  61. # debug_dict_summary(get_fd_info(sys.stderr))
  62. class SmartFormatter(DjangoHelpFormatter, RichHelpFormatter):
  63. """Patched formatter that prints newlines in argparse help strings"""
  64. def _split_lines(self, text, width):
  65. if '\n' in text:
  66. return text.splitlines()
  67. return RichHelpFormatter._split_lines(self, text, width)
  68. def reject_stdin(caller: str, stdin: Optional[IO]=sys.stdin) -> None:
  69. """Tell the user they passed stdin to a command that doesn't accept it"""
  70. if not stdin:
  71. return None
  72. if os.environ.get('IN_DOCKER') in ('1', 'true', 'True', 'TRUE', 'yes'):
  73. # when TTY is disabled in docker we cant tell if stdin is being piped in or not
  74. # if we try to read stdin when its not piped we will hang indefinitely waiting for it
  75. return None
  76. if not stdin.isatty():
  77. # stderr('READING STDIN TO REJECT...')
  78. stdin_raw_text = stdin.read()
  79. if stdin_raw_text.strip():
  80. # stderr('GOT STDIN!', len(stdin_str))
  81. stderr(f'[!] The "{caller}" command does not accept stdin (ignoring).', color='red')
  82. stderr(f' Run archivebox "{caller} --help" to see usage and examples.')
  83. stderr()
  84. # raise SystemExit(1)
  85. return None
  86. def accept_stdin(stdin: Optional[IO]=sys.stdin) -> Optional[str]:
  87. """accept any standard input and return it as a string or None"""
  88. if not stdin:
  89. return None
  90. if not stdin.isatty():
  91. # stderr('READING STDIN TO ACCEPT...')
  92. stdin_str = stdin.read()
  93. if stdin_str:
  94. # stderr('GOT STDIN...', len(stdin_str))
  95. return stdin_str
  96. return None
  97. class TimedProgress:
  98. """Show a progress bar and measure elapsed time until .end() is called"""
  99. def __init__(self, seconds, prefix=''):
  100. self.SHOW_PROGRESS = SHELL_CONFIG.SHOW_PROGRESS
  101. self.ANSI = SHELL_CONFIG.ANSI
  102. if self.SHOW_PROGRESS:
  103. self.p = Process(target=progress_bar, args=(seconds, prefix, self.ANSI))
  104. self.p.start()
  105. self.stats = {'start_ts': datetime.now(timezone.utc), 'end_ts': None}
  106. def end(self):
  107. """immediately end progress, clear the progressbar line, and save end_ts"""
  108. end_ts = datetime.now(timezone.utc)
  109. self.stats['end_ts'] = end_ts
  110. if self.SHOW_PROGRESS:
  111. # terminate if we havent already terminated
  112. try:
  113. # kill the progress bar subprocess
  114. try:
  115. self.p.close() # must be closed *before* its terminnated
  116. except (KeyboardInterrupt, SystemExit):
  117. print()
  118. raise
  119. except BaseException: # lgtm [py/catch-base-exception]
  120. pass
  121. self.p.terminate()
  122. self.p.join()
  123. # clear whole terminal line
  124. try:
  125. sys.stdout.write('\r{}{}\r'.format((' ' * SHELL_CONFIG.TERM_WIDTH), self.ANSI['reset']))
  126. except (IOError, BrokenPipeError):
  127. # ignore when the parent proc has stopped listening to our stdout
  128. pass
  129. except ValueError:
  130. pass
  131. @enforce_types
  132. def progress_bar(seconds: int, prefix: str='', ANSI: Dict[str, str]=ANSI) -> None:
  133. """show timer in the form of progress bar, with percentage and seconds remaining"""
  134. output_buf = (sys.stdout or sys.__stdout__ or sys.stderr or sys.__stderr__)
  135. chunk = '█' if output_buf and output_buf.encoding.upper() == 'UTF-8' else '#'
  136. last_width = SHELL_CONFIG.TERM_WIDTH
  137. chunks = last_width - len(prefix) - 20 # number of progress chunks to show (aka max bar width)
  138. try:
  139. for s in range(seconds * chunks):
  140. max_width = SHELL_CONFIG.TERM_WIDTH
  141. if max_width < last_width:
  142. # when the terminal size is shrunk, we have to write a newline
  143. # otherwise the progress bar will keep wrapping incorrectly
  144. sys.stdout.write('\r\n')
  145. sys.stdout.flush()
  146. chunks = max_width - len(prefix) - 20
  147. pct_complete = s / chunks / seconds * 100
  148. log_pct = (log(pct_complete or 1, 10) / 2) * 100 # everyone likes faster progress bars ;)
  149. bar_width = round(log_pct/(100/chunks))
  150. last_width = max_width
  151. # ████████████████████ 0.9% (1/60sec)
  152. sys.stdout.write('\r{0}{1}{2}{3} {4}% ({5}/{6}sec)'.format(
  153. prefix,
  154. ANSI['green' if pct_complete < 80 else 'lightyellow'],
  155. (chunk * bar_width).ljust(chunks),
  156. ANSI['reset'],
  157. round(pct_complete, 1),
  158. round(s/chunks),
  159. seconds,
  160. ))
  161. sys.stdout.flush()
  162. time.sleep(1 / chunks)
  163. # ██████████████████████████████████ 100.0% (60/60sec)
  164. sys.stdout.write('\r{0}{1}{2}{3} {4}% ({5}/{6}sec)'.format(
  165. prefix,
  166. ANSI['red'],
  167. chunk * chunks,
  168. ANSI['reset'],
  169. 100.0,
  170. seconds,
  171. seconds,
  172. ))
  173. sys.stdout.flush()
  174. # uncomment to have it disappear when it hits 100% instead of staying full red:
  175. # time.sleep(0.5)
  176. # sys.stdout.write('\r{}{}\r'.format((' ' * SHELL_CONFIG.TERM_WIDTH), ANSI['reset']))
  177. # sys.stdout.flush()
  178. except (KeyboardInterrupt, BrokenPipeError):
  179. print()
  180. def log_cli_command(subcommand: str, subcommand_args: Iterable[str]=(), stdin: str | IO | None=None, pwd: str='.'):
  181. args = ' '.join(subcommand_args)
  182. version_msg = '[dark_magenta]\\[{now}][/dark_magenta] [dark_red]ArchiveBox[/dark_red] [dark_goldenrod]v{VERSION}[/dark_goldenrod]: [green4]archivebox [green3]{subcommand}[green2] {args}[/green2]'.format(
  183. now=datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S'),
  184. VERSION=VERSION,
  185. subcommand=subcommand,
  186. args=args,
  187. )
  188. # stderr()
  189. # stderr('[bright_black] > {pwd}[/]'.format(pwd=pwd, **ANSI))
  190. # stderr()
  191. print(Panel(version_msg), file=sys.stderr)
  192. ### Parsing Stage
  193. def log_importing_started(urls: Union[str, List[str]], depth: int, index_only: bool):
  194. _LAST_RUN_STATS.parse_start_ts = datetime.now(timezone.utc)
  195. print('[green][+] [{}] Adding {} links to index (crawl depth={}){}...[/]'.format(
  196. _LAST_RUN_STATS.parse_start_ts.strftime('%Y-%m-%d %H:%M:%S'),
  197. len(urls) if isinstance(urls, list) else len(urls.split('\n')),
  198. depth,
  199. ' (index only)' if index_only else '',
  200. ))
  201. def log_source_saved(source_file: str):
  202. print(' > Saved verbatim input to {}/{}'.format(CONSTANTS.SOURCES_DIR_NAME, source_file.rsplit('/', 1)[-1]))
  203. def log_parsing_finished(num_parsed: int, parser_name: str):
  204. _LAST_RUN_STATS.parse_end_ts = datetime.now(timezone.utc)
  205. print(' > Parsed {} URLs from input ({})'.format(num_parsed, parser_name))
  206. def log_deduping_finished(num_new_links: int):
  207. print(' > Found {} new URLs not already in index'.format(num_new_links))
  208. def log_crawl_started(new_links):
  209. print()
  210. print(f'[green][*] Starting crawl of {len(new_links)} sites 1 hop out from starting point[/]')
  211. ### Indexing Stage
  212. def log_indexing_process_started(num_links: int):
  213. start_ts = datetime.now(timezone.utc)
  214. _LAST_RUN_STATS.index_start_ts = start_ts
  215. print()
  216. print('[bright_black][*] [{}] Writing {} links to main index...[/]'.format(
  217. start_ts.strftime('%Y-%m-%d %H:%M:%S'),
  218. num_links,
  219. ))
  220. def log_indexing_process_finished():
  221. end_ts = datetime.now(timezone.utc)
  222. _LAST_RUN_STATS.index_end_ts = end_ts
  223. def log_indexing_started(out_path: str):
  224. if SHELL_CONFIG.IS_TTY:
  225. sys.stdout.write(f' > ./{Path(out_path).relative_to(DATA_DIR)}')
  226. def log_indexing_finished(out_path: str):
  227. print(f'\r √ ./{Path(out_path).relative_to(DATA_DIR)}')
  228. ### Archiving Stage
  229. def log_archiving_started(num_links: int, resume: Optional[float]=None):
  230. start_ts = datetime.now(timezone.utc)
  231. _LAST_RUN_STATS.archiving_start_ts = start_ts
  232. print()
  233. if resume:
  234. print('[green][▶] [{}] Resuming archive updating for {} pages starting from {}...[/]'.format(
  235. start_ts.strftime('%Y-%m-%d %H:%M:%S'),
  236. num_links,
  237. resume,
  238. ))
  239. else:
  240. print('[green][▶] [{}] Starting archiving of {} snapshots in index...[/]'.format(
  241. start_ts.strftime('%Y-%m-%d %H:%M:%S'),
  242. num_links,
  243. ))
  244. def log_archiving_paused(num_links: int, idx: int, timestamp: str):
  245. end_ts = datetime.now(timezone.utc)
  246. _LAST_RUN_STATS.archiving_end_ts = end_ts
  247. print()
  248. print('\n[yellow3][X] [{now}] Downloading paused on link {timestamp} ({idx}/{total})[/]'.format(
  249. now=end_ts.strftime('%Y-%m-%d %H:%M:%S'),
  250. idx=idx+1,
  251. timestamp=timestamp,
  252. total=num_links,
  253. ))
  254. print()
  255. print(' Continue archiving where you left off by running:')
  256. print(' archivebox update --resume={}'.format(timestamp))
  257. def log_archiving_finished(num_links: int):
  258. from core.models import Snapshot
  259. end_ts = datetime.now(timezone.utc)
  260. _LAST_RUN_STATS.archiving_end_ts = end_ts
  261. assert _LAST_RUN_STATS.archiving_start_ts is not None
  262. seconds = end_ts.timestamp() - _LAST_RUN_STATS.archiving_start_ts.timestamp()
  263. if seconds > 60:
  264. duration = '{0:.2f} min'.format(seconds / 60)
  265. else:
  266. duration = '{0:.2f} sec'.format(seconds)
  267. print()
  268. print('[green][√] [{}] Update of {} pages complete ({})[/]'.format(
  269. end_ts.strftime('%Y-%m-%d %H:%M:%S'),
  270. num_links,
  271. duration,
  272. ))
  273. print(' - {} links skipped'.format(_LAST_RUN_STATS.skipped))
  274. print(' - {} links updated'.format(_LAST_RUN_STATS.succeeded + _LAST_RUN_STATS.failed))
  275. print(' - {} links had errors'.format(_LAST_RUN_STATS.failed))
  276. if Snapshot.objects.count() < 50:
  277. print()
  278. print(' [violet]Hint:[/] To manage your archive in a Web UI, run:')
  279. print(' archivebox server 0.0.0.0:8000')
  280. def log_link_archiving_started(link: "Link", link_dir: str, is_new: bool):
  281. # [*] [2019-03-22 13:46:45] "Log Structured Merge Trees - ben stopford"
  282. # http://www.benstopford.com/2015/02/14/log-structured-merge-trees/
  283. # > output/archive/1478739709
  284. print('\n[[{symbol_color}]{symbol}[/]] [[{symbol_color}]{now}[/]] "{title}"'.format(
  285. symbol_color='green' if is_new else 'bright_black',
  286. symbol='+' if is_new else '√',
  287. now=datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S'),
  288. title=link.title or link.base_url,
  289. ))
  290. print(f' [sky_blue1]{link.url}[/]')
  291. print(' {} {}'.format(
  292. '>' if is_new else '√',
  293. pretty_path(link_dir),
  294. ))
  295. def log_link_archiving_finished(link: "Link", link_dir: str, is_new: bool, stats: dict, start_ts: datetime):
  296. total = sum(stats.values())
  297. if stats['failed'] > 0 :
  298. _LAST_RUN_STATS.failed += 1
  299. elif stats['skipped'] == total:
  300. _LAST_RUN_STATS.skipped += 1
  301. else:
  302. _LAST_RUN_STATS.succeeded += 1
  303. try:
  304. size = get_dir_size(link_dir)
  305. except FileNotFoundError:
  306. size = (0, None, '0')
  307. end_ts = datetime.now(timezone.utc)
  308. duration = str(end_ts - start_ts).split('.')[0]
  309. print(' [bright_black]{} files ({}) in {}s [/]'.format(size[2], printable_filesize(size[0]), duration))
  310. def log_archive_method_started(method: str):
  311. print(' > {}'.format(method))
  312. def log_archive_method_finished(result: "ArchiveResult"):
  313. """
  314. quote the argument with whitespace in a command so the user can
  315. copy-paste the outputted string directly to run the cmd
  316. """
  317. # Prettify CMD string and make it safe to copy-paste by quoting arguments
  318. quoted_cmd = ' '.join(
  319. '"{}"'.format(arg) if (' ' in arg) or (':' in arg) else arg
  320. for arg in result.cmd
  321. )
  322. if result.status == 'failed':
  323. if result.output.__class__.__name__ == 'TimeoutExpired':
  324. duration = (result.end_ts - result.start_ts).seconds
  325. hint_header = [
  326. f'[yellow3]Extractor timed out after {duration}s.[/]',
  327. ]
  328. else:
  329. error_name = result.output.__class__.__name__.replace('ArchiveError', '')
  330. hint_header = [
  331. '[yellow3]Extractor failed:[/]',
  332. f' {error_name} [red1]{result.output}[/]',
  333. ]
  334. # import pudb; pudb.set_trace()
  335. # Prettify error output hints string and limit to five lines
  336. hints = getattr(result.output, 'hints', None) or ()
  337. if hints:
  338. if isinstance(hints, (list, tuple, type(_ for _ in ()))):
  339. hints = [hint.decode() if isinstance(hint, bytes) else str(hint) for hint in hints]
  340. else:
  341. if isinstance(hints, bytes):
  342. hints = hints.decode()
  343. hints = hints.split('\n')
  344. hints = (
  345. f' [yellow1]{line.strip()}[/]'
  346. for line in list(hints)[:5] if line.strip()
  347. )
  348. docker_hints = ()
  349. if os.environ.get('IN_DOCKER') in ('1', 'true', 'True', 'TRUE', 'yes'):
  350. docker_hints = (
  351. ' docker run -it -v $PWD/data:/data archivebox/archivebox /bin/bash',
  352. )
  353. # Collect and prefix output lines with indentation
  354. output_lines = [
  355. *hint_header,
  356. *hints,
  357. '[violet]Run to see full output:[/]',
  358. *docker_hints,
  359. *([' cd {};'.format(result.pwd)] if result.pwd else []),
  360. ' {}'.format(quoted_cmd),
  361. ]
  362. print('\n'.join(
  363. ' {}'.format(line)
  364. for line in output_lines
  365. if line
  366. ))
  367. print()
  368. def log_list_started(filter_patterns: Optional[List[str]], filter_type: str):
  369. print(f'[green][*] Finding links in the archive index matching these {filter_type} patterns:[/]')
  370. print(' {}'.format(' '.join(filter_patterns or ())))
  371. def log_list_finished(links):
  372. from ..index.csv import links_to_csv
  373. print()
  374. print('---------------------------------------------------------------------------------------------------')
  375. print(links_to_csv(links, cols=['timestamp', 'is_archived', 'num_outputs', 'url'], header=True, ljust=16, separator=' | '))
  376. print('---------------------------------------------------------------------------------------------------')
  377. print()
  378. def log_removal_started(links: List["Link"], yes: bool, delete: bool):
  379. print(f'[yellow3][i] Found {len(links)} matching URLs to remove.[/]')
  380. if delete:
  381. file_counts = [link.num_outputs for link in links if os.access(link.link_dir, os.R_OK)]
  382. print(
  383. f' {len(links)} Links will be de-listed from the main index, and their archived content folders will be deleted from disk.\n'
  384. f' ({len(file_counts)} data folders with {sum(file_counts)} archived files will be deleted!)'
  385. )
  386. else:
  387. print(
  388. ' Matching links will be de-listed from the main index, but their archived content folders will remain in place on disk.\n'
  389. ' (Pass --delete if you also want to permanently delete the data folders)'
  390. )
  391. if not yes:
  392. print()
  393. print('[yellow3][?] Do you want to proceed with removing these {len(links)} links?[/]')
  394. try:
  395. assert input(' y/[n]: ').lower() == 'y'
  396. except (KeyboardInterrupt, EOFError, AssertionError):
  397. raise SystemExit(0)
  398. def log_removal_finished(all_links: int, to_remove: int):
  399. if all_links == 0:
  400. print()
  401. print('[red1][X] No matching links found.[/]')
  402. else:
  403. print()
  404. print(f'[red1][√] Removed {to_remove} out of {all_links} links from the archive index.[/]')
  405. print(f' Index now contains {all_links - to_remove} links.')
  406. ### Helpers
  407. @enforce_types
  408. def pretty_path(path: Union[Path, str], pwd: Union[Path, str]=DATA_DIR, color: bool=True) -> str:
  409. """convert paths like .../ArchiveBox/archivebox/../output/abc into output/abc"""
  410. pwd = str(Path(pwd)) # .resolve()
  411. path = str(path)
  412. if not path:
  413. return path
  414. # replace long absolute paths with ./ relative ones to save on terminal output width
  415. if path.startswith(pwd) and (pwd != '/') and path != pwd:
  416. if color:
  417. path = path.replace(pwd, '[light_slate_blue].[/light_slate_blue]', 1)
  418. else:
  419. path = path.replace(pwd, '.', 1)
  420. # quote paths containing spaces
  421. if ' ' in path:
  422. path = f'"{path}"'
  423. # replace home directory with ~ for shorter output
  424. path = path.replace(str(Path('~').expanduser()), '~')
  425. return path
  426. @enforce_types
  427. def printable_filesize(num_bytes: Union[int, float]) -> str:
  428. for count in ['Bytes','KB','MB','GB']:
  429. if num_bytes > -1024.0 and num_bytes < 1024.0:
  430. return '%3.1f %s' % (num_bytes, count)
  431. num_bytes /= 1024.0
  432. return '%3.1f %s' % (num_bytes, 'TB')
  433. @enforce_types
  434. def printable_folders(folders: Dict[str, Optional["Link"]], with_headers: bool=False) -> str:
  435. return '\n'.join(
  436. f'{folder} {link and link.url} "{link and link.title}"'
  437. for folder, link in folders.items()
  438. )
  439. @enforce_types
  440. def printable_config(config: dict, prefix: str='') -> str:
  441. return f'\n{prefix}'.join(
  442. f'{key}={val}'
  443. for key, val in config.items()
  444. if not (isinstance(val, dict) or callable(val))
  445. )
  446. @enforce_types
  447. def printable_folder_status(name: str, folder: Dict) -> str:
  448. if folder['enabled']:
  449. if folder['is_valid']:
  450. color, symbol, note, num_files = 'green', '√', 'valid', ''
  451. else:
  452. color, symbol, note, num_files = 'red', 'X', 'invalid', '?'
  453. else:
  454. color, symbol, note, num_files = 'grey53', '-', 'unused', '-'
  455. if folder['path']:
  456. if os.access(folder['path'], os.R_OK):
  457. try:
  458. num_files = (
  459. f'{len(os.listdir(folder["path"]))} files'
  460. if os.path.isdir(folder['path']) else
  461. printable_filesize(Path(folder['path']).stat().st_size)
  462. )
  463. except PermissionError:
  464. num_files = 'error'
  465. else:
  466. num_files = 'missing'
  467. if folder.get('is_mount'):
  468. # add symbol @ next to filecount if path is a remote filesystem mount
  469. num_files = f'{num_files} @' if num_files else '@'
  470. path = pretty_path(folder['path'])
  471. return ' '.join((
  472. f'[{color}]',
  473. symbol,
  474. '[/]',
  475. name.ljust(21).replace('DATA_DIR', '[light_slate_blue]DATA_DIR[/light_slate_blue]'),
  476. num_files.ljust(14).replace('missing', '[grey53]missing[/grey53]'),
  477. f'[{color}]',
  478. note.ljust(8),
  479. '[/]',
  480. path.ljust(76),
  481. ))
  482. @enforce_types
  483. def printable_dependency_version(name: str, dependency: Dict) -> str:
  484. color, symbol, note, version = 'red', 'X', 'invalid', '?'
  485. if dependency['enabled']:
  486. if dependency['is_valid']:
  487. color, symbol, note = 'green', '√', 'valid'
  488. parsed_version_num = re.search(r'[\d\.]+', dependency['version'])
  489. if parsed_version_num:
  490. version = f'v{parsed_version_num[0]}'
  491. else:
  492. color, symbol, note, version = 'lightyellow', '-', 'disabled', '-'
  493. path = pretty_path(dependency['path'])
  494. return ' '.join((
  495. ANSI[color],
  496. symbol,
  497. ANSI['reset'],
  498. name.ljust(21),
  499. version.ljust(14),
  500. ANSI[color],
  501. note.ljust(8),
  502. ANSI['reset'],
  503. path.ljust(76),
  504. ))