config.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. """
  2. ArchiveBox config definitons (including defaults and dynamic config options).
  3. Config Usage Example:
  4. archivebox config --set MEDIA_TIMEOUT=600
  5. env MEDIA_TIMEOUT=600 USE_COLOR=False ... archivebox [subcommand] ...
  6. Config Precedence Order:
  7. 1. cli args (--update-all / --index-only / etc.)
  8. 2. shell environment vars (env USE_COLOR=False archivebox add '...')
  9. 3. config file (echo "SAVE_FAVICON=False" >> ArchiveBox.conf)
  10. 4. defaults (defined below in Python)
  11. Documentation:
  12. https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration
  13. """
  14. __package__ = 'archivebox'
  15. import os
  16. import io
  17. import re
  18. import sys
  19. import json
  20. import getpass
  21. import platform
  22. import shutil
  23. import django
  24. from hashlib import md5
  25. from pathlib import Path
  26. from typing import Optional, Type, Tuple, Dict, Union, List
  27. from subprocess import run, PIPE, DEVNULL
  28. from configparser import ConfigParser
  29. from collections import defaultdict
  30. from .config_stubs import (
  31. SimpleConfigValueDict,
  32. ConfigValue,
  33. ConfigDict,
  34. ConfigDefaultValue,
  35. ConfigDefaultDict,
  36. )
  37. ############################### Config Schema ##################################
  38. CONFIG_SCHEMA: Dict[str, ConfigDefaultDict] = {
  39. 'SHELL_CONFIG': {
  40. 'IS_TTY': {'type': bool, 'default': lambda _: sys.stdout.isatty()},
  41. 'USE_COLOR': {'type': bool, 'default': lambda c: c['IS_TTY']},
  42. 'SHOW_PROGRESS': {'type': bool, 'default': lambda c: (c['IS_TTY'] and platform.system() != 'Darwin')}, # progress bars are buggy on mac, disable for now
  43. 'IN_DOCKER': {'type': bool, 'default': False},
  44. # TODO: 'SHOW_HINTS': {'type: bool, 'default': True},
  45. },
  46. 'GENERAL_CONFIG': {
  47. 'OUTPUT_DIR': {'type': str, 'default': None},
  48. 'CONFIG_FILE': {'type': str, 'default': None},
  49. 'ONLY_NEW': {'type': bool, 'default': True},
  50. 'TIMEOUT': {'type': int, 'default': 60},
  51. 'MEDIA_TIMEOUT': {'type': int, 'default': 3600},
  52. 'OUTPUT_PERMISSIONS': {'type': str, 'default': '755'},
  53. 'RESTRICT_FILE_NAMES': {'type': str, 'default': 'windows'},
  54. 'URL_BLACKLIST': {'type': str, 'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'}, # to avoid downloading code assets as their own pages
  55. },
  56. 'SERVER_CONFIG': {
  57. 'SECRET_KEY': {'type': str, 'default': None},
  58. 'BIND_ADDR': {'type': str, 'default': lambda c: ['127.0.0.1:8000', '0.0.0.0:8000'][c['IN_DOCKER']]},
  59. 'ALLOWED_HOSTS': {'type': str, 'default': '*'},
  60. 'DEBUG': {'type': bool, 'default': False},
  61. 'PUBLIC_INDEX': {'type': bool, 'default': True},
  62. 'PUBLIC_SNAPSHOTS': {'type': bool, 'default': True},
  63. 'PUBLIC_ADD_VIEW': {'type': bool, 'default': False},
  64. 'FOOTER_INFO': {'type': str, 'default': 'Content is hosted for personal archiving purposes only. Contact server owner for any takedown requests.'},
  65. },
  66. 'ARCHIVE_METHOD_TOGGLES': {
  67. 'SAVE_TITLE': {'type': bool, 'default': True, 'aliases': ('FETCH_TITLE',)},
  68. 'SAVE_FAVICON': {'type': bool, 'default': True, 'aliases': ('FETCH_FAVICON',)},
  69. 'SAVE_WGET': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET',)},
  70. 'SAVE_WGET_REQUISITES': {'type': bool, 'default': True, 'aliases': ('FETCH_WGET_REQUISITES',)},
  71. 'SAVE_SINGLEFILE': {'type': bool, 'default': True, 'aliases': ('FETCH_SINGLEFILE',)},
  72. 'SAVE_READABILITY': {'type': bool, 'default': True, 'aliases': ('FETCH_READABILITY',)},
  73. 'SAVE_MERCURY': {'type': bool, 'default': True, 'aliases': ('FETCH_MERCURY',)},
  74. 'SAVE_PDF': {'type': bool, 'default': True, 'aliases': ('FETCH_PDF',)},
  75. 'SAVE_SCREENSHOT': {'type': bool, 'default': True, 'aliases': ('FETCH_SCREENSHOT',)},
  76. 'SAVE_DOM': {'type': bool, 'default': True, 'aliases': ('FETCH_DOM',)},
  77. 'SAVE_HEADERS': {'type': bool, 'default': True, 'aliases': ('FETCH_HEADERS',)},
  78. 'SAVE_WARC': {'type': bool, 'default': True, 'aliases': ('FETCH_WARC',)},
  79. 'SAVE_GIT': {'type': bool, 'default': True, 'aliases': ('FETCH_GIT',)},
  80. 'SAVE_MEDIA': {'type': bool, 'default': True, 'aliases': ('FETCH_MEDIA',)},
  81. 'SAVE_ARCHIVE_DOT_ORG': {'type': bool, 'default': True, 'aliases': ('SUBMIT_ARCHIVE_DOT_ORG',)},
  82. },
  83. 'ARCHIVE_METHOD_OPTIONS': {
  84. 'RESOLUTION': {'type': str, 'default': '1440,2000', 'aliases': ('SCREENSHOT_RESOLUTION',)},
  85. 'GIT_DOMAINS': {'type': str, 'default': 'github.com,bitbucket.org,gitlab.com'},
  86. 'CHECK_SSL_VALIDITY': {'type': bool, 'default': True},
  87. 'CURL_USER_AGENT': {'type': str, 'default': 'ArchiveBox/{VERSION} (+https://github.com/ArchiveBox/ArchiveBox/) curl/{CURL_VERSION}'},
  88. 'WGET_USER_AGENT': {'type': str, 'default': 'ArchiveBox/{VERSION} (+https://github.com/ArchiveBox/ArchiveBox/) wget/{WGET_VERSION}'},
  89. 'CHROME_USER_AGENT': {'type': str, 'default': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'},
  90. 'COOKIES_FILE': {'type': str, 'default': None},
  91. 'CHROME_USER_DATA_DIR': {'type': str, 'default': None},
  92. 'CHROME_HEADLESS': {'type': bool, 'default': True},
  93. 'CHROME_SANDBOX': {'type': bool, 'default': lambda c: not c['IN_DOCKER']},
  94. 'YOUTUBEDL_ARGS': {'type': list, 'default': ['--write-description',
  95. '--write-info-json',
  96. '--write-annotations',
  97. '--write-thumbnail',
  98. '--no-call-home',
  99. '--all-subs',
  100. '--yes-playlist',
  101. '--continue',
  102. '--ignore-errors',
  103. '--geo-bypass',
  104. '--add-metadata',
  105. '--max-filesize=750m',
  106. ]},
  107. 'WGET_ARGS': {'type': list, 'default': ['--no-verbose',
  108. '--adjust-extension',
  109. '--convert-links',
  110. '--force-directories',
  111. '--backup-converted',
  112. '--span-hosts',
  113. '--no-parent',
  114. '-e', 'robots=off',
  115. ]},
  116. 'CURL_ARGS': {'type': list, 'default': ['--silent',
  117. '--location',
  118. '--compressed'
  119. ]},
  120. 'GIT_ARGS': {'type': list, 'default': ['--recursive']},
  121. },
  122. 'SEARCH_BACKEND_CONFIG' : {
  123. 'USE_INDEXING_BACKEND': {'type': bool, 'default': True},
  124. 'USE_SEARCHING_BACKEND': {'type': bool, 'default': True},
  125. 'SEARCH_BACKEND_ENGINE': {'type': str, 'default': 'ripgrep'},
  126. 'SEARCH_BACKEND_HOST_NAME': {'type': str, 'default': 'localhost'},
  127. 'SEARCH_BACKEND_PORT': {'type': int, 'default': 1491},
  128. 'SEARCH_BACKEND_PASSWORD': {'type': str, 'default': 'SecretPassword'},
  129. # SONIC
  130. 'SONIC_COLLECTION': {'type': str, 'default': 'archivebox'},
  131. 'SONIC_BUCKET': {'type': str, 'default': 'snapshots'},
  132. },
  133. 'DEPENDENCY_CONFIG': {
  134. 'USE_CURL': {'type': bool, 'default': True},
  135. 'USE_WGET': {'type': bool, 'default': True},
  136. 'USE_SINGLEFILE': {'type': bool, 'default': True},
  137. 'USE_READABILITY': {'type': bool, 'default': True},
  138. 'USE_MERCURY': {'type': bool, 'default': True},
  139. 'USE_GIT': {'type': bool, 'default': True},
  140. 'USE_CHROME': {'type': bool, 'default': True},
  141. 'USE_NODE': {'type': bool, 'default': True},
  142. 'USE_YOUTUBEDL': {'type': bool, 'default': True},
  143. 'USE_RIPGREP': {'type': bool, 'default': True},
  144. 'CURL_BINARY': {'type': str, 'default': 'curl'},
  145. 'GIT_BINARY': {'type': str, 'default': 'git'},
  146. 'WGET_BINARY': {'type': str, 'default': 'wget'},
  147. 'SINGLEFILE_BINARY': {'type': str, 'default': 'single-file'},
  148. 'READABILITY_BINARY': {'type': str, 'default': 'readability-extractor'},
  149. 'MERCURY_BINARY': {'type': str, 'default': 'mercury-parser'},
  150. 'YOUTUBEDL_BINARY': {'type': str, 'default': 'youtube-dl'},
  151. 'NODE_BINARY': {'type': str, 'default': 'node'},
  152. 'RIPGREP_BINARY': {'type': str, 'default': 'rg'},
  153. 'CHROME_BINARY': {'type': str, 'default': None},
  154. 'POCKET_CONSUMER_KEY': {'type': str, 'default': None},
  155. 'POCKET_ACCESS_TOKENS': {'type': dict, 'default': {}},
  156. },
  157. }
  158. ########################## Backwards-Compatibility #############################
  159. # for backwards compatibility with old config files, check old/deprecated names for each key
  160. CONFIG_ALIASES = {
  161. alias: key
  162. for section in CONFIG_SCHEMA.values()
  163. for key, default in section.items()
  164. for alias in default.get('aliases', ())
  165. }
  166. USER_CONFIG = {key for section in CONFIG_SCHEMA.values() for key in section.keys()}
  167. def get_real_name(key: str) -> str:
  168. """get the current canonical name for a given deprecated config key"""
  169. return CONFIG_ALIASES.get(key.upper().strip(), key.upper().strip())
  170. ################################ Constants #####################################
  171. PACKAGE_DIR_NAME = 'archivebox'
  172. TEMPLATES_DIR_NAME = 'templates'
  173. ARCHIVE_DIR_NAME = 'archive'
  174. SOURCES_DIR_NAME = 'sources'
  175. LOGS_DIR_NAME = 'logs'
  176. SQL_INDEX_FILENAME = 'index.sqlite3'
  177. JSON_INDEX_FILENAME = 'index.json'
  178. HTML_INDEX_FILENAME = 'index.html'
  179. ROBOTS_TXT_FILENAME = 'robots.txt'
  180. FAVICON_FILENAME = 'favicon.ico'
  181. CONFIG_FILENAME = 'ArchiveBox.conf'
  182. DEFAULT_CLI_COLORS = {
  183. 'reset': '\033[00;00m',
  184. 'lightblue': '\033[01;30m',
  185. 'lightyellow': '\033[01;33m',
  186. 'lightred': '\033[01;35m',
  187. 'red': '\033[01;31m',
  188. 'green': '\033[01;32m',
  189. 'blue': '\033[01;34m',
  190. 'white': '\033[01;37m',
  191. 'black': '\033[01;30m',
  192. }
  193. ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()}
  194. COLOR_DICT = defaultdict(lambda: [(0, 0, 0), (0, 0, 0)], {
  195. '00': [(0, 0, 0), (0, 0, 0)],
  196. '30': [(0, 0, 0), (0, 0, 0)],
  197. '31': [(255, 0, 0), (128, 0, 0)],
  198. '32': [(0, 200, 0), (0, 128, 0)],
  199. '33': [(255, 255, 0), (128, 128, 0)],
  200. '34': [(0, 0, 255), (0, 0, 128)],
  201. '35': [(255, 0, 255), (128, 0, 128)],
  202. '36': [(0, 255, 255), (0, 128, 128)],
  203. '37': [(255, 255, 255), (255, 255, 255)],
  204. })
  205. STATICFILE_EXTENSIONS = {
  206. # 99.999% of the time, URLs ending in these extensions are static files
  207. # that can be downloaded as-is, not html pages that need to be rendered
  208. 'gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff', 'wbmp', 'ico', 'jng', 'bmp',
  209. 'svg', 'svgz', 'webp', 'ps', 'eps', 'ai',
  210. 'mp3', 'mp4', 'm4a', 'mpeg', 'mpg', 'mkv', 'mov', 'webm', 'm4v',
  211. 'flv', 'wmv', 'avi', 'ogg', 'ts', 'm3u8',
  212. 'pdf', 'txt', 'rtf', 'rtfd', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx',
  213. 'atom', 'rss', 'css', 'js', 'json',
  214. 'dmg', 'iso', 'img',
  215. 'rar', 'war', 'hqx', 'zip', 'gz', 'bz2', '7z',
  216. # Less common extensions to consider adding later
  217. # jar, swf, bin, com, exe, dll, deb
  218. # ear, hqx, eot, wmlc, kml, kmz, cco, jardiff, jnlp, run, msi, msp, msm,
  219. # pl pm, prc pdb, rar, rpm, sea, sit, tcl tk, der, pem, crt, xpi, xspf,
  220. # ra, mng, asx, asf, 3gpp, 3gp, mid, midi, kar, jad, wml, htc, mml
  221. # These are always treated as pages, not as static files, never add them:
  222. # html, htm, shtml, xhtml, xml, aspx, php, cgi
  223. }
  224. ############################## Derived Config ##################################
  225. DYNAMIC_CONFIG_SCHEMA: ConfigDefaultDict = {
  226. 'TERM_WIDTH': {'default': lambda c: lambda: shutil.get_terminal_size((100, 10)).columns},
  227. 'USER': {'default': lambda c: getpass.getuser() or os.getlogin()},
  228. 'ANSI': {'default': lambda c: DEFAULT_CLI_COLORS if c['USE_COLOR'] else {k: '' for k in DEFAULT_CLI_COLORS.keys()}},
  229. 'PACKAGE_DIR': {'default': lambda c: Path(__file__).resolve().parent},
  230. 'TEMPLATES_DIR': {'default': lambda c: c['PACKAGE_DIR'] / TEMPLATES_DIR_NAME},
  231. 'OUTPUT_DIR': {'default': lambda c: Path(c['OUTPUT_DIR']).resolve() if c['OUTPUT_DIR'] else Path(os.curdir).resolve()},
  232. 'ARCHIVE_DIR': {'default': lambda c: c['OUTPUT_DIR'] / ARCHIVE_DIR_NAME},
  233. 'SOURCES_DIR': {'default': lambda c: c['OUTPUT_DIR'] / SOURCES_DIR_NAME},
  234. 'LOGS_DIR': {'default': lambda c: c['OUTPUT_DIR'] / LOGS_DIR_NAME},
  235. 'CONFIG_FILE': {'default': lambda c: Path(c['CONFIG_FILE']).resolve() if c['CONFIG_FILE'] else c['OUTPUT_DIR'] / CONFIG_FILENAME},
  236. 'COOKIES_FILE': {'default': lambda c: c['COOKIES_FILE'] and Path(c['COOKIES_FILE']).resolve()},
  237. 'CHROME_USER_DATA_DIR': {'default': lambda c: find_chrome_data_dir() if c['CHROME_USER_DATA_DIR'] is None else (Path(c['CHROME_USER_DATA_DIR']).resolve() if c['CHROME_USER_DATA_DIR'] else None)}, # None means unset, so we autodetect it with find_chrome_Data_dir(), but emptystring '' means user manually set it to '', and we should store it as None
  238. 'URL_BLACKLIST_PTN': {'default': lambda c: c['URL_BLACKLIST'] and re.compile(c['URL_BLACKLIST'] or '', re.IGNORECASE | re.UNICODE | re.MULTILINE)},
  239. 'ARCHIVEBOX_BINARY': {'default': lambda c: sys.argv[0]},
  240. 'VERSION': {'default': lambda c: json.loads((Path(c['PACKAGE_DIR']) / 'package.json').read_text().strip())['version']},
  241. 'GIT_SHA': {'default': lambda c: c['VERSION'].split('+')[-1] or 'unknown'},
  242. 'PYTHON_BINARY': {'default': lambda c: sys.executable},
  243. 'PYTHON_ENCODING': {'default': lambda c: sys.stdout.encoding.upper()},
  244. 'PYTHON_VERSION': {'default': lambda c: '{}.{}.{}'.format(*sys.version_info[:3])},
  245. 'DJANGO_BINARY': {'default': lambda c: django.__file__.replace('__init__.py', 'bin/django-admin.py')},
  246. 'DJANGO_VERSION': {'default': lambda c: '{}.{}.{} {} ({})'.format(*django.VERSION)},
  247. 'USE_CURL': {'default': lambda c: c['USE_CURL'] and (c['SAVE_FAVICON'] or c['SAVE_TITLE'] or c['SAVE_ARCHIVE_DOT_ORG'])},
  248. 'CURL_VERSION': {'default': lambda c: bin_version(c['CURL_BINARY']) if c['USE_CURL'] else None},
  249. 'CURL_USER_AGENT': {'default': lambda c: c['CURL_USER_AGENT'].format(**c)},
  250. 'CURL_ARGS': {'default': lambda c: c['CURL_ARGS'] or []},
  251. 'SAVE_FAVICON': {'default': lambda c: c['USE_CURL'] and c['SAVE_FAVICON']},
  252. 'SAVE_ARCHIVE_DOT_ORG': {'default': lambda c: c['USE_CURL'] and c['SAVE_ARCHIVE_DOT_ORG']},
  253. 'USE_WGET': {'default': lambda c: c['USE_WGET'] and (c['SAVE_WGET'] or c['SAVE_WARC'])},
  254. 'WGET_VERSION': {'default': lambda c: bin_version(c['WGET_BINARY']) if c['USE_WGET'] else None},
  255. 'WGET_AUTO_COMPRESSION': {'default': lambda c: wget_supports_compression(c) if c['USE_WGET'] else False},
  256. 'WGET_USER_AGENT': {'default': lambda c: c['WGET_USER_AGENT'].format(**c)},
  257. 'SAVE_WGET': {'default': lambda c: c['USE_WGET'] and c['SAVE_WGET']},
  258. 'SAVE_WARC': {'default': lambda c: c['USE_WGET'] and c['SAVE_WARC']},
  259. 'WGET_ARGS': {'default': lambda c: c['WGET_ARGS'] or []},
  260. 'RIPGREP_VERSION': {'default': lambda c: bin_version(c['RIPGREP_BINARY']) if c['USE_RIPGREP'] else None},
  261. 'USE_SINGLEFILE': {'default': lambda c: c['USE_SINGLEFILE'] and c['SAVE_SINGLEFILE']},
  262. 'SINGLEFILE_VERSION': {'default': lambda c: bin_version(c['SINGLEFILE_BINARY']) if c['USE_SINGLEFILE'] else None},
  263. 'USE_READABILITY': {'default': lambda c: c['USE_READABILITY'] and c['SAVE_READABILITY']},
  264. 'READABILITY_VERSION': {'default': lambda c: bin_version(c['READABILITY_BINARY']) if c['USE_READABILITY'] else None},
  265. 'USE_MERCURY': {'default': lambda c: c['USE_MERCURY'] and c['SAVE_MERCURY']},
  266. 'MERCURY_VERSION': {'default': lambda c: '1.0.0' if shutil.which(str(bin_path(c['MERCURY_BINARY']))) else None}, # mercury is unversioned
  267. 'USE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
  268. 'GIT_VERSION': {'default': lambda c: bin_version(c['GIT_BINARY']) if c['USE_GIT'] else None},
  269. 'SAVE_GIT': {'default': lambda c: c['USE_GIT'] and c['SAVE_GIT']},
  270. 'USE_YOUTUBEDL': {'default': lambda c: c['USE_YOUTUBEDL'] and c['SAVE_MEDIA']},
  271. 'YOUTUBEDL_VERSION': {'default': lambda c: bin_version(c['YOUTUBEDL_BINARY']) if c['USE_YOUTUBEDL'] else None},
  272. 'SAVE_MEDIA': {'default': lambda c: c['USE_YOUTUBEDL'] and c['SAVE_MEDIA']},
  273. 'YOUTUBEDL_ARGS': {'default': lambda c: c['YOUTUBEDL_ARGS'] or []},
  274. 'USE_CHROME': {'default': lambda c: c['USE_CHROME'] and (c['SAVE_PDF'] or c['SAVE_SCREENSHOT'] or c['SAVE_DOM'] or c['SAVE_SINGLEFILE'])},
  275. 'CHROME_BINARY': {'default': lambda c: c['CHROME_BINARY'] if c['CHROME_BINARY'] else find_chrome_binary()},
  276. 'CHROME_VERSION': {'default': lambda c: bin_version(c['CHROME_BINARY']) if c['USE_CHROME'] else None},
  277. 'SAVE_PDF': {'default': lambda c: c['USE_CHROME'] and c['SAVE_PDF']},
  278. 'SAVE_SCREENSHOT': {'default': lambda c: c['USE_CHROME'] and c['SAVE_SCREENSHOT']},
  279. 'SAVE_DOM': {'default': lambda c: c['USE_CHROME'] and c['SAVE_DOM']},
  280. 'SAVE_SINGLEFILE': {'default': lambda c: c['USE_CHROME'] and c['SAVE_SINGLEFILE'] and c['USE_NODE']},
  281. 'SAVE_READABILITY': {'default': lambda c: c['USE_READABILITY'] and c['USE_NODE']},
  282. 'SAVE_MERCURY': {'default': lambda c: c['USE_MERCURY'] and c['USE_NODE']},
  283. 'USE_NODE': {'default': lambda c: c['USE_NODE'] and (c['SAVE_READABILITY'] or c['SAVE_SINGLEFILE'] or c['SAVE_MERCURY'])},
  284. 'NODE_VERSION': {'default': lambda c: bin_version(c['NODE_BINARY']) if c['USE_NODE'] else None},
  285. 'DEPENDENCIES': {'default': lambda c: get_dependency_info(c)},
  286. 'CODE_LOCATIONS': {'default': lambda c: get_code_locations(c)},
  287. 'EXTERNAL_LOCATIONS': {'default': lambda c: get_external_locations(c)},
  288. 'DATA_LOCATIONS': {'default': lambda c: get_data_locations(c)},
  289. 'CHROME_OPTIONS': {'default': lambda c: get_chrome_info(c)},
  290. }
  291. ################################### Helpers ####################################
  292. def load_config_val(key: str,
  293. default: ConfigDefaultValue=None,
  294. type: Optional[Type]=None,
  295. aliases: Optional[Tuple[str, ...]]=None,
  296. config: Optional[ConfigDict]=None,
  297. env_vars: Optional[os._Environ]=None,
  298. config_file_vars: Optional[Dict[str, str]]=None) -> ConfigValue:
  299. """parse bool, int, and str key=value pairs from env"""
  300. config_keys_to_check = (key, *(aliases or ()))
  301. for key in config_keys_to_check:
  302. if env_vars:
  303. val = env_vars.get(key)
  304. if val:
  305. break
  306. if config_file_vars:
  307. val = config_file_vars.get(key)
  308. if val:
  309. break
  310. if type is None or val is None:
  311. if callable(default):
  312. assert isinstance(config, dict)
  313. return default(config)
  314. return default
  315. elif type is bool:
  316. if val.lower() in ('true', 'yes', '1'):
  317. return True
  318. elif val.lower() in ('false', 'no', '0'):
  319. return False
  320. else:
  321. raise ValueError(f'Invalid configuration option {key}={val} (expected a boolean: True/False)')
  322. elif type is str:
  323. if val.lower() in ('true', 'false', 'yes', 'no', '1', '0'):
  324. raise ValueError(f'Invalid configuration option {key}={val} (expected a string)')
  325. return val.strip()
  326. elif type is int:
  327. if not val.isdigit():
  328. raise ValueError(f'Invalid configuration option {key}={val} (expected an integer)')
  329. return int(val)
  330. elif type is list or type is dict:
  331. return json.loads(val)
  332. raise Exception('Config values can only be str, bool, int or json')
  333. def load_config_file(out_dir: str=None) -> Optional[Dict[str, str]]:
  334. """load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf"""
  335. out_dir = out_dir or Path(os.getenv('OUTPUT_DIR', '.')).resolve()
  336. config_path = Path(out_dir) / CONFIG_FILENAME
  337. if config_path.exists():
  338. config_file = ConfigParser()
  339. config_file.optionxform = str
  340. config_file.read(config_path)
  341. # flatten into one namespace
  342. config_file_vars = {
  343. key.upper(): val
  344. for section, options in config_file.items()
  345. for key, val in options.items()
  346. }
  347. # print('[i] Loaded config file', os.path.abspath(config_path))
  348. # print(config_file_vars)
  349. return config_file_vars
  350. return None
  351. def write_config_file(config: Dict[str, str], out_dir: str=None) -> ConfigDict:
  352. """load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf"""
  353. from .system import atomic_write
  354. CONFIG_HEADER = (
  355. """# This is the config file for your ArchiveBox collection.
  356. #
  357. # You can add options here manually in INI format, or automatically by running:
  358. # archivebox config --set KEY=VALUE
  359. #
  360. # If you modify this file manually, make sure to update your archive after by running:
  361. # archivebox init
  362. #
  363. # A list of all possible config with documentation and examples can be found here:
  364. # https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration
  365. """)
  366. out_dir = out_dir or Path(os.getenv('OUTPUT_DIR', '.')).resolve()
  367. config_path = Path(out_dir) / CONFIG_FILENAME
  368. if not config_path.exists():
  369. atomic_write(config_path, CONFIG_HEADER)
  370. config_file = ConfigParser()
  371. config_file.optionxform = str
  372. config_file.read(config_path)
  373. with open(config_path, 'r') as old:
  374. atomic_write(f'{config_path}.bak', old.read())
  375. find_section = lambda key: [name for name, opts in CONFIG_SCHEMA.items() if key in opts][0]
  376. # Set up sections in empty config file
  377. for key, val in config.items():
  378. section = find_section(key)
  379. if section in config_file:
  380. existing_config = dict(config_file[section])
  381. else:
  382. existing_config = {}
  383. config_file[section] = {**existing_config, key: val}
  384. # always make sure there's a SECRET_KEY defined for Django
  385. existing_secret_key = None
  386. if 'SERVER_CONFIG' in config_file and 'SECRET_KEY' in config_file['SERVER_CONFIG']:
  387. existing_secret_key = config_file['SERVER_CONFIG']['SECRET_KEY']
  388. if (not existing_secret_key) or ('not a valid secret' in existing_secret_key):
  389. from django.utils.crypto import get_random_string
  390. chars = 'abcdefghijklmnopqrstuvwxyz0123456789-_+!.'
  391. random_secret_key = get_random_string(50, chars)
  392. if 'SERVER_CONFIG' in config_file:
  393. config_file['SERVER_CONFIG']['SECRET_KEY'] = random_secret_key
  394. else:
  395. config_file['SERVER_CONFIG'] = {'SECRET_KEY': random_secret_key}
  396. with open(config_path, 'w+') as new:
  397. config_file.write(new)
  398. try:
  399. # validate the config by attempting to re-parse it
  400. CONFIG = load_all_config()
  401. return {
  402. key.upper(): CONFIG.get(key.upper())
  403. for key in config.keys()
  404. }
  405. except:
  406. # something went horribly wrong, rever to the previous version
  407. with open(f'{config_path}.bak', 'r') as old:
  408. atomic_write(config_path, old.read())
  409. if Path(f'{config_path}.bak').exists():
  410. os.remove(f'{config_path}.bak')
  411. return {}
  412. def load_config(defaults: ConfigDefaultDict,
  413. config: Optional[ConfigDict]=None,
  414. out_dir: Optional[str]=None,
  415. env_vars: Optional[os._Environ]=None,
  416. config_file_vars: Optional[Dict[str, str]]=None) -> ConfigDict:
  417. env_vars = env_vars or os.environ
  418. config_file_vars = config_file_vars or load_config_file(out_dir=out_dir)
  419. extended_config: ConfigDict = config.copy() if config else {}
  420. for key, default in defaults.items():
  421. try:
  422. extended_config[key] = load_config_val(
  423. key,
  424. default=default['default'],
  425. type=default.get('type'),
  426. aliases=default.get('aliases'),
  427. config=extended_config,
  428. env_vars=env_vars,
  429. config_file_vars=config_file_vars,
  430. )
  431. except KeyboardInterrupt:
  432. raise SystemExit(0)
  433. except Exception as e:
  434. stderr()
  435. stderr(f'[X] Error while loading configuration value: {key}', color='red', config=extended_config)
  436. stderr(' {}: {}'.format(e.__class__.__name__, e))
  437. stderr()
  438. stderr(' Check your config for mistakes and try again (your archive data is unaffected).')
  439. stderr()
  440. stderr(' For config documentation and examples see:')
  441. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration')
  442. stderr()
  443. raise
  444. raise SystemExit(2)
  445. return extended_config
  446. # def write_config(config: ConfigDict):
  447. # with open(os.path.join(config['OUTPUT_DIR'], CONFIG_FILENAME), 'w+') as f:
  448. # Logging Helpers
  449. def stdout(*args, color: Optional[str]=None, prefix: str='', config: Optional[ConfigDict]=None) -> None:
  450. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  451. if color:
  452. strs = [ansi[color], ' '.join(str(a) for a in args), ansi['reset'], '\n']
  453. else:
  454. strs = [' '.join(str(a) for a in args), '\n']
  455. sys.stdout.write(prefix + ''.join(strs))
  456. def stderr(*args, color: Optional[str]=None, prefix: str='', config: Optional[ConfigDict]=None) -> None:
  457. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  458. if color:
  459. strs = [ansi[color], ' '.join(str(a) for a in args), ansi['reset'], '\n']
  460. else:
  461. strs = [' '.join(str(a) for a in args), '\n']
  462. sys.stderr.write(prefix + ''.join(strs))
  463. def hint(text: Union[Tuple[str, ...], List[str], str], prefix=' ', config: Optional[ConfigDict]=None) -> None:
  464. ansi = DEFAULT_CLI_COLORS if (config or {}).get('USE_COLOR') else ANSI
  465. if isinstance(text, str):
  466. stderr('{}{lightred}Hint:{reset} {}'.format(prefix, text, **ansi))
  467. else:
  468. stderr('{}{lightred}Hint:{reset} {}'.format(prefix, text[0], **ansi))
  469. for line in text[1:]:
  470. stderr('{} {}'.format(prefix, line))
  471. # Dependency Metadata Helpers
  472. def bin_version(binary: Optional[str]) -> Optional[str]:
  473. """check the presence and return valid version line of a specified binary"""
  474. abspath = bin_path(binary)
  475. if not binary or not abspath:
  476. return None
  477. try:
  478. version_str = run([abspath, "--version"], stdout=PIPE).stdout.strip().decode()
  479. # take first 3 columns of first line of version info
  480. return ' '.join(version_str.split('\n')[0].strip().split()[:3])
  481. except OSError:
  482. pass
  483. # stderr(f'[X] Unable to find working version of dependency: {binary}', color='red')
  484. # stderr(' Make sure it\'s installed, then confirm it\'s working by running:')
  485. # stderr(f' {binary} --version')
  486. # stderr()
  487. # stderr(' If you don\'t want to install it, you can disable it via config. See here for more info:')
  488. # stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Install')
  489. return None
  490. def bin_path(binary: Optional[str]) -> Optional[str]:
  491. if binary is None:
  492. return None
  493. node_modules_bin = Path('.') / 'node_modules' / '.bin' / binary
  494. if node_modules_bin.exists():
  495. return str(node_modules_bin.resolve())
  496. return shutil.which(str(Path(binary).expanduser())) or shutil.which(str(binary)) or binary
  497. def bin_hash(binary: Optional[str]) -> Optional[str]:
  498. if binary is None:
  499. return None
  500. abs_path = bin_path(binary)
  501. if abs_path is None or not Path(abs_path).exists():
  502. return None
  503. file_hash = md5()
  504. with io.open(abs_path, mode='rb') as f:
  505. for chunk in iter(lambda: f.read(io.DEFAULT_BUFFER_SIZE), b''):
  506. file_hash.update(chunk)
  507. return f'md5:{file_hash.hexdigest()}'
  508. def find_chrome_binary() -> Optional[str]:
  509. """find any installed chrome binaries in the default locations"""
  510. # Precedence: Chromium, Chrome, Beta, Canary, Unstable, Dev
  511. # make sure data dir finding precedence order always matches binary finding order
  512. default_executable_paths = (
  513. 'chromium-browser',
  514. 'chromium',
  515. '/Applications/Chromium.app/Contents/MacOS/Chromium',
  516. 'chrome',
  517. 'google-chrome',
  518. '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  519. 'google-chrome-stable',
  520. 'google-chrome-beta',
  521. 'google-chrome-canary',
  522. '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
  523. 'google-chrome-unstable',
  524. 'google-chrome-dev',
  525. )
  526. for name in default_executable_paths:
  527. full_path_exists = shutil.which(name)
  528. if full_path_exists:
  529. return name
  530. return None
  531. def find_chrome_data_dir() -> Optional[str]:
  532. """find any installed chrome user data directories in the default locations"""
  533. # Precedence: Chromium, Chrome, Beta, Canary, Unstable, Dev
  534. # make sure data dir finding precedence order always matches binary finding order
  535. default_profile_paths = (
  536. '~/.config/chromium',
  537. '~/Library/Application Support/Chromium',
  538. '~/AppData/Local/Chromium/User Data',
  539. '~/.config/chrome',
  540. '~/.config/google-chrome',
  541. '~/Library/Application Support/Google/Chrome',
  542. '~/AppData/Local/Google/Chrome/User Data',
  543. '~/.config/google-chrome-stable',
  544. '~/.config/google-chrome-beta',
  545. '~/Library/Application Support/Google/Chrome Canary',
  546. '~/AppData/Local/Google/Chrome SxS/User Data',
  547. '~/.config/google-chrome-unstable',
  548. '~/.config/google-chrome-dev',
  549. )
  550. for path in default_profile_paths:
  551. full_path = Path(path).resolve()
  552. if full_path.exists():
  553. return full_path
  554. return None
  555. def wget_supports_compression(config):
  556. try:
  557. cmd = [
  558. config['WGET_BINARY'],
  559. "--compression=auto",
  560. "--help",
  561. ]
  562. return not run(cmd, stdout=DEVNULL, stderr=DEVNULL).returncode
  563. except (FileNotFoundError, OSError):
  564. return False
  565. def get_code_locations(config: ConfigDict) -> SimpleConfigValueDict:
  566. return {
  567. 'PACKAGE_DIR': {
  568. 'path': (config['PACKAGE_DIR']).resolve(),
  569. 'enabled': True,
  570. 'is_valid': (config['PACKAGE_DIR'] / '__main__.py').exists(),
  571. },
  572. 'TEMPLATES_DIR': {
  573. 'path': (config['TEMPLATES_DIR']).resolve(),
  574. 'enabled': True,
  575. 'is_valid': (config['TEMPLATES_DIR'] / 'static').exists(),
  576. },
  577. # 'NODE_MODULES_DIR': {
  578. # 'path': ,
  579. # 'enabled': ,
  580. # 'is_valid': (...).exists(),
  581. # },
  582. }
  583. def get_external_locations(config: ConfigDict) -> ConfigValue:
  584. abspath = lambda path: None if path is None else Path(path).resolve()
  585. return {
  586. 'CHROME_USER_DATA_DIR': {
  587. 'path': abspath(config['CHROME_USER_DATA_DIR']),
  588. 'enabled': config['USE_CHROME'] and config['CHROME_USER_DATA_DIR'],
  589. 'is_valid': False if config['CHROME_USER_DATA_DIR'] is None else (Path(config['CHROME_USER_DATA_DIR']) / 'Default').exists(),
  590. },
  591. 'COOKIES_FILE': {
  592. 'path': abspath(config['COOKIES_FILE']),
  593. 'enabled': config['USE_WGET'] and config['COOKIES_FILE'],
  594. 'is_valid': False if config['COOKIES_FILE'] is None else Path(config['COOKIES_FILE']).exists(),
  595. },
  596. }
  597. def get_data_locations(config: ConfigDict) -> ConfigValue:
  598. return {
  599. 'OUTPUT_DIR': {
  600. 'path': config['OUTPUT_DIR'].resolve(),
  601. 'enabled': True,
  602. 'is_valid': (config['OUTPUT_DIR'] / SQL_INDEX_FILENAME).exists(),
  603. },
  604. 'SOURCES_DIR': {
  605. 'path': config['SOURCES_DIR'].resolve(),
  606. 'enabled': True,
  607. 'is_valid': config['SOURCES_DIR'].exists(),
  608. },
  609. 'LOGS_DIR': {
  610. 'path': config['LOGS_DIR'].resolve(),
  611. 'enabled': True,
  612. 'is_valid': config['LOGS_DIR'].exists(),
  613. },
  614. 'ARCHIVE_DIR': {
  615. 'path': config['ARCHIVE_DIR'].resolve(),
  616. 'enabled': True,
  617. 'is_valid': config['ARCHIVE_DIR'].exists(),
  618. },
  619. 'CONFIG_FILE': {
  620. 'path': config['CONFIG_FILE'].resolve(),
  621. 'enabled': True,
  622. 'is_valid': config['CONFIG_FILE'].exists(),
  623. },
  624. 'SQL_INDEX': {
  625. 'path': (config['OUTPUT_DIR'] / SQL_INDEX_FILENAME).resolve(),
  626. 'enabled': True,
  627. 'is_valid': (config['OUTPUT_DIR'] / SQL_INDEX_FILENAME).exists(),
  628. },
  629. }
  630. def get_dependency_info(config: ConfigDict) -> ConfigValue:
  631. return {
  632. 'ARCHIVEBOX_BINARY': {
  633. 'path': bin_path(config['ARCHIVEBOX_BINARY']),
  634. 'version': config['VERSION'],
  635. 'hash': bin_hash(config['ARCHIVEBOX_BINARY']),
  636. 'enabled': True,
  637. 'is_valid': True,
  638. },
  639. 'PYTHON_BINARY': {
  640. 'path': bin_path(config['PYTHON_BINARY']),
  641. 'version': config['PYTHON_VERSION'],
  642. 'hash': bin_hash(config['PYTHON_BINARY']),
  643. 'enabled': True,
  644. 'is_valid': bool(config['PYTHON_VERSION']),
  645. },
  646. 'DJANGO_BINARY': {
  647. 'path': bin_path(config['DJANGO_BINARY']),
  648. 'version': config['DJANGO_VERSION'],
  649. 'hash': bin_hash(config['DJANGO_BINARY']),
  650. 'enabled': True,
  651. 'is_valid': bool(config['DJANGO_VERSION']),
  652. },
  653. 'CURL_BINARY': {
  654. 'path': bin_path(config['CURL_BINARY']),
  655. 'version': config['CURL_VERSION'],
  656. 'hash': bin_hash(config['CURL_BINARY']),
  657. 'enabled': config['USE_CURL'],
  658. 'is_valid': bool(config['CURL_VERSION']),
  659. },
  660. 'WGET_BINARY': {
  661. 'path': bin_path(config['WGET_BINARY']),
  662. 'version': config['WGET_VERSION'],
  663. 'hash': bin_hash(config['WGET_BINARY']),
  664. 'enabled': config['USE_WGET'],
  665. 'is_valid': bool(config['WGET_VERSION']),
  666. },
  667. 'NODE_BINARY': {
  668. 'path': bin_path(config['NODE_BINARY']),
  669. 'version': config['NODE_VERSION'],
  670. 'hash': bin_hash(config['NODE_BINARY']),
  671. 'enabled': config['USE_NODE'],
  672. 'is_valid': bool(config['NODE_VERSION']),
  673. },
  674. 'SINGLEFILE_BINARY': {
  675. 'path': bin_path(config['SINGLEFILE_BINARY']),
  676. 'version': config['SINGLEFILE_VERSION'],
  677. 'hash': bin_hash(config['SINGLEFILE_BINARY']),
  678. 'enabled': config['USE_SINGLEFILE'],
  679. 'is_valid': bool(config['SINGLEFILE_VERSION']),
  680. },
  681. 'READABILITY_BINARY': {
  682. 'path': bin_path(config['READABILITY_BINARY']),
  683. 'version': config['READABILITY_VERSION'],
  684. 'hash': bin_hash(config['READABILITY_BINARY']),
  685. 'enabled': config['USE_READABILITY'],
  686. 'is_valid': bool(config['READABILITY_VERSION']),
  687. },
  688. 'MERCURY_BINARY': {
  689. 'path': bin_path(config['MERCURY_BINARY']),
  690. 'version': config['MERCURY_VERSION'],
  691. 'hash': bin_hash(config['MERCURY_BINARY']),
  692. 'enabled': config['USE_MERCURY'],
  693. 'is_valid': bool(config['MERCURY_VERSION']),
  694. },
  695. 'GIT_BINARY': {
  696. 'path': bin_path(config['GIT_BINARY']),
  697. 'version': config['GIT_VERSION'],
  698. 'hash': bin_hash(config['GIT_BINARY']),
  699. 'enabled': config['USE_GIT'],
  700. 'is_valid': bool(config['GIT_VERSION']),
  701. },
  702. 'YOUTUBEDL_BINARY': {
  703. 'path': bin_path(config['YOUTUBEDL_BINARY']),
  704. 'version': config['YOUTUBEDL_VERSION'],
  705. 'hash': bin_hash(config['YOUTUBEDL_BINARY']),
  706. 'enabled': config['USE_YOUTUBEDL'],
  707. 'is_valid': bool(config['YOUTUBEDL_VERSION']),
  708. },
  709. 'CHROME_BINARY': {
  710. 'path': bin_path(config['CHROME_BINARY']),
  711. 'version': config['CHROME_VERSION'],
  712. 'hash': bin_hash(config['CHROME_BINARY']),
  713. 'enabled': config['USE_CHROME'],
  714. 'is_valid': bool(config['CHROME_VERSION']),
  715. },
  716. 'RIPGREP_BINARY': {
  717. 'path': bin_path(config['RIPGREP_BINARY']),
  718. 'version': config['RIPGREP_VERSION'],
  719. 'hash': bin_hash(config['RIPGREP_BINARY']),
  720. 'enabled': config['USE_RIPGREP'],
  721. 'is_valid': bool(config['RIPGREP_VERSION']),
  722. },
  723. # TODO: add an entry for the sonic search backend?
  724. # 'SONIC_BINARY': {
  725. # 'path': bin_path(config['SONIC_BINARY']),
  726. # 'version': config['SONIC_VERSION'],
  727. # 'hash': bin_hash(config['SONIC_BINARY']),
  728. # 'enabled': config['USE_SONIC'],
  729. # 'is_valid': bool(config['SONIC_VERSION']),
  730. # },
  731. }
  732. def get_chrome_info(config: ConfigDict) -> ConfigValue:
  733. return {
  734. 'TIMEOUT': config['TIMEOUT'],
  735. 'RESOLUTION': config['RESOLUTION'],
  736. 'CHECK_SSL_VALIDITY': config['CHECK_SSL_VALIDITY'],
  737. 'CHROME_BINARY': config['CHROME_BINARY'],
  738. 'CHROME_HEADLESS': config['CHROME_HEADLESS'],
  739. 'CHROME_SANDBOX': config['CHROME_SANDBOX'],
  740. 'CHROME_USER_AGENT': config['CHROME_USER_AGENT'],
  741. 'CHROME_USER_DATA_DIR': config['CHROME_USER_DATA_DIR'],
  742. }
  743. # ******************************************************************************
  744. # ******************************************************************************
  745. # ******************************** Load Config *********************************
  746. # ******* (compile the defaults, configs, and metadata all into CONFIG) ********
  747. # ******************************************************************************
  748. # ******************************************************************************
  749. def load_all_config():
  750. CONFIG: ConfigDict = {}
  751. for section_name, section_config in CONFIG_SCHEMA.items():
  752. CONFIG = load_config(section_config, CONFIG)
  753. return load_config(DYNAMIC_CONFIG_SCHEMA, CONFIG)
  754. # add all final config values in CONFIG to globals in this file
  755. CONFIG = load_all_config()
  756. globals().update(CONFIG)
  757. # this lets us do: from .config import DEBUG, MEDIA_TIMEOUT, ...
  758. # ******************************************************************************
  759. # ******************************************************************************
  760. # ******************************************************************************
  761. # ******************************************************************************
  762. # ******************************************************************************
  763. ########################### System Environment Setup ###########################
  764. # Set timezone to UTC and umask to OUTPUT_PERMISSIONS
  765. os.environ["TZ"] = 'UTC'
  766. os.umask(0o777 - int(OUTPUT_PERMISSIONS, base=8)) # noqa: F821
  767. # add ./node_modules/.bin to $PATH so we can use node scripts in extractors
  768. NODE_BIN_PATH = str((Path(CONFIG["OUTPUT_DIR"]).absolute() / 'node_modules' / '.bin'))
  769. sys.path.append(NODE_BIN_PATH)
  770. if not CHECK_SSL_VALIDITY:
  771. import urllib3
  772. import requests
  773. requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
  774. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  775. ########################### Config Validity Checkers ###########################
  776. def check_system_config(config: ConfigDict=CONFIG) -> None:
  777. ### Check system environment
  778. if config['USER'] == 'root':
  779. stderr('[!] ArchiveBox should never be run as root!', color='red')
  780. stderr(' For more information, see the security overview documentation:')
  781. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Security-Overview#do-not-run-as-root')
  782. raise SystemExit(2)
  783. ### Check Python environment
  784. if sys.version_info[:3] < (3, 6, 0):
  785. stderr(f'[X] Python version is not new enough: {config["PYTHON_VERSION"]} (>3.6 is required)', color='red')
  786. stderr(' See https://github.com/ArchiveBox/ArchiveBox/wiki/Troubleshooting#python for help upgrading your Python installation.')
  787. raise SystemExit(2)
  788. if config['PYTHON_ENCODING'] not in ('UTF-8', 'UTF8'):
  789. stderr(f'[X] Your system is running python3 scripts with a bad locale setting: {config["PYTHON_ENCODING"]} (it should be UTF-8).', color='red')
  790. stderr(' To fix it, add the line "export PYTHONIOENCODING=UTF-8" to your ~/.bashrc file (without quotes)')
  791. stderr(' Or if you\'re using ubuntu/debian, run "dpkg-reconfigure locales"')
  792. stderr('')
  793. stderr(' Confirm that it\'s fixed by opening a new shell and running:')
  794. stderr(' python3 -c "import sys; print(sys.stdout.encoding)" # should output UTF-8')
  795. raise SystemExit(2)
  796. # stderr('[i] Using Chrome binary: {}'.format(shutil.which(CHROME_BINARY) or CHROME_BINARY))
  797. # stderr('[i] Using Chrome data dir: {}'.format(os.path.abspath(CHROME_USER_DATA_DIR)))
  798. if config['CHROME_USER_DATA_DIR'] is not None:
  799. if not (Path(config['CHROME_USER_DATA_DIR']) / 'Default').exists():
  800. stderr('[X] Could not find profile "Default" in CHROME_USER_DATA_DIR.', color='red')
  801. stderr(f' {config["CHROME_USER_DATA_DIR"]}')
  802. stderr(' Make sure you set it to a Chrome user data directory containing a Default profile folder.')
  803. stderr(' For more info see:')
  804. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#CHROME_USER_DATA_DIR')
  805. if '/Default' in str(config['CHROME_USER_DATA_DIR']):
  806. stderr()
  807. stderr(' Try removing /Default from the end e.g.:')
  808. stderr(' CHROME_USER_DATA_DIR="{}"'.format(config['CHROME_USER_DATA_DIR'].split('/Default')[0]))
  809. raise SystemExit(2)
  810. def check_dependencies(config: ConfigDict=CONFIG, show_help: bool=True) -> None:
  811. invalid_dependencies = [
  812. (name, info) for name, info in config['DEPENDENCIES'].items()
  813. if info['enabled'] and not info['is_valid']
  814. ]
  815. if invalid_dependencies and show_help:
  816. stderr(f'[!] Warning: Missing {len(invalid_dependencies)} recommended dependencies', color='lightyellow')
  817. for dependency, info in invalid_dependencies:
  818. stderr(
  819. ' ! {}: {} ({})'.format(
  820. dependency,
  821. info['path'] or 'unable to find binary',
  822. info['version'] or 'unable to detect version',
  823. )
  824. )
  825. if dependency in ('SINGLEFILE_BINARY', 'READABILITY_BINARY', 'MERCURY_BINARY'):
  826. hint(('npm install --prefix . "git+https://github.com/ArchiveBox/ArchiveBox.git"',
  827. f'or archivebox config --set SAVE_{dependency.rsplit("_", 1)[0]}=False to silence this warning',
  828. ''), prefix=' ')
  829. stderr('')
  830. if config['TIMEOUT'] < 5:
  831. stderr(f'[!] Warning: TIMEOUT is set too low! (currently set to TIMEOUT={config["TIMEOUT"]} seconds)', color='red')
  832. stderr(' You must allow *at least* 5 seconds for indexing and archive methods to run succesfully.')
  833. stderr(' (Setting it to somewhere between 30 and 3000 seconds is recommended)')
  834. stderr()
  835. stderr(' If you want to make ArchiveBox run faster, disable specific archive methods instead:')
  836. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#archive-method-toggles')
  837. stderr()
  838. elif config['USE_CHROME'] and config['TIMEOUT'] < 15:
  839. stderr(f'[!] Warning: TIMEOUT is set too low! (currently set to TIMEOUT={config["TIMEOUT"]} seconds)', color='red')
  840. stderr(' Chrome will fail to archive all sites if set to less than ~15 seconds.')
  841. stderr(' (Setting it to somewhere between 30 and 300 seconds is recommended)')
  842. stderr()
  843. stderr(' If you want to make ArchiveBox run faster, disable specific archive methods instead:')
  844. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#archive-method-toggles')
  845. stderr()
  846. if config['USE_YOUTUBEDL'] and config['MEDIA_TIMEOUT'] < 20:
  847. stderr(f'[!] Warning: MEDIA_TIMEOUT is set too low! (currently set to MEDIA_TIMEOUT={config["MEDIA_TIMEOUT"]} seconds)', color='red')
  848. stderr(' Youtube-dl will fail to archive all media if set to less than ~20 seconds.')
  849. stderr(' (Setting it somewhere over 60 seconds is recommended)')
  850. stderr()
  851. stderr(' If you want to disable media archiving entirely, set SAVE_MEDIA=False instead:')
  852. stderr(' https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#save_media')
  853. stderr()
  854. def check_data_folder(out_dir: Union[str, Path, None]=None, config: ConfigDict=CONFIG) -> None:
  855. output_dir = out_dir or config['OUTPUT_DIR']
  856. assert isinstance(output_dir, (str, Path))
  857. sql_index_exists = (Path(output_dir) / SQL_INDEX_FILENAME).exists()
  858. if not sql_index_exists:
  859. stderr('[X] No archivebox index found in the current directory.', color='red')
  860. stderr(f' {output_dir}', color='lightyellow')
  861. stderr()
  862. stderr(' {lightred}Hint{reset}: Are you running archivebox in the right folder?'.format(**config['ANSI']))
  863. stderr(' cd path/to/your/archive/folder')
  864. stderr(' archivebox [command]')
  865. stderr()
  866. stderr(' {lightred}Hint{reset}: To create a new archive collection or import existing data in this folder, run:'.format(**config['ANSI']))
  867. stderr(' archivebox init')
  868. raise SystemExit(2)
  869. from .index.sql import list_migrations
  870. pending_migrations = [name for status, name in list_migrations() if not status]
  871. if (not sql_index_exists) or pending_migrations:
  872. if sql_index_exists:
  873. pending_operation = f'apply the {len(pending_migrations)} pending migrations'
  874. else:
  875. pending_operation = 'generate the new SQL main index'
  876. stderr('[X] This collection was created with an older version of ArchiveBox and must be upgraded first.', color='lightyellow')
  877. stderr(f' {output_dir}')
  878. stderr()
  879. stderr(f' To upgrade it to the latest version and {pending_operation} run:')
  880. stderr(' archivebox init')
  881. raise SystemExit(3)
  882. sources_dir = Path(output_dir) / SOURCES_DIR_NAME
  883. if not sources_dir.exists():
  884. sources_dir.mkdir()
  885. def setup_django(out_dir: Path=None, check_db=False, config: ConfigDict=CONFIG, in_memory_db=False) -> None:
  886. check_system_config()
  887. output_dir = out_dir or Path(config['OUTPUT_DIR'])
  888. assert isinstance(output_dir, Path) and isinstance(config['PACKAGE_DIR'], Path)
  889. try:
  890. import django
  891. sys.path.append(str(config['PACKAGE_DIR']))
  892. os.environ.setdefault('OUTPUT_DIR', str(output_dir))
  893. assert (config['PACKAGE_DIR'] / 'core' / 'settings.py').exists(), 'settings.py was not found at archivebox/core/settings.py'
  894. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
  895. if in_memory_db:
  896. # Put the db in memory and run migrations in case any command requires it
  897. from django.core.management import call_command
  898. os.environ.setdefault("ARCHIVEBOX_DATABASE_NAME", ":memory:")
  899. django.setup()
  900. call_command("migrate", interactive=False, verbosity=0)
  901. else:
  902. django.setup()
  903. if check_db:
  904. sql_index_path = Path(output_dir) / SQL_INDEX_FILENAME
  905. assert sql_index_path.exists(), (
  906. f'No database file {SQL_INDEX_FILENAME} found in OUTPUT_DIR: {config["OUTPUT_DIR"]}')
  907. except KeyboardInterrupt:
  908. raise SystemExit(2)