~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Merge hpss-no-vfs branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import getpass
29
29
import ntpath
30
30
import posixpath
 
31
import select
31
32
# We need to import both shutil and rmtree as we export the later on posix
32
33
# and need the former on windows
33
34
import shutil
99
100
    mod = os.lstat(filename).st_mode
100
101
    if not stat.S_ISLNK(mod):
101
102
        mod = mod & 0777555
102
 
        os.chmod(filename, mod)
 
103
        chmod_if_possible(filename, mod)
103
104
 
104
105
 
105
106
def make_writable(filename):
106
107
    mod = os.lstat(filename).st_mode
107
108
    if not stat.S_ISLNK(mod):
108
109
        mod = mod | 0200
109
 
        os.chmod(filename, mod)
 
110
        chmod_if_possible(filename, mod)
 
111
 
 
112
 
 
113
def chmod_if_possible(filename, mode):
 
114
    # Set file mode if that can be safely done.
 
115
    # Sometimes even on unix the filesystem won't allow it - see
 
116
    # https://bugs.launchpad.net/bzr/+bug/606537
 
117
    try:
 
118
        # It is probably faster to just do the chmod, rather than
 
119
        # doing a stat, and then trying to compare
 
120
        os.chmod(filename, mode)
 
121
    except (IOError, OSError),e:
 
122
        # Permission/access denied seems to commonly happen on smbfs; there's
 
123
        # probably no point warning about it.
 
124
        # <https://bugs.launchpad.net/bzr/+bug/606537>
 
125
        if getattr(e, 'errno') in (errno.EPERM, errno.EACCES):
 
126
            trace.mutter("ignore error on chmod of %r: %r" % (
 
127
                filename, e))
 
128
            return
 
129
        raise
110
130
 
111
131
 
112
132
def minimum_path_selection(paths):
2006
2026
    return get_terminal_encoding()
2007
2027
 
2008
2028
 
 
2029
_message_encoding = None
 
2030
 
 
2031
 
 
2032
def get_message_encoding():
 
2033
    """Return the encoding used for messages
 
2034
 
 
2035
    While the message encoding is a general setting it should usually only be
 
2036
    needed for decoding system error strings such as from OSError instances.
 
2037
    """
 
2038
    global _message_encoding
 
2039
    if _message_encoding is None:
 
2040
        if os.name == "posix":
 
2041
            import locale
 
2042
            # This is a process-global setting that can change, but should in
 
2043
            # general just get set once at process startup then be constant.
 
2044
            _message_encoding = locale.getlocale(locale.LC_MESSAGES)[1]
 
2045
        else:
 
2046
            # On windows want the result of GetACP() which this boils down to.
 
2047
            _message_encoding = get_user_encoding()
 
2048
    return _message_encoding or "ascii"
 
2049
        
 
2050
 
2009
2051
def get_host_name():
2010
2052
    """Return the current unicode host name.
2011
2053
 
2518
2560
    fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))
2519
2561
    if fn is not None:
2520
2562
        fn(fileno)
 
2563
 
 
2564
 
 
2565
def ensure_empty_directory_exists(path, exception_class):
 
2566
    """Make sure a local directory exists and is empty.
 
2567
    
 
2568
    If it does not exist, it is created.  If it exists and is not empty, an
 
2569
    instance of exception_class is raised.
 
2570
    """
 
2571
    try:
 
2572
        os.mkdir(path)
 
2573
    except OSError, e:
 
2574
        if e.errno != errno.EEXIST:
 
2575
            raise
 
2576
        if os.listdir(path) != []:
 
2577
            raise exception_class(path)
 
2578
 
 
2579
 
 
2580
def is_environment_error(evalue):
 
2581
    """True if exception instance is due to a process environment issue
 
2582
 
 
2583
    This includes OSError and IOError, but also other errors that come from
 
2584
    the operating system or core libraries but are not subclasses of those.
 
2585
    """
 
2586
    if isinstance(evalue, (EnvironmentError, select.error)):
 
2587
        return True
 
2588
    if sys.platform == "win32" and win32utils._is_pywintypes_error(evalue):
 
2589
        return True
 
2590
    return False