~jameinel/bzr/fix-push2

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-13 02:44:23 UTC
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20060913024423-2f0729076ddd4e31
lazy_import osutils and sign_my_commits
Move doc tests into test_osutils, since lazy_import doesn't play nicely
with DocTest

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
19
from cStringIO import StringIO
 
20
import os
 
21
import re
 
22
import stat
 
23
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
 
24
                  S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
 
25
import sys
 
26
import time
 
27
 
 
28
from bzrlib.lazy_import import lazy_import
 
29
lazy_import(globals(), """
20
30
import errno
21
31
from ntpath import (abspath as _nt_abspath,
22
32
                    join as _nt_join,
24
34
                    realpath as _nt_realpath,
25
35
                    splitdrive as _nt_splitdrive,
26
36
                    )
27
 
import os
28
 
from os import listdir
29
37
import posixpath
30
 
import re
31
38
import sha
32
39
import shutil
33
 
from shutil import copyfile
34
 
import stat
35
 
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
36
 
                  S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
 
40
from shutil import (
 
41
    rmtree,
 
42
    )
37
43
import string
38
 
import sys
39
 
import time
40
 
import types
41
44
import tempfile
 
45
from tempfile import (
 
46
    mkdtemp,
 
47
    )
42
48
import unicodedata
 
49
""")
43
50
 
44
51
import bzrlib
45
 
from bzrlib.errors import (BzrError,
46
 
                           BzrBadParameterNotUnicode,
47
 
                           NoSuchFile,
48
 
                           PathNotChild,
49
 
                           IllegalPath,
50
 
                           )
51
 
from bzrlib.symbol_versioning import (deprecated_function, 
52
 
        zero_nine)
 
52
from bzrlib import (
 
53
    errors,
 
54
    )
 
55
from bzrlib.errors import (
 
56
    BzrError,
 
57
    BzrBadParameterNotUnicode,
 
58
    NoSuchFile,
 
59
    PathNotChild,
 
60
    IllegalPath,
 
61
    )
 
62
from bzrlib.symbol_versioning import (
 
63
    deprecated_function,
 
64
    zero_nine,
 
65
    )
53
66
from bzrlib.trace import mutter
54
67
 
55
68
 
122
135
        return _mapper(_lstat(f).st_mode)
123
136
    except OSError, e:
124
137
        if getattr(e, 'errno', None) == errno.ENOENT:
125
 
            raise bzrlib.errors.NoSuchFile(f)
 
138
            raise errors.NoSuchFile(f)
126
139
        raise
127
140
 
128
141
 
302
315
pathjoin = os.path.join
303
316
normpath = os.path.normpath
304
317
getcwd = os.getcwdu
305
 
mkdtemp = tempfile.mkdtemp
306
318
rename = os.rename
307
319
dirname = os.path.dirname
308
320
basename = os.path.basename
309
 
rmtree = shutil.rmtree
 
321
# These were already imported into local scope
 
322
# mkdtemp = tempfile.mkdtemp
 
323
# rmtree = shutil.rmtree
310
324
 
311
325
MIN_ABS_PATHLENGTH = 1
312
326
 
330
344
        if function in (os.remove, os.rmdir) \
331
345
            and type_ == OSError \
332
346
            and value.errno == errno.EACCES:
333
 
            bzrlib.osutils.make_writable(path)
 
347
            make_writable(path)
334
348
            function(path)
335
349
        else:
336
350
            raise
440
454
    
441
455
    The empty string as a dir name is taken as top-of-tree and matches 
442
456
    everything.
443
 
    
444
 
    >>> is_inside('src', pathjoin('src', 'foo.c'))
445
 
    True
446
 
    >>> is_inside('src', 'srccontrol')
447
 
    False
448
 
    >>> is_inside('src', pathjoin('src', 'a', 'a', 'a', 'foo.c'))
449
 
    True
450
 
    >>> is_inside('foo.c', 'foo.c')
451
 
    True
452
 
    >>> is_inside('foo.c', '')
453
 
    False
454
 
    >>> is_inside('', 'foo.c')
455
 
    True
456
457
    """
457
458
    # XXX: Most callers of this can actually do something smarter by 
458
459
    # looking at the inventory
642
643
## decomposition (might be too tricksy though.)
643
644
 
644
645
def splitpath(p):
645
 
    """Turn string into list of parts.
646
 
 
647
 
    >>> splitpath('a')
648
 
    ['a']
649
 
    >>> splitpath('a/b')
650
 
    ['a', 'b']
651
 
    >>> splitpath('a/./b')
652
 
    ['a', 'b']
653
 
    >>> splitpath('a/.b')
654
 
    ['a', '.b']
655
 
    >>> splitpath('a/../b')
656
 
    Traceback (most recent call last):
657
 
    ...
658
 
    BzrError: sorry, '..' not allowed in path
659
 
    """
660
 
    assert isinstance(p, types.StringTypes)
 
646
    """Turn string into list of parts."""
 
647
    assert isinstance(p, basestring)
661
648
 
662
649
    # split on either delimiter because people might use either on
663
650
    # Windows
705
692
def link_or_copy(src, dest):
706
693
    """Hardlink a file, or copy it if it can't be hardlinked."""
707
694
    if not hardlinks_good():
708
 
        copyfile(src, dest)
 
695
        shutil.copyfile(src, dest)
709
696
        return
710
697
    try:
711
698
        os.link(src, dest)
712
699
    except (OSError, IOError), e:
713
700
        if e.errno != errno.EXDEV:
714
701
            raise
715
 
        copyfile(src, dest)
 
702
        shutil.copyfile(src, dest)
716
703
 
717
704
def delete_any(full_path):
718
705
    """Delete a file or directory."""
941
928
    lstat = os.lstat
942
929
    pending = []
943
930
    _directory = _directory_kind
944
 
    _listdir = listdir
 
931
    _listdir = os.listdir
945
932
    pending = [(prefix, "", _directory, None, top)]
946
933
    while pending:
947
934
        dirblock = []