mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Improvements to Bastian's build_scripts command:
* 'first_line_re' loosened up * command description improved * replaced '_copy_files()' and '_adjust_files()' with one method that does everything, 'copy_scripts()' -- this should be more efficient than Bastian's version, should behave better in dry-run mode, and does timestamp dependency-checking
This commit is contained in:
parent
029e302786
commit
80fa55e71f
1 changed files with 58 additions and 31 deletions
|
@ -6,15 +6,16 @@ Implements the Distutils 'build_scripts' command."""
|
||||||
|
|
||||||
__revision__ = "$Id$"
|
__revision__ = "$Id$"
|
||||||
|
|
||||||
import sys,os,re
|
import sys, os, re
|
||||||
from distutils.core import Command
|
from distutils.core import Command
|
||||||
|
from distutils.dep_util import newer
|
||||||
|
|
||||||
# check if Python is called on the first line with this expression
|
# check if Python is called on the first line with this expression
|
||||||
first_line_re = re.compile(r"^#!.+python(\s-\w+)*")
|
first_line_re = re.compile(r'^#!.*python(\s+.*)?')
|
||||||
|
|
||||||
class build_scripts (Command):
|
class build_scripts (Command):
|
||||||
|
|
||||||
description = "\"build\" scripts"
|
description = "\"build\" scripts (copy and fixup #! line)"
|
||||||
|
|
||||||
user_options = [
|
user_options = [
|
||||||
('build-dir=', 'd', "directory to \"build\" (copy) to"),
|
('build-dir=', 'd', "directory to \"build\" (copy) to"),
|
||||||
|
@ -38,34 +39,60 @@ class build_scripts (Command):
|
||||||
def run (self):
|
def run (self):
|
||||||
if not self.scripts:
|
if not self.scripts:
|
||||||
return
|
return
|
||||||
self._copy_files()
|
self.copy_scripts()
|
||||||
self._adjust_files()
|
|
||||||
|
|
||||||
def _copy_files(self):
|
def copy_scripts (self):
|
||||||
"""Copy all the scripts to the build dir"""
|
"""Copy each script listed in 'self.scripts'; if it's marked as a
|
||||||
self.outfiles = []
|
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 intepreter as we copy.
|
||||||
|
"""
|
||||||
|
outfiles = []
|
||||||
self.mkpath(self.build_dir)
|
self.mkpath(self.build_dir)
|
||||||
for f in self.scripts:
|
for script in self.scripts:
|
||||||
print self.build_dir
|
adjust = 0
|
||||||
if self.copy_file(f, self.build_dir):
|
outfile = os.path.join(self.build_dir, script)
|
||||||
self.outfiles.append(os.path.join(self.build_dir, f))
|
|
||||||
|
if not self.force and not newer(script, outfile):
|
||||||
def _adjust_files(self):
|
self.announce("not copying %s (output up-to-date)" % script)
|
||||||
"""If the first line begins with #! and ends with python
|
continue
|
||||||
replace it with the current python interpreter"""
|
|
||||||
for f in self.outfiles:
|
# Always open the file, but ignore failures in dry-run mode --
|
||||||
if not self.dry_run:
|
# that way, we'll get accurate feedback if we can read the
|
||||||
data = open(f, "r").readlines()
|
# script.
|
||||||
if not data:
|
try:
|
||||||
self.warn("%s is an empty file!" % f)
|
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)
|
||||||
continue
|
continue
|
||||||
mo = first_line_re.match(data[0])
|
|
||||||
if mo:
|
match = first_line_re.match(first_line)
|
||||||
self.announce("Adjusting first line of file %s" % f)
|
if match:
|
||||||
data[0] = "#!"+sys.executable
|
adjust = 1
|
||||||
# add optional command line options
|
post_interp = match.group(1)
|
||||||
if mo.group(1):
|
|
||||||
data[0] = data[0] + mo.group(1)
|
if adjust:
|
||||||
else:
|
self.announce("copying and adjusting %s -> %s" %
|
||||||
data[0] = data[0] + "\n"
|
(script, self.build_dir))
|
||||||
open(f, "w").writelines(data)
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue