2000-05-24 22:20:15 -03:00
|
|
|
"""distutils.command.build_scripts
|
|
|
|
|
|
|
|
Implements the Distutils 'build_scripts' command."""
|
|
|
|
|
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os, re
|
2003-01-24 10:56:52 -04:00
|
|
|
from stat import ST_MODE
|
2000-05-24 22:20:15 -03:00
|
|
|
from distutils.core import Command
|
2000-05-24 23:03:56 -03:00
|
|
|
from distutils.dep_util import newer
|
2001-07-29 18:39:18 -03:00
|
|
|
from distutils.util import convert_path
|
2002-06-04 17:14:43 -03:00
|
|
|
from distutils import log
|
2000-05-24 22:20:15 -03:00
|
|
|
|
2001-07-25 17:20:11 -03:00
|
|
|
# check if Python is called on the first line with this expression
|
2003-06-27 16:33:38 -03:00
|
|
|
first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
|
2000-05-24 22:20:15 -03:00
|
|
|
|
|
|
|
class build_scripts (Command):
|
|
|
|
|
2000-05-24 23:03:56 -03:00
|
|
|
description = "\"build\" scripts (copy and fixup #! line)"
|
2000-05-24 22:20:15 -03:00
|
|
|
|
|
|
|
user_options = [
|
|
|
|
('build-dir=', 'd', "directory to \"build\" (copy) to"),
|
|
|
|
('force', 'f', "forcibly build everything (ignore file timestamps"),
|
2004-08-25 08:37:43 -03:00
|
|
|
('executable=', 'e', "specify final destination interpreter path"),
|
2000-05-24 22:20:15 -03:00
|
|
|
]
|
|
|
|
|
2000-09-24 22:41:15 -03:00
|
|
|
boolean_options = ['force']
|
|
|
|
|
2000-05-24 22:20:15 -03:00
|
|
|
|
|
|
|
def initialize_options (self):
|
|
|
|
self.build_dir = None
|
|
|
|
self.scripts = None
|
|
|
|
self.force = None
|
2004-08-25 08:37:43 -03:00
|
|
|
self.executable = None
|
2000-05-24 22:20:15 -03:00
|
|
|
self.outfiles = None
|
|
|
|
|
|
|
|
def finalize_options (self):
|
2000-09-30 15:27:54 -03:00
|
|
|
self.set_undefined_options('build',
|
|
|
|
('build_scripts', 'build_dir'),
|
2004-08-25 08:37:43 -03:00
|
|
|
('force', 'force'),
|
|
|
|
('executable', 'executable'))
|
2000-05-24 22:20:15 -03:00
|
|
|
self.scripts = self.distribution.scripts
|
|
|
|
|
2004-03-25 18:04:52 -04:00
|
|
|
def get_source_files(self):
|
|
|
|
return self.scripts
|
2000-05-24 22:20:15 -03:00
|
|
|
|
|
|
|
def run (self):
|
|
|
|
if not self.scripts:
|
|
|
|
return
|
2000-05-24 23:03:56 -03:00
|
|
|
self.copy_scripts()
|
|
|
|
|
2001-03-02 03:28:03 -04:00
|
|
|
|
2000-05-24 23:03:56 -03:00
|
|
|
def copy_scripts (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
|
2000-07-26 23:13:20 -03:00
|
|
|
line to refer to the current Python interpreter as we copy.
|
2000-05-24 23:03:56 -03:00
|
|
|
"""
|
2010-01-23 05:23:15 -04:00
|
|
|
_sysconfig = __import__('sysconfig')
|
2000-05-24 22:20:15 -03:00
|
|
|
self.mkpath(self.build_dir)
|
2003-01-24 10:56:52 -04:00
|
|
|
outfiles = []
|
2000-05-24 23:03:56 -03:00
|
|
|
for script in self.scripts:
|
|
|
|
adjust = 0
|
2001-07-29 18:39:18 -03:00
|
|
|
script = convert_path(script)
|
2000-05-25 17:05:52 -03:00
|
|
|
outfile = os.path.join(self.build_dir, os.path.basename(script))
|
2003-01-24 10:56:52 -04:00
|
|
|
outfiles.append(outfile)
|
2000-05-24 23:03:56 -03:00
|
|
|
|
|
|
|
if not self.force and not newer(script, outfile):
|
2002-06-04 17:14:43 -03:00
|
|
|
log.debug("not copying %s (up-to-date)", script)
|
2000-05-24 23:03:56 -03:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Always open the file, but ignore failures in dry-run mode --
|
|
|
|
# that way, we'll get accurate feedback if we can read the
|
|
|
|
# script.
|
|
|
|
try:
|
|
|
|
f = open(script, "r")
|
|
|
|
except IOError:
|
|
|
|
if not self.dry_run:
|
|
|
|
raise
|
|
|
|
f = None
|
|
|
|
else:
|
|
|
|
first_line = f.readline()
|
|
|
|
if not first_line:
|
|
|
|
self.warn("%s is an empty file (skipping)" % script)
|
2000-05-24 22:20:15 -03:00
|
|
|
continue
|
2000-05-24 23:03:56 -03:00
|
|
|
|
|
|
|
match = first_line_re.match(first_line)
|
|
|
|
if match:
|
|
|
|
adjust = 1
|
2001-12-11 16:44:42 -04:00
|
|
|
post_interp = match.group(1) or ''
|
2000-05-24 23:03:56 -03:00
|
|
|
|
|
|
|
if adjust:
|
2002-06-04 17:14:43 -03:00
|
|
|
log.info("copying and adjusting %s -> %s", script,
|
|
|
|
self.build_dir)
|
2000-05-24 23:03:56 -03:00
|
|
|
if not self.dry_run:
|
|
|
|
outf = open(outfile, "w")
|
2010-01-23 05:23:15 -04:00
|
|
|
if not _sysconfig.is_python_build():
|
2004-07-18 03:16:08 -03:00
|
|
|
outf.write("#!%s%s\n" %
|
2004-08-25 08:37:43 -03:00
|
|
|
(self.executable,
|
2001-12-10 12:15:44 -04:00
|
|
|
post_interp))
|
|
|
|
else:
|
2003-06-27 16:33:38 -03:00
|
|
|
outf.write("#!%s%s\n" %
|
2001-12-10 12:15:44 -04:00
|
|
|
(os.path.join(
|
2010-01-23 05:23:15 -04:00
|
|
|
_sysconfig.get_config_var("BINDIR"),
|
|
|
|
"python%s%s" % (_sysconfig.get_config_var("VERSION"),
|
|
|
|
_sysconfig.get_config_var("EXE"))),
|
2001-12-10 12:15:44 -04:00
|
|
|
post_interp))
|
2000-05-24 23:03:56 -03:00
|
|
|
outf.writelines(f.readlines())
|
|
|
|
outf.close()
|
|
|
|
if f:
|
|
|
|
f.close()
|
|
|
|
else:
|
2008-01-02 15:00:46 -04:00
|
|
|
if f:
|
|
|
|
f.close()
|
2000-05-24 23:03:56 -03:00
|
|
|
self.copy_file(script, outfile)
|
|
|
|
|
2003-01-24 10:56:52 -04:00
|
|
|
if os.name == 'posix':
|
|
|
|
for file in outfiles:
|
|
|
|
if self.dry_run:
|
|
|
|
log.info("changing mode of %s", file)
|
|
|
|
else:
|
2003-01-29 12:58:31 -04:00
|
|
|
oldmode = os.stat(file)[ST_MODE] & 07777
|
|
|
|
newmode = (oldmode | 0555) & 07777
|
|
|
|
if newmode != oldmode:
|
|
|
|
log.info("changing mode of %s from %o to %o",
|
|
|
|
file, oldmode, newmode)
|
|
|
|
os.chmod(file, newmode)
|
2003-01-24 10:56:52 -04:00
|
|
|
|
2000-05-24 23:03:56 -03:00
|
|
|
# copy_scripts ()
|
|
|
|
|
|
|
|
# class build_scripts
|