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