1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
|