build_env.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. # Under the assumption that a brand is always a fork assure that the settings
  11. # file from reposetorie's working tree is used to generate the build_env, not
  12. # from /etc/searx/settings.yml.
  13. os.environ['SEARX_SETTINGS_PATH'] = abspath(dirname(__file__) + sep + 'settings.yml')
  14. def _env(*arg, **kwargs):
  15. val = get_setting(*arg, **kwargs)
  16. if val is True:
  17. val = '1'
  18. elif val is False:
  19. val = ''
  20. return val
  21. # If you add or remove variables here, do not forgett to update:
  22. # - ./docs/admin/engines/settings.rst
  23. # - ./docs/dev/makefile.rst (section make buildenv)
  24. # - ./manage function buildenv.unset_env()
  25. name_val = [
  26. ('GIT_URL' , 'brand.git_url'),
  27. ('GIT_BRANCH' , 'brand.git_branch'),
  28. ('SEARX_URL' , 'server.base_url'),
  29. ('SEARX_PORT' , 'server.port'),
  30. ('SEARX_BIND_ADDRESS' , 'server.bind_address'),
  31. ]
  32. brand_env = 'utils' + sep + 'brand.env'
  33. # Some defaults in the settings.yml are taken from the environment,
  34. # e.g. SEARX_BIND_ADDRESS (:py:obj:`searx.settings_defaults.SHEMA`). When the
  35. # 'brand.env' file is created these enviroment variables should be unset first::
  36. _unset = object()
  37. for name, option in name_val:
  38. if not os.environ.get(name, _unset) is _unset:
  39. del os.environ[name]
  40. # After the variables are unset in the environ, we can import settings
  41. # (get_setting) from searx module.
  42. from searx import get_setting
  43. print('build %s (settings from: %s)' % (brand_env, os.environ['SEARX_SETTINGS_PATH']))
  44. sys.path.insert(0, repo_root)
  45. from searx import settings
  46. with open(repo_root + sep + brand_env, 'w', encoding='utf-8') as f:
  47. for name, option in name_val:
  48. print("export %s='%s'" % (name, _env(option)), file=f)