Commit 459ee708 authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

new function is_file_newer

"pushing" logic abilities to the limit
parent 9f1e7931
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ from ..revision import version as version_info
from ..iostrings import encode
from .reader     import read_echo, read_magnetic, read_xyz
from .writer     import EchoWriter
from .utils      import get_nsefiletype
from .utils      import get_nsefiletype, is_file_newer

#FIXME: we need to work on the idea of a schema (or not???)
SCHEMA_DEFAULT = {
@@ -225,10 +225,7 @@ def convert_to_hdf(filename, outdir, **kwargs):
    outfile  = _make_outfilename(basename, outdir=outdir, filetype=filetype)

    _log.info('converting %s to HDF5', basename)
    if os.path.exists(outfile) and not overwrite:
        t_out = os.path.getctime(outfile)
        t_src = os.path.getctime(filename)
        if t_out > t_src:
    if not overwrite and is_file_newer(outfile, filename):
        _log.info('file %s is newer than %s', outfile, filename)
        return outfile

+20 −0
Original line number Diff line number Diff line
@@ -87,3 +87,23 @@ def get_nsefiletype(filename):
    if callable(filetype):
        return filetype(filename)
    return filetype

def is_file_newer(outfile, inpfile=None):
    """check if outfile is newer than inpfile

    returns False - if outfile does not exist
    returns True  - if inpfile does not exist
    returns True  - if outfile is newer than inpfile
    returns False
    """
    # outfile does not exist
    if not os.path.exists(outfile):
        return False
    # inpfile does not exist
    if inpfile is None or not os.path.exists(inpfile):
        return True
    # both files exist
    t_inp = os.path.getmtime(inpfile)
    t_out = os.path.getmtime(outfile)
    # return True is outfile is newer than inpfile
    return not t_inp > t_out
+25 −0
Original line number Diff line number Diff line
@@ -79,6 +79,31 @@ class IOUtilsTestCase(unittest.TestCase):
        with self.assertRaises(FileNotFoundError):
            utils.get_nsefiletype(os.path.join(TestDataDir,'not-here.dat'))

    def test_is_file_newer(self):
        """test check if outfile is newer than inpfile
        # GO   == False - file is not newer, proceed
        # STOP == True  - file is newer, stop
        """
        # GO    outfile does not exist
        self.assertFalse(utils.is_file_newer( os.path.join(TestDataDir,'outfile.nxs.h5')))
        self.assertFalse(utils.is_file_newer( os.path.join(TestDataDir,'outfile.nxs.h5'),
                                              os.path.join(TestDataDir,'NSE_15617.nxs.h5')))

        # STOP  outfile does exist, but inpfile is None or does not exist
        self.assertTrue( utils.is_file_newer( os.path.join(TestDataDir,'NSE_15617.nxs.h5')))
        self.assertTrue( utils.is_file_newer( os.path.join(TestDataDir,'NSE_15617.nxs.h5'),
                                              os.path.join(TestDataDir,'does-not-exist.dat')))

        # both files exist
        # GO    out=15617 is older than inp=16247
        self.assertFalse(utils.is_file_newer( os.path.join(TestDataDir,'NSE_15617.nxs.h5'),
                                              os.path.join(TestDataDir,'NSE_16247.nxs.h5')))
        # STOP  out=16247 is newer than inp=15617
        self.assertTrue( utils.is_file_newer( os.path.join(TestDataDir,'NSE_16247.nxs.h5'),
                                              os.path.join(TestDataDir,'NSE_15617.nxs.h5')))
        # STOP  out=15617 is same as inp=15617
        self.assertTrue( utils.is_file_newer( os.path.join(TestDataDir,'NSE_15617.nxs.h5'),
                                              os.path.join(TestDataDir,'NSE_15617.nxs.h5')))


if __name__ == "__main__":