| 1 | n/a | """distutils.command.build_scripts |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Implements the Distutils 'build_scripts' command.""" |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import os, re |
|---|
| 6 | n/a | from stat import ST_MODE |
|---|
| 7 | n/a | from distutils import sysconfig |
|---|
| 8 | n/a | from distutils.core import Command |
|---|
| 9 | n/a | from distutils.dep_util import newer |
|---|
| 10 | n/a | from distutils.util import convert_path, Mixin2to3 |
|---|
| 11 | n/a | from distutils import log |
|---|
| 12 | n/a | import tokenize |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # check if Python is called on the first line with this expression |
|---|
| 15 | n/a | first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$') |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | class build_scripts(Command): |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | description = "\"build\" scripts (copy and fixup #! line)" |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | user_options = [ |
|---|
| 22 | n/a | ('build-dir=', 'd', "directory to \"build\" (copy) to"), |
|---|
| 23 | n/a | ('force', 'f', "forcibly build everything (ignore file timestamps"), |
|---|
| 24 | n/a | ('executable=', 'e', "specify final destination interpreter path"), |
|---|
| 25 | n/a | ] |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | boolean_options = ['force'] |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def initialize_options(self): |
|---|
| 31 | n/a | self.build_dir = None |
|---|
| 32 | n/a | self.scripts = None |
|---|
| 33 | n/a | self.force = None |
|---|
| 34 | n/a | self.executable = None |
|---|
| 35 | n/a | self.outfiles = None |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def finalize_options(self): |
|---|
| 38 | n/a | self.set_undefined_options('build', |
|---|
| 39 | n/a | ('build_scripts', 'build_dir'), |
|---|
| 40 | n/a | ('force', 'force'), |
|---|
| 41 | n/a | ('executable', 'executable')) |
|---|
| 42 | n/a | self.scripts = self.distribution.scripts |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def get_source_files(self): |
|---|
| 45 | n/a | return self.scripts |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def run(self): |
|---|
| 48 | n/a | if not self.scripts: |
|---|
| 49 | n/a | return |
|---|
| 50 | n/a | self.copy_scripts() |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | def copy_scripts(self): |
|---|
| 54 | n/a | r"""Copy each script listed in 'self.scripts'; if it's marked as a |
|---|
| 55 | n/a | Python script in the Unix way (first line matches 'first_line_re', |
|---|
| 56 | n/a | ie. starts with "\#!" and contains "python"), then adjust the first |
|---|
| 57 | n/a | line to refer to the current Python interpreter as we copy. |
|---|
| 58 | n/a | """ |
|---|
| 59 | n/a | self.mkpath(self.build_dir) |
|---|
| 60 | n/a | outfiles = [] |
|---|
| 61 | n/a | updated_files = [] |
|---|
| 62 | n/a | for script in self.scripts: |
|---|
| 63 | n/a | adjust = False |
|---|
| 64 | n/a | script = convert_path(script) |
|---|
| 65 | n/a | outfile = os.path.join(self.build_dir, os.path.basename(script)) |
|---|
| 66 | n/a | outfiles.append(outfile) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | if not self.force and not newer(script, outfile): |
|---|
| 69 | n/a | log.debug("not copying %s (up-to-date)", script) |
|---|
| 70 | n/a | continue |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | # Always open the file, but ignore failures in dry-run mode -- |
|---|
| 73 | n/a | # that way, we'll get accurate feedback if we can read the |
|---|
| 74 | n/a | # script. |
|---|
| 75 | n/a | try: |
|---|
| 76 | n/a | f = open(script, "rb") |
|---|
| 77 | n/a | except OSError: |
|---|
| 78 | n/a | if not self.dry_run: |
|---|
| 79 | n/a | raise |
|---|
| 80 | n/a | f = None |
|---|
| 81 | n/a | else: |
|---|
| 82 | n/a | encoding, lines = tokenize.detect_encoding(f.readline) |
|---|
| 83 | n/a | f.seek(0) |
|---|
| 84 | n/a | first_line = f.readline() |
|---|
| 85 | n/a | if not first_line: |
|---|
| 86 | n/a | self.warn("%s is an empty file (skipping)" % script) |
|---|
| 87 | n/a | continue |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | match = first_line_re.match(first_line) |
|---|
| 90 | n/a | if match: |
|---|
| 91 | n/a | adjust = True |
|---|
| 92 | n/a | post_interp = match.group(1) or b'' |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | if adjust: |
|---|
| 95 | n/a | log.info("copying and adjusting %s -> %s", script, |
|---|
| 96 | n/a | self.build_dir) |
|---|
| 97 | n/a | updated_files.append(outfile) |
|---|
| 98 | n/a | if not self.dry_run: |
|---|
| 99 | n/a | if not sysconfig.python_build: |
|---|
| 100 | n/a | executable = self.executable |
|---|
| 101 | n/a | else: |
|---|
| 102 | n/a | executable = os.path.join( |
|---|
| 103 | n/a | sysconfig.get_config_var("BINDIR"), |
|---|
| 104 | n/a | "python%s%s" % (sysconfig.get_config_var("VERSION"), |
|---|
| 105 | n/a | sysconfig.get_config_var("EXE"))) |
|---|
| 106 | n/a | executable = os.fsencode(executable) |
|---|
| 107 | n/a | shebang = b"#!" + executable + post_interp + b"\n" |
|---|
| 108 | n/a | # Python parser starts to read a script using UTF-8 until |
|---|
| 109 | n/a | # it gets a #coding:xxx cookie. The shebang has to be the |
|---|
| 110 | n/a | # first line of a file, the #coding:xxx cookie cannot be |
|---|
| 111 | n/a | # written before. So the shebang has to be decodable from |
|---|
| 112 | n/a | # UTF-8. |
|---|
| 113 | n/a | try: |
|---|
| 114 | n/a | shebang.decode('utf-8') |
|---|
| 115 | n/a | except UnicodeDecodeError: |
|---|
| 116 | n/a | raise ValueError( |
|---|
| 117 | n/a | "The shebang ({!r}) is not decodable " |
|---|
| 118 | n/a | "from utf-8".format(shebang)) |
|---|
| 119 | n/a | # If the script is encoded to a custom encoding (use a |
|---|
| 120 | n/a | # #coding:xxx cookie), the shebang has to be decodable from |
|---|
| 121 | n/a | # the script encoding too. |
|---|
| 122 | n/a | try: |
|---|
| 123 | n/a | shebang.decode(encoding) |
|---|
| 124 | n/a | except UnicodeDecodeError: |
|---|
| 125 | n/a | raise ValueError( |
|---|
| 126 | n/a | "The shebang ({!r}) is not decodable " |
|---|
| 127 | n/a | "from the script encoding ({})" |
|---|
| 128 | n/a | .format(shebang, encoding)) |
|---|
| 129 | n/a | with open(outfile, "wb") as outf: |
|---|
| 130 | n/a | outf.write(shebang) |
|---|
| 131 | n/a | outf.writelines(f.readlines()) |
|---|
| 132 | n/a | if f: |
|---|
| 133 | n/a | f.close() |
|---|
| 134 | n/a | else: |
|---|
| 135 | n/a | if f: |
|---|
| 136 | n/a | f.close() |
|---|
| 137 | n/a | updated_files.append(outfile) |
|---|
| 138 | n/a | self.copy_file(script, outfile) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | if os.name == 'posix': |
|---|
| 141 | n/a | for file in outfiles: |
|---|
| 142 | n/a | if self.dry_run: |
|---|
| 143 | n/a | log.info("changing mode of %s", file) |
|---|
| 144 | n/a | else: |
|---|
| 145 | n/a | oldmode = os.stat(file)[ST_MODE] & 0o7777 |
|---|
| 146 | n/a | newmode = (oldmode | 0o555) & 0o7777 |
|---|
| 147 | n/a | if newmode != oldmode: |
|---|
| 148 | n/a | log.info("changing mode of %s from %o to %o", |
|---|
| 149 | n/a | file, oldmode, newmode) |
|---|
| 150 | n/a | os.chmod(file, newmode) |
|---|
| 151 | n/a | # XXX should we modify self.outfiles? |
|---|
| 152 | n/a | return outfiles, updated_files |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | class build_scripts_2to3(build_scripts, Mixin2to3): |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def copy_scripts(self): |
|---|
| 157 | n/a | outfiles, updated_files = build_scripts.copy_scripts(self) |
|---|
| 158 | n/a | if not self.dry_run: |
|---|
| 159 | n/a | self.run_2to3(updated_files) |
|---|
| 160 | n/a | return outfiles, updated_files |
|---|