+++ /dev/null
-"""distutils.command.build_scripts\r
-\r
-Implements the Distutils 'build_scripts' command."""\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os, re\r
-from stat import ST_MODE\r
-from distutils.core import Command\r
-from distutils.dep_util import newer\r
-from distutils.util import convert_path\r
-from distutils import log\r
-\r
-# check if Python is called on the first line with this expression\r
-first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')\r
-\r
-class build_scripts (Command):\r
-\r
- description = "\"build\" scripts (copy and fixup #! line)"\r
-\r
- user_options = [\r
- ('build-dir=', 'd', "directory to \"build\" (copy) to"),\r
- ('force', 'f', "forcibly build everything (ignore file timestamps"),\r
- ('executable=', 'e', "specify final destination interpreter path"),\r
- ]\r
-\r
- boolean_options = ['force']\r
-\r
-\r
- def initialize_options (self):\r
- self.build_dir = None\r
- self.scripts = None\r
- self.force = None\r
- self.executable = None\r
- self.outfiles = None\r
-\r
- def finalize_options (self):\r
- self.set_undefined_options('build',\r
- ('build_scripts', 'build_dir'),\r
- ('force', 'force'),\r
- ('executable', 'executable'))\r
- self.scripts = self.distribution.scripts\r
-\r
- def get_source_files(self):\r
- return self.scripts\r
-\r
- def run (self):\r
- if not self.scripts:\r
- return\r
- self.copy_scripts()\r
-\r
-\r
- def copy_scripts (self):\r
- """Copy each script listed in 'self.scripts'; if it's marked as a\r
- Python script in the Unix way (first line matches 'first_line_re',\r
- ie. starts with "\#!" and contains "python"), then adjust the first\r
- line to refer to the current Python interpreter as we copy.\r
- """\r
- _sysconfig = __import__('sysconfig')\r
- self.mkpath(self.build_dir)\r
- outfiles = []\r
- for script in self.scripts:\r
- adjust = 0\r
- script = convert_path(script)\r
- outfile = os.path.join(self.build_dir, os.path.basename(script))\r
- outfiles.append(outfile)\r
-\r
- if not self.force and not newer(script, outfile):\r
- log.debug("not copying %s (up-to-date)", script)\r
- continue\r
-\r
- # Always open the file, but ignore failures in dry-run mode --\r
- # that way, we'll get accurate feedback if we can read the\r
- # script.\r
- try:\r
- f = open(script, "r")\r
- except IOError:\r
- if not self.dry_run:\r
- raise\r
- f = None\r
- else:\r
- first_line = f.readline()\r
- if not first_line:\r
- self.warn("%s is an empty file (skipping)" % script)\r
- continue\r
-\r
- match = first_line_re.match(first_line)\r
- if match:\r
- adjust = 1\r
- post_interp = match.group(1) or ''\r
-\r
- if adjust:\r
- log.info("copying and adjusting %s -> %s", script,\r
- self.build_dir)\r
- if not self.dry_run:\r
- outf = open(outfile, "w")\r
- if not _sysconfig.is_python_build():\r
- outf.write("#!%s%s\n" %\r
- (self.executable,\r
- post_interp))\r
- else:\r
- outf.write("#!%s%s\n" %\r
- (os.path.join(\r
- _sysconfig.get_config_var("BINDIR"),\r
- "python%s%s" % (_sysconfig.get_config_var("VERSION"),\r
- _sysconfig.get_config_var("EXE"))),\r
- post_interp))\r
- outf.writelines(f.readlines())\r
- outf.close()\r
- if f:\r
- f.close()\r
- else:\r
- if f:\r
- f.close()\r
- self.copy_file(script, outfile)\r
-\r
- if os.name == 'posix':\r
- for file in outfiles:\r
- if self.dry_run:\r
- log.info("changing mode of %s", file)\r
- else:\r
- oldmode = os.stat(file)[ST_MODE] & 07777\r
- newmode = (oldmode | 0555) & 07777\r
- if newmode != oldmode:\r
- log.info("changing mode of %s from %o to %o",\r
- file, oldmode, newmode)\r
- os.chmod(file, newmode)\r
-\r
- # copy_scripts ()\r
-\r
-# class build_scripts\r