build_env.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. name_val = [
  22. ('SEARX_URL' , 'server.base_url'),
  23. ('GIT_URL' , 'brand.git_url'),
  24. ('GIT_BRANCH' , 'brand.git_branch'),
  25. ('ISSUE_URL' , 'brand.issue_url'),
  26. ('DOCS_URL' , 'brand.docs_url'),
  27. ('PUBLIC_INSTANCES' , 'brand.public_instances'),
  28. ('WIKI_URL' , 'brand.wiki_url'),
  29. ]
  30. brand_env = 'utils' + sep + 'brand.env'
  31. # Some defaults in the settings.yml are taken from the environment,
  32. # e.g. SEARX_BIND_ADDRESS (:py:obj:`searx.settings_defaults.SHEMA`). When the
  33. # 'brand.env' file is created these enviroment variables should be unset first::
  34. _unset = object()
  35. for name, option in name_val:
  36. if not os.environ.get(name, _unset) is _unset:
  37. del os.environ[name]
  38. # After the variables are unset in the environ, we can import settings
  39. # (get_setting) from searx module.
  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. from searx import settings
  44. with open(repo_root + sep + brand_env, 'w', encoding='utf-8') as f:
  45. for name, option in name_val:
  46. print("export %s='%s'" % (name, _env(option)), file=f)