Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/command/build_scripts.py @ 15842:f6cd36498a50 legacy-trunk
Document extensions to .pth files.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 13 Jan 2001 09:54:41 +0000 |
| parents | 7acb8bbc215b |
| children | 2d2ee30f43ea |
| rev | line source |
|---|---|
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
1 """distutils.command.build_scripts |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
2 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
3 Implements the Distutils 'build_scripts' command.""" |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
4 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
5 # created 2000/05/23, Bastian Kleineidam |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
6 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
7 __revision__ = "$Id$" |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
9 import sys, os, re |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
10 from distutils.core import Command |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
11 from distutils.dep_util import newer |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
12 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
13 # check if Python is called on the first line with this expression |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
14 first_line_re = re.compile(r'^#!.*python(\s+.*)?') |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
15 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
16 class build_scripts (Command): |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
17 |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
18 description = "\"build\" scripts (copy and fixup #! line)" |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
19 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
20 user_options = [ |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
21 ('build-dir=', 'd', "directory to \"build\" (copy) to"), |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
22 ('force', 'f', "forcibly build everything (ignore file timestamps"), |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
23 ] |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
24 |
|
14829
b1f220f636ed
Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents:
13725
diff
changeset
|
25 boolean_options = ['force'] |
|
b1f220f636ed
Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents:
13725
diff
changeset
|
26 |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
27 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
28 def initialize_options (self): |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
29 self.build_dir = None |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
30 self.scripts = None |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
31 self.force = None |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
32 self.outfiles = None |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
33 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
34 def finalize_options (self): |
|
14948
7acb8bbc215b
Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents:
14829
diff
changeset
|
35 self.set_undefined_options('build', |
|
7acb8bbc215b
Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents:
14829
diff
changeset
|
36 ('build_scripts', 'build_dir'), |
|
7acb8bbc215b
Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents:
14829
diff
changeset
|
37 ('force', 'force')) |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
38 self.scripts = self.distribution.scripts |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
39 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
40 |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
41 def run (self): |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
42 if not self.scripts: |
|
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
43 return |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
44 self.copy_scripts() |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
45 |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
46 |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
47 def copy_scripts (self): |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
48 """Copy each script listed in 'self.scripts'; if it's marked as a |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
49 Python script in the Unix way (first line matches 'first_line_re', |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
50 ie. starts with "\#!" and contains "python"), then adjust the first |
|
13725
449dd3a00bd7
Fixed a grab-bag of typos spotted by Rob Hooft.
Greg Ward <gward@python.net>
parents:
12752
diff
changeset
|
51 line to refer to the current Python interpreter as we copy. |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
52 """ |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
53 outfiles = [] |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
54 self.mkpath(self.build_dir) |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
55 for script in self.scripts: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
56 adjust = 0 |
|
12752
7b49334a6369
Take the basename of the script before concatenating it with the build dir.
Greg Ward <gward@python.net>
parents:
12748
diff
changeset
|
57 outfile = os.path.join(self.build_dir, os.path.basename(script)) |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
58 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
59 if not self.force and not newer(script, outfile): |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
60 self.announce("not copying %s (output up-to-date)" % script) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
61 continue |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
62 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
63 # Always open the file, but ignore failures in dry-run mode -- |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
64 # that way, we'll get accurate feedback if we can read the |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
65 # script. |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
66 try: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
67 f = open(script, "r") |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
68 except IOError: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
69 if not self.dry_run: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
70 raise |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
71 f = None |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
72 else: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
73 first_line = f.readline() |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
74 if not first_line: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
75 self.warn("%s is an empty file (skipping)" % script) |
|
12745
dad205d13bb5
Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
76 continue |
|
12748
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
77 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
78 match = first_line_re.match(first_line) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
79 if match: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
80 adjust = 1 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
81 post_interp = match.group(1) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
82 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
83 if adjust: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
84 self.announce("copying and adjusting %s -> %s" % |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
85 (script, self.build_dir)) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
86 if not self.dry_run: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
87 outf = open(outfile, "w") |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
88 outf.write("#!%s%s\n" % |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
89 (os.path.normpath(sys.executable), post_interp)) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
90 outf.writelines(f.readlines()) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
91 outf.close() |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
92 if f: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
93 f.close() |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
94 else: |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
95 f.close() |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
96 self.copy_file(script, outfile) |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
97 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
98 # copy_scripts () |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
99 |
|
eb562df0cebe
Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents:
12745
diff
changeset
|
100 # class build_scripts |
