summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Ward <gward@python.net>2000-05-25 01:20:15 (GMT)
committerGreg Ward <gward@python.net>2000-05-25 01:20:15 (GMT)
commita4efe65b431664ea9104ce2457637429ebd87ebb (patch)
treee3bd439e168771c7828f532d1458ef88ffa0b699
parent8d5881a2ba7a7ed83f26ed55dbde2ef4753556da (diff)
downloadcpython-a4efe65b431664ea9104ce2457637429ebd87ebb.zip
cpython-a4efe65b431664ea9104ce2457637429ebd87ebb.tar.gz
cpython-a4efe65b431664ea9104ce2457637429ebd87ebb.tar.bz2
Bastian Kleineidam: the "build_scripts" command.
-rw-r--r--Lib/distutils/command/build_scripts.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/Lib/distutils/command/build_scripts.py b/Lib/distutils/command/build_scripts.py
new file mode 100644
index 0000000..1e7279d
--- /dev/null
+++ b/Lib/distutils/command/build_scripts.py
@@ -0,0 +1,71 @@
+"""distutils.command.build_scripts
+
+Implements the Distutils 'build_scripts' command."""
+
+# created 2000/05/23, Bastian Kleineidam
+
+__revision__ = "$Id$"
+
+import sys,os,re
+from distutils.core import Command
+
+# check if Python is called on the first line with this expression
+first_line_re = re.compile(r"^#!.+python(\s-\w+)*")
+
+class build_scripts (Command):
+
+ description = "\"build\" scripts"
+
+ user_options = [
+ ('build-dir=', 'd', "directory to \"build\" (copy) to"),
+ ('force', 'f', "forcibly build everything (ignore file timestamps"),
+ ]
+
+
+ def initialize_options (self):
+ self.build_dir = None
+ self.scripts = None
+ self.force = None
+ self.outfiles = None
+
+ def finalize_options (self):
+ self.set_undefined_options ('build',
+ ('build_scripts', 'build_dir'),
+ ('force', 'force'))
+ self.scripts = self.distribution.scripts
+
+
+ def run (self):
+ if not self.scripts:
+ return
+ self._copy_files()
+ self._adjust_files()
+
+ def _copy_files(self):
+ """Copy all the scripts to the build dir"""
+ self.outfiles = []
+ self.mkpath(self.build_dir)
+ for f in self.scripts:
+ print self.build_dir
+ if self.copy_file(f, self.build_dir):
+ self.outfiles.append(os.path.join(self.build_dir, f))
+
+ def _adjust_files(self):
+ """If the first line begins with #! and ends with python
+ replace it with the current python interpreter"""
+ for f in self.outfiles:
+ if not self.dry_run:
+ data = open(f, "r").readlines()
+ if not data:
+ self.warn("%s is an empty file!" % f)
+ continue
+ mo = first_line_re.match(data[0])
+ if mo:
+ self.announce("Adjusting first line of file %s" % f)
+ data[0] = "#!"+sys.executable
+ # add optional command line options
+ if mo.group(1):
+ data[0] = data[0] + mo.group(1)
+ else:
+ data[0] = data[0] + "\n"
+ open(f, "w").writelines(data)