| 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 """Hard link support for Windows. |
| 32 |
| 33 This module is a SCons tool which should be include in the topmost windows |
| 34 environment. It is usually included by the target_platform_windows tool. |
| 35 """ |
| 36 |
| 37 |
| 38 import os |
| 39 import stat |
| 40 import sys |
| 41 import SCons |
| 42 |
| 43 if sys.platform in ('win32', 'cygwin'): |
| 44 # Only attempt to load pywin32 on Windows systems |
| 45 try: |
| 46 import win32file |
| 47 except ImportError: |
| 48 print ('Warning: Unable to load win32file module; using copy instead of' |
| 49 ' hard linking for env.Install(). Is pywin32 present?') |
| 50 |
| 51 #------------------------------------------------------------------------------ |
| 52 # Python 2.4 and 2.5's os module doesn't support os.link on Windows, even |
| 53 # though Windows does have hard-link capability on NTFS filesystems. So by |
| 54 # default, SCons will insist on copying files instead of linking them as it |
| 55 # does on other (linux,mac) OS's. |
| 56 # |
| 57 # Use the CreateHardLink() functionality from pywin32 to provide hard link |
| 58 # capability on Windows also. |
| 59 |
| 60 |
| 61 def _HardLink(fs, src, dst): |
| 62 """Hard link function for hooking into SCons.Node.FS. |
| 63 |
| 64 Args: |
| 65 fs: Filesystem class to use. |
| 66 src: Source filename to link to. |
| 67 dst: Destination link name to create. |
| 68 |
| 69 Raises: |
| 70 OSError: The link could not be created. |
| 71 """ |
| 72 # A hard link shares file permissions from the source. On Windows, the write |
| 73 # access of the file itself determines whether the file can be deleted |
| 74 # (unlike Linux/Mac, where it's the write access of the containing |
| 75 # directory). So if we made a link from a read-only file, the only way to |
| 76 # delete it would be to make the link writable, which would have the |
| 77 # unintended effect of making the source writable too. |
| 78 # |
| 79 # So if the source is read-only, we can't hard link from it. |
| 80 if not stat.S_IMODE(fs.stat(src)[stat.ST_MODE]) & stat.S_IWRITE: |
| 81 raise OSError('Unsafe to hard-link read-only file: %s' % src) |
| 82 |
| 83 # If the file is writable, only hard-link from it if it was build by SCons. |
| 84 # Those files shouldn't later become read-only. We don't hard-link from |
| 85 # writable files which SCons didn't create, because those could become |
| 86 # read-only (for example, following a 'p4 submit'), which as indicated above |
| 87 # would make our link read-only too. |
| 88 if not fs.File(src).has_builder(): |
| 89 raise OSError('Unsafe to hard-link file not built by SCons: %s' % src) |
| 90 |
| 91 try: |
| 92 win32file.CreateHardLink(dst, src) |
| 93 except win32file.error, msg: |
| 94 # Translate errors into standard OSError which SCons expects. |
| 95 raise OSError(msg) |
| 96 |
| 97 |
| 98 #------------------------------------------------------------------------------ |
| 99 |
| 100 |
| 101 def generate(env): |
| 102 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 103 """SCons entry point for this tool.""" |
| 104 env = env # Silence gpylint |
| 105 |
| 106 # Patch in our hard link function, if we were able to load pywin32 |
| 107 if 'win32file' in globals(): |
| 108 SCons.Node.FS._hardlink_func = _HardLink |
| OLD | NEW |