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
|
#!/usr/bin/env python
import os, shutil, json, pprint, sys
from collections import OrderedDict
help_text = """
Usage: python {} [options] <full-noridc-sdk-path> <nrf51-sdk-yotta-module-path>
options: --purge : to delete all existing files and start again
--dry-run : to list the files to be copied but not actually copy them
""".format(os.path.basename(__file__))
# exclude path to avoid confusion over files of the same name
exclude_path = ["examples", "SVD", "s110", "s120", "s210", "s310", "nrf_soc_nosd", "serialization/connectivity",
'components/libraries/hci/config', 'components/libraries/bootloader_dfu/ble_transport']
def find(name, path):
paths = []
for root, dirs, files in os.walk(path):
if True not in [x in root for x in exclude_path]:
if name in files:
paths.append(os.path.join(root, name))
if len(paths) == 0:
print "-"*30
print "Warning! No {} found!!!!".format(name)
print "-"*30
return None
elif len(paths) > 1:
print "-"*30
print "Warning! More than one {} found!!!!".format(name)
print paths
print "-"*30
return None
else:
return paths[0]
def find_dir(dir_name, path):
paths = []
for root, dirs, files in os.walk(path):
if dir_name in root:
for fn in files:
paths.append(os.path.join(root, fn))
return paths
if __name__ == "__main__":
# define source and destination of copy
arg_valid = True
if len(sys.argv) not in [3, 4]:
arg_valid = False
else:
src_folder = sys.argv[-2]
yt_module_dir = sys.argv[-1]
for d in [src_folder, yt_module_dir]:
if not os.path.isdir(d):
arg_valid = False
print src_folder, "is not a folder"
purge = ("--purge" in sys.argv)
dry_run = ("--dry-run" in sys.argv)
if not arg_valid:
print help_text
sys.exit(1)
dst_folder = os.path.join(yt_module_dir, "source/nordic_sdk")
# build a file_list from required_files.txt
file_list = []
with open("required_files.txt", "r") as fd:
for line in fd:
line = line.strip()
if line.startswith("D "):
directory = line.split(" ")[-1]
file_list += find_dir(directory, src_folder)
elif not line.startswith("#") and line != '':
fn = os.path.basename(line).strip()
fn = find(fn, src_folder)
file_list.append(fn)
# remove everything from the destination folder
if purge and not dry_run and os.path.exists(dst_folder):
shutil.rmtree(dst_folder)
# copy files
extra_includes = []
for src in file_list:
if src:
rel_dst = os.path.relpath(src, src_folder)
dst = os.path.join(dst_folder, rel_dst)
print src, "->", dst
directory = os.path.dirname(dst)
if not os.path.exists(directory):
print "Creating directory:", directory
if not dry_run:
os.makedirs(directory)
if not os.path.isfile(dst):
print "Copying file", dst
if not dry_run:
shutil.copyfile(src, dst)
# build a list of extra includes to be added to module.json
if dst.endswith(".h"):
inc_rel_path = os.path.relpath(dst, yt_module_dir)
inc_dir_path = os.path.dirname(inc_rel_path)
if inc_dir_path not in extra_includes:
extra_includes.append(inc_dir_path)
# write extraIncludes in the module.json file
mod_json = os.path.join(yt_module_dir, "module.json")
print "-"*30
print "Writing extra_includes to {}".format(mod_json)
print "-"*30
for n in sorted(extra_includes):
print n
if not dry_run:
with open(mod_json, 'r+') as fd:
jobj = json.loads(fd.read(), object_pairs_hook=OrderedDict)
jobj['extraIncludes'] = sorted(extra_includes)
jdump = json.dumps(jobj, indent=2, separators=(',', ': '))
fd.seek(0)
fd.write(jdump)
fd.write("\n")
fd.truncate()
|