filename.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2016 Philemon <mmgen-py@yandex.com>
  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,check_infile
  24. class Filename(MMGenObject):
  25. def __init__(self,fn,ftype=None,write=False):
  26. self.name = fn
  27. self.dirname = os.path.dirname(fn)
  28. self.basename = os.path.basename(fn)
  29. self.ext = None
  30. self.ftype = ftype
  31. # This should be done before license msg instead
  32. # check_infile(fn)
  33. if not ftype:
  34. self.ext = get_extension(fn)
  35. if not (self.ext):
  36. die(2,"Unrecognized extension '.%s' for file '%s'" % (self.ext,fn))
  37. # TODO: Check for Windows
  38. mode = (os.O_RDONLY,os.O_RDWR)[bool(write)]
  39. import stat
  40. if stat.S_ISBLK(os.stat(fn).st_mode):
  41. try:
  42. fd = os.open(fn, mode)
  43. except OSError as e:
  44. if e.errno == 13:
  45. die(2,"'%s': permission denied" % fn)
  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 = os.stat(fn).st_size