filename.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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. filename.py: File and MMGenFile classes and methods for the MMGen suite
  20. """
  21. import sys,os
  22. from .obj import *
  23. from .util import die,get_extension
  24. from .seed import *
  25. class File:
  26. def __init__(self,fn,write=False):
  27. self.name = fn
  28. self.dirname = os.path.dirname(fn)
  29. self.basename = os.path.basename(fn)
  30. self.ext = get_extension(fn)
  31. self.mtime = None
  32. self.ctime = None
  33. self.atime = None
  34. try:
  35. st = os.stat(fn)
  36. except:
  37. die( 'FileNotFound', f'{fn!r}: file not found' )
  38. import stat
  39. if stat.S_ISBLK(st.st_mode):
  40. mode = (os.O_RDONLY,os.O_RDWR)[bool(write)]
  41. if g.platform == 'win':
  42. mode |= os.O_BINARY
  43. try:
  44. fd = os.open(fn, mode)
  45. except OSError as e:
  46. if e.errno == 13:
  47. die(2,f'{fn!r}: permission denied')
  48. # if e.errno != 17: raise
  49. else:
  50. self.size = os.lseek(fd, 0, os.SEEK_END)
  51. os.close(fd)
  52. else:
  53. self.size = st.st_size
  54. self.mtime = st.st_mtime
  55. self.ctime = st.st_ctime
  56. self.atime = st.st_atime
  57. class FileList(list):
  58. def __init__(self,fns,write=False):
  59. return list.__init__(
  60. self,
  61. [File(fn,write) for fn in fns] )
  62. def names(self):
  63. return [f.name for f in self]
  64. def sort_by_age(self,key='mtime',reverse=False):
  65. assert key in ('atime','ctime','mtime'), f'{key!r}: invalid sort key'
  66. self.sort( key=lambda a: getattr(a,key), reverse=reverse )
  67. class MMGenFile(File):
  68. def __init__(self,fn,base_class=None,subclass=None,proto=None,write=False):
  69. """
  70. 'base_class' - a base class with an 'ext_to_cls' method
  71. 'subclass' - a subclass with an 'ext' attribute
  72. One or the other must be provided, but not both.
  73. The base class signals support for the MMGenFile API by setting its 'filename_api'
  74. attribute to True.
  75. """
  76. super().__init__(fn,write)
  77. assert (subclass or base_class) and not (subclass and base_class), 'MMGenFile chk1'
  78. if not getattr(subclass or base_class,'filename_api',False):
  79. die(3,f'Class {(subclass or base_class).__name__!r} does not support the MMGenFile API')
  80. if base_class:
  81. subclass = base_class.ext_to_cls( self.ext, proto )
  82. if not subclass:
  83. die( 'BadFileExtension', f'{self.ext!r}: not a recognized file extension for {base_class}' )
  84. self.subclass = subclass
  85. class MMGenFileList(FileList):
  86. def __init__(self,fns,base_class,proto=None,write=False):
  87. return list.__init__(
  88. self,
  89. [MMGenFile( fn, base_class=base_class, proto=proto, write=write ) for fn in fns] )
  90. def find_files_in_dir(subclass,fdir,no_dups=False):
  91. assert isinstance(subclass,type), f'{subclass}: not a class'
  92. if not getattr(subclass,'filename_api',False):
  93. die(3,f'Class {subclass.__name__!r} does not support the MMGenFile API')
  94. matches = [l for l in os.listdir(fdir) if l.endswith('.'+subclass.ext)]
  95. if no_dups:
  96. if len(matches) == 1:
  97. return os.path.join(fdir,matches[0])
  98. elif matches:
  99. die(1,f'ERROR: more than one {subclass.__name__} file in directory {fdir!r}')
  100. else:
  101. return None
  102. else:
  103. return [os.path.join(fdir,m) for m in matches]
  104. def find_file_in_dir(subclass,fdir):
  105. return find_files_in_dir(subclass,fdir,no_dups=True)