Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:22

0001 #!/usr/bin/env python3
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
0006 #
0007 # Authors:
0008 # - Wen Guan, <wen.guan@cern.ch>, 2019 - 2025
0009 
0010 """
0011 Post-install helper: generate Apache httpd config from templates.
0012 
0013 Run this script AFTER 'pip install idds-server' (or after any pip install that
0014 places the iDDS package under a conda/virtualenv prefix).  It resolves the
0015 correct Python site-packages, home, and bin paths from the *running* interpreter
0016 and writes a ready-to-copy *.install_template file for each Apache conf template.
0017 
0018 Usage (run as the install user, with the conda/venv already activated):
0019 
0020     python /opt/idds/tools/env/setup_httpd_conf.py
0021 
0022 Or, with an explicit install prefix:
0023 
0024     python /opt/idds/tools/env/setup_httpd_conf.py --prefix /opt/idds
0025 
0026 Then copy the generated .install_template to httpd conf.d, e.g.:
0027 
0028     cp /opt/idds/etc/idds/rest/httpd-idds-443-py39-cc7.conf.install_template \\
0029        /etc/httpd/conf.d/httpd-idds-443-py39-cc7.conf
0030 """
0031 
0032 import argparse
0033 import io
0034 import os
0035 import sys
0036 import sysconfig
0037 import glob
0038 
0039 
0040 def get_python_lib():
0041     return sysconfig.get_paths()["purelib"]
0042 
0043 
0044 def get_python_bin_path():
0045     return sysconfig.get_paths()["scripts"]
0046 
0047 
0048 def get_python_home():
0049     return sys.exec_prefix
0050 
0051 
0052 def replace_python_path(conf_files, python_lib_path, install_bin_path, install_home_path):
0053     """Expand {python_site_packages_path} / {python_site_home_path} / {python_site_bin_path}
0054     placeholders in each template and write a *.install_template beside it."""
0055     for conf_file in conf_files:
0056         if not os.path.exists(conf_file):
0057             print(f"  [skip] {conf_file} not found")
0058             continue
0059         new_file = conf_file.replace('.template', '.install_template')
0060         with io.open(conf_file, 'r', encoding='utf8') as f:
0061             template = f.read()
0062         rendered = template.format(
0063             python_site_packages_path=python_lib_path,
0064             GLOBAL='GLOBAL',
0065             REQUEST_METHOD='REQUEST_METHOD',
0066             python_site_home_path=install_home_path,
0067             python_site_bin_path=install_bin_path,
0068         )
0069         with io.open(new_file, 'w', encoding='utf8') as f:
0070             f.write(rendered)
0071         print(f"  [ok]   {conf_file}")
0072         print(f"         -> {new_file}")
0073 
0074 
0075 def main():
0076     parser = argparse.ArgumentParser(
0077         description="Generate Apache httpd *.install_template files for iDDS."
0078     )
0079     parser.add_argument(
0080         "--prefix",
0081         default=None,
0082         help="iDDS install prefix (default: auto-detected from running Python, i.e. sys.prefix)",
0083     )
0084     args = parser.parse_args()
0085 
0086     if args.prefix:
0087         # When a custom prefix is given we still use the *running* interpreter's
0088         # lib/bin paths, which should already be under that prefix if the correct
0089         # conda env / venv is activated.
0090         prefix = args.prefix
0091     else:
0092         prefix = sys.prefix
0093 
0094     python_lib_path = get_python_lib()
0095     install_bin_path = get_python_bin_path()
0096     install_home_path = get_python_home()
0097 
0098     print(f"Python prefix      : {prefix}")
0099     print(f"Site-packages path : {python_lib_path}")
0100     print(f"Bin path           : {install_bin_path}")
0101     print(f"Home path          : {install_home_path}")
0102     print()
0103 
0104     # Look for all httpd conf templates shipped under the install prefix
0105     template_glob = os.path.join(prefix, "etc", "idds", "rest", "*.conf.template")
0106     conf_files = sorted(glob.glob(template_glob))
0107 
0108     if not conf_files:
0109         print(f"No *.conf.template files found under {template_glob}")
0110         print("Make sure the iDDS server package is installed and the prefix is correct.")
0111         sys.exit(1)
0112 
0113     print(f"Generating .install_template files from {len(conf_files)} template(s):")
0114     replace_python_path(conf_files, python_lib_path, install_bin_path, install_home_path)
0115 
0116     print()
0117     print("Done.  Copy the desired .install_template to /etc/httpd/conf.d/ to activate.")
0118 
0119 
0120 if __name__ == "__main__":
0121     main()