filename.py 3.6 KB

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