Browse Source

Отказаться от tarfile в пользу archive
2613c273019f722f1204406c28ef0d2fd7d1ab22

vturov 10 months ago
parent
commit
23fa83f57d

+ 26 - 30
build/plugins/lib/nots/package_manager/base/node_modules_bundler.py

@@ -1,9 +1,5 @@
 import os
-import sys
-import subprocess
-import tarfile
-
-from io import BytesIO
+import tempfile
 
 from .utils import build_nm_path
 
@@ -25,24 +21,30 @@ def bundle_node_modules(build_root, peers, node_modules_path, bundle_path):
     :param bundle_path: tarball path
     :type bundle_path: str
     """
-    with tarfile.open(bundle_path, "w") as tf:
-        tf.add(node_modules_path, arcname=".")
+    import library.python.archive as archive
 
-        # Peers' node_modules.
-        added_peers = []
-        for p in peers:
-            peer_nm_path = build_nm_path(os.path.join(build_root, p))
-            peer_bundled_nm_path = build_nm_path(os.path.join(PEERS_DIR, p))
-            if not os.path.isdir(peer_nm_path):
-                continue
-            tf.add(peer_nm_path, arcname=peer_bundled_nm_path)
-            added_peers.append(p)
+    paths_to_pack = []
+    paths_to_pack.append((node_modules_path, "."))
+
+    # Peers' node_modules.
+    added_peers = []
+    for p in peers:
+        peer_nm_path = build_nm_path(os.path.join(build_root, p))
+        peer_bundled_nm_path = build_nm_path(os.path.join(PEERS_DIR, p))
+        if not os.path.isdir(peer_nm_path):
+            continue
+        paths_to_pack.append((peer_nm_path, peer_bundled_nm_path))
+        added_peers.append(p)
 
-        # Peers index.
-        peers_index = "\n".join(added_peers)
-        ti = tarfile.TarInfo(name=os.path.join(PEERS_DIR, PEERS_INDEX))
-        ti.size = len(peers_index)
-        tf.addfile(ti, BytesIO(peers_index.encode()))
+    # Peers index.
+    with tempfile.TemporaryDirectory() as temp_dir:
+        peers_index_tmppath = os.path.join(temp_dir, PEERS_INDEX)
+        peers_index_relpath = os.path.join(PEERS_DIR, PEERS_INDEX)
+        with open(peers_index_tmppath, "w") as peers_index:
+            peers_index.write("\n".join(added_peers))
+        paths_to_pack.append((peers_index_tmppath, peers_index_relpath))
+
+        archive.tar(paths_to_pack, bundle_path, compression_filter=None, compression_level=None, fixed_mtime=0)
 
 
 def extract_node_modules(build_root, node_modules_path, bundle_path):
@@ -55,16 +57,10 @@ def extract_node_modules(build_root, node_modules_path, bundle_path):
     :param bundle_path: tarball path
     :type bundle_path: str
     """
-    os.makedirs(node_modules_path, exist_ok=True)
-    tar_unpack_cmd = ["tar", "xf", bundle_path, "-C", node_modules_path]
-    p = subprocess.run(tar_unpack_cmd, capture_output=True, text=True)
-    if p.returncode != 0:
-        if p.stdout:
-            sys.stderr.write(f"stdout:\n{p.stdout}\n")
-        if p.stderr:
-            sys.stderr.write(f"stderr:\n{p.stderr}\n")
+    import library.python.archive as archive
 
-        return False
+    os.makedirs(node_modules_path, exist_ok=True)
+    archive.extract_tar(bundle_path, node_modules_path, fail_on_duplicates=False)
 
     with open(os.path.join(node_modules_path, PEERS_DIR, PEERS_INDEX)) as peers_file:
         peers = peers_file.read().split("\n")

+ 1 - 0
build/plugins/lib/nots/package_manager/base/ya.make

@@ -16,6 +16,7 @@ PY_SRCS(
 
 PEERDIR(
     contrib/python/six
+    library/python/archive
 )
 
 END()