aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/base/node_modules_bundler.py
blob: c632ecb24a389ed77b85e28a9ae74da3e44ef668 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import sys
import subprocess
import tarfile

from io import BytesIO

from .utils import build_nm_path

PEERS_DIR = ".peers"
PEERS_INDEX = "index"


def bundle_node_modules(build_root, peers, node_modules_path, bundle_path):
    """
    Creates node_modules bundle.
    Bundle contains node_modules directory, peers' node_modules directories,
    and index file with the list of added peers (\\n delimited).
    :param build_root: arcadia build root
    :type build_root: str
    :param peers: list of peers (arcadia root related)
    :type peers: list of str
    :param node_modules_path: node_modules path
    :type node_modules_path: str
    :param bundle_path: tarball path
    :type bundle_path: str
    """
    with tarfile.open(bundle_path, "w") as tf:
        tf.add(node_modules_path, arcname=".")

        # 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)

        # 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()))


def extract_node_modules(build_root, node_modules_path, bundle_path):
    """
    Extracts node_modules bundle.
    :param build_root: arcadia build root
    :type build_root: str
    :param node_modules_path: node_modules path
    :type node_modules_path: str
    :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")

        return False

    with open(os.path.join(node_modules_path, PEERS_DIR, PEERS_INDEX)) as peers_file:
        peers = peers_file.read().split("\n")
        for p in peers:
            if not p:
                continue
            bundled_nm_path = build_nm_path(os.path.join(node_modules_path, PEERS_DIR, p))
            nm_path = build_nm_path(os.path.join(build_root, p))
            os.rename(bundled_nm_path, nm_path)

    return True