__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.overlay.__init__: Initialize the MMGen test suite overlay tree
  12. """
  13. import sys,os,shutil
  14. def get_overlay_tree_dir(repo_root):
  15. return os.path.join(repo_root,'test','overlay','tree')
  16. def overlay_setup(repo_root):
  17. def process_srcdir(pkgname,d):
  18. relpath = d.split('.')
  19. srcdir = os.path.join(repo_root,*relpath)
  20. destdir = os.path.join(overlay_tree_dir,*relpath)
  21. fakemod_dir = os.path.join(fakemod_root,pkgname,*(relpath[1:]))
  22. os.makedirs(destdir)
  23. for fn in os.listdir(srcdir):
  24. if (
  25. fn.endswith('.py') or
  26. d == f'{pkgname}.data' or
  27. (d == 'mmgen.proto.secp256k1' and fn.startswith('secp256k1'))
  28. ):
  29. if fn.endswith('.py') and os.path.exists(os.path.join(fakemod_dir,fn)):
  30. make_link(
  31. os.path.join(fakemod_dir,fn),
  32. os.path.join(destdir,fn) )
  33. link_fn = fn.removesuffix('.py') + '_orig.py'
  34. else:
  35. link_fn = fn
  36. make_link(
  37. os.path.join(srcdir,fn),
  38. os.path.join(destdir,link_fn) )
  39. overlay_tree_dir = get_overlay_tree_dir(repo_root)
  40. fakemod_root = os.path.join(repo_root,'test','overlay','fakemods')
  41. common_path = os.path.join(os.path.sep,'test','overlay','fakemods')
  42. pkgdata = ((
  43. os.path.realpath(e.path).removesuffix(os.path.join(common_path,e.name)),
  44. e.name
  45. ) for e in os.scandir(fakemod_root) if e.is_dir() )
  46. for repodir,pkgname in pkgdata:
  47. if not os.path.exists(os.path.join(overlay_tree_dir,pkgname)):
  48. sys.stderr.write(f'Setting up overlay tree: {pkgname}\n')
  49. make_link = os.symlink if sys.platform == 'linux' else shutil.copy2
  50. shutil.rmtree(os.path.join(overlay_tree_dir,pkgname),ignore_errors=True)
  51. import configparser
  52. cfg = configparser.ConfigParser()
  53. cfg.read(os.path.join(repodir,'setup.cfg'))
  54. for d in cfg.get('options','packages').split():
  55. process_srcdir(pkgname,d)
  56. sys.path.insert(0,overlay_tree_dir)
  57. return overlay_tree_dir