Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
distutils.command.build_scripts.build_scripts Class Reference
Inheritance diagram for distutils.command.build_scripts.build_scripts:
Inheritance graph
[legend]
Collaboration diagram for distutils.command.build_scripts.build_scripts:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def initialize_options
def finalize_options
def get_source_files
def run
def copy_scripts

Public Attributes

 build_dir
 scripts
 force
 executable
 outfiles

Static Public Attributes

string description = "\"build\" scripts (copy and fixup #! line)"
list user_options
list boolean_options = ['force']

Detailed Description

Definition at line 17 of file build_scripts.py.


Member Function Documentation

Copy each script listed in 'self.scripts'; if it's marked as a
Python script in the Unix way (first line matches 'first_line_re',
ie. starts with "\#!" and contains "python"), then adjust the first
line to refer to the current Python interpreter as we copy.

Reimplemented in setup.PyBuildScripts, and distutils.command.build_scripts.build_scripts_2to3.

Definition at line 53 of file build_scripts.py.

00053 
00054     def copy_scripts(self):
00055         """Copy each script listed in 'self.scripts'; if it's marked as a
00056         Python script in the Unix way (first line matches 'first_line_re',
00057         ie. starts with "\#!" and contains "python"), then adjust the first
00058         line to refer to the current Python interpreter as we copy.
00059         """
00060         self.mkpath(self.build_dir)
00061         outfiles = []
00062         updated_files = []
00063         for script in self.scripts:
00064             adjust = False
00065             script = convert_path(script)
00066             outfile = os.path.join(self.build_dir, os.path.basename(script))
00067             outfiles.append(outfile)
00068 
00069             if not self.force and not newer(script, outfile):
00070                 log.debug("not copying %s (up-to-date)", script)
00071                 continue
00072 
00073             # Always open the file, but ignore failures in dry-run mode --
00074             # that way, we'll get accurate feedback if we can read the
00075             # script.
00076             try:
00077                 f = open(script, "rb")
00078             except IOError:
00079                 if not self.dry_run:
00080                     raise
00081                 f = None
00082             else:
00083                 encoding, lines = tokenize.detect_encoding(f.readline)
00084                 f.seek(0)
00085                 first_line = f.readline()
00086                 if not first_line:
00087                     self.warn("%s is an empty file (skipping)" % script)
00088                     continue
00089 
00090                 match = first_line_re.match(first_line)
00091                 if match:
00092                     adjust = True
00093                     post_interp = match.group(1) or b''
00094 
00095             if adjust:
00096                 log.info("copying and adjusting %s -> %s", script,
00097                          self.build_dir)
00098                 updated_files.append(outfile)
00099                 if not self.dry_run:
00100                     if not sysconfig.python_build:
00101                         executable = self.executable
00102                     else:
00103                         executable = os.path.join(
00104                             sysconfig.get_config_var("BINDIR"),
00105                            "python%s%s" % (sysconfig.get_config_var("VERSION"),
00106                                            sysconfig.get_config_var("EXE")))
00107                     executable = os.fsencode(executable)
00108                     shebang = b"#!" + executable + post_interp + b"\n"
00109                     # Python parser starts to read a script using UTF-8 until
00110                     # it gets a #coding:xxx cookie. The shebang has to be the
00111                     # first line of a file, the #coding:xxx cookie cannot be
00112                     # written before. So the shebang has to be decodable from
00113                     # UTF-8.
00114                     try:
00115                         shebang.decode('utf-8')
00116                     except UnicodeDecodeError:
00117                         raise ValueError(
00118                             "The shebang ({!r}) is not decodable "
00119                             "from utf-8".format(shebang))
00120                     # If the script is encoded to a custom encoding (use a
00121                     # #coding:xxx cookie), the shebang has to be decodable from
00122                     # the script encoding too.
00123                     try:
00124                         shebang.decode(encoding)
00125                     except UnicodeDecodeError:
00126                         raise ValueError(
00127                             "The shebang ({!r}) is not decodable "
00128                             "from the script encoding ({})"
00129                             .format(shebang, encoding))
00130                     outf = open(outfile, "wb")
00131                     outf.write(shebang)
00132                     outf.writelines(f.readlines())
00133                     outf.close()
00134                 if f:
00135                     f.close()
00136             else:
00137                 if f:
00138                     f.close()
00139                 updated_files.append(outfile)
00140                 self.copy_file(script, outfile)
00141 
00142         if os.name == 'posix':
00143             for file in outfiles:
00144                 if self.dry_run:
00145                     log.info("changing mode of %s", file)
00146                 else:
00147                     oldmode = os.stat(file)[ST_MODE] & 0o7777
00148                     newmode = (oldmode | 0o555) & 0o7777
00149                     if newmode != oldmode:
00150                         log.info("changing mode of %s from %o to %o",
00151                                  file, oldmode, newmode)
00152                         os.chmod(file, newmode)
00153         # XXX should we modify self.outfiles?
00154         return outfiles, updated_files

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 37 of file build_scripts.py.

00037 
00038     def finalize_options(self):
00039         self.set_undefined_options('build',
00040                                    ('build_scripts', 'build_dir'),
00041                                    ('force', 'force'),
00042                                    ('executable', 'executable'))
00043         self.scripts = self.distribution.scripts

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 44 of file build_scripts.py.

00044 
00045     def get_source_files(self):
00046         return self.scripts

Definition at line 30 of file build_scripts.py.

00030 
00031     def initialize_options(self):
00032         self.build_dir = None
00033         self.scripts = None
00034         self.force = None
00035         self.executable = None
00036         self.outfiles = None

Definition at line 47 of file build_scripts.py.

00047 
00048     def run(self):
00049         if not self.scripts:
00050             return
00051         self.copy_scripts()
00052 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 27 of file build_scripts.py.

Definition at line 31 of file build_scripts.py.

string distutils.command.build_scripts.build_scripts.description = "\"build\" scripts (copy and fixup #! line)" [static]

Definition at line 19 of file build_scripts.py.

Definition at line 34 of file build_scripts.py.

Definition at line 33 of file build_scripts.py.

Definition at line 35 of file build_scripts.py.

Definition at line 32 of file build_scripts.py.

Initial value:
[
        ('build-dir=', 'd', "directory to \"build\" (copy) to"),
        ('force', 'f', "forcibly build everything (ignore file timestamps"),
        ('executable=', 'e', "specify final destination interpreter path"),
        ]

Definition at line 21 of file build_scripts.py.


The documentation for this class was generated from the following file: