common.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. common.py: Shared routines for the test suites
  20. """
  21. class TestSuiteException(Exception): pass
  22. class TestSuiteFatalException(Exception): pass
  23. import os
  24. from binascii import hexlify
  25. from mmgen.common import *
  26. def getrandnum(n): return int(hexlify(os.urandom(n)),16)
  27. def getrandhex(n): return hexlify(os.urandom(n)).decode()
  28. def getrandnum_range(nbytes,rn_max):
  29. while True:
  30. rn = int(hexlify(os.urandom(nbytes)),16)
  31. if rn < rn_max: return rn
  32. def getrandstr(num_chars,no_space=False):
  33. n,m = 95,32
  34. if no_space: n,m = 94,33
  35. return ''.join([chr(i%n+m) for i in list(os.urandom(num_chars))])
  36. # Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
  37. def cleandir(d,do_msg=False):
  38. d_enc = d.encode()
  39. try: files = os.listdir(d_enc)
  40. except: return
  41. from shutil import rmtree
  42. if do_msg: gmsg("Cleaning directory '{}'".format(d))
  43. for f in files:
  44. try:
  45. os.unlink(os.path.join(d_enc,f))
  46. except:
  47. rmtree(os.path.join(d_enc,f))
  48. def mk_tmpdir(d):
  49. try: os.mkdir(d,0o755)
  50. except OSError as e:
  51. if e.errno != 17: raise
  52. else:
  53. vmsg("Created directory '{}'".format(d))
  54. # def mk_tmpdir_path(path,cfg):
  55. # try:
  56. # name = os.path.split(cfg['tmpdir'])[-1]
  57. # src = os.path.join(path,name)
  58. # try:
  59. # os.unlink(cfg['tmpdir'])
  60. # except OSError as e:
  61. # if e.errno != 2: raise
  62. # finally:
  63. # os.mkdir(src)
  64. # os.symlink(src,cfg['tmpdir'])
  65. # except OSError as e:
  66. # if e.errno != 17: raise
  67. # else: msg("Created directory '{}'".format(cfg['tmpdir']))
  68. def get_tmpfile(cfg,fn):
  69. return os.path.join(cfg['tmpdir'],fn)
  70. def write_to_file(fn,data,binary=False):
  71. write_data_to_file( fn,
  72. data,
  73. silent = True,
  74. binary = binary,
  75. ignore_opt_outdir = True )
  76. def write_to_tmpfile(cfg,fn,data,binary=False):
  77. write_to_file( os.path.join(cfg['tmpdir'],fn), data=data, binary=binary )
  78. def read_from_file(fn,binary=False):
  79. from mmgen.util import get_data_from_file
  80. return get_data_from_file(fn,silent=True,binary=binary)
  81. def read_from_tmpfile(cfg,fn,binary=False):
  82. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  83. def joinpath(*args,**kwargs):
  84. return os.path.join(*args,**kwargs)
  85. def ok():
  86. if opt.profile: return
  87. if opt.verbose or opt.exact_output:
  88. gmsg('OK')
  89. else: msg(' OK')
  90. def cmp_or_die(s,t,desc=None):
  91. if s != t:
  92. m = 'ERROR: recoded data:\n{!r}\ndiffers from original data:\n{!r}'
  93. if desc: m = 'For {}:\n{}'.format(desc,m)
  94. raise TestSuiteFatalException(m.format(t,s))
  95. def init_coverage():
  96. coverdir = os.path.join('test','trace')
  97. acc_file = os.path.join('test','trace.acc')
  98. try: os.mkdir(coverdir,0o755)
  99. except: pass
  100. return coverdir,acc_file