cfgfile.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. cfgfile: API for the MMGen runtime configuration file and related files
  20. """
  21. import os,re
  22. from collections import namedtuple
  23. from .cfg import gc
  24. from .util import msg,ymsg,suf,fmt,fmt_list,oneshot_warning,strip_comment,capfirst,die
  25. def mmgen_cfg_file(cfg,id_str):
  26. return cfg_file.get_cls_by_id(id_str)(cfg)
  27. class cfg_file:
  28. cur_ver = 2
  29. ver = None
  30. write_ok = False
  31. warn_missing = True
  32. write_metadata = False
  33. line_data = namedtuple('cfgfile_line',['name','value','lineno','chunk'])
  34. fn_base = 'mmgen.cfg'
  35. class warn_missing_file(oneshot_warning):
  36. color = 'yellow' # has no effect, as color not initialized yet
  37. message = '{} not found at {!r}'
  38. def get_data(self,fn):
  39. try:
  40. with open(fn) as fp:
  41. return fp.read().splitlines()
  42. except:
  43. if self.warn_missing:
  44. self.warn_missing_file( div=fn, fmt_args=(self.desc,fn) )
  45. return ''
  46. def copy_system_data(self,fn):
  47. assert self.write_ok, f'writing to file {fn!r} not allowed!'
  48. src = mmgen_cfg_file(self.cfg,'sys')
  49. if src.data:
  50. data = src.data + src.make_metadata() if self.write_metadata else src.data
  51. try:
  52. with open(fn,'w') as fp:
  53. fp.write('\n'.join(data)+'\n')
  54. os.chmod(fn,0o600)
  55. except:
  56. die(2,f'ERROR: unable to write to {fn!r}')
  57. def parse_value(self,value,refval):
  58. if isinstance(refval,dict):
  59. m = re.fullmatch(r'((\s+\w+:\S+)+)',' '+value) # expect one or more colon-separated values
  60. if m:
  61. return dict([i.split(':') for i in m[1].split()])
  62. elif isinstance(refval,(list,tuple)):
  63. m = re.fullmatch(r'((\s+\S+)+)',' '+value) # expect single value or list
  64. if m:
  65. ret = m[1].split()
  66. return ret if isinstance(refval,list) else tuple(ret)
  67. else:
  68. return value
  69. def get_lines(self):
  70. def gen_lines():
  71. for lineno,line in enumerate(self.data,1):
  72. line = strip_comment(line)
  73. if line == '':
  74. continue
  75. m = re.fullmatch(r'(\w+)(\s+)(.*)',line)
  76. if m:
  77. yield self.line_data(m[1],m[3],lineno,None)
  78. else:
  79. die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
  80. return gen_lines()
  81. @classmethod
  82. def get_cls_by_id(cls,id_str):
  83. d = {
  84. 'usr': CfgFileUsr,
  85. 'sys': CfgFileSampleSys,
  86. 'sample': CfgFileSampleUsr,
  87. }
  88. return d[id_str]
  89. class cfg_file_sample(cfg_file):
  90. @classmethod
  91. def cls_make_metadata(cls,data):
  92. return [f'# Version {cls.cur_ver} {cls.compute_chksum(data)}']
  93. @staticmethod
  94. def compute_chksum(data):
  95. import hashlib
  96. return hashlib.new('ripemd160','\n'.join(data).encode()).hexdigest()
  97. @property
  98. def computed_chksum(self):
  99. return type(self).compute_chksum(self.data)
  100. def get_lines(self):
  101. """
  102. The config file template contains some 'magic':
  103. - lines must either be empty or begin with '# '
  104. - each commented chunk must end with a parsable cfg variable line
  105. - chunks are delimited by one or more blank lines
  106. - lines beginning with '##' are ignored
  107. - everything up to first line beginning with '##' is ignored
  108. - last line is metadata line of the form '# Version VER_NUM HASH'
  109. """
  110. def process_chunk(chunk,lineno):
  111. m = re.fullmatch(r'(#\s*)(\w+)(\s+)(.*)',chunk[-1])
  112. if m:
  113. return self.line_data(m[2],m[4],lineno,chunk)
  114. else:
  115. die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
  116. def gen_chunks(lines):
  117. hdr = True
  118. chunk = []
  119. in_chunk = False
  120. for lineno,line in enumerate(lines,1):
  121. if line.startswith('##'):
  122. hdr = False
  123. continue
  124. if hdr:
  125. continue
  126. if line == '':
  127. in_chunk = False
  128. elif line.startswith('#'):
  129. if in_chunk is False:
  130. if chunk:
  131. yield process_chunk(chunk,last_nonblank)
  132. chunk = [line]
  133. in_chunk = True
  134. else:
  135. chunk.append(line)
  136. last_nonblank = lineno
  137. else:
  138. die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
  139. if chunk:
  140. yield process_chunk(chunk,last_nonblank)
  141. return list(gen_chunks(self.data))
  142. class CfgFileUsr(cfg_file):
  143. desc = 'user configuration file'
  144. warn_missing = False
  145. write_ok = True
  146. def __init__(self,cfg):
  147. self.cfg = cfg
  148. self.fn = os.path.join(cfg.data_dir_root,self.fn_base)
  149. self.data = self.get_data(self.fn)
  150. if not self.data:
  151. self.copy_system_data(self.fn)
  152. class CfgFileSampleSys(cfg_file_sample):
  153. desc = 'system sample configuration file'
  154. test_fn_subdir = 'usr.local.share'
  155. def __init__(self,cfg):
  156. self.cfg = cfg
  157. if self.cfg.test_suite_cfgtest:
  158. self.fn = os.path.join(cfg.data_dir_root,self.test_fn_subdir,self.fn_base)
  159. with open(self.fn) as fp:
  160. self.data = fp.read().splitlines()
  161. else:
  162. # self.fn is used for error msgs only, so file need not exist on filesystem
  163. self.fn = os.path.join(os.path.dirname(__file__),'data',self.fn_base)
  164. self.data = gc.get_mmgen_data_file(self.fn_base).splitlines()
  165. def make_metadata(self):
  166. return [f'# Version {self.cur_ver} {self.computed_chksum}']
  167. class CfgFileSampleUsr(cfg_file_sample):
  168. desc = 'sample configuration file'
  169. warn_missing = False
  170. write_ok = True
  171. chksum = None
  172. write_metadata = True
  173. details_confirm_prompt = 'View details?'
  174. out_of_date_fs = 'File {!r} is out of date - replacing'
  175. altered_by_user_fs = 'File {!r} was altered by user - replacing'
  176. def __init__(self,cfg):
  177. self.cfg = cfg
  178. self.fn = os.path.join(cfg.data_dir_root,f'{self.fn_base}.sample')
  179. self.data = self.get_data(self.fn)
  180. src = mmgen_cfg_file(cfg,'sys')
  181. if not src.data:
  182. return
  183. if self.data:
  184. if self.parse_metadata():
  185. if self.chksum == self.computed_chksum:
  186. diff = self.diff(self.get_lines(),src.get_lines())
  187. if not diff:
  188. return
  189. self.show_changes(diff)
  190. else:
  191. msg(self.altered_by_user_fs.format(self.fn))
  192. else:
  193. msg(self.out_of_date_fs.format(self.fn))
  194. self.copy_system_data(self.fn)
  195. def parse_metadata(self):
  196. if self.data:
  197. m = re.match(r'# Version (\d+) ([a-f0-9]{40})$',self.data[-1])
  198. if m:
  199. self.ver = m[1]
  200. self.chksum = m[2]
  201. self.data = self.data[:-1] # remove metadata line
  202. return True
  203. def diff(self,a_tup,b_tup): # a=user, b=system
  204. a = [i.name for i in a_tup]#[3:] # Debug
  205. b = [i.name for i in b_tup]#[:-2] # Debug
  206. removed = set(a) - set(b)
  207. added = set(b) - set(a)
  208. if removed or added:
  209. return {
  210. 'removed': [i for i in a_tup if i.name in removed],
  211. 'added': [i for i in b_tup if i.name in added],
  212. }
  213. else:
  214. return None
  215. def show_changes(self,diff):
  216. ymsg('Warning: configuration file options have changed!\n')
  217. for desc in ('added','removed'):
  218. data = diff[desc]
  219. if data:
  220. opts = fmt_list([i.name for i in data],fmt='bare')
  221. msg(f' The following option{suf(data,verb="has")} been {desc}:\n {opts}\n')
  222. if desc == 'removed' and data:
  223. uc = mmgen_cfg_file(self.cfg,'usr')
  224. usr_names = [i.name for i in uc.get_lines()]
  225. rm_names = [i.name for i in data]
  226. bad = sorted(set(usr_names).intersection(rm_names))
  227. if bad:
  228. m = f"""
  229. The following removed option{suf(bad,verb='is')} set in {uc.fn!r}
  230. and must be deleted or commented out:
  231. {' ' + fmt_list(bad,fmt='bare')}
  232. """
  233. ymsg(fmt(m,indent=' ',strip_char='\t'))
  234. from .ui import keypress_confirm,do_pager
  235. while True:
  236. if not keypress_confirm( self.cfg, self.details_confirm_prompt, no_nl=True ):
  237. return
  238. def get_details():
  239. for desc,data in diff.items():
  240. sep,sep2 = ('\n ','\n\n ')
  241. if data:
  242. yield (
  243. f'{capfirst(desc)} section{suf(data)}:'
  244. + sep2
  245. + sep2.join([f'{sep.join(v.chunk)}' for v in data])
  246. )
  247. do_pager(
  248. 'CONFIGURATION FILE CHANGES\n\n'
  249. + '\n\n'.join(get_details()) + '\n'
  250. )