filename.py 3.5 KB

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