1 """distutils.command.build_scripts
3 Implements the Distutils 'build_scripts' command."""
8 from stat
import ST_MODE
9 from distutils
import sysconfig
10 from distutils
.core
import Command
11 from distutils
.dep_util
import newer
12 from distutils
.util
import convert_path
13 from distutils
import log
15 # check if Python is called on the first line with this expression
16 first_line_re
= re
.compile('^#!.*python[0-9.]*([ \t].*)?$')
18 class build_scripts (Command
):
20 description
= "\"build\" scripts (copy and fixup #! line)"
23 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
24 ('force', 'f', "forcibly build everything (ignore file timestamps"),
25 ('executable=', 'e', "specify final destination interpreter path"),
28 boolean_options
= ['force']
31 def initialize_options (self
):
35 self
.executable
= None
38 def finalize_options (self
):
39 self
.set_undefined_options('build',
40 ('build_scripts', 'build_dir'),
42 ('executable', 'executable'))
43 self
.scripts
= self
.distribution
.scripts
45 def get_source_files(self
):
54 def copy_scripts (self
):
55 """Copy each script listed in 'self.scripts'; if it's marked as a
56 Python script in the Unix way (first line matches 'first_line_re',
57 ie. starts with "\#!" and contains "python"), then adjust the first
58 line to refer to the current Python interpreter as we copy.
60 self
.mkpath(self
.build_dir
)
62 for script
in self
.scripts
:
64 script
= convert_path(script
)
65 outfile
= os
.path
.join(self
.build_dir
, os
.path
.basename(script
))
66 outfiles
.append(outfile
)
68 if not self
.force
and not newer(script
, outfile
):
69 log
.debug("not copying %s (up-to-date)", script
)
72 # Always open the file, but ignore failures in dry-run mode --
73 # that way, we'll get accurate feedback if we can read the
82 first_line
= f
.readline()
84 self
.warn("%s is an empty file (skipping)" % script
)
87 match
= first_line_re
.match(first_line
)
90 post_interp
= match
.group(1) or ''
93 log
.info("copying and adjusting %s -> %s", script
,
96 outf
= open(outfile
, "w")
97 if not sysconfig
.python_build
:
98 outf
.write("#!%s%s\n" %
102 outf
.write("#!%s%s\n" %
104 sysconfig
.get_config_var("BINDIR"),
105 "python%s%s" % (sysconfig
.get_config_var("VERSION"),
106 sysconfig
.get_config_var("EXE"))),
108 outf
.writelines(f
.readlines())
115 self
.copy_file(script
, outfile
)
117 if os
.name
== 'posix':
118 for file in outfiles
:
120 log
.info("changing mode of %s", file)
122 oldmode
= os
.stat(file)[ST_MODE
] & 07777
123 newmode
= (oldmode |
0555) & 07777
124 if newmode
!= oldmode
:
125 log
.info("changing mode of %s from %o to %o",
126 file, oldmode
, newmode
)
127 os
.chmod(file, newmode
)
131 # class build_scripts