nrf51-sdk
Fork of nrf51-sdk by
script/pick_nrf51_files.py@9:43576f23865a, 2018-11-06 (annotated)
- Committer:
- jancumps
- Date:
- Tue Nov 06 20:57:21 2018 +0000
- Revision:
- 9:43576f23865a
- Parent:
- 0:bc2961fa1ef0
i did not change a thing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:bc2961fa1ef0 | 1 | #!/usr/bin/env python |
Jonathan Austin |
0:bc2961fa1ef0 | 2 | |
Jonathan Austin |
0:bc2961fa1ef0 | 3 | import os, shutil, json, pprint, sys |
Jonathan Austin |
0:bc2961fa1ef0 | 4 | from collections import OrderedDict |
Jonathan Austin |
0:bc2961fa1ef0 | 5 | |
Jonathan Austin |
0:bc2961fa1ef0 | 6 | help_text = """ |
Jonathan Austin |
0:bc2961fa1ef0 | 7 | Usage: python {} [options] <full-noridc-sdk-path> <nrf51-sdk-yotta-module-path> |
Jonathan Austin |
0:bc2961fa1ef0 | 8 | options: --purge : to delete all existing files and start again |
Jonathan Austin |
0:bc2961fa1ef0 | 9 | --dry-run : to list the files to be copied but not actually copy them |
Jonathan Austin |
0:bc2961fa1ef0 | 10 | """.format(os.path.basename(__file__)) |
Jonathan Austin |
0:bc2961fa1ef0 | 11 | |
Jonathan Austin |
0:bc2961fa1ef0 | 12 | # exclude path to avoid confusion over files of the same name |
Jonathan Austin |
0:bc2961fa1ef0 | 13 | exclude_path = ["examples", "SVD", "s110", "s120", "s210", "s310", "nrf_soc_nosd", "serialization/connectivity", |
Jonathan Austin |
0:bc2961fa1ef0 | 14 | 'components/libraries/hci/config', 'components/libraries/bootloader_dfu/ble_transport'] |
Jonathan Austin |
0:bc2961fa1ef0 | 15 | |
Jonathan Austin |
0:bc2961fa1ef0 | 16 | def find(name, path): |
Jonathan Austin |
0:bc2961fa1ef0 | 17 | paths = [] |
Jonathan Austin |
0:bc2961fa1ef0 | 18 | for root, dirs, files in os.walk(path): |
Jonathan Austin |
0:bc2961fa1ef0 | 19 | if True not in [x in root for x in exclude_path]: |
Jonathan Austin |
0:bc2961fa1ef0 | 20 | if name in files: |
Jonathan Austin |
0:bc2961fa1ef0 | 21 | paths.append(os.path.join(root, name)) |
Jonathan Austin |
0:bc2961fa1ef0 | 22 | |
Jonathan Austin |
0:bc2961fa1ef0 | 23 | if len(paths) == 0: |
Jonathan Austin |
0:bc2961fa1ef0 | 24 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 25 | print "Warning! No {} found!!!!".format(name) |
Jonathan Austin |
0:bc2961fa1ef0 | 26 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 27 | return None |
Jonathan Austin |
0:bc2961fa1ef0 | 28 | elif len(paths) > 1: |
Jonathan Austin |
0:bc2961fa1ef0 | 29 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 30 | print "Warning! More than one {} found!!!!".format(name) |
Jonathan Austin |
0:bc2961fa1ef0 | 31 | print paths |
Jonathan Austin |
0:bc2961fa1ef0 | 32 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 33 | return None |
Jonathan Austin |
0:bc2961fa1ef0 | 34 | else: |
Jonathan Austin |
0:bc2961fa1ef0 | 35 | return paths[0] |
Jonathan Austin |
0:bc2961fa1ef0 | 36 | |
Jonathan Austin |
0:bc2961fa1ef0 | 37 | def find_dir(dir_name, path): |
Jonathan Austin |
0:bc2961fa1ef0 | 38 | paths = [] |
Jonathan Austin |
0:bc2961fa1ef0 | 39 | for root, dirs, files in os.walk(path): |
Jonathan Austin |
0:bc2961fa1ef0 | 40 | if dir_name in root: |
Jonathan Austin |
0:bc2961fa1ef0 | 41 | for fn in files: |
Jonathan Austin |
0:bc2961fa1ef0 | 42 | paths.append(os.path.join(root, fn)) |
Jonathan Austin |
0:bc2961fa1ef0 | 43 | return paths |
Jonathan Austin |
0:bc2961fa1ef0 | 44 | |
Jonathan Austin |
0:bc2961fa1ef0 | 45 | if __name__ == "__main__": |
Jonathan Austin |
0:bc2961fa1ef0 | 46 | # define source and destination of copy |
Jonathan Austin |
0:bc2961fa1ef0 | 47 | arg_valid = True |
Jonathan Austin |
0:bc2961fa1ef0 | 48 | if len(sys.argv) not in [3, 4]: |
Jonathan Austin |
0:bc2961fa1ef0 | 49 | arg_valid = False |
Jonathan Austin |
0:bc2961fa1ef0 | 50 | else: |
Jonathan Austin |
0:bc2961fa1ef0 | 51 | src_folder = sys.argv[-2] |
Jonathan Austin |
0:bc2961fa1ef0 | 52 | yt_module_dir = sys.argv[-1] |
Jonathan Austin |
0:bc2961fa1ef0 | 53 | |
Jonathan Austin |
0:bc2961fa1ef0 | 54 | for d in [src_folder, yt_module_dir]: |
Jonathan Austin |
0:bc2961fa1ef0 | 55 | if not os.path.isdir(d): |
Jonathan Austin |
0:bc2961fa1ef0 | 56 | arg_valid = False |
Jonathan Austin |
0:bc2961fa1ef0 | 57 | print src_folder, "is not a folder" |
Jonathan Austin |
0:bc2961fa1ef0 | 58 | |
Jonathan Austin |
0:bc2961fa1ef0 | 59 | purge = ("--purge" in sys.argv) |
Jonathan Austin |
0:bc2961fa1ef0 | 60 | dry_run = ("--dry-run" in sys.argv) |
Jonathan Austin |
0:bc2961fa1ef0 | 61 | |
Jonathan Austin |
0:bc2961fa1ef0 | 62 | if not arg_valid: |
Jonathan Austin |
0:bc2961fa1ef0 | 63 | print help_text |
Jonathan Austin |
0:bc2961fa1ef0 | 64 | sys.exit(1) |
Jonathan Austin |
0:bc2961fa1ef0 | 65 | |
Jonathan Austin |
0:bc2961fa1ef0 | 66 | dst_folder = os.path.join(yt_module_dir, "source/nordic_sdk") |
Jonathan Austin |
0:bc2961fa1ef0 | 67 | |
Jonathan Austin |
0:bc2961fa1ef0 | 68 | # build a file_list from required_files.txt |
Jonathan Austin |
0:bc2961fa1ef0 | 69 | file_list = [] |
Jonathan Austin |
0:bc2961fa1ef0 | 70 | with open("required_files.txt", "r") as fd: |
Jonathan Austin |
0:bc2961fa1ef0 | 71 | for line in fd: |
Jonathan Austin |
0:bc2961fa1ef0 | 72 | line = line.strip() |
Jonathan Austin |
0:bc2961fa1ef0 | 73 | if line.startswith("D "): |
Jonathan Austin |
0:bc2961fa1ef0 | 74 | directory = line.split(" ")[-1] |
Jonathan Austin |
0:bc2961fa1ef0 | 75 | file_list += find_dir(directory, src_folder) |
Jonathan Austin |
0:bc2961fa1ef0 | 76 | elif not line.startswith("#") and line != '': |
Jonathan Austin |
0:bc2961fa1ef0 | 77 | fn = os.path.basename(line).strip() |
Jonathan Austin |
0:bc2961fa1ef0 | 78 | fn = find(fn, src_folder) |
Jonathan Austin |
0:bc2961fa1ef0 | 79 | file_list.append(fn) |
Jonathan Austin |
0:bc2961fa1ef0 | 80 | |
Jonathan Austin |
0:bc2961fa1ef0 | 81 | # remove everything from the destination folder |
Jonathan Austin |
0:bc2961fa1ef0 | 82 | if purge and not dry_run and os.path.exists(dst_folder): |
Jonathan Austin |
0:bc2961fa1ef0 | 83 | shutil.rmtree(dst_folder) |
Jonathan Austin |
0:bc2961fa1ef0 | 84 | |
Jonathan Austin |
0:bc2961fa1ef0 | 85 | # copy files |
Jonathan Austin |
0:bc2961fa1ef0 | 86 | extra_includes = [] |
Jonathan Austin |
0:bc2961fa1ef0 | 87 | for src in file_list: |
Jonathan Austin |
0:bc2961fa1ef0 | 88 | if src: |
Jonathan Austin |
0:bc2961fa1ef0 | 89 | rel_dst = os.path.relpath(src, src_folder) |
Jonathan Austin |
0:bc2961fa1ef0 | 90 | dst = os.path.join(dst_folder, rel_dst) |
Jonathan Austin |
0:bc2961fa1ef0 | 91 | print src, "->", dst |
Jonathan Austin |
0:bc2961fa1ef0 | 92 | |
Jonathan Austin |
0:bc2961fa1ef0 | 93 | directory = os.path.dirname(dst) |
Jonathan Austin |
0:bc2961fa1ef0 | 94 | if not os.path.exists(directory): |
Jonathan Austin |
0:bc2961fa1ef0 | 95 | print "Creating directory:", directory |
Jonathan Austin |
0:bc2961fa1ef0 | 96 | if not dry_run: |
Jonathan Austin |
0:bc2961fa1ef0 | 97 | os.makedirs(directory) |
Jonathan Austin |
0:bc2961fa1ef0 | 98 | if not os.path.isfile(dst): |
Jonathan Austin |
0:bc2961fa1ef0 | 99 | print "Copying file", dst |
Jonathan Austin |
0:bc2961fa1ef0 | 100 | if not dry_run: |
Jonathan Austin |
0:bc2961fa1ef0 | 101 | shutil.copyfile(src, dst) |
Jonathan Austin |
0:bc2961fa1ef0 | 102 | |
Jonathan Austin |
0:bc2961fa1ef0 | 103 | # build a list of extra includes to be added to module.json |
Jonathan Austin |
0:bc2961fa1ef0 | 104 | if dst.endswith(".h"): |
Jonathan Austin |
0:bc2961fa1ef0 | 105 | inc_rel_path = os.path.relpath(dst, yt_module_dir) |
Jonathan Austin |
0:bc2961fa1ef0 | 106 | inc_dir_path = os.path.dirname(inc_rel_path) |
Jonathan Austin |
0:bc2961fa1ef0 | 107 | if inc_dir_path not in extra_includes: |
Jonathan Austin |
0:bc2961fa1ef0 | 108 | extra_includes.append(inc_dir_path) |
Jonathan Austin |
0:bc2961fa1ef0 | 109 | |
Jonathan Austin |
0:bc2961fa1ef0 | 110 | # write extraIncludes in the module.json file |
Jonathan Austin |
0:bc2961fa1ef0 | 111 | mod_json = os.path.join(yt_module_dir, "module.json") |
Jonathan Austin |
0:bc2961fa1ef0 | 112 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 113 | print "Writing extra_includes to {}".format(mod_json) |
Jonathan Austin |
0:bc2961fa1ef0 | 114 | print "-"*30 |
Jonathan Austin |
0:bc2961fa1ef0 | 115 | for n in sorted(extra_includes): |
Jonathan Austin |
0:bc2961fa1ef0 | 116 | print n |
Jonathan Austin |
0:bc2961fa1ef0 | 117 | |
Jonathan Austin |
0:bc2961fa1ef0 | 118 | if not dry_run: |
Jonathan Austin |
0:bc2961fa1ef0 | 119 | with open(mod_json, 'r+') as fd: |
Jonathan Austin |
0:bc2961fa1ef0 | 120 | jobj = json.loads(fd.read(), object_pairs_hook=OrderedDict) |
Jonathan Austin |
0:bc2961fa1ef0 | 121 | jobj['extraIncludes'] = sorted(extra_includes) |
Jonathan Austin |
0:bc2961fa1ef0 | 122 | jdump = json.dumps(jobj, indent=2, separators=(',', ': ')) |
Jonathan Austin |
0:bc2961fa1ef0 | 123 | fd.seek(0) |
Jonathan Austin |
0:bc2961fa1ef0 | 124 | fd.write(jdump) |
Jonathan Austin |
0:bc2961fa1ef0 | 125 | fd.write("\n") |
Jonathan Austin |
0:bc2961fa1ef0 | 126 | fd.truncate() |