R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pick_nrf51_files.py Source File

pick_nrf51_files.py

00001 #!/usr/bin/env python
00002 
00003 import os, shutil, json, pprint, sys
00004 from collections import OrderedDict
00005 
00006 help_text = """
00007 Usage: python {} [options] <full-noridc-sdk-path> <nrf51-sdk-yotta-module-path>
00008 options: --purge   : to delete all existing files and start again
00009          --dry-run : to list the files to be copied but not actually copy them
00010 """.format(os.path.basename(__file__))
00011 
00012 # exclude path to avoid confusion over files of the same name
00013 exclude_path = ["examples", "SVD", "s110", "s120", "s210", "s310", "nrf_soc_nosd", "serialization/connectivity",
00014                 'components/libraries/hci/config', 'components/libraries/bootloader_dfu/ble_transport']
00015 
00016 def find(name, path):
00017     paths = []
00018     for root, dirs, files in os.walk(path):
00019         if True not in [x in root for x in exclude_path]:
00020             if name in files:
00021                 paths.append(os.path.join(root, name))
00022 
00023     if len(paths) == 0:
00024         print "-"*30
00025         print "Warning! No {} found!!!!".format(name)
00026         print "-"*30
00027         return None
00028     elif len(paths) > 1:
00029         print "-"*30
00030         print "Warning! More than one {} found!!!!".format(name)
00031         print paths
00032         print "-"*30
00033         return None
00034     else:
00035         return paths[0]
00036 
00037 def find_dir(dir_name, path):
00038     paths = []
00039     for root, dirs, files in os.walk(path):
00040         if dir_name in root:
00041             for fn in files:
00042                 paths.append(os.path.join(root, fn))
00043     return paths
00044 
00045 if __name__ == "__main__":
00046     # define source and destination of copy
00047     arg_valid = True
00048     if len(sys.argv) not in [3, 4]:
00049         arg_valid = False
00050     else:
00051         src_folder = sys.argv[-2]
00052         yt_module_dir = sys.argv[-1]
00053 
00054         for d in [src_folder, yt_module_dir]:
00055             if not os.path.isdir(d):
00056                 arg_valid = False
00057                 print src_folder, "is not a folder"
00058 
00059     purge = ("--purge" in sys.argv)
00060     dry_run = ("--dry-run" in sys.argv)
00061 
00062     if not arg_valid:
00063         print help_text
00064         sys.exit(1)
00065 
00066     dst_folder = os.path.join(yt_module_dir, "source/nordic_sdk")
00067 
00068     # build a file_list from required_files.txt
00069     file_list = []
00070     with open("required_files.txt", "r") as fd:
00071         for line in fd:
00072             line = line.strip()
00073             if line.startswith("D "):
00074                 directory = line.split(" ")[-1]
00075                 file_list += find_dir(directory, src_folder)
00076             elif not line.startswith("#") and line != '':
00077                 fn = os.path.basename(line).strip()
00078                 fn = find(fn, src_folder)
00079                 file_list.append(fn)
00080 
00081     # remove everything from the destination folder
00082     if purge and not dry_run and os.path.exists(dst_folder):
00083         shutil.rmtree(dst_folder)
00084 
00085     # copy files
00086     extra_includes = []
00087     for src in file_list:
00088         if src:
00089             rel_dst = os.path.relpath(src, src_folder)
00090             dst = os.path.join(dst_folder, rel_dst)
00091             print src, "->", dst
00092 
00093             directory = os.path.dirname(dst)
00094             if not os.path.exists(directory):
00095                 print "Creating directory:", directory
00096                 if not dry_run:
00097                     os.makedirs(directory)
00098             if not os.path.isfile(dst):
00099                 print "Copying file", dst
00100                 if not dry_run:
00101                     shutil.copyfile(src, dst)
00102 
00103             # build a list of extra includes to be added to module.json
00104             if dst.endswith(".h"):
00105                 inc_rel_path = os.path.relpath(dst, yt_module_dir)
00106                 inc_dir_path = os.path.dirname(inc_rel_path)
00107                 if inc_dir_path not in extra_includes:
00108                     extra_includes.append(inc_dir_path)
00109 
00110     # write extraIncludes in the module.json file
00111     mod_json = os.path.join(yt_module_dir, "module.json")
00112     print "-"*30
00113     print "Writing extra_includes to {}".format(mod_json)
00114     print "-"*30
00115     for n in sorted(extra_includes):
00116         print n
00117 
00118     if not dry_run:
00119         with open(mod_json, 'r+') as fd:
00120             jobj = json.loads(fd.read(), object_pairs_hook=OrderedDict)
00121             jobj['extraIncludes'] = sorted(extra_includes)
00122             jdump = json.dumps(jobj, indent=2, separators=(',', ': '))
00123             fd.seek(0)
00124             fd.write(jdump)
00125             fd.write("\n")
00126             fd.truncate()