|
| 1 | +"""SCons.Tool.zip |
| 2 | +
|
| 3 | +Tool-specific initialization for zip. |
| 4 | +
|
| 5 | +There normally shouldn't be any need to import this module directly. |
| 6 | +It will usually be imported through the generic SCons.Tool.Tool() |
| 7 | +selection method. |
| 8 | +
|
| 9 | +This version applies the patch from scons.tigris.org/issues/show_bug.cgi?id=2575 |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +# |
| 14 | +# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation |
| 15 | +# |
| 16 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 17 | +# a copy of this software and associated documentation files (the |
| 18 | +# "Software"), to deal in the Software without restriction, including |
| 19 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 20 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 21 | +# permit persons to whom the Software is furnished to do so, subject to |
| 22 | +# the following conditions: |
| 23 | +# |
| 24 | +# The above copyright notice and this permission notice shall be included |
| 25 | +# in all copies or substantial portions of the Software. |
| 26 | +# |
| 27 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 28 | +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 29 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 31 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 32 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 | +# |
| 35 | + |
| 36 | +__revision__ = "src/engine/SCons/Tool/zip.py 5134 2010/08/16 23:02:40 bdeegan" |
| 37 | + |
| 38 | +import os.path |
| 39 | + |
| 40 | +import SCons.Builder |
| 41 | +import SCons.Defaults |
| 42 | +import SCons.Node.FS |
| 43 | +import SCons.Util |
| 44 | + |
| 45 | +try: |
| 46 | + import zipfile |
| 47 | + internal_zip = 1 |
| 48 | +except ImportError: |
| 49 | + internal_zip = 0 |
| 50 | + |
| 51 | +if internal_zip: |
| 52 | + zipcompression = zipfile.ZIP_DEFLATED |
| 53 | + def zip(target, source, env): |
| 54 | + compression = env.get('ZIPCOMPRESSION', 0) |
| 55 | + zf = zipfile.ZipFile(target[0].abspath, 'w', compression) |
| 56 | + for s in source: |
| 57 | + if s.isdir(): |
| 58 | + for dirpath, dirnames, filenames in os.walk(os.path.relpath(s.abspath)): |
| 59 | + for fname in filenames: |
| 60 | + path = os.path.join(dirpath, fname) |
| 61 | + if os.path.isfile(path): |
| 62 | + zf.write(path) |
| 63 | + else: |
| 64 | + zf.write(os.path.relpath(s.abspath)) |
| 65 | + zf.close() |
| 66 | +else: |
| 67 | + zipcompression = 0 |
| 68 | + zip = "$ZIP $ZIPFLAGS ${TARGET.abspath} $SOURCES" |
| 69 | + |
| 70 | + |
| 71 | +zipAction = SCons.Action.Action(zip, varlist=['ZIPCOMPRESSION']) |
| 72 | + |
| 73 | +ZipBuilder = SCons.Builder.Builder(action = SCons.Action.Action('$ZIPCOM', '$ZIPCOMSTR'), |
| 74 | + source_factory = SCons.Node.FS.Entry, |
| 75 | + source_scanner = SCons.Defaults.DirScanner, |
| 76 | + suffix = '$ZIPSUFFIX', |
| 77 | + multi = 1) |
| 78 | + |
| 79 | + |
| 80 | +def generate(env): |
| 81 | + """Add Builders and construction variables for zip to an Environment.""" |
| 82 | + try: |
| 83 | + bld = env['BUILDERS']['Zip'] |
| 84 | + except KeyError: |
| 85 | + bld = ZipBuilder |
| 86 | + env['BUILDERS']['Zip'] = bld |
| 87 | + |
| 88 | + env['ZIP'] = 'zip' |
| 89 | + env['ZIPFLAGS'] = SCons.Util.CLVar('') |
| 90 | + env['ZIPCOM'] = zipAction |
| 91 | + env['ZIPCOMPRESSION'] = zipcompression |
| 92 | + env['ZIPSUFFIX'] = '.zip' |
| 93 | + |
| 94 | +def exists(env): |
| 95 | + return internal_zip or env.Detect('zip') |
| 96 | + |
| 97 | +# Local Variables: |
| 98 | +# tab-width:4 |
| 99 | +# indent-tabs-mode:nil |
| 100 | +# End: |
| 101 | +# vim: set expandtab tabstop=4 shiftwidth=4: |
0 commit comments