| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 |
| 32 """SCons tool for 7zip.""" |
| 33 |
| 34 |
| 35 import os |
| 36 import shutil |
| 37 import subprocess |
| 38 import tempfile |
| 39 import SCons.Script |
| 40 |
| 41 |
| 42 def SevenZipGetFiles(env, source): |
| 43 """SCons emitter for 7zip extract. |
| 44 |
| 45 Examines the source 7z archive to determine the list of files which will be |
| 46 created by extract/unzip operation. |
| 47 Args: |
| 48 env: The SCons environment to get the 7zip command line from. |
| 49 source: The 7zip archive to examine. |
| 50 Returns: |
| 51 The list of filenames in the archive. |
| 52 """ |
| 53 # Expand the command to list archive contents. |
| 54 cmd = env.subst('$SEVEN_ZIP l "%s"' % source) |
| 55 # Run it and capture output. |
| 56 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 57 # Strip off 7-line header and 3-line trailer from 7zip output. |
| 58 lines = output.split('\r\n')[7:-3] |
| 59 # Trim out just the files and their names. |
| 60 files = [i[53:] for i in lines if i[20] != 'D'] |
| 61 return files |
| 62 |
| 63 |
| 64 def SevenZipEmitter(target, source, env): |
| 65 """An emitter that decides what nodes are vented from a 7zip archive. |
| 66 |
| 67 Args: |
| 68 target: The target directory node. |
| 69 source: The source archive node. |
| 70 env: The environment in which the emit takes place. |
| 71 Returns: |
| 72 The pair (target, source) which lists the emitted targets and sources. |
| 73 """ |
| 74 # Remember out dir for later. |
| 75 env['SEVEN_ZIP_OUT_DIR'] = target[0].dir |
| 76 # Get out contents |
| 77 files = SevenZipGetFiles(env, env.subst('$SOURCE', source=source)) |
| 78 # Extract a layer deeper if there is only one, and it extension is 7z. |
| 79 if env.get('SEVEN_ZIP_PEEL_LAYERS', False): |
| 80 assert len(files) == 1 and os.path.splitext(files[0])[1] == '.7z' |
| 81 # Create a temporary directory. |
| 82 tmp_dir = tempfile.mkdtemp() |
| 83 # Expand the command to extract the archive to a temporary location. |
| 84 cmd = env.subst('$SEVEN_ZIP x $SOURCE -o"%s"' % tmp_dir, source=source[0]) |
| 85 # Run it and swallow output. |
| 86 subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate() |
| 87 # Get contents. |
| 88 inner_files = SevenZipGetFiles(env, os.path.join(tmp_dir, files[0])) |
| 89 # Make into file nodes. |
| 90 inner_files = [target[0].dir.File(i) for i in inner_files] |
| 91 # Delete temp_dir. |
| 92 shutil.rmtree(tmp_dir) |
| 93 # Decide where to extra working file to. |
| 94 working_file = env.Dir(target[0].dir.abspath + |
| 95 '.7zip_extract').File(files[0]) |
| 96 # Combine everything. |
| 97 files = [working_file] + inner_files |
| 98 else: |
| 99 # Make into file nodes. |
| 100 files = [target[0].dir.File(i) for i in files] |
| 101 # Return files as actual target. |
| 102 return (files, source) |
| 103 |
| 104 |
| 105 def SevenZipGenerator(source, target, env, for_signature): |
| 106 """The generator function which decides how to extract a file.""" |
| 107 |
| 108 # Silence lint. |
| 109 source = source |
| 110 target = target |
| 111 for_signature = for_signature |
| 112 |
| 113 if env.get('SEVEN_ZIP_PEEL_LAYERS', False): |
| 114 return [SCons.Script.Delete('$SEVEN_ZIP_OUT_DIR'), |
| 115 '$SEVEN_ZIP x $SOURCE -o"${TARGET.dir}"', |
| 116 '$SEVEN_ZIP x $TARGET -o"$SEVEN_ZIP_OUT_DIR"'] |
| 117 else: |
| 118 return [SCons.Script.Delete('$SEVEN_ZIP_OUT_DIR'), |
| 119 '$SEVEN_ZIP x $SOURCE -o"$SEVEN_ZIP_OUT_DIR"'] |
| 120 |
| 121 |
| 122 def generate(env): |
| 123 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 124 """SCons entry point for this tool.""" |
| 125 |
| 126 env.Replace( |
| 127 SEVEN_ZIP='$SEVEN_ZIP_DIR/7za.exe', |
| 128 SEVEN_ZIP_ARCHIVE_OPTIONS = ['-t7z', '-mx0'], |
| 129 SEVEN_ZIP_COMPRESS_OPTIONS = ['-t7z', '-mx9'], |
| 130 ) |
| 131 |
| 132 b = SCons.Script.Builder(generator=SevenZipGenerator, |
| 133 emitter=SevenZipEmitter) |
| 134 env['BUILDERS']['Extract7zip'] = b |
| 135 |
| 136 b = SCons.Script.Builder( |
| 137 action=('cd $SOURCE && ' |
| 138 '$SEVEN_ZIP a $SEVEN_ZIP_ARCHIVE_OPTIONS ${TARGET.abspath} ./')) |
| 139 env['BUILDERS']['Archive7zip'] = b |
| 140 |
| 141 b = SCons.Script.Builder( |
| 142 action=('cd ${SOURCE.dir} && ' |
| 143 '$SEVEN_ZIP a $SEVEN_ZIP_COMPRESS_OPTIONS ' |
| 144 '${TARGET.abspath} ${SOURCE.file}')) |
| 145 env['BUILDERS']['Compress7zip'] = b |
| OLD | NEW |