Skip to content

Commit 80fa55e

Browse files
committed
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
1 parent 029e302 commit 80fa55e

1 file changed

Lines changed: 58 additions & 31 deletions

File tree

‎Lib/distutils/command/build_scripts.py‎

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
__revision__ = "$Id$"
88

9-
import sys,os,re
9+
import sys, os, re
1010
from distutils.core import Command
11+
from distutils.dep_util import newer
1112

1213
# check if Python is called on the first line with this expression
13-
first_line_re = re.compile(r"^#!.+python(\s-\w+)*")
14+
first_line_re = re.compile(r'^#!.*python(\s+.*)?')
1415

1516
class build_scripts (Command):
1617

17-
description = "\"build\" scripts"
18+
description = "\"build\" scripts (copy and fixup #! line)"
1819

1920
user_options = [
2021
('build-dir=', 'd', "directory to \"build\" (copy) to"),
@@ -38,34 +39,60 @@ def finalize_options (self):
3839
def run (self):
3940
if not self.scripts:
4041
return
41-
self._copy_files()
42-
self._adjust_files()
42+
self.copy_scripts()
43+
4344

44-
def _copy_files(self):
45-
"""Copy all the scripts to the build dir"""
46-
self.outfiles = []
45+
def copy_scripts (self):
46+
"""Copy each script listed in 'self.scripts'; if it's marked as a
47+
Python script in the Unix way (first line matches 'first_line_re',
48+
ie. starts with "\#!" and contains "python"), then adjust the first
49+
line to refer to the current Python intepreter as we copy.
50+
"""
51+
outfiles = []
4752
self.mkpath(self.build_dir)
48-
for f in self.scripts:
49-
print self.build_dir
50-
if self.copy_file(f, self.build_dir):
51-
self.outfiles.append(os.path.join(self.build_dir, f))
52-
53-
def _adjust_files(self):
54-
"""If the first line begins with #! and ends with python
55-
replace it with the current python interpreter"""
56-
for f in self.outfiles:
57-
if not self.dry_run:
58-
data = open(f, "r").readlines()
59-
if not data:
60-
self.warn("%s is an empty file!" % f)
53+
for script in self.scripts:
54+
adjust = 0
55+
outfile = os.path.join(self.build_dir, script)
56+
57+
if not self.force and not newer(script, outfile):
58+
self.announce("not copying %s (output up-to-date)" % script)
59+
continue
60+
61+
# Always open the file, but ignore failures in dry-run mode --
62+
# that way, we'll get accurate feedback if we can read the
63+
# script.
64+
try:
65+
f = open(script, "r")
66+
except IOError:
67+
if not self.dry_run:
68+
raise
69+
f = None
70+
else:
71+
first_line = f.readline()
72+
if not first_line:
73+
self.warn("%s is an empty file (skipping)" % script)
6174
continue
62-
mo = first_line_re.match(data[0])
63-
if mo:
64-
self.announce("Adjusting first line of file %s" % f)
65-
data[0] = "#!"+sys.executable
66-
# add optional command line options
67-
if mo.group(1):
68-
data[0] = data[0] + mo.group(1)
69-
else:
70-
data[0] = data[0] + "\n"
71-
open(f, "w").writelines(data)
75+
76+
match = first_line_re.match(first_line)
77+
if match:
78+
adjust = 1
79+
post_interp = match.group(1)
80+
81+
if adjust:
82+
self.announce("copying and adjusting %s -> %s" %
83+
(script, self.build_dir))
84+
if not self.dry_run:
85+
outf = open(outfile, "w")
86+
outf.write("#!%s%s\n" %
87+
(os.path.normpath(sys.executable), post_interp))
88+
outf.writelines(f.readlines())
89+
outf.close()
90+
if f:
91+
f.close()
92+
else:
93+
f.close()
94+
self.copy_file(script, outfile)
95+
96+
# copy_scripts ()
97+
98+
# class build_scripts

0 commit comments

Comments
 (0)