opts.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2015 Philemon <mmgen-py@yandex.com>
  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. opts.py: Further options processing after mmgen.share.Opts
  20. """
  21. import sys
  22. import mmgen.config as g
  23. import mmgen.share.Opts
  24. import opt
  25. from mmgen.util import msg,msgrepr_exit,msgrepr
  26. def usage(opts_data):
  27. print "USAGE: %s %s" % (opts_data['prog_name'], opts_data['usage'])
  28. sys.exit(2)
  29. def print_version_info():
  30. print """
  31. '{g.prog_name}' version {g.version}. Part of the {g.proj_name} suite.
  32. Copyright (C) {g.Cdates} by {g.author} {g.email}.
  33. """.format(g=g).strip()
  34. def warn_incompatible_opts(incompat_list):
  35. bad = [k for k in opt.__dict__ if opt.__dict__[k] and k in incompat_list]
  36. if len(bad) > 1:
  37. msg("Mutually exclusive options: %s" % " ".join(
  38. ["--"+b.replace("_","-") for b in bad]))
  39. sys.exit(1)
  40. def typeconvert_from_dfl(opts,opt):
  41. vtype = type(g.__dict__[opt])
  42. if g.debug: print "Override opt: %-15s [%s]" % (opt,vtype)
  43. try:
  44. opts[opt] = vtype(opts[opt])
  45. except:
  46. d = {
  47. 'int': 'an integer',
  48. 'str': 'a string',
  49. 'float': 'a float',
  50. 'bool': 'a boolean value',
  51. }
  52. m = [d[k] for k in d if __builtins__[k] == vtype][0]
  53. msg("'%s': invalid parameter for '--%s' option (not %s)" %
  54. (opts[opt],opt.replace("_","-"),m))
  55. sys.exit(1)
  56. def init(opts_data,add_opts=[]):
  57. if len(sys.argv) == 2 and sys.argv[1] == '--version':
  58. print_version_info(); sys.exit()
  59. opts,args,short_opts,long_opts = mmgen.share.Opts.parse_opts(sys.argv,opts_data)
  60. if g.debug:
  61. print "short opts: %s" % repr(short_opts)
  62. print "long opts: %s" % repr(long_opts)
  63. print "user-selected opts: %s" % repr(opts)
  64. print "cmd args: %s" % repr(args)
  65. # check opts without modifying them
  66. if not check_opts(opts,long_opts): sys.exit(1)
  67. # If user opt is set, an opt in mmgen.config is set to 'True'
  68. for v in g.usr_set_vars:
  69. if v in opts:
  70. g.__dict__[g.usr_set_vars[v]] = True
  71. # If user opt is unset, set it to default value in mmgen.config (g):
  72. # If set, convert its type based on value in mmgen.config
  73. for v in g.dfl_vars:
  74. if v in opts: typeconvert_from_dfl(opts,v)
  75. else: opts[v] = g.__dict__[v]
  76. if g.debug: print "opts after typeconvert: %s" % opts
  77. # A hack, but harmless
  78. extra_opts = [
  79. "quiet","verbose","debug",
  80. "outdir","echo_passphrase","passwd_file"
  81. ] + add_opts
  82. # Transfer opts into our custom namespace
  83. for o in [s.rstrip("=") for s in long_opts] + extra_opts:
  84. opt.__dict__[o] = opts[o] if o in opts else None
  85. for l in (
  86. ('from_incog_hidden','from_incog','from_seed','from_mnemonic','from_brain'),
  87. ('export_incog','export_incog_hex','export_incog_hidden','export_mnemonic',
  88. 'export_seed'),
  89. ('quiet','verbose')
  90. ): warn_incompatible_opts(l)
  91. del mmgen.share.Opts
  92. return args
  93. def show_opts_and_cmd_args(cmd_args):
  94. print "Processed options:"
  95. d = opt.__dict__
  96. for k in d:
  97. if k[:2] != "__" and k != "opts" and d[k] != None:
  98. msg("%-20s: %s" % (k, d[k]))
  99. print "Cmd args: %s" % repr(cmd_args)
  100. def show_all_opts():
  101. msg("Processed options:")
  102. d = opt.__dict__
  103. for k in d:
  104. if k[:2] != "__" and k != "opts":
  105. msg("%-20s: %s" % (k, d[k]))
  106. def check_opts(opts,long_opts): # Returns false if any check fails
  107. def opt_splits(val,sep,n,what):
  108. sepword = "comma" if sep == "," else (
  109. "colon" if sep == ":" else ("'"+sep+"'"))
  110. try: l = val.split(sep)
  111. except:
  112. msg("'%s': invalid %s (not %s-separated list)" % (val,what,sepword))
  113. return False
  114. if len(l) == n: return True
  115. else:
  116. msg("'%s': invalid %s (%s %s-separated items required)" %
  117. (val,what,n,sepword))
  118. return False
  119. def opt_compares(val,op,target,what):
  120. if not eval("%s %s %s" % (val, op, target)):
  121. msg("%s: invalid %s (not %s %s)" % (val,what,op,target))
  122. return False
  123. return True
  124. def opt_is_int(val,what):
  125. try: int(val)
  126. except:
  127. msg("'%s': invalid %s (not an integer)" % (val,what))
  128. return False
  129. return True
  130. def opt_is_in_list(val,lst,what):
  131. if val not in lst:
  132. q,sep = ("'","','") if type(lst[0]) == str else ("",",")
  133. msg("{q}{}{q}: invalid {}\nValid options: {q}{}{q}".format(
  134. val,what,sep.join([str(i) for i in sorted(lst)]),q=q))
  135. return False
  136. return True
  137. for opt,val in opts.items():
  138. what = "parameter for '--%s' option" % opt.replace("_","-")
  139. # Check for file existence and readability
  140. if opt in ('keys_from_file','mmgen_keys_from_file',
  141. 'passwd_file','keysforaddrs','comment_file'):
  142. from mmgen.util import check_infile
  143. check_infile(val) # exits on error
  144. continue
  145. if opt == 'outdir':
  146. from mmgen.util import check_outdir
  147. check_outdir(val) # exits on error
  148. elif opt == 'label':
  149. if not opt_compares(len(val),"<=",g.max_wallet_label_len,"label length"):
  150. return False
  151. try: val.decode("ascii")
  152. except:
  153. msg("ERROR: label contains a non-ASCII symbol")
  154. return False
  155. w = "character in label"
  156. for ch in list(val):
  157. if not opt_is_in_list(ch,g.wallet_label_symbols,w): return False
  158. elif opt == 'export_incog_hidden' or opt == 'from_incog_hidden':
  159. if opt == 'from_incog_hidden':
  160. if not opt_splits(val,",",3,what): return False
  161. infile,offset,seed_len = val.split(",")
  162. from mmgen.util import check_infile
  163. check_infile(infile)
  164. w = "seed length " + what
  165. if not opt_is_int(seed_len,w): return False
  166. if not opt_is_in_list(int(seed_len),g.seed_lens,w): return False
  167. else:
  168. from mmgen.util import check_outfile
  169. if not opt_splits(val,",",2,what): return False
  170. outfile,offset = val.split(",")
  171. check_outfile(outfile)
  172. w = "offset " + what
  173. if not opt_is_int(offset,w): return False
  174. if not opt_compares(offset,">=",0,what): return False
  175. elif opt == 'from_brain':
  176. if not opt_splits(val,",",2,what): return False
  177. l,p = val.split(",")
  178. w = "seed length " + what
  179. if not opt_is_int(l,w): return False
  180. if not opt_is_in_list(int(l),g.seed_lens,w): return False
  181. w = "hash preset " + what
  182. if not opt_is_in_list(p,g.hash_presets.keys(),w): return False
  183. elif opt == 'seed_len':
  184. if not opt_is_int(val,what): return False
  185. if not opt_is_in_list(int(val),g.seed_lens,what): return False
  186. elif opt == 'hash_preset':
  187. if not opt_is_in_list(val,g.hash_presets.keys(),what): return False
  188. elif opt == 'usr_randchars':
  189. if not opt_is_int(val,what): return False
  190. if not opt_compares(val,">=",g.min_urandchars,what): return False
  191. if not opt_compares(val,"<=",g.max_urandchars,what): return False
  192. else:
  193. if 'debug' in opts: print "check_opts(): No test for opt '%s'" % opt
  194. return True