Code source de pyspc.binutils.get_oldest_file

#!/usr/bin/python3
# -*- coding: utf-8 -*-
########################################################################
#
# This file is part of python module <pySPC>.
# Copyright (C) 2013-2020  R. Marty
#   (renaud.marty@developpement-durable.gouv.fr)
#
# 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 (see COPYING.txt).
# If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
"""
Find and return the oldest file of input file names.
Only one wins tie. Values based on time distance from present.
Use of `_invert` inverts logic to make this a youngest routine,
to be used more clearly via `get_youngest_file`.
"""
import os
import time
import operator


[docs] def get_oldest_file(files, _invert=False): """ Find and return the oldest file of input file names. Only one wins tie. Values based on time distance from present. Use of `_invert` inverts logic to make this a youngest routine, to be used more clearly via `get_youngest_file`. """ gt = operator.lt if _invert else operator.gt # Check for empty list. if not files: return None # Raw epoch distance. now = time.time() # Select first as arbitrary sentinel file, storing name and age. oldest = files[0], now - os.path.getctime(files[0]) # Iterate over all remaining files. for f in files[1:]: age = now - os.path.getctime(f) if gt(age, oldest[1]): # Set new oldest. oldest = f, age # Return just the name of oldest file. return oldest[0]