~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

remove all trailing whitespace from bzr source

Show diffs side-by-side

added added

removed removed

Lines of Context:
121
121
    global _QUOTE_RE
122
122
    if _QUOTE_RE is None:
123
123
        _QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/\\_~-])')
124
 
        
 
124
 
125
125
    if _QUOTE_RE.search(f):
126
126
        return '"' + f + '"'
127
127
    else:
171
171
 
172
172
def fancy_rename(old, new, rename_func, unlink_func):
173
173
    """A fancy rename, when you don't have atomic rename.
174
 
    
 
174
 
175
175
    :param old: The old path, to rename from
176
176
    :param new: The new path, to rename to
177
177
    :param rename_func: The potentially non-atomic rename function
317
317
    """We expect to be able to atomically replace 'new' with old.
318
318
 
319
319
    On win32, if new exists, it must be moved out of the way first,
320
 
    and then deleted. 
 
320
    and then deleted.
321
321
    """
322
322
    try:
323
323
        fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
324
324
    except OSError, e:
325
325
        if e.errno in (errno.EPERM, errno.EACCES, errno.EBUSY, errno.EINVAL):
326
 
            # If we try to rename a non-existant file onto cwd, we get 
327
 
            # EPERM or EACCES instead of ENOENT, this will raise ENOENT 
 
326
            # If we try to rename a non-existant file onto cwd, we get
 
327
            # EPERM or EACCES instead of ENOENT, this will raise ENOENT
328
328
            # if the old path doesn't exist, sometimes we get EACCES
329
329
            # On Linux, we seem to get EBUSY, on Mac we get EINVAL
330
330
            os.lstat(old)
470
470
 
471
471
def is_inside(dir, fname):
472
472
    """True if fname is inside dir.
473
 
    
 
473
 
474
474
    The parameters should typically be passed to osutils.normpath first, so
475
475
    that . and .. and repeated slashes are eliminated, and the separators
476
476
    are canonical for the platform.
477
 
    
478
 
    The empty string as a dir name is taken as top-of-tree and matches 
 
477
 
 
478
    The empty string as a dir name is taken as top-of-tree and matches
479
479
    everything.
480
480
    """
481
 
    # XXX: Most callers of this can actually do something smarter by 
 
481
    # XXX: Most callers of this can actually do something smarter by
482
482
    # looking at the inventory
483
483
    if dir == fname:
484
484
        return True
485
 
    
 
485
 
486
486
    if dir == '':
487
487
        return True
488
488
 
639
639
    return offset.days * 86400 + offset.seconds
640
640
 
641
641
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
642
 
    
 
642
 
643
643
def format_date(t, offset=0, timezone='original', date_fmt=None,
644
644
                show_offset=True):
645
645
    """Return a formatted date string.
701
701
 
702
702
def compact_date(when):
703
703
    return time.strftime('%Y%m%d%H%M%S', time.gmtime(when))
704
 
    
 
704
 
705
705
 
706
706
def format_delta(delta):
707
707
    """Get a nice looking string for a time delta.
783
783
ALNUM = '0123456789abcdefghijklmnopqrstuvwxyz'
784
784
def rand_chars(num):
785
785
    """Return a random string of num alphanumeric characters
786
 
    
787
 
    The result only contains lowercase chars because it may be used on 
 
786
 
 
787
    The result only contains lowercase chars because it may be used on
788
788
    case-insensitive filesystems.
789
789
    """
790
790
    s = ''
1025
1025
 
1026
1026
    If it is unicode, it is returned.
1027
1027
    Otherwise it is decoded from utf-8. If a decoding error
1028
 
    occurs, it is wrapped as a If the decoding fails, the exception is wrapped 
 
1028
    occurs, it is wrapped as a If the decoding fails, the exception is wrapped
1029
1029
    as a BzrBadParameter exception.
1030
1030
    """
1031
1031
    if isinstance(unicode_or_utf8_string, unicode):
1119
1119
 
1120
1120
    On platforms where the system normalizes filenames (Mac OSX),
1121
1121
    you can access a file by any path which will normalize correctly.
1122
 
    On platforms where the system does not normalize filenames 
 
1122
    On platforms where the system does not normalize filenames
1123
1123
    (Windows, Linux), you have to access a file by its exact path.
1124
1124
 
1125
 
    Internally, bzr only supports NFC normalization, since that is 
 
1125
    Internally, bzr only supports NFC normalization, since that is
1126
1126
    the standard for XML documents.
1127
1127
 
1128
1128
    So return the normalized path, and a flag indicating if the file
1208
1208
 
1209
1209
 
1210
1210
def check_legal_path(path):
1211
 
    """Check whether the supplied path is legal.  
 
1211
    """Check whether the supplied path is legal.
1212
1212
    This is only required on Windows, so we don't test on other platforms
1213
1213
    right now.
1214
1214
    """
1248
1248
 
1249
1249
def walkdirs(top, prefix=""):
1250
1250
    """Yield data about all the directories in a tree.
1251
 
    
 
1251
 
1252
1252
    This yields all the data about the contents of a directory at a time.
1253
1253
    After each directory has been yielded, if the caller has mutated the list
1254
1254
    to exclude some directories, they are then not descended into.
1255
 
    
 
1255
 
1256
1256
    The data yielded is of the form:
1257
1257
    ((directory-relpath, directory-path-from-top),
1258
1258
    [(relpath, basename, kind, lstat, path-from-top), ...]),
1259
1259
     - directory-relpath is the relative path of the directory being returned
1260
1260
       with respect to top. prefix is prepended to this.
1261
 
     - directory-path-from-root is the path including top for this directory. 
 
1261
     - directory-path-from-root is the path including top for this directory.
1262
1262
       It is suitable for use with os functions.
1263
1263
     - relpath is the relative path within the subtree being walked.
1264
1264
     - basename is the basename of the path
1266
1266
       present within the tree - but it may be recorded as versioned. See
1267
1267
       versioned_kind.
1268
1268
     - lstat is the stat data *if* the file was statted.
1269
 
     - planned, not implemented: 
 
1269
     - planned, not implemented:
1270
1270
       path_from_tree_root is the path from the root of the tree.
1271
1271
 
1272
 
    :param prefix: Prefix the relpaths that are yielded with 'prefix'. This 
 
1272
    :param prefix: Prefix the relpaths that are yielded with 'prefix'. This
1273
1273
        allows one to walk a subtree but get paths that are relative to a tree
1274
1274
        rooted higher up.
1275
1275
    :return: an iterator over the dirs.
1276
1276
    """
1277
1277
    #TODO there is a bit of a smell where the results of the directory-
1278
 
    # summary in this, and the path from the root, may not agree 
 
1278
    # summary in this, and the path from the root, may not agree
1279
1279
    # depending on top and prefix - i.e. ./foo and foo as a pair leads to
1280
1280
    # potentially confusing output. We should make this more robust - but
1281
1281
    # not at a speed cost. RBC 20060731
1451
1451
def copy_tree(from_path, to_path, handlers={}):
1452
1452
    """Copy all of the entries in from_path into to_path.
1453
1453
 
1454
 
    :param from_path: The base directory to copy. 
 
1454
    :param from_path: The base directory to copy.
1455
1455
    :param to_path: The target directory. If it does not exist, it will
1456
1456
        be created.
1457
1457
    :param handlers: A dictionary of functions, which takes a source and