node_modules_bundler.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import tempfile
  3. import logging
  4. from .utils import build_nm_path
  5. from .timeit import timeit
  6. PEERS_DIR = ".peers"
  7. PEERS_INDEX = "index"
  8. @timeit
  9. def bundle_node_modules(build_root, peers, node_modules_path, bundle_path):
  10. """
  11. Creates node_modules bundle.
  12. Bundle contains node_modules directory, peers' node_modules directories,
  13. and index file with the list of added peers (\\n delimited).
  14. :param build_root: arcadia build root
  15. :type build_root: str
  16. :param peers: list of peers (arcadia root related)
  17. :type peers: list of str
  18. :param node_modules_path: node_modules path
  19. :type node_modules_path: str
  20. :param bundle_path: tarball path
  21. :type bundle_path: str
  22. """
  23. import library.python.archive as archive
  24. paths_to_pack = []
  25. paths_to_pack.append((node_modules_path, "."))
  26. # Peers' node_modules.
  27. added_peers = []
  28. peers = set(peers)
  29. for p in peers:
  30. peer_nm_path = build_nm_path(os.path.join(build_root, p))
  31. peer_bundled_nm_path = build_nm_path(os.path.join(PEERS_DIR, p))
  32. if not os.path.isdir(peer_nm_path):
  33. continue
  34. paths_to_pack.append((peer_nm_path, peer_bundled_nm_path))
  35. added_peers.append(p)
  36. # Peers index.
  37. with tempfile.TemporaryDirectory() as temp_dir:
  38. peers_index_tmppath = os.path.join(temp_dir, PEERS_INDEX)
  39. peers_index_relpath = os.path.join(PEERS_DIR, PEERS_INDEX)
  40. with open(peers_index_tmppath, "w") as peers_index:
  41. peers_index.write("\n".join(added_peers))
  42. paths_to_pack.append((peers_index_tmppath, peers_index_relpath))
  43. archive.tar(set(paths_to_pack), bundle_path, compression_filter=None, compression_level=None, fixed_mtime=0)
  44. def extract_node_modules(build_root, node_modules_path, bundle_path):
  45. """
  46. Extracts node_modules bundle.
  47. :param build_root: arcadia build root
  48. :type build_root: str
  49. :param node_modules_path: node_modules path
  50. :type node_modules_path: str
  51. :param bundle_path: tarball path
  52. :type bundle_path: str
  53. """
  54. import library.python.archive as archive
  55. os.makedirs(node_modules_path, exist_ok=True)
  56. archive.extract_tar(bundle_path, node_modules_path, fail_on_duplicates=False)
  57. with open(os.path.join(node_modules_path, PEERS_DIR, PEERS_INDEX)) as peers_file:
  58. peers = peers_file.read().split("\n")
  59. for p in peers:
  60. if not p:
  61. continue
  62. bundled_nm_path = build_nm_path(os.path.join(node_modules_path, PEERS_DIR, p))
  63. nm_path = build_nm_path(os.path.join(build_root, p))
  64. try:
  65. if os.path.exists(bundled_nm_path):
  66. os.rename(bundled_nm_path, nm_path)
  67. except FileNotFoundError as e:
  68. parent_directory = os.path.dirname(nm_path)
  69. logging.error(f"bundled_nm exists={os.path.exists(bundled_nm_path)} : {bundled_nm_path}")
  70. logging.error(f"nm_path/.. exists={os.path.exists(parent_directory)} : {parent_directory}")
  71. raise e
  72. return True