Path: blob/main/test/lib/python3.9/site-packages/setuptools/_distutils/dep_util.py
4799 views
"""distutils.dep_util12Utility functions for simple, timestamp-based dependency of files3and groups of files; also, function based entirely on such4timestamp dependency analysis."""56import os7from distutils.errors import DistutilsFileError8910def newer (source, target):11"""Return true if 'source' exists and is more recently modified than12'target', or if 'source' exists and 'target' doesn't. Return false if13both exist and 'target' is the same age or younger than 'source'.14Raise DistutilsFileError if 'source' does not exist.15"""16if not os.path.exists(source):17raise DistutilsFileError("file '%s' does not exist" %18os.path.abspath(source))19if not os.path.exists(target):20return 12122from stat import ST_MTIME23mtime1 = os.stat(source)[ST_MTIME]24mtime2 = os.stat(target)[ST_MTIME]2526return mtime1 > mtime22728# newer ()293031def newer_pairwise (sources, targets):32"""Walk two filename lists in parallel, testing if each source is newer33than its corresponding target. Return a pair of lists (sources,34targets) where source is newer than target, according to the semantics35of 'newer()'.36"""37if len(sources) != len(targets):38raise ValueError("'sources' and 'targets' must be same length")3940# build a pair of lists (sources, targets) where source is newer41n_sources = []42n_targets = []43for i in range(len(sources)):44if newer(sources[i], targets[i]):45n_sources.append(sources[i])46n_targets.append(targets[i])4748return (n_sources, n_targets)4950# newer_pairwise ()515253def newer_group (sources, target, missing='error'):54"""Return true if 'target' is out-of-date with respect to any file55listed in 'sources'. In other words, if 'target' exists and is newer56than every file in 'sources', return false; otherwise return true.57'missing' controls what we do when a source file is missing; the58default ("error") is to blow up with an OSError from inside 'stat()';59if it is "ignore", we silently drop any missing source files; if it is60"newer", any missing source files make us assume that 'target' is61out-of-date (this is handy in "dry-run" mode: it'll make you pretend to62carry out commands that wouldn't work because inputs are missing, but63that doesn't matter because you're not actually going to run the64commands).65"""66# If the target doesn't even exist, then it's definitely out-of-date.67if not os.path.exists(target):68return 16970# Otherwise we have to find out the hard way: if *any* source file71# is more recent than 'target', then 'target' is out-of-date and72# we can immediately return true. If we fall through to the end73# of the loop, then 'target' is up-to-date and we return false.74from stat import ST_MTIME75target_mtime = os.stat(target)[ST_MTIME]76for source in sources:77if not os.path.exists(source):78if missing == 'error': # blow up when we stat() the file79pass80elif missing == 'ignore': # missing source dropped from81continue # target's dependency list82elif missing == 'newer': # missing source means target is83return 1 # out-of-date8485source_mtime = os.stat(source)[ST_MTIME]86if source_mtime > target_mtime:87return 188else:89return 09091# newer_group ()929394