build_env.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """build environment used by shell scripts
  3. """
  4. # set path
  5. import sys
  6. import os
  7. from os.path import realpath, dirname, join, sep, abspath
  8. repo_root = realpath(dirname(realpath(__file__)) + sep + '..')
  9. sys.path.insert(0, repo_root)
  10. # Assure that the settings file from repository's working tree is used to
  11. # generate the build_env, not from /etc/searxng/settings.yml.
  12. os.environ['SEARXNG_SETTINGS_PATH'] = join(repo_root, 'etc', 'settings.yml')
  13. def _env(*arg, **kwargs):
  14. val = get_setting(*arg, **kwargs)
  15. if val is True:
  16. val = '1'
  17. elif val is False:
  18. val = ''
  19. return val
  20. # If you add or remove variables here, do not forget to update:
  21. # - ./docs/admin/engines/settings.rst
  22. # - ./docs/dev/makefile.rst (section make buildenv)
  23. name_val = [
  24. ('SEARXNG_URL' , 'server.base_url'),
  25. ('SEARXNG_PORT' , 'server.port'),
  26. ('SEARXNG_BIND_ADDRESS' , 'server.bind_address'),
  27. ]
  28. brand_env = 'utils' + sep + 'brand.env'
  29. # Some defaults in the settings.yml are taken from the environment,
  30. # e.g. SEARXNG_BIND_ADDRESS (:py:obj:`searx.settings_defaults.SHEMA`). When the
  31. # 'brand.env' file is created these environment variables should be unset first::
  32. _unset = object()
  33. for name, option in name_val:
  34. if not os.environ.get(name, _unset) is _unset:
  35. del os.environ[name]
  36. # After the variables are unset in the environ, we can import from the searx
  37. # package (what will read the values from the settings.yml).
  38. from searx.version import GIT_URL, GIT_BRANCH
  39. from searx import get_setting
  40. print('build %s (settings from: %s)' % (brand_env, os.environ['SEARXNG_SETTINGS_PATH']))
  41. sys.path.insert(0, repo_root)
  42. with open(repo_root + sep + brand_env, 'w', encoding='utf-8') as f:
  43. for name, option in name_val:
  44. print("export %s='%s'" % (name, _env(option)), file=f)
  45. print(f"export GIT_URL='{GIT_URL}'", file=f)
  46. print(f"export GIT_BRANCH='{GIT_BRANCH}'", file=f)