mpy_cross_all.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import os.path
  5. argparser = argparse.ArgumentParser(description="Compile all .py files to .mpy recursively")
  6. argparser.add_argument("-o", "--out", help="output directory (default: input dir)")
  7. argparser.add_argument("--target", help="select MicroPython target config")
  8. argparser.add_argument("-mcache-lookup-bc", action="store_true", help="cache map lookups in the bytecode")
  9. argparser.add_argument("dir", help="input directory")
  10. args = argparser.parse_args()
  11. TARGET_OPTS = {
  12. "unix": "-mcache-lookup-bc",
  13. "baremetal": "",
  14. }
  15. args.dir = args.dir.rstrip("/")
  16. if not args.out:
  17. args.out = args.dir
  18. path_prefix_len = len(args.dir) + 1
  19. for path, subdirs, files in os.walk(args.dir):
  20. for f in files:
  21. if f.endswith(".py"):
  22. fpath = path + "/" + f
  23. #print(fpath)
  24. out_fpath = args.out + "/" + fpath[path_prefix_len:-3] + ".mpy"
  25. out_dir = os.path.dirname(out_fpath)
  26. if not os.path.isdir(out_dir):
  27. os.makedirs(out_dir)
  28. cmd = "mpy-cross -v -v %s -s %s %s -o %s" % (TARGET_OPTS.get(args.target, ""),
  29. fpath[path_prefix_len:], fpath, out_fpath)
  30. #print(cmd)
  31. res = os.system(cmd)
  32. assert res == 0