Skip to content

Commit ae68847

Browse files
authored
copy newer_group form distutils.dep_util to avoid import distutils (PaddlePaddle#5183)
1 parent f727741 commit ae68847

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

paddlenlp/ops/ext_utils.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import subprocess
1919
import sys
2020
import textwrap
21-
from distutils.dep_util import newer_group
2221
from pathlib import Path
2322

2423
from filelock import FileLock
@@ -71,6 +70,47 @@ def _get_files(path):
7170
return all_files
7271

7372

73+
# copy form distutils.dep_util to avoid import distutils
74+
def newer_group(sources, target, missing="error"):
75+
"""Return true if 'target' is out-of-date with respect to any file
76+
listed in 'sources'. In other words, if 'target' exists and is newer
77+
than every file in 'sources', return false; otherwise return true.
78+
'missing' controls what we do when a source file is missing; the
79+
default ("error") is to blow up with an OSError from inside 'stat()';
80+
if it is "ignore", we silently drop any missing source files; if it is
81+
"newer", any missing source files make us assume that 'target' is
82+
out-of-date (this is handy in "dry-run" mode: it'll make you pretend to
83+
carry out commands that wouldn't work because inputs are missing, but
84+
that doesn't matter because you're not actually going to run the
85+
commands).
86+
"""
87+
# If the target doesn't even exist, then it's definitely out-of-date.
88+
if not os.path.exists(target):
89+
return 1
90+
91+
# Otherwise we have to find out the hard way: if *any* source file
92+
# is more recent than 'target', then 'target' is out-of-date and
93+
# we can immediately return true. If we fall through to the end
94+
# of the loop, then 'target' is up-to-date and we return false.
95+
from stat import ST_MTIME
96+
97+
target_mtime = os.stat(target)[ST_MTIME]
98+
for source in sources:
99+
if not os.path.exists(source):
100+
if missing == "error": # blow up when we stat() the file
101+
pass
102+
elif missing == "ignore": # missing source dropped from
103+
continue # target's dependency list
104+
elif missing == "newer": # missing source means target is
105+
return 1 # out-of-date
106+
107+
source_mtime = os.stat(source)[ST_MTIME]
108+
if source_mtime > target_mtime:
109+
return 1
110+
else:
111+
return 0
112+
113+
74114
class CMakeExtension(Extension):
75115
def __init__(self, name, source_dir=None):
76116
# A CMakeExtension needs a source_dir instead of a file list.

0 commit comments

Comments
 (0)