| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #!/usr/bin/env python3
- #
- # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
- # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- """
- filename.py: Filename class and methods for the MMGen suite
- """
- import sys,os
- from .obj import *
- from .util import die,get_extension
- from .seed import *
- class Filename(MMGenObject):
- def __init__(self,fn,ftype=None,from_extension=None,write=False):
- """
- 'ftype' - a subclass with an 'ext' attribute
- 'from_extension' - a base class with an 'ext_to_type' method
- One or the other must be provided, but not both.
- The base class signals support for the Filename API by setting its 'filename_api'
- attribute to True.
- """
- self.name = fn
- self.dirname = os.path.dirname(fn)
- self.basename = os.path.basename(fn)
- self.ext = get_extension(fn)
- self.mtime = None
- self.ctime = None
- self.atime = None
- assert (ftype or from_extension) and not (ftype and from_extension), 'Filename chk1'
- if not getattr(ftype or from_extension,'filename_api',False):
- die(3,f'Class {(ftype or from_extension).__name__!r} does not support the Filename API')
- if from_extension:
- ftype = from_extension.ext_to_type(self.ext)
- if not ftype:
- from .exception import BadFileExtension
- raise BadFileExtension(f'{self.ext!r}: not a recognized file extension for {from_extension}')
- self.ftype = ftype
- try:
- st = os.stat(fn)
- except:
- from .exception import FileNotFound
- raise FileNotFound(f'{fn!r}: file not found')
- import stat
- if stat.S_ISBLK(st.st_mode):
- mode = (os.O_RDONLY,os.O_RDWR)[bool(write)]
- if g.platform == 'win':
- mode |= os.O_BINARY
- try:
- fd = os.open(fn, mode)
- except OSError as e:
- if e.errno == 13:
- die(2,f'{fn!r}: permission denied')
- # if e.errno != 17: raise
- else:
- self.size = os.lseek(fd, 0, os.SEEK_END)
- os.close(fd)
- else:
- self.size = st.st_size
- self.mtime = st.st_mtime
- self.ctime = st.st_ctime
- self.atime = st.st_atime
- class MMGenFileList(list,MMGenObject):
- def __init__(self,fns,ftype):
- flist = [Filename(fn,ftype) for fn in fns]
- return list.__init__(self,flist)
- def names(self):
- return [f.name for f in self]
- def sort_by_age(self,key='mtime',reverse=False):
- if key not in ('atime','ctime','mtime'):
- die(1,f'{key!r}: illegal sort key')
- self.sort(key=lambda a: getattr(a,key),reverse=reverse)
- def find_files_in_dir(ftype,fdir,no_dups=False):
- assert isinstance(ftype,type), f'{ftype}: not a class'
- if not getattr(ftype,'filename_api',False):
- die(3,f'Class {ftype.__name__!r} does not support the Filename API')
- matches = [l for l in os.listdir(fdir) if l.endswith('.'+ftype.ext)]
- if no_dups:
- if len(matches) == 1:
- return os.path.join(fdir,matches[0])
- elif matches:
- die(1,f'ERROR: more than one {ftype.__name__} file in directory {fdir!r}')
- else:
- return None
- else:
- return [os.path.join(fdir,m) for m in matches]
- def find_file_in_dir(ftype,fdir):
- return find_files_in_dir(ftype,fdir,no_dups=True)
|