From 6916b45f114cf363eaef659914f6deb95dfa1a0b Mon Sep 17 00:00:00 2001 From: hploetz Date: Fri, 8 Jun 2007 05:33:29 +0000 Subject: [PATCH] Reading from and saving to filesets works git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@230 f711b948-2313-0410-aaa9-d29f33439f0b --- cards/passport_application.py | 74 ++++++++++++++++++++++++++++++++++- readpass.py | 38 ++++++++++++------ 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/cards/passport_application.py b/cards/passport_application.py index 925405a..b05b0a0 100644 --- a/cards/passport_application.py +++ b/cards/passport_application.py @@ -853,9 +853,81 @@ class Passport(object): format is the same one that is being used by the Golden Reader Tool. Alternatively you may give the filemap argument which must be a mapping to filenames with the keys "COM", "SOD", "DG1", "DG2", etc. (Only COM, SOD and DG1 are mandatory.) - One of basename or filemap _must_ be specified.""" + Exactly one of basename or filemap _must_ be specified.""" + + p = cls() + filemap = p._make_filemap(basename, filemap, True) + + for name, _ in Passport_Application.INTERESTING_FILES: + if not filemap.has_key(name): + continue + + try: + data = "" + fp = file(filemap[name], "rb") + try: + data = fp.read() + finally: + fp.close() + except (SystemExit, KeyboardInterrupt): + raise + except IOError: + if sys.exc_info()[1].errno == 2: + # Ignore + pass + else: + raise + else: + if data != "": + setattr(p, "contents_%s" % name, data) + if hasattr(p, "parse_%s" % name): + getattr(p, "parse_%s" % name)(data) + + return p from_files = classmethod(from_files) + def to_files(self, basename = None, filemap = None): + """Save an instance to a number of files. + The format is the same one that is being used by the Golden Reader Tool. Alternatively you may give + the filemap argument which must be a mapping to filenames with the keys "COM", "SOD", "DG1", "DG2", etc. + (All data files in memory are mandatory.) + Exactly one of basename or filemap _must_ be specified.""" + + filemap = self._make_filemap(basename, filemap, False) + + for name, _ in Passport_Application.INTERESTING_FILES: + if not hasattr(self, "contents_%s" % name): + continue + + try: + fp = file(filemap[name], "wb") + try: + fp.write( getattr(self, "contents_%s" % name) ) + finally: + fp.close() + except (SystemExit, KeyboardInterrupt): + raise + + def _make_filemap(self, basename, filemap, assert_existing=False): + "Internal function for code shared between from_files and to_files" + if filemap is None and basename is None: + raise TypeError, "Either basename or filemap must be specified, both cannot be empty." + if filemap is not None and basename is not None: + raise TypeError, "Either basename or filemap must be specified, both cannot be set." + + if filemap is None: + filemap = {} + for name, _ in Passport_Application.INTERESTING_FILES: + filemap[name] = "%s%s.bin" % (basename, name) + if assert_existing and not os.path.exists(filemap[name]): + del filemap[name] + + if not sum([not filemap.has_key(e) for e in "COM", "SOD", "DG1"]) == 0: + raise KeyError, "In order to read a file set from disk, COM, SOD and DG1 must exist" + + return filemap + + def parse_DG1(self, contents): structure = TLV_utils.unpack(contents) try: diff --git a/readpass.py b/readpass.py index b439b1f..1e36ba2 100755 --- a/readpass.py +++ b/readpass.py @@ -4,10 +4,12 @@ from utils import pycsc import utils, cards, TLV_utils, sys, binascii, time, traceback -OPTIONS = "G" -LONG_OPTIONS = ["no-gui"] +OPTIONS = "GW:R:" +LONG_OPTIONS = ["no-gui", "write-files-basename", "read-files-basename"] use_gui = True +write_files = None +read_files = None if __name__ == "__main__": c = utils.CommandLineArgumentHelper() @@ -17,19 +19,29 @@ if __name__ == "__main__": for option, value in options: if option in ("-G","--no-gui"): use_gui = False + elif option in ("-W","--write-files-basename"): + write_files = value + elif option in ("-R","--read-files-basename"): + read_files = value - pycsc_card = c.connect() - card = cards.new_card_object(pycsc_card) - cards.generic_card.DEBUG = False - - print >>sys.stderr, "Using %s" % card.DRIVER_NAME - - if len(arguments) > 1: - p = cards.passport_application.Passport.from_card(card, arguments[:2]) - elif len(arguments) == 1: - p = cards.passport_application.Passport.from_card(card, ["",arguments[0]]) + if read_files is None: + pycsc_card = c.connect() + card = cards.new_card_object(pycsc_card) + cards.generic_card.DEBUG = False + + print >>sys.stderr, "Using %s" % card.DRIVER_NAME + + if len(arguments) > 1: + p = cards.passport_application.Passport.from_card(card, arguments[:2]) + elif len(arguments) == 1: + p = cards.passport_application.Passport.from_card(card, ["",arguments[0]]) + else: + p = cards.passport_application.Passport.from_card(card) else: - p = cards.passport_application.Passport.from_card(card) + p = cards.passport_application.Passport.from_files(basename=read_files) + + if write_files is not None: + p.to_files(basename=write_files) if use_gui: import gui