Skip to content

Commit 824b5c7

Browse files
committed
Fix issue copying npython to scripts.
1 parent 535babc commit 824b5c7

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

setup.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
from distutils.command.build_scripts import build_scripts
88
from distutils.command.install_lib import install_lib
99
from distutils.sysconfig import get_config_var
10+
from distutils.util import convert_path
11+
from distutils.dep_util import newer
12+
from distutils import log
1013
from platform import architecture
1114
from subprocess import Popen, CalledProcessError, PIPE, check_call
1215
from glob import glob
13-
import shutil
16+
import stat
1417
import sys
1518
import os
1619

@@ -274,6 +277,60 @@ def finalize_options(self):
274277
scripts.append(script)
275278
self.scripts = scripts
276279

280+
def copy_scripts(self):
281+
# Look for the npython script as it can't be copied by the base class'
282+
# copy_scripts as it attempts to determine the file encoding as if it
283+
# were a text file.
284+
npython = None
285+
for script in self.scripts:
286+
if os.path.basename(script) == _npython_exe:
287+
npython = script
288+
289+
if npython is None:
290+
return build_scripts.copy_scripts(self)
291+
292+
# Call the base class copy_scripts with anything other than npython
293+
scripts = self.scripts
294+
self.scripts = [x for x in scripts if x != npython]
295+
try:
296+
base_result = build_scripts.copy_scripts(self)
297+
finally:
298+
self.scripts = scripts
299+
300+
# Copy npython
301+
outfiles = []
302+
updated_files = []
303+
304+
script = convert_path(npython)
305+
outfile = os.path.join(self.build_dir, os.path.basename(script))
306+
outfiles.append(outfile)
307+
308+
if not self.force and not newer(script, outfile):
309+
log.debug("not copying %s (up-to-date)", script)
310+
else:
311+
updated_files.append(outfile)
312+
self.copy_file(script, outfile)
313+
314+
if os.name == 'posix':
315+
for file in outfiles:
316+
if self.dry_run:
317+
log.info("changing mode of %s", file)
318+
else:
319+
oldmode = os.stat(file)[stat.ST_MODE] & 0o7777
320+
newmode = (oldmode | 0o555) & 0o7777
321+
if newmode != oldmode:
322+
log.info("changing mode of %s from %o to %o",
323+
file, oldmode, newmode)
324+
os.chmod(file, newmode)
325+
326+
# Some versions of build_command.copy_scripts return (outfiles, updated_files),
327+
# older versions return None.
328+
if base_result is not None:
329+
base_outfiles, base_updated_files = base_result
330+
outfiles.extend(base_outfiles)
331+
updated_files.extend(base_updated_files)
332+
return outfiles, updated_files
333+
277334

278335
def _check_output(*popenargs, **kwargs):
279336
"""subprocess.check_output from python 2.7.
@@ -309,9 +366,9 @@ def _check_output(*popenargs, **kwargs):
309366
scripts=[_npython_exe],
310367
zip_safe=False,
311368
cmdclass={
312-
"build_ext" : PythonNET_BuildExt,
313-
"build_scripts" : PythonNET_BuildScripts,
314-
"install_lib" : PythonNET_InstallLib
369+
"build_ext": PythonNET_BuildExt,
370+
"build_scripts": PythonNET_BuildScripts,
371+
"install_lib": PythonNET_InstallLib,
315372
}
316373
)
317374

0 commit comments

Comments
 (0)