Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 1 | """distutils.command.build_scripts |
| 2 | |
| 3 | Implements the Distutils 'build_scripts' command.""" |
| 4 | |
| 5 | # created 2000/05/23, Bastian Kleineidam |
| 6 | |
| 7 | __revision__ = "$Id$" |
| 8 | |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 9 | import sys, os, re |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 10 | from distutils.core import Command |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 11 | from distutils.dep_util import newer |
Just van Rossum | 8d8e7a3 | 2001-07-29 21:39:18 +0000 | [diff] [blame^] | 12 | from distutils.util import convert_path |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 13 | |
Greg Ward | 7cf7e7e | 2001-07-25 20:20:11 +0000 | [diff] [blame] | 14 | # check if Python is called on the first line with this expression |
| 15 | first_line_re = re.compile(r'^#!.*python(\s+.*)?') |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 16 | |
| 17 | class build_scripts (Command): |
| 18 | |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 19 | description = "\"build\" scripts (copy and fixup #! line)" |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 20 | |
| 21 | user_options = [ |
| 22 | ('build-dir=', 'd', "directory to \"build\" (copy) to"), |
| 23 | ('force', 'f', "forcibly build everything (ignore file timestamps"), |
| 24 | ] |
| 25 | |
Greg Ward | 99b032e | 2000-09-25 01:41:15 +0000 | [diff] [blame] | 26 | boolean_options = ['force'] |
| 27 | |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 28 | |
| 29 | def initialize_options (self): |
| 30 | self.build_dir = None |
| 31 | self.scripts = None |
| 32 | self.force = None |
| 33 | self.outfiles = None |
| 34 | |
| 35 | def finalize_options (self): |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 36 | self.set_undefined_options('build', |
| 37 | ('build_scripts', 'build_dir'), |
| 38 | ('force', 'force')) |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 39 | self.scripts = self.distribution.scripts |
| 40 | |
| 41 | |
| 42 | def run (self): |
| 43 | if not self.scripts: |
| 44 | return |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 45 | self.copy_scripts() |
| 46 | |
Fred Drake | 53a7906 | 2001-03-02 07:28:03 +0000 | [diff] [blame] | 47 | |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 48 | def copy_scripts (self): |
| 49 | """Copy each script listed in 'self.scripts'; if it's marked as a |
| 50 | Python script in the Unix way (first line matches 'first_line_re', |
| 51 | ie. starts with "\#!" and contains "python"), then adjust the first |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 52 | line to refer to the current Python interpreter as we copy. |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 53 | """ |
| 54 | outfiles = [] |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 55 | self.mkpath(self.build_dir) |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 56 | for script in self.scripts: |
| 57 | adjust = 0 |
Just van Rossum | 8d8e7a3 | 2001-07-29 21:39:18 +0000 | [diff] [blame^] | 58 | script = convert_path(script) |
Greg Ward | fe55e86 | 2000-05-25 20:05:52 +0000 | [diff] [blame] | 59 | outfile = os.path.join(self.build_dir, os.path.basename(script)) |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 60 | |
| 61 | if not self.force and not newer(script, outfile): |
Fred Drake | 53a7906 | 2001-03-02 07:28:03 +0000 | [diff] [blame] | 62 | self.announce("not copying %s (up-to-date)" % script) |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 63 | continue |
| 64 | |
| 65 | # Always open the file, but ignore failures in dry-run mode -- |
| 66 | # that way, we'll get accurate feedback if we can read the |
| 67 | # script. |
| 68 | try: |
| 69 | f = open(script, "r") |
| 70 | except IOError: |
| 71 | if not self.dry_run: |
| 72 | raise |
| 73 | f = None |
| 74 | else: |
| 75 | first_line = f.readline() |
| 76 | if not first_line: |
| 77 | self.warn("%s is an empty file (skipping)" % script) |
Greg Ward | a4efe65 | 2000-05-25 01:20:15 +0000 | [diff] [blame] | 78 | continue |
Greg Ward | 80fa55e | 2000-05-25 02:03:56 +0000 | [diff] [blame] | 79 | |
| 80 | match = first_line_re.match(first_line) |
| 81 | if match: |
| 82 | adjust = 1 |
| 83 | post_interp = match.group(1) |
| 84 | |
| 85 | if adjust: |
| 86 | self.announce("copying and adjusting %s -> %s" % |
| 87 | (script, self.build_dir)) |
| 88 | if not self.dry_run: |
| 89 | outf = open(outfile, "w") |
| 90 | outf.write("#!%s%s\n" % |
| 91 | (os.path.normpath(sys.executable), post_interp)) |
| 92 | outf.writelines(f.readlines()) |
| 93 | outf.close() |
| 94 | if f: |
| 95 | f.close() |
| 96 | else: |
| 97 | f.close() |
| 98 | self.copy_file(script, outfile) |
| 99 | |
| 100 | # copy_scripts () |
| 101 | |
| 102 | # class build_scripts |