__init__.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2023 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
  9. # https://gitlab.com/mmgen/mmgen
  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 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' # Python 3.9
  34. link_fn = fn[:-3] + '_orig.py'
  35. else:
  36. link_fn = fn
  37. make_link(
  38. os.path.join(srcdir,fn),
  39. os.path.join(destdir,link_fn) )
  40. overlay_tree_dir = get_overlay_tree_dir(repo_root)
  41. fakemod_root = os.path.join(repo_root,'test','overlay','fakemods')
  42. common_path = os.path.join(os.path.sep,'test','overlay','fakemods')
  43. pkgdata = ((
  44. os.path.realpath(e.path)[:-len(os.path.join(common_path,e.name))], # Python 3.9: removesuffix()
  45. e.name
  46. ) for e in os.scandir(fakemod_root) if e.is_dir() )
  47. for repodir,pkgname in pkgdata:
  48. if not os.path.exists(os.path.join(overlay_tree_dir,pkgname)):
  49. sys.stderr.write(f'Setting up overlay tree: {pkgname}\n')
  50. make_link = os.symlink if sys.platform == 'linux' else shutil.copy2
  51. shutil.rmtree(os.path.join(overlay_tree_dir,pkgname),ignore_errors=True)
  52. import configparser
  53. cfg = configparser.ConfigParser()
  54. cfg.read(os.path.join(repodir,'setup.cfg'))
  55. for d in cfg.get('options','packages').split():
  56. process_srcdir(pkgname,d)
  57. sys.path.insert(0,overlay_tree_dir)
  58. return overlay_tree_dir