aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/deps/import_rpclib.py
blob: 2baccab1ae121d61ac68f29837535049a04c9663 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
#
# Copyright 2017 Ettus Research (National Instruments)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""
Tool to import external dependency rpclib into UHD tree
"""

import argparse
from distutils.dir_util import copy_tree
import os
import re
import shutil
import subprocess
import tempfile

from mako.template import Template

IMPORTANT_FILES = [
    "lib",
    "include",
    "LICENSE.md",
    "README.md",
    "CHANGELOG.md"
]

REPLACE_EXPR = [
    ("RPCLIB_ASIO", "boost::asio"),
    ("\"asio.hpp\"", "<boost/asio.hpp>"),
    ("std::error_code", "boost::system::error_code")
]


def fix_naming(workdir):
    """
    Applies all the REPLACE_EXPR in all the files
    """
    for root, dirs, files in os.walk(workdir):
        for cur_file in files:
            if re.match(".*[hc](?:c?|\.in)|inl",cur_file):
                new_file = tempfile.NamedTemporaryFile()
                with new_file as new:
                    with open(os.path.join(root,cur_file), "r") as old:
                        for line in old:
                            for repl in REPLACE_EXPR:
                                line = line.replace(*repl)
                            new.write(line)
                    new.truncate()
                    shutil.copyfile(new_file.name, os.path.join(root, cur_file))

def extract_rpclib_version(rpclib_root):
    """
    match version definition in rpclib cmakelists
    """
    with open(os.path.join(rpclib_root, "CMakeLists.txt")) as rpclib_cmake:
        version_dict = {}
        for line in rpclib_cmake:
            version_match =re.match("^.*RPCLIB_VERSION_(?P<which>MAJOR|MINOR|PATCH) (?P<number>[0-9]+).*$", line)
            if version_match:
                version_dict[version_match.group("which").lower()] = int(version_match.group("number"))
        if not set(version_dict.keys()).issuperset(set(["major", "minor", "patch"])):
            raise Exception("couldn't find right version definitions in CMakeLists.txt")
        return (version_dict["major"], version_dict["minor"], version_dict["patch"])


def copy_important_files(target_root, rpclib_root):
    """
    copy files/subdirs we consider important (see IMPORTANT_FILES)
    """
    for rpc_file in os.listdir(rpclib_root):
        if rpc_file in IMPORTANT_FILES:
            if os.path.isdir(os.path.join(rpclib_root, rpc_file)):
                copy_tree(
                    os.path.join(rpclib_root, rpc_file),
                    os.path.join(target_root, rpc_file)
                )
            else:
                shutil.copyfile(
                    os.path.join(rpclib_root, rpc_file),
                    os.path.join(target_root, rpc_file)
                )


def patch_rpclib(rpclib_root):
    """
    applies all *.patch files in the same directory as the script
    """
    curr_dir = os.path.dirname(os.path.realpath(__file__))
    for rpc_patch in (patch for patch in sorted(os.listdir(curr_dir))
                      if patch.endswith("patch")):
        print("patching with: {0}".format(rpc_patch))
        patch = open(os.path.join(curr_dir, rpc_patch), "r")
        patch_proc = subprocess.Popen(['patch', '--quiet', '-p1'], stdin=patch, cwd=rpclib_root)


def extract_rpclib(args):
    """
    THE extraction function
    """
    workdir = tempfile.mkdtemp()
    rpclib_root = args.rpclib_root
    new_rpclib_root = tempfile.mkdtemp()
    copy_tree(rpclib_root, new_rpclib_root)
    patch_rpclib(new_rpclib_root)
    copy_important_files(workdir, new_rpclib_root)
    version = extract_rpclib_version(new_rpclib_root)
    shutil.rmtree(new_rpclib_root)
    cmake_template = Template(
        filename=os.path.join(os.path.dirname(os.path.realpath(__file__)), "rpc_CMakeLists.txt"))
    real_cmake_file = cmake_template.render(
        rpclib_major_version=version[0],
        rpclib_minor_version=version[1],
        rpclib_patch_version=version[2]
    )
    with open(os.path.join(workdir, "CMakeLists.txt"), "w") as cmake_file:
        cmake_file.write(real_cmake_file)
        cmake_file.truncate()
    fix_naming(workdir)
    copy_tree(workdir, args.rpclib_target, update=True)
    shutil.rmtree(workdir)


def parse_args():
    """
    parse cmdline arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("rpclib_root")
    parser.add_argument('rpclib_target')
    return parser.parse_args()


def main():
    """
    Execute this tool
    """
    args = parse_args()
    extract_rpclib(args)
    return True

if __name__ == "__main__":
    exit(not main())