aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/base/node_modules_bundler.py
blob: 3a1d7213d122fb444e349b203c54742b12301f93 (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
import os
import tempfile

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
    """
    import library.python.archive as archive

    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.
    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):
    """
    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
    """
    import library.python.archive as archive

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