utils.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. from .constants import NODE_MODULES_DIRNAME, NODE_MODULES_WORKSPACE_BUNDLE_FILENAME, PACKAGE_JSON_FILENAME
  3. def home_dir():
  4. """
  5. Stolen from ya (in the root of arcadia)
  6. """
  7. # Do not trust $HOME, as it is unreliable in certain environments
  8. # Temporarily delete os.environ["HOME"] to force reading current home directory from /etc/passwd
  9. home_from_env = os.environ.pop("HOME", None)
  10. try:
  11. home_from_passwd = os.path.expanduser("~")
  12. if os.path.isabs(home_from_passwd):
  13. # This home dir is valid, prefer it over $HOME
  14. return home_from_passwd
  15. else:
  16. # When python is built with musl (this is quire weird though),
  17. # only users from /etc/passwd will be properly resolved,
  18. # as musl does not have nss module for LDAP integration.
  19. return home_from_env
  20. finally:
  21. if home_from_env is not None:
  22. os.environ["HOME"] = home_from_env
  23. def s_rooted(p):
  24. return os.path.join("$S", p)
  25. def b_rooted(p):
  26. return os.path.join("$B", p)
  27. def build_pj_path(p):
  28. return os.path.join(p, PACKAGE_JSON_FILENAME)
  29. def build_tmp_pj_path(p):
  30. return os.path.join(p, "tmp." + PACKAGE_JSON_FILENAME)
  31. def build_nm_path(p):
  32. return os.path.join(p, NODE_MODULES_DIRNAME)
  33. def build_nm_bundle_path(p):
  34. return os.path.join(p, NODE_MODULES_WORKSPACE_BUNDLE_FILENAME)
  35. def build_nm_store_path(moddir: str) -> str:
  36. nots_store_path = os.getenv("NOTS_STORE_PATH", os.path.join(home_dir(), ".nots"))
  37. build_path = os.path.join(nots_store_path, "nm_store", moddir)
  38. return build_path
  39. def extract_package_name_from_path(p):
  40. # if we have scope prefix then we are using the first two tokens, otherwise - only the first one
  41. parts = p.split("/", 2)
  42. return "/".join(parts[:2]) if p.startswith("@") else parts[0]