build_env.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 reposetorie's working tree is used to
  11. # generate the build_env, not from /etc/searx/settings.yml.
  12. os.environ['SEARX_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 forgett to update:
  21. # - ./docs/admin/engines/settings.rst
  22. # - ./docs/dev/makefile.rst (section make buildenv)
  23. # - ./manage function buildenv.unset_env()
  24. name_val = [
  25. ('SEARX_URL' , 'server.base_url'),
  26. ('SEARX_PORT' , 'server.port'),
  27. ('SEARX_BIND_ADDRESS' , 'server.bind_address'),
  28. ]
  29. brand_env = 'utils' + sep + 'brand.env'
  30. # Some defaults in the settings.yml are taken from the environment,
  31. # e.g. SEARX_BIND_ADDRESS (:py:obj:`searx.settings_defaults.SHEMA`). When the
  32. # 'brand.env' file is created these enviroment variables should be unset first::
  33. _unset = object()
  34. for name, option in name_val:
  35. if not os.environ.get(name, _unset) is _unset:
  36. del os.environ[name]
  37. # After the variables are unset in the environ, we can import from the searx
  38. # package (what will read the values from the settings.yml).
  39. from searx.version import GIT_URL, GIT_BRANCH
  40. from searx import get_setting
  41. print('build %s (settings from: %s)' % (brand_env, os.environ['SEARX_SETTINGS_PATH']))
  42. sys.path.insert(0, repo_root)
  43. with open(repo_root + sep + brand_env, 'w', encoding='utf-8') as f:
  44. for name, option in name_val:
  45. print("export %s='%s'" % (name, _env(option)), file=f)
  46. print(f"export GIT_URL='{GIT_URL}'", file=f)
  47. print(f"export GIT_BRANCH='{GIT_BRANCH}'", file=f)