##// END OF EJS Templates
util: add safename function for generating safe names to rename to...
Mark Thomas -
r34555:6f11a74d default
parent child Browse files
Show More
@@ -26,6 +26,7 b' import errno'
26 import gc
26 import gc
27 import hashlib
27 import hashlib
28 import imp
28 import imp
29 import itertools
29 import mmap
30 import mmap
30 import os
31 import os
31 import platform as pyplatform
32 import platform as pyplatform
@@ -3835,3 +3836,26 b' i18nfunctions = bundlecompressiontopics('
3835
3836
3836 # convenient shortcut
3837 # convenient shortcut
3837 dst = debugstacktrace
3838 dst = debugstacktrace
3839
3840 def safename(f, tag, ctx, others=None):
3841 """
3842 Generate a name that it is safe to rename f to in the given context.
3843
3844 f: filename to rename
3845 tag: a string tag that will be included in the new name
3846 ctx: a context, in which the new name must not exist
3847 others: a set of other filenames that the new name must not be in
3848
3849 Returns a file name of the form oldname~tag[~number] which does not exist
3850 in the provided context and is not in the set of other names.
3851 """
3852 if others is None:
3853 others = set()
3854
3855 fn = '%s~%s' % (f, tag)
3856 if fn not in ctx and fn not in others:
3857 return fn
3858 for n in itertools.count(1):
3859 fn = '%s~%s~%s' % (f, tag, n)
3860 if fn not in ctx and fn not in others:
3861 return fn
General Comments 0
You need to be logged in to leave comments. Login now