filename.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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 mmgen.exception import BadFileExtension,FileNotFound
  23. from mmgen.obj import *
  24. from mmgen.util import die,get_extension
  25. from mmgen.seed import *
  26. class Filename(MMGenObject):
  27. def __init__(self,fn,ftype=None,write=False):
  28. self.name = fn
  29. self.dirname = os.path.dirname(fn)
  30. self.basename = os.path.basename(fn)
  31. self.ext = get_extension(fn)
  32. self.ftype = None # the file's associated class
  33. self.mtime = None
  34. self.ctime = None
  35. self.atime = None
  36. from mmgen.seed import Wallet
  37. from mmgen.tx import MMGenTX
  38. if ftype:
  39. if isinstance(ftype,type):
  40. if issubclass(ftype,(Wallet,MMGenTX)):
  41. self.ftype = ftype
  42. # elif: # other MMGen file types
  43. else:
  44. die(3,"'{}': not a recognized file type for Wallet".format(ftype))
  45. else:
  46. die(3,"'{}': not a class".format(ftype))
  47. else:
  48. # TODO: other file types
  49. self.ftype = Wallet.ext_to_type(self.ext)
  50. if not self.ftype:
  51. m = "'{}': not a recognized Wallet file extension".format(self.ext)
  52. raise BadFileExtension(m)
  53. try:
  54. st = os.stat(fn)
  55. except:
  56. raise FileNotFound('{!r}: file not found'.format(fn))
  57. import stat
  58. if stat.S_ISBLK(st.st_mode):
  59. mode = (os.O_RDONLY,os.O_RDWR)[bool(write)]
  60. if g.platform == 'win': mode |= os.O_BINARY
  61. try:
  62. fd = os.open(fn, mode)
  63. except OSError as e:
  64. if e.errno == 13:
  65. die(2,"'{}': permission denied".format(fn))
  66. # if e.errno != 17: raise
  67. else:
  68. self.size = os.lseek(fd, 0, os.SEEK_END)
  69. os.close(fd)
  70. else:
  71. self.size = st.st_size
  72. self.mtime = st.st_mtime
  73. self.ctime = st.st_ctime
  74. self.atime = st.st_atime
  75. class MMGenFileList(list,MMGenObject):
  76. def __init__(self,fns,ftype):
  77. flist = [Filename(fn,ftype) for fn in fns]
  78. return list.__init__(self,flist)
  79. def names(self):
  80. return [f.name for f in self]
  81. def sort_by_age(self,key='mtime',reverse=False):
  82. if key not in ('atime','ctime','mtime'):
  83. die(1,"'{}': illegal sort key".format(key))
  84. self.sort(key=lambda a: getattr(a,key),reverse=reverse)
  85. def find_files_in_dir(ftype,fdir,no_dups=False):
  86. if not isinstance(ftype,type):
  87. die(3,"'{}': is of type {} (not a subclass of type 'type')".format(ftype,type(ftype)))
  88. from mmgen.seed import Wallet
  89. if not issubclass(ftype,Wallet):
  90. die(3,"'{}': not a recognized file type".format(ftype))
  91. try: dirlist = os.listdir(fdir)
  92. except: die(3,"ERROR: unable to read directory '{}'".format(fdir))
  93. matches = [l for l in dirlist if l[-len(ftype.ext)-1:]=='.'+ftype.ext]
  94. if no_dups:
  95. if len(matches) > 1:
  96. die(1,"ERROR: more than one {} file in directory '{}'".format(ftype.__name__,fdir))
  97. return os.path.join(fdir,matches[0]) if len(matches) else None
  98. else:
  99. return [os.path.join(fdir,m) for m in matches]
  100. def find_file_in_dir(ftype,fdir,no_dups=True):
  101. return find_files_in_dir(ftype,fdir,no_dups=no_dups)