filename.py 3.6 KB

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