/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Parth Malwankar
  • Date: 2010-03-06 01:26:10 UTC
  • mfrom: (5076 +trunk)
  • mto: (5094.3.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5107.
  • Revision ID: parth.malwankar@gmail.com-20100306012610-a32cmtc938t6vw8p
merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
                  S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
22
22
import sys
23
23
import time
 
24
import codecs
24
25
import warnings
25
26
 
26
27
from bzrlib.lazy_import import lazy_import
27
28
lazy_import(globals(), """
28
 
import codecs
29
29
from datetime import datetime
30
30
import errno
31
31
from ntpath import (abspath as _nt_abspath,
87
87
# be opened in binary mode, rather than text mode.
88
88
# On other platforms, O_BINARY doesn't exist, because
89
89
# they always open in binary mode, so it is okay to
90
 
# OR with 0 on those platforms
 
90
# OR with 0 on those platforms.
 
91
# O_NOINHERIT and O_TEXT exists only on win32 too.
91
92
O_BINARY = getattr(os, 'O_BINARY', 0)
 
93
O_TEXT = getattr(os, 'O_TEXT', 0)
 
94
O_NOINHERIT = getattr(os, 'O_NOINHERIT', 0)
92
95
 
93
96
 
94
97
def get_unicode_argv():
665
668
def sha_file_by_name(fname):
666
669
    """Calculate the SHA1 of a file by reading the full text"""
667
670
    s = sha()
668
 
    f = os.open(fname, os.O_RDONLY | O_BINARY)
 
671
    f = os.open(fname, os.O_RDONLY | O_BINARY | O_NOINHERIT)
669
672
    try:
670
673
        while True:
671
674
            b = os.read(f, 1<<16)
1442
1445
    if width is not None:
1443
1446
        os.environ['COLUMNS'] = str(width)
1444
1447
 
1445
 
if sys.platform == 'win32':
1446
 
    # Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from ReadConsoleInput but
1447
 
    # I've no idea how to plug that in the current design -- vila 20091216
1448
 
    pass
1449
 
else:
1450
 
    signal.signal(signal.SIGWINCH, _terminal_size_changed)
 
1448
 
 
1449
_registered_sigwinch = False
 
1450
 
 
1451
def watch_sigwinch():
 
1452
    """Register for SIGWINCH, once and only once."""
 
1453
    global _registered_sigwinch
 
1454
    if not _registered_sigwinch:
 
1455
        if sys.platform == 'win32':
 
1456
            # Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from
 
1457
            # ReadConsoleInput but I've no idea how to plug that in
 
1458
            # the current design -- vila 20091216
 
1459
            pass
 
1460
        else:
 
1461
            signal.signal(signal.SIGWINCH, _terminal_size_changed)
 
1462
        _registered_sigwinch = True
1451
1463
 
1452
1464
 
1453
1465
def supports_executable():
2150
2162
        else:
2151
2163
            data, _ = self.encode(object, self.errors)
2152
2164
            self.stream.write(data)
 
2165
 
 
2166
if sys.platform == 'win32':
 
2167
    def open_file(filename, mode='r', bufsize=-1):
 
2168
        """This function is used to override the ``open`` builtin.
 
2169
        
 
2170
        But it uses O_NOINHERIT flag so the file handle is not inherited by
 
2171
        child processes.  Deleting or renaming a closed file opened with this
 
2172
        function is not blocking child processes.
 
2173
        """
 
2174
        writing = 'w' in mode
 
2175
        appending = 'a' in mode
 
2176
        updating = '+' in mode
 
2177
        binary = 'b' in mode
 
2178
 
 
2179
        flags = O_NOINHERIT
 
2180
        # see http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx
 
2181
        # for flags for each modes.
 
2182
        if binary:
 
2183
            flags |= O_BINARY
 
2184
        else:
 
2185
            flags |= O_TEXT
 
2186
 
 
2187
        if writing:
 
2188
            if updating:
 
2189
                flags |= os.O_RDWR
 
2190
            else:
 
2191
                flags |= os.O_WRONLY
 
2192
            flags |= os.O_CREAT | os.O_TRUNC
 
2193
        elif appending:
 
2194
            if updating:
 
2195
                flags |= os.O_RDWR
 
2196
            else:
 
2197
                flags |= os.O_WRONLY
 
2198
            flags |= os.O_CREAT | os.O_APPEND
 
2199
        else: #reading
 
2200
            if updating:
 
2201
                flags |= os.O_RDWR
 
2202
            else:
 
2203
                flags |= os.O_RDONLY
 
2204
 
 
2205
        return os.fdopen(os.open(filename, flags), mode, bufsize)
 
2206
else:
 
2207
    open_file = open