blob: 07745ee1be942c904b5e2007421683787e90c494 [file] [log] [blame]
developer843fd2b2022-04-15 18:30:12 +08001#!/usr/bin/python3 -u
2import argparse
3import os
4import sys
5import traceback
6
7__author__ = "Sam Shih <sam.shih@medaitek.com>"
8__copyright__ = "Copyright 2022, MediaTek Inc"
9
10
11class formatter(argparse.HelpFormatter):
12 def _format_usage(self, usage, actions, groups, prefix):
13 if prefix is None:
14 prefix = "usage: "
15 if usage is not None:
16 usage = usage % dict(prog=self._prog)
17
18 elif usage is None and not actions:
19 usage = "%(prog)s" % dict(prog=self._prog)
20 elif usage is None:
21 prog = "%(prog)s" % dict(prog=self._prog)
22 action_usage = self._format_actions_usage(actions, groups)
23 usage = " ".join([s for s in [prog, action_usage] if s])
24 return "%s%s\n\n" % (prefix, usage)
25
26
27parser = argparse.ArgumentParser(formatter_class=formatter)
28parser.add_argument(
29 "input", nargs="+", help="openwrt patch folder which should be converted"
30)
31parser.add_argument(
32 "--output", help="output .inc file used by RDKB", default="$(name).inc"
33)
34args = parser.parse_args()
35
36
37def find_patch_series(root, level=0, max_level=1):
38 patch = []
39 dir_l = os.listdir(root)
40 dir_l.sort()
41 for name in dir_l:
42 path = os.path.join(root, name)
43 if not os.path.isdir(path):
44 if "." in name:
45 if name.split(".")[-1] == "patch":
46 if "cover-letter" not in name:
47 patch.append(name)
48 patch.sort()
49 return patch
50
51
52def create_inc_file(output, name, patch_l):
53 f = open(output, "w")
54 header_code = (
55 "#patch %s (come from openwrt/lede/target/linux/mediatek)\n" % name
56 )
57 script_code = "SRC_URI_append = \" \\\n"
58 space = " "
59 prefix = "%sfile://" % space
60 postfix = " \\"
61 patch_l_code = "\n".join((prefix + t + postfix) for t in patch_l) + "\n"
62 footer = '%s"\n' % space
63 code = header_code + script_code + patch_l_code + footer
64 f.write(code)
65 f.close()
66
67
68try:
69 input_l = args.input
70 if len(input_l) == 1:
71 name = os.path.basename(os.path.realpath(input_l[0]))
72 else:
73 name = "_".join(os.path.basename(os.path.realpath(t)) for t in input_l)
74 output = args.output.replace("$(name)", name)
75 patch_l = []
76 for input in input_l:
77 patch_l.extend(find_patch_series(input))
78 create_inc_file(output, name, patch_l)
79 print('saved RDKB .inc file to "%s"' % output)
80
81except:
82 traceback.print_exc()