Opts.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013-2014 by 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. import sys
  19. import mmgen.config as g
  20. import mmgen.opt.Opts
  21. from mmgen.util import msg,check_infile,check_outfile,check_outdir
  22. def usage(hd): mmgen.opt.Opts.usage(hd)
  23. def print_version_info():
  24. print """
  25. '{g.prog_name}' version {g.version}. Part of the {g.proj_name} suite.
  26. Copyright (C) {g.Cdates} by {g.author} {g.email}.
  27. """.format(g=g).strip()
  28. def warn_incompatible_opts(opts,incompat_list):
  29. bad = [k for k in opts.keys() if k in incompat_list]
  30. if len(bad) > 1:
  31. msg("Mutually exclusive options: %s" % " ".join(
  32. ["--"+b.replace("_","-") for b in bad]))
  33. sys.exit(1)
  34. def parse_opts(argv,help_data):
  35. if len(argv) == 2 and argv[1] == '--version':
  36. print_version_info(); sys.exit()
  37. opts,args,short_opts,long_opts = mmgen.opt.Opts.parse_opts(argv,help_data)
  38. if g.debug:
  39. print "short opts: %s" % repr(short_opts)
  40. print "long opts: %s" % repr(long_opts)
  41. print "user-selected opts: %s" % repr(opts)
  42. print "cmd args: %s" % repr(args)
  43. for l in (
  44. ('outdir', 'export_incog_hidden'),
  45. ('from_incog_hidden','from_incog','from_seed','from_mnemonic','from_brain'),
  46. ('export_incog','export_incog_hex','export_incog_hidden','export_mnemonic',
  47. 'export_seed'),
  48. ('quiet','verbose')
  49. ): warn_incompatible_opts(opts,l)
  50. # check_opts() doesn't touch opts[]
  51. if not check_opts(opts,long_opts): sys.exit(1)
  52. # If unset, set these to default values in mmgen.config:
  53. for v in g.cl_override_vars:
  54. if v in opts: typeconvert_override_var(opts,v)
  55. else: opts[v] = eval("g."+v)
  56. if g.debug: print "opts after typeconvert: %s" % opts
  57. return opts,args
  58. def show_opts_and_cmd_args(opts,cmd_args):
  59. print "Processed options: %s" % repr(opts)
  60. print "Cmd args: %s" % repr(cmd_args)
  61. def check_opts(opts,long_opts):
  62. def opt_splits(val,sep,n,what):
  63. sepword = "comma" if sep == "," else (
  64. "colon" if sep == ":" else ("'"+sep+"'"))
  65. try: l = val.split(sep)
  66. except:
  67. msg("'%s': invalid %s (not %s-separated list)" % (val,what,sepword))
  68. return False
  69. if len(l) == n: return True
  70. else:
  71. msg("'%s': invalid %s (%s %s-separated items required)" %
  72. (val,what,n,sepword))
  73. return False
  74. def opt_compares(val,op,target,what):
  75. if not eval("%s %s %s" % (val, op, target)):
  76. msg("%s: invalid %s (not %s %s)" % (val,what,op,target))
  77. return False
  78. return True
  79. def opt_is_int(val,what):
  80. try: int(val)
  81. except:
  82. msg("'%s': invalid %s (not an integer)" % (val,what))
  83. return False
  84. return True
  85. def opt_is_in_list(val,lst,what):
  86. if val not in lst:
  87. q,sep = ("'","','") if type(lst[0]) == str else ("",",")
  88. msg("{q}{}{q}: invalid {}\nValid options: {q}{}{q}".format(
  89. val,what,sep.join([str(i) for i in sorted(lst)]),q=q))
  90. return False
  91. return True
  92. for opt,val in opts.items():
  93. what = "parameter for '--%s' option" % opt.replace("_","-")
  94. # Check for file existence and readability
  95. if opt in ('keys_from_file','all_keys_from_file','addrlist',
  96. 'passwd_file','keysforaddrs'):
  97. check_infile(val) # exits on error
  98. continue
  99. if opt == 'outdir':
  100. check_outdir(val) # exits on error
  101. elif opt == 'label':
  102. if not opt_compares(len(val),"<=",g.max_wallet_label_len,"label length"):
  103. return False
  104. try: val.decode("ascii")
  105. except:
  106. msg("ERROR: label contains a non-ASCII symbol")
  107. return False
  108. w = "character in label"
  109. for ch in list(val):
  110. if not opt_is_in_list(ch,g.wallet_label_symbols,w): return False
  111. elif opt == 'export_incog_hidden' or opt == 'from_incog_hidden':
  112. if opt == 'from_incog_hidden':
  113. if not opt_splits(val,",",3,what): return False
  114. infile,offset,seed_len = val.split(",")
  115. check_infile(infile)
  116. w = "seed length " + what
  117. if not opt_is_int(seed_len,w): return False
  118. if not opt_is_in_list(int(seed_len),g.seed_lens,w): return False
  119. else:
  120. if not opt_splits(val,",",2,what): return False
  121. outfile,offset = val.split(",")
  122. check_outfile(outfile)
  123. w = "offset " + what
  124. if not opt_is_int(offset,w): return False
  125. if not opt_compares(offset,">=",0,what): return False
  126. elif opt == 'from_brain':
  127. if not opt_splits(val,",",2,what): return False
  128. l,p = val.split(",")
  129. w = "seed length " + what
  130. if not opt_is_int(l,w): return False
  131. if not opt_is_in_list(int(l),g.seed_lens,w): return False
  132. w = "hash preset " + what
  133. if not opt_is_in_list(p,g.hash_presets.keys(),w): return False
  134. elif opt == 'seed_len':
  135. if not opt_is_int(val,what): return False
  136. if not opt_is_in_list(int(val),g.seed_lens,what): return False
  137. elif opt == 'hash_preset':
  138. if not opt_is_in_list(val,g.hash_presets.keys(),what): return False
  139. elif opt == 'usr_randchars':
  140. if not opt_is_int(val,what): return False
  141. if not opt_compares(val,">=",g.min_urandchars,what): return False
  142. if not opt_compares(val,"<=",g.max_urandchars,what): return False
  143. else:
  144. if g.debug: print "check_opts(): No test for opt '%s'" % opt
  145. return True
  146. def typeconvert_override_var(opts,opt):
  147. vtype = type(eval("g."+opt))
  148. if g.debug: print "Override opt: %-15s [%s]" % (opt,vtype)
  149. if vtype == int: f,t = int,"an integer"
  150. elif vtype == str: f,t = str,"a string"
  151. elif vtype == float: f,t = float,"a float"
  152. try:
  153. opts[opt] = f(opts[opt])
  154. except:
  155. msg("'%s': invalid parameter for '--%s' option (not %s)" %
  156. (opts[opt],opt.replace("_","-"),t))
  157. sys.exit(1)