aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/base/node_modules_bundler.py
blob: c835c4d7ca3fa01041e5e34743549bceffbed1b3 (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
import os
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
    """
    with tarfile.open(bundle_path) as tf:
        tf.extractall(node_modules_path)

    peers = open(os.path.join(node_modules_path, PEERS_DIR, PEERS_INDEX)).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)