util.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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. util: Frequently-used variables, classes and utility functions for the MMGen suite
  20. """
  21. import sys,os,time,re
  22. from .color import *
  23. from .cfg import gv,gc
  24. ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
  25. hexdigits = '0123456789abcdefABCDEF'
  26. hexdigits_uc = '0123456789ABCDEF'
  27. hexdigits_lc = '0123456789abcdef'
  28. def noop(*args,**kwargs):
  29. pass
  30. class Util:
  31. def __init__(self,cfg):
  32. self.cfg = cfg
  33. if cfg.quiet:
  34. self.qmsg = self.qmsg_r = noop
  35. else:
  36. self.qmsg = msg
  37. self.qmsg_r = msg_r
  38. if cfg.verbose:
  39. self.vmsg = msg
  40. self.vmsg_r = msg_r
  41. self.Vmsg = Msg
  42. self.Vmsg_r = Msg_r
  43. else:
  44. self.vmsg = self.vmsg_r = self.Vmsg = self.Vmsg_r = noop
  45. self.dmsg = msg if cfg.debug else noop
  46. if cfg.pager:
  47. from .ui import do_pager
  48. self.stdout_or_pager = do_pager
  49. else:
  50. self.stdout_or_pager = Msg_r
  51. def compare_chksums(self,chk1,desc1,chk2,desc2,hdr='',die_on_fail=False,verbose=False):
  52. if not chk1 == chk2:
  53. fs = "{} ERROR: {} checksum ({}) doesn't match {} checksum ({})"
  54. m = fs.format((hdr+':\n ' if hdr else 'CHECKSUM'),desc2,chk2,desc1,chk1)
  55. if die_on_fail:
  56. die(3,m)
  57. else:
  58. if verbose or self.cfg.verbose:
  59. msg(m)
  60. return False
  61. if self.cfg.verbose:
  62. msg(f'{capfirst(desc1)} checksum OK ({chk1})')
  63. return True
  64. def compare_or_die(self, val1, desc1, val2, desc2, e='Error'):
  65. if val1 != val2:
  66. die(3,f"{e}: {desc2} ({val2}) doesn't match {desc1} ({val1})")
  67. if self.cfg.debug:
  68. msg(f'{capfirst(desc2)} OK ({val2})')
  69. return True
  70. if gc.platform == 'win':
  71. def msg_r(s):
  72. try:
  73. gv.stderr.write(s)
  74. gv.stderr.flush()
  75. except:
  76. os.write(2,s.encode())
  77. def msg(s):
  78. msg_r(s + '\n')
  79. def Msg_r(s):
  80. try:
  81. gv.stdout.write(s)
  82. gv.stdout.flush()
  83. except:
  84. os.write(1,s.encode())
  85. def Msg(s):
  86. Msg_r(s + '\n')
  87. else:
  88. def msg(s):
  89. gv.stderr.write(s + '\n')
  90. def msg_r(s):
  91. gv.stderr.write(s)
  92. gv.stderr.flush()
  93. def Msg(s):
  94. gv.stdout.write(s + '\n')
  95. def Msg_r(s):
  96. gv.stdout.write(s)
  97. gv.stdout.flush()
  98. def rmsg(s):
  99. msg(red(s))
  100. def ymsg(s):
  101. msg(yellow(s))
  102. def gmsg(s):
  103. msg(green(s))
  104. def gmsg_r(s):
  105. msg_r(green(s))
  106. def bmsg(s):
  107. msg(blue(s))
  108. def pumsg(s):
  109. msg(purple(s))
  110. def mmsg(*args):
  111. for d in args:
  112. Msg(repr(d))
  113. def mdie(*args):
  114. mmsg(*args)
  115. sys.exit(0)
  116. def die(ev,s='',stdout=False):
  117. if isinstance(ev,int):
  118. from .exception import MMGenSystemExit,MMGenError
  119. if ev <= 2:
  120. raise MMGenSystemExit(ev,s,stdout)
  121. else:
  122. raise MMGenError(ev,s,stdout)
  123. elif isinstance(ev,str):
  124. import mmgen.exception
  125. raise getattr(mmgen.exception,ev)(s)
  126. else:
  127. raise ValueError(f'{ev}: exit value must be string or int instance')
  128. def Die(ev=0,s=''):
  129. die(ev=ev,s=s,stdout=True)
  130. def pp_fmt(d):
  131. import pprint
  132. return pprint.PrettyPrinter(indent=4,compact=False).pformat(d)
  133. def pp_msg(d):
  134. msg(pp_fmt(d))
  135. def fmt(s,indent='',strip_char=None,append='\n'):
  136. "de-indent multiple lines of text, or indent with specified string"
  137. return indent + ('\n'+indent).join([l.strip(strip_char) for l in s.strip().splitlines()]) + append
  138. def fmt_list(iterable,fmt='dfl',indent=''):
  139. "pretty-format a list"
  140. sep,lq,rq = {
  141. 'utf8': ("“, ”", "“", "”"),
  142. 'dfl': ("', '", "'", "'"),
  143. 'bare': (' ', '', '' ),
  144. 'no_quotes': (', ', '', '' ),
  145. 'no_spc': ("','", "'", "'"),
  146. 'min': (",", "'", "'"),
  147. 'col': ('\n'+indent, indent, '' ),
  148. 'list': ('\n- '+indent, '- '+indent, '' ),
  149. }[fmt]
  150. return lq + sep.join(str(i) for i in iterable) + rq
  151. def list_gen(*data):
  152. """
  153. add element to list if condition is true or absent
  154. """
  155. assert type(data) in (list,tuple), f'{type(data).__name__} not in (list,tuple)'
  156. def gen():
  157. for i in data:
  158. assert type(i) == list, f'{type(i).__name__} != list'
  159. assert len(i) in (1,2), f'{len(i)} not in (1,2)'
  160. if len(i) == 1 or i[1]:
  161. yield i[0]
  162. return list(gen())
  163. def remove_dups(iterable,edesc='element',desc='list',quiet=False,hide=False):
  164. """
  165. Remove duplicate occurrences of iterable elements, preserving first occurrence
  166. If iterable is a generator, return a list, else type(iterable)
  167. """
  168. ret = []
  169. for e in iterable:
  170. if e in ret:
  171. if not quiet:
  172. ymsg(f'Warning: removing duplicate {edesc} {"(hidden)" if hide else e} in {desc}')
  173. else:
  174. ret.append(e)
  175. return ret if type(iterable).__name__ == 'generator' else type(iterable)(ret)
  176. def contains_any(target_list,source_list):
  177. return any(map(target_list.count,source_list))
  178. def suf(arg,suf_type='s',verb='none'):
  179. suf_types = {
  180. 'none': {
  181. 's': ('s', ''),
  182. 'es': ('es', ''),
  183. 'ies': ('ies','y'),
  184. },
  185. 'is': {
  186. 's': ('s are', ' is'),
  187. 'es': ('es are', ' is'),
  188. 'ies': ('ies are','y is'),
  189. },
  190. 'has': {
  191. 's': ('s have', ' has'),
  192. 'es': ('es have', ' has'),
  193. 'ies': ('ies have','y has'),
  194. },
  195. }
  196. if isinstance(arg,int):
  197. n = arg
  198. elif isinstance(arg,(list,tuple,set,dict)):
  199. n = len(arg)
  200. else:
  201. die(2,f'{arg}: invalid parameter for suf()')
  202. return suf_types[verb][suf_type][n == 1]
  203. def get_extension(fn):
  204. return os.path.splitext(fn)[1][1:]
  205. def remove_extension(fn,ext):
  206. a,b = os.path.splitext(fn)
  207. return a if b[1:] == ext else fn
  208. def make_chksum_N(s,nchars,sep=False,rounds=2,upper=True):
  209. if isinstance(s,str):
  210. s = s.encode()
  211. from hashlib import sha256
  212. for i in range(rounds):
  213. s = sha256(s).digest()
  214. ret = s.hex()[:nchars]
  215. if sep:
  216. assert 4 <= nchars <= 64 and (not nchars % 4), 'illegal ‘nchars’ value'
  217. ret = ' '.join( ret[i:i+4] for i in range(0,nchars,4) )
  218. else:
  219. assert 4 <= nchars <= 64, 'illegal ‘nchars’ value'
  220. return ret.upper() if upper else ret
  221. def make_chksum_8(s,sep=False):
  222. from .obj import HexStr
  223. from hashlib import sha256
  224. s = HexStr(sha256(sha256(s).digest()).hexdigest()[:8].upper(),case='upper')
  225. return '{} {}'.format(s[:4],s[4:]) if sep else s
  226. def make_chksum_6(s):
  227. from .obj import HexStr
  228. from hashlib import sha256
  229. if isinstance(s,str):
  230. s = s.encode()
  231. return HexStr(sha256(s).hexdigest()[:6])
  232. def is_chksum_6(s):
  233. return len(s) == 6 and set(s) <= set(hexdigits_lc)
  234. def split_into_cols(col_wid,s):
  235. return ' '.join([s[col_wid*i:col_wid*(i+1)] for i in range(len(s)//col_wid+1)]).rstrip()
  236. def capfirst(s): # different from str.capitalize() - doesn't downcase any uc in string
  237. return s if len(s) == 0 else s[0].upper() + s[1:]
  238. def decode_timestamp(s):
  239. # tz_save = open('/etc/timezone').read().rstrip()
  240. os.environ['TZ'] = 'UTC'
  241. # os.environ['TZ'] = tz_save
  242. return int(time.mktime( time.strptime(s,'%Y%m%d_%H%M%S') ))
  243. def make_timestamp(secs=None):
  244. return '{:04d}{:02d}{:02d}_{:02d}{:02d}{:02d}'.format(*time.gmtime(
  245. int(secs) if secs != None else time.time() )[:6])
  246. def make_timestr(secs=None):
  247. return '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*time.gmtime(
  248. int(secs) if secs != None else time.time() )[:6])
  249. def secs_to_dhms(secs):
  250. hrs = secs // 3600
  251. return '{}{:02d}:{:02d}:{:02d} h/m/s'.format(
  252. ('{} day{}, '.format(hrs//24,suf(hrs//24)) if hrs > 24 else ''),
  253. hrs % 24,
  254. (secs // 60) % 60,
  255. secs % 60
  256. )
  257. def secs_to_hms(secs):
  258. return '{:02d}:{:02d}:{:02d}'.format(secs//3600, (secs//60) % 60, secs % 60)
  259. def secs_to_ms(secs):
  260. return '{:02d}:{:02d}'.format(secs//60, secs % 60)
  261. def is_int(s):
  262. try:
  263. int(str(s))
  264. return True
  265. except:
  266. return False
  267. def check_int_between(val,imin,imax,desc):
  268. if not imin <= int(val) <= imax:
  269. die(1,f'{val}: invalid value for {desc} (must be between {imin} and {imax})')
  270. return int(val)
  271. def is_hex_str(s):
  272. return set(s) <= set(hexdigits)
  273. def is_hex_str_lc(s):
  274. return set(s) <= set(hexdigits_lc)
  275. def is_utf8(s):
  276. try: s.decode('utf8')
  277. except: return False
  278. else: return True
  279. def remove_whitespace(s,ws='\t\r\n '):
  280. return s.translate(dict((ord(e),None) for e in ws))
  281. def strip_comment(line):
  282. return re.sub('#.*','',line).rstrip()
  283. def strip_comments(lines):
  284. pat = re.compile('#.*')
  285. return [m for m in [pat.sub('',l).rstrip() for l in lines] if m != '']
  286. def make_full_path(outdir,outfile):
  287. return os.path.normpath(os.path.join(outdir, os.path.basename(outfile)))
  288. class oneshot_warning:
  289. color = 'nocolor'
  290. def __init__(self,div=None,fmt_args=[],reverse=False):
  291. self.do(type(self),div,fmt_args,reverse)
  292. def do(self,wcls,div,fmt_args,reverse):
  293. def do_warning():
  294. import mmgen.color
  295. message = getattr(wcls,'message')
  296. color = getattr( mmgen.color, getattr(wcls,'color') )
  297. msg(color('WARNING: ' + message.format(*fmt_args)))
  298. if not hasattr(wcls,'data'):
  299. setattr(wcls,'data',[])
  300. data = getattr(wcls,'data')
  301. condition = (div in data) if reverse else (not div in data)
  302. if not div in data:
  303. data.append(div)
  304. if condition:
  305. do_warning()
  306. self.warning_shown = True
  307. else:
  308. self.warning_shown = False
  309. class oneshot_warning_group(oneshot_warning):
  310. def __init__(self,wcls,div=None,fmt_args=[],reverse=False):
  311. self.do(getattr(self,wcls),div,fmt_args,reverse)
  312. def get_subclasses(cls,names=False):
  313. def gen(cls):
  314. for i in cls.__subclasses__():
  315. yield i
  316. for j in gen(i):
  317. yield j
  318. return tuple((c.__name__ for c in gen(cls)) if names else gen(cls))
  319. def async_run(coro):
  320. import asyncio
  321. return asyncio.run(coro)
  322. def wrap_ripemd160(called=[]):
  323. if not called:
  324. try:
  325. import hashlib
  326. hashlib.new('ripemd160')
  327. except ValueError:
  328. def hashlib_new_wrapper(name,*args,**kwargs):
  329. if name == 'ripemd160':
  330. return ripemd160(*args,**kwargs)
  331. else:
  332. return hashlib_new(name,*args,**kwargs)
  333. from .contrib.ripemd160 import ripemd160
  334. hashlib_new = hashlib.new
  335. hashlib.new = hashlib_new_wrapper
  336. called.append(True)
  337. def exit_if_mswin(feature):
  338. if gc.platform == 'win':
  339. die(2, capfirst(feature) + ' not supported on the MSWin / MSYS2 platform' )