filename.py 3.8 KB

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