developer | 843fd2b | 2022-04-15 18:30:12 +0800 | [diff] [blame^] | 1 | #!/usr/bin/python3 -u |
| 2 | import argparse |
| 3 | import os |
| 4 | import sys |
| 5 | import traceback |
| 6 | |
| 7 | __author__ = "Sam Shih <sam.shih@medaitek.com>" |
| 8 | __copyright__ = "Copyright 2022, MediaTek Inc" |
| 9 | |
| 10 | |
| 11 | class 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 | |
| 27 | parser = argparse.ArgumentParser(formatter_class=formatter) |
| 28 | parser.add_argument( |
| 29 | "input", nargs="+", help="openwrt patch folder which should be converted" |
| 30 | ) |
| 31 | parser.add_argument( |
| 32 | "--output", help="output .inc file used by RDKB", default="$(name).inc" |
| 33 | ) |
| 34 | args = parser.parse_args() |
| 35 | |
| 36 | |
| 37 | def 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 | |
| 52 | def 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 | |
| 68 | try: |
| 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 | |
| 81 | except: |
| 82 | traceback.print_exc() |