Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-19 08:00:00

0001 import getpass
0002 import glob
0003 import grp
0004 import os
0005 import pwd
0006 import re
0007 import stat
0008 import sys
0009 import sysconfig
0010 
0011 from hatchling.builders.hooks.plugin.interface import BuildHookInterface
0012 
0013 
0014 class CustomHook(BuildHookInterface):
0015     def initialize(self, version, build_data):
0016         # chmod +x - only for intended executable files
0017         executable_extensions = {'.sh','.py','.pl',''}
0018         for f in glob.glob("./templates/bin/*"):
0019             # Check if file should be executable - FIXED INDENTATION
0020             _, ext = os.path.splitext(f)
0021             if ext.lower() in executable_extensions:
0022                 st = os.stat(f)
0023                 os.chmod(f, st.st_mode | stat.S_IXUSR)
0024         
0025         # user
0026         if os.getgid() == 0:
0027             panda_user = "atlpan"
0028             panda_group = "zp"
0029         else:
0030             panda_user = getpass.getuser()
0031             panda_group = grp.getgrgid(os.getgid()).gr_name
0032         
0033         # parameters to be resolved
0034         self.params = {}
0035         self.params["install_dir"] = os.environ.get("PANDA_INSTALL_TARGET")
0036         if self.params["install_dir"]:
0037             # non-standard installation path
0038             self.params["install_purelib"] = self.params["install_dir"]
0039             self.params["install_scripts"] = os.path.join(self.params["install_dir"], "bin")
0040         else:
0041             self.params["install_dir"] = sys.prefix
0042             try:
0043                 # python3.2 or higher
0044                 self.params["install_purelib"] = sysconfig.get_path("purelib")
0045                 self.params["install_scripts"] = sysconfig.get_path("scripts")
0046             except Exception:
0047                 # old python
0048                 import distutils
0049 
0050                 self.params["install_purelib"] = distutils.sysconfig.get_python_lib()
0051                 self.params["install_scripts"] = os.path.join(sys.prefix, "bin")
0052         
0053         for k in self.params:
0054             path = self.params[k]
0055             self.params[k] = os.path.abspath(os.path.expanduser(path))
0056         
0057         # other parameters
0058         self.params["panda_user"] = panda_user
0059         self.params["panda_group"] = panda_group
0060         self.params["python_exec_version"] = "%s.%s" % sys.version_info[:2]
0061         self.params["virtual_env"] = ""
0062         self.params["virtual_env_setup"] = ""
0063         
0064         if "VIRTUAL_ENV" in os.environ:
0065             self.params["virtual_env"] = os.environ["VIRTUAL_ENV"]
0066             self.params["virtual_env_setup"] = f"source {os.environ['VIRTUAL_ENV']}/bin/activate"
0067         elif sys.executable:
0068             venv_dir = os.path.dirname(os.path.dirname(sys.executable))
0069             py_venv_activate = os.path.join(venv_dir, "bin/activate")
0070             if os.path.exists(py_venv_activate):
0071                 self.params["virtual_env"] = venv_dir
0072                 self.params["virtual_env_setup"] = f"source {py_venv_activate}"
0073         
0074         # instantiate templates
0075         for in_f in glob.glob("./templates/**", recursive=True):
0076             if not in_f.endswith(".template"):
0077                 continue
0078             with open(in_f) as in_fh:
0079                 file_data = in_fh.read()
0080                 # replace patterns
0081                 for item in re.findall(r"@@([^@]+)@@", file_data):
0082                     if item not in self.params:
0083                         raise RuntimeError(f"unknown pattern {item} in {in_f}")
0084                     # get pattern
0085                     patt = self.params[item]
0086                     # convert to absolute path
0087                     if item.startswith("install"):
0088                         patt = os.path.abspath(patt)
0089                     # remove build/*/dump for bdist
0090                     patt = re.sub("build/[^/]+/dump", "", patt)
0091                     # remove /var/tmp/*-buildroot for bdist_rpm
0092                     patt = re.sub("/var/tmp/.*-buildroot", "", patt)
0093                     # replace
0094                     file_data = file_data.replace(f"@@{item}@@", patt)
0095                 
0096                 out_f = re.sub(r"(\.exe)*\.template$", "", in_f)
0097                 with open(out_f, "w") as out_fh:
0098                     out_fh.write(file_data)
0099                 
0100                 # chmod +x - Consider making this consistent too
0101                 if in_f.endswith(".exe.template"):
0102                     tmp_st = os.stat(out_f)
0103                     os.chmod(out_f, tmp_st.st_mode | stat.S_IXUSR)  # Made consistent with owner-only permissions
0104 
0105     def finalize(self, version, build_data, artifact_path):
0106         pass
0107         # post install
0108         # uid = pwd.getpwnam(self.params["panda_user"]).pw_uid
0109         # gid = grp.getgrnam(self.params["panda_group"]).gr_gid
0110         # for directory in ["/var/log/panda", "/var/log/panda/wsgisocks", "/var/log/panda/fastsocks"]:
0111         #     directory = self.params["virtual_env"] + directory
0112         #     if not os.path.exists(directory):
0113         #         os.makedirs(directory)
0114         #         os.chown(directory, uid, gid)
0115         # if self.params["virtual_env"]:
0116         #     target_dir = os.path.join(self.params["virtual_env"], "etc/sysconfig")
0117         #     if not os.path.exists(target_dir):
0118         #         os.makedirs(target_dir)
0119         #     target = os.path.join(target_dir, "panda_server")
0120         #     try:
0121         #         os.symlink(os.path.join(self.params["virtual_env"], "etc/panda/panda_server.sysconfig"), target)
0122         #     except Exception:
0123         #         pass