src/modules/portable/os_windows.py
author Rich Burridge <rich.burridge@sun.com>
Mon, 30 Nov 2009 13:01:40 -0800
changeset 1516 8c950a3b4171
parent 1407 56042994e222
child 2144 2ac203b826ab
permissions -rw-r--r--
10485 move pkg(5) to Python 2.6 10482 upgrade to cherrypy 3.1.2 11836 shebang line for python modules should be python version-agnostic 11950 ldtp used by pkg build process not setup to easily use Python 2.6 11989 pkg python dependency analysis tests fail
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1407
diff changeset
     1
#!/usr/bin/python
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     2
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     3
# CDDL HEADER START
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     4
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     8
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
     9
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    11
# See the License for the specific language governing permissions
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    12
# and limitations under the License.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    13
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    15
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    19
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    20
# CDDL HEADER END
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    21
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    22
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    23
# Use is subject to license terms.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    24
#
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    25
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    26
"""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    27
Windows has a specific implementation for most of these.  For the
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    28
group and user id-related (credential) APIs, no implementation
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    29
is provided.  This causes the file and directory actions to not
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    30
utilize any credential metadata when acting on Windows-compatible
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    31
systems. In the future, this may be able to be mapped onto the
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    32
NTFS group mechanism if deemed useful.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    33
"""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    34
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    35
import getpass
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    36
import shutil
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    37
import os
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    38
import errno
1407
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
    39
import stat
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    40
import tempfile
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
    41
import threading
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    42
import util as os_util
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    43
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    44
def get_isainfo():
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    45
        """ TODO: Detect Windows 64-bit"""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    46
        return ['i386']
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    47
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    48
def get_release():
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    49
        return os_util.get_os_release()
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    50
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    51
def get_platform():
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    52
        """ TODO: any other windows platforms to support?"""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    53
        return 'i86pc'
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    54
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    55
def get_group_by_name(name, dirpath, use_file):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    56
        """group names/numbers are ignored on Windows."""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    57
        return -1
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    58
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    59
def get_user_by_name(name, dirpath, use_file):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    60
        """group names/numbers are ignored on Windows."""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    61
        return -1
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    62
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    63
def get_name_by_gid(gid, dirpath, use_file):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    64
        """group names/numbers are ignored on Windows."""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    65
        return ''
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    66
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    67
def get_name_by_uid(uid, dirpath, use_file):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    68
        """group names/numbers are ignored on Windows."""
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    69
        return '' 
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    70
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents: 481
diff changeset
    71
def get_userid():
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents: 481
diff changeset
    72
        """group names/numbers are ignored on Windows."""
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents: 481
diff changeset
    73
        return -1
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents: 481
diff changeset
    74
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    75
def get_username():
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    76
        try:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    77
                return getpass.getuser()
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    78
        except ImportError:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    79
                # getpass.getuser() will fail on systems without
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    80
                # pwd module, so try a common python windows add-on
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    81
                try:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    82
                        import win32api
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    83
                        return win32api.GetUserName()
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    84
                except ImportError:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    85
                        return None
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    86
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    87
def is_admin():
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    88
        try:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    89
                # ctypes only available in python 2.5 or later
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    90
                import ctypes
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    91
                return ctypes.windll.shell32.IsUserAnAdmin() != 0
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    92
        except ImportError:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    93
                return False
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    94
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    95
def chown(path, owner, group):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    96
        """
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    97
        group names/numbers are ignored on Windows, so changing
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    98
        ownership of a file makes no sense.
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
    99
        """
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   100
        return
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   101
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   102
# On Windows, rename to existing file is not allowed, so the
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   103
# destination file must be deleted first. But if the destination file 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   104
# is locked, it cannot be deleted. Windows has 3 types of file locking
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   105
# 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   106
# 1. Using share access controls that allow applications to specify 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   107
#    whole-file access sharing for read, write or delete;
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   108
# 2. Using byte range locks to arbitrate read and write access to 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   109
#    regions within a single file; and
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   110
# 3. By Windows file systems disallowing executing files from being 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   111
#    opened for write or delete access.
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   112
#
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   113
# The code here deals only with locks of type 3. If the lock on the open 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   114
# file is for a running executable, then the destination file can be renamed
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   115
# and deleted later when the file is no longer in use. For a rename to a 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   116
# destination locked with type 1 or 2, rename throws an OSError exception.
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   117
#
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   118
# To accomplish the delayed delete, the file is renamed to a trash folder
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   119
# within the image containing the file. A single image is assumed to be 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   120
# contained within a single file system, thus making the rename feasible. 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   121
# The empty_trash method is called at the end of rename to cleanup any 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   122
# trash files that were left from earlier calls to rename, typically by 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   123
# previous processes. The empty_trash method needs to be fast most of the 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   124
# time, so the real work is only done the first time a rename operation is 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   125
# done on an image. The image module cannot be imported when this module is
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   126
# initialized because that leads to a circular dependency.  So it is imported
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   127
# as needed.
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   128
        
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   129
trashname = "trash"
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   130
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   131
# cached_image_info is a list of tuples (image root, image trash directory) for
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   132
# all of the images that have been accessed by this process. It is used to
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   133
# quickly find the trash directory for a path without having to create an
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   134
# image object for it each time.
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   135
cached_image_info = []
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   136
cache_lock = threading.Lock()
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   137
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   138
def get_trashdir(path):
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   139
        """
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   140
        Use path to determine the trash directory.  This method does not create
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   141
        the directory. If path is not contained within an image, return None.
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   142
        The directories for the images that have already been accessed are
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   143
        cached to improve the speed of this method.
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   144
        """
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   145
        global cached_image_info
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   146
        global cache_lock
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   147
        import pkg.client.image as image
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   148
        from pkg.client.api_errors import ImageNotFoundException
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   149
        try:
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   150
            cache_lock.acquire()
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   151
            for iroot, itrash in cached_image_info:
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   152
                    if path.startswith(iroot):
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   153
                            return itrash
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   154
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   155
            try:
1370
face1fe579ab 11400 image objects should always have a root
Brock Pytlik <bpytlik@sun.com>
parents: 1305
diff changeset
   156
                    img = image.Image(os.path.dirname(path))
1305
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   157
            except ImageNotFoundException:
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   158
                    # path is not within an image, no trash dir
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   159
                    return None
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   160
            trashdir = os.path.join(img.imgdir, trashname)
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   161
            # this is the first time putting something in the trash for
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   162
            # this image, so try to empty the trash first
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   163
            shutil.rmtree(trashdir, True)
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   164
            cached_image_info.append((img.get_root(), trashdir))
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   165
            return trashdir
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   166
        finally:
8abd42081a9f 8312 os_windows.cached_image causes multi-threading problems
Tom Mueller <Tom.Mueller@sun.com>
parents: 613
diff changeset
   167
            cache_lock.release()
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   168
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   169
def move_to_trash(path):
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   170
        """
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   171
        Move the file to a trash folder within its containing image. If the 
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   172
        file is not in an image, just return without moving it. If the file
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   173
        cannot be removed, raise an OSError exception.
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   174
        """
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   175
        trashdir = get_trashdir(path)
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   176
        if not trashdir:
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   177
                return
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   178
        if not os.path.exists(trashdir):
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   179
                os.mkdir(trashdir)
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   180
        tdir = tempfile.mkdtemp(dir = trashdir)
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   181
        # this rename will raise an exception if the file is
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   182
        # locked and cannot be renamed.
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   183
        os.rename(path, os.path.join(tdir, os.path.basename(path)))
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   184
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   185
def rename(src, dst):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   186
        """
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   187
        Rename the src file to the dst name, deleting dst if necessary.
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   188
        """
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   189
        try:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   190
                os.rename(src, dst)
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   191
        except OSError, err:
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   192
                if err.errno != errno.EEXIST:
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   193
                        raise
402
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   194
                try:
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   195
                        os.unlink(dst)
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   196
                except OSError:
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   197
                        move_to_trash(dst)
3ec50f01e77e 954 Updating of IPS using IPS fails
Tom Mueller <Tom.Mueller@sun.com>
parents: 290
diff changeset
   198
                # finally rename the file
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   199
                os.rename(src, dst)
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   200
481
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   201
def remove(path):
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   202
        """
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   203
        Remove the given path. The file is moved to the trash area of the
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   204
        image if necessary where it will be removed at a later time.
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   205
        """
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   206
        try:
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   207
                os.unlink(path)
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   208
        except OSError, err:
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   209
                if err.errno != errno.EACCES:
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   210
                        raise
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   211
                move_to_trash(path)
9fa0541013b3 2402 pkg unable to remove executable file that is in use on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 409
diff changeset
   212
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   213
def link(src, dst):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   214
        copyfile(src, dst)
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   215
        
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   216
def split_path(path):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   217
        drivepath = os.path.splitdrive(path)
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   218
        return drivepath[1].split('\\')
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   219
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   220
def get_root(path):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   221
        drivepath = os.path.splitdrive(path)
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   222
        if drivepath[0] == "":
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   223
                return os.path.sep
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   224
        else:
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   225
                return drivepath[0] + '\\'
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   226
1407
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   227
def assert_mode(path, mode):
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   228
        # only compare user's permission bits on Windows
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   229
        fmode = stat.S_IMODE(os.lstat(path).st_mode)
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   230
        if (mode & stat.S_IRWXU) != (fmode & stat.S_IRWXU):
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   231
                ae = AssertionError("mode mismatch for %s, has %o, want %o" % 
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   232
                    (path, fmode, mode))
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   233
                ae.mode = fmode;
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   234
                raise ae
56042994e222 8049 leading / not stripped from file action path on Windows
Tom Mueller <Tom.Mueller@sun.com>
parents: 1370
diff changeset
   235
290
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   236
def copyfile(src, dst):
6c5c87515614 160 Make IPS cross-platform for use across other supported OS's (missing files)
Tom Mueller <Tom.Mueller@Sun.COM>
parents:
diff changeset
   237
        shutil.copyfile(src, dst)