2000-05-24 22:20:15 -03:00
|
|
|
"""distutils.command.build_scripts
|
|
|
|
|
|
|
|
Implements the Distutils 'build_scripts' command."""
|
|
|
|
|
|
|
|
# created 2000/05/23, Bastian Kleineidam
|
|
|
|
|
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2000-05-24 23:03:56 -03:00
|
|
|
import sys, os, re
|
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
|
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
|
|
|
|
first_line_re = re.compile(r'^#!.*python(\s+.*)?')
|
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"),
|
|
|
|
]
|
|
|
|
|
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
|
|
|
|
self.outfiles = None
|
|
|
|
|
|
|
|
def finalize_options (self):
|
2000-09-30 15:27:54 -03:00
|
|
|
self.set_undefined_options('build',
|
|
|
|
('build_scripts', 'build_dir'),
|
|
|
|
('force', 'force'))
|
2000-05-24 22:20:15 -03:00
|
|
|
self.scripts = self.distribution.scripts
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
2000-05-24 22:20:15 -03:00
|
|
|
self.mkpath(self.build_dir)
|
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))
|
2000-05-24 23:03:56 -03:00
|
|
|
|
|
|
|
if not self.force and not newer(script, outfile):
|
2001-03-02 03:28:03 -04:00
|
|
|
self.announce("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
|
|
|
|
post_interp = match.group(1)
|
|
|
|
|
|
|
|
if adjust:
|
|
|
|
self.announce("copying and adjusting %s -> %s" %
|
|
|
|
(script, self.build_dir))
|
|
|
|
if not self.dry_run:
|
|
|
|
outf = open(outfile, "w")
|
|
|
|
outf.write("#!%s%s\n" %
|
|
|
|
(os.path.normpath(sys.executable), post_interp))
|
|
|
|
outf.writelines(f.readlines())
|
|
|
|
outf.close()
|
|
|
|
if f:
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
self.copy_file(script, outfile)
|
|
|
|
|
|
|
|
# copy_scripts ()
|
|
|
|
|
|
|
|
# class build_scripts
|