Skip to content

Commit c4e27db

Browse files
committed
"Refactor to newer_group to utilize higher level constructs ("any"), re-use _newer logic, and avoid complexity in branching."
1 parent 4a70331 commit c4e27db

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

distutils/dep_util.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
from .errors import DistutilsFileError
77

88

9+
def _newer(source, target):
10+
if not os.path.exists(target):
11+
return True
12+
13+
mtime1 = os.stat(source)[stat.ST_MTIME]
14+
mtime2 = os.stat(target)[stat.ST_MTIME]
15+
16+
return mtime1 > mtime2
17+
18+
919
def newer(source, target):
1020
"""
1121
Is source modified more recently than target.
@@ -18,13 +28,7 @@ def newer(source, target):
1828
if not os.path.exists(source):
1929
raise DistutilsFileError("file '%s' does not exist" % os.path.abspath(source))
2030

21-
if not os.path.exists(target):
22-
return True
23-
24-
mtime1 = os.stat(source)[stat.ST_MTIME]
25-
mtime2 = os.stat(target)[stat.ST_MTIME]
26-
27-
return mtime1 > mtime2
31+
return _newer(source, target)
2832

2933

3034
def newer_pairwise(sources, targets):
@@ -66,26 +70,12 @@ def newer_group(sources, target, missing='error'):
6670
commands that wouldn't work because inputs are missing, but
6771
that doesn't matter because dry-run won't run the commands.
6872
"""
69-
# If the target doesn't even exist, then it's definitely out-of-date.
70-
if not os.path.exists(target):
71-
return True
7273

73-
# If *any* source file
74-
# is more recent than 'target', then 'target' is out-of-date and
75-
# we can immediately return True. If the loop completes, then
76-
# 'target' is up-to-date.
77-
target_mtime = os.stat(target)[stat.ST_MTIME]
78-
for source in sources:
79-
if not os.path.exists(source):
80-
if missing == 'error': # blow up when we stat() the file
81-
pass
82-
elif missing == 'ignore': # missing source dropped from
83-
continue # target's dependency list
84-
elif missing == 'newer': # missing source means target is
85-
return True # out-of-date
86-
87-
source_mtime = os.stat(source)[stat.ST_MTIME]
88-
if source_mtime > target_mtime:
89-
return True
90-
else:
91-
return False
74+
def missing_as_newer(source):
75+
return missing == 'newer' and not os.path.exists(source)
76+
77+
ignored = os.path.exists if missing == 'ignore' else None
78+
return any(
79+
missing_as_newer(source) or _newer(source, target)
80+
for source in filter(ignored, sources)
81+
)

0 commit comments

Comments
 (0)