__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import sys,os,shutil
  2. def get_overlay_tree_dir(repo_root):
  3. return os.path.join(repo_root,'test','overlay','tree')
  4. def overlay_setup(repo_root):
  5. def process_srcdir(pkgname,d):
  6. relpath = d.split('.')
  7. srcdir = os.path.join(repo_root,*relpath)
  8. destdir = os.path.join(overlay_tree_dir,*relpath)
  9. fakemod_dir = os.path.join(fakemod_root,pkgname,*(relpath[1:]))
  10. os.makedirs(destdir)
  11. for fn in os.listdir(srcdir):
  12. if (
  13. fn.endswith('.py') or
  14. d == f'{pkgname}.data' or
  15. (d == f'mmgen.proto.secp256k1' and fn.startswith('secp256k1'))
  16. ):
  17. if os.path.exists(os.path.join(fakemod_dir,fn)):
  18. make_link(
  19. os.path.join(fakemod_dir,fn),
  20. os.path.join(destdir,fn) )
  21. # link_fn = fn.removesuffix('.py') + '_orig.py' # Python 3.9
  22. link_fn = fn[:-3] + '_orig.py'
  23. else:
  24. link_fn = fn
  25. make_link(
  26. os.path.join(srcdir,fn),
  27. os.path.join(destdir,link_fn) )
  28. overlay_tree_dir = get_overlay_tree_dir(repo_root)
  29. fakemod_root = os.path.join(repo_root,'test','overlay','fakemods')
  30. common_path = os.path.join(os.path.sep,'test','overlay','fakemods')
  31. pkgdata = ((
  32. os.path.realpath(e.path)[:-len(os.path.join(common_path,e.name))], # Python 3.9: removesuffix()
  33. e.name
  34. ) for e in os.scandir(fakemod_root) if e.is_dir() )
  35. for repodir,pkgname in pkgdata:
  36. if not os.path.exists(os.path.join(overlay_tree_dir,pkgname)):
  37. sys.stderr.write(f'Setting up overlay tree: {pkgname}\n')
  38. make_link = os.symlink if sys.platform == 'linux' else shutil.copy2
  39. shutil.rmtree(os.path.join(overlay_tree_dir,pkgname),ignore_errors=True)
  40. import configparser
  41. cfg = configparser.ConfigParser()
  42. cfg.read(os.path.join(repodir,'setup.cfg'))
  43. for d in cfg.get('options','packages').split():
  44. process_srcdir(pkgname,d)
  45. return overlay_tree_dir