"""distutils.command.build_scripts
Implements the Distutils 'build_scripts' command."""
# This module should be kept compatible with Python 2.1.
__revision__
= "$Id: build_scripts.py,v 1.25 2004/11/10 22:23:15 loewis Exp $"
from distutils
import sysconfig
from distutils
.core
import Command
from distutils
.dep_util
import newer
from distutils
.util
import convert_path
from distutils
import log
# check if Python is called on the first line with this expression
first_line_re
= re
.compile('^#!.*python[0-9.]*([ \t].*)?$')
class build_scripts (Command
):
description
= "\"build\" scripts (copy and fixup #! line)"
('build-dir=', 'd', "directory to \"build\" (copy) to"),
('force', 'f', "forcibly build everything (ignore file timestamps"),
('executable=', 'e', "specify final destination interpreter path"),
boolean_options
= ['force']
def initialize_options (self
):
def finalize_options (self
):
self
.set_undefined_options('build',
('build_scripts', 'build_dir'),
('executable', 'executable'))
self
.scripts
= self
.distribution
.scripts
def get_source_files(self
):
"""Copy each script listed in 'self.scripts'; if it's marked as a
Python script in the Unix way (first line matches 'first_line_re',
ie. starts with "\#!" and contains "python"), then adjust the first
line to refer to the current Python interpreter as we copy.
self
.mkpath(self
.build_dir
)
for script
in self
.scripts
:
script
= convert_path(script
)
outfile
= os
.path
.join(self
.build_dir
, os
.path
.basename(script
))
if not self
.force
and not newer(script
, outfile
):
log
.debug("not copying %s (up-to-date)", script
)
# Always open the file, but ignore failures in dry-run mode --
# that way, we'll get accurate feedback if we can read the
first_line
= f
.readline()
self
.warn("%s is an empty file (skipping)" % script
)
match
= first_line_re
.match(first_line
)
post_interp
= match
.group(1) or ''
log
.info("copying and adjusting %s -> %s", script
,
outf
= open(outfile
, "w")
if not sysconfig
.python_build
:
sysconfig
.get_config_var("BINDIR"),
"python" + sysconfig
.get_config_var("EXE")),
outf
.writelines(f
.readlines())
self
.copy_file(script
, outfile
)
log
.info("changing mode of %s", file)
oldmode
= os
.stat(file)[ST_MODE
] & 07777
newmode
= (oldmode |
0555) & 07777
log
.info("changing mode of %s from %o to %o",