[Kimchi-devel] [PATCH v2 2/3] Support different "tags" when generating a new name in "get_next_clone_name"

Crístian Viana vianac at linux.vnet.ibm.com
Wed Dec 10 17:12:57 UTC 2014


The current implementation of "kimchi.utils.get_next_clone_name" always
appends the tag "-clone" in its return values. That may not be suitable
for other cases when we want a new resource name without that tag.

Add an optional parameter in "get_next_clone_name" so it supports
different tags.

Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 src/kimchi/utils.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index 3af701c..bac07df 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -312,10 +312,11 @@ def validate_repo_url(url):
         raise InvalidParameter("KCHREPOS0002E")
 
 
-def get_next_clone_name(all_names, basename, name_suffix=''):
+def get_next_clone_name(all_names, basename, name_suffix='',
+                        clone_tag='-clone'):
     """Find the next available name for a cloned resource.
 
-    If any resource named "<basename>-clone-<number><name_suffix>" is found
+    If any resource named "<basename><tag>-<number><name_suffix>" is found
     in "all_names", use the maximum "number" + 1; else, use 1.
 
     Arguments:
@@ -325,15 +326,17 @@ def get_next_clone_name(all_names, basename, name_suffix=''):
     basename -- The name of the original resource.
     name_suffix -- The resource name suffix (optional). This parameter
         exist so that a resource named "foo.img" gets the name
-        "foo-clone-1.img" instead of "foo.img-clone-1". If this parameter
+        "foo<tag>-1.img" instead of "foo.img<tag>-1". If this parameter
         is used, the suffix should not be present in "basename".
+    clone_tag -- The tag which will be appended to the new name (optional).
+        Default value: "-clone"
 
     Return:
-    A UTF-8 string in the format "<basename>-clone-<number><name_suffix>".
+    A UTF-8 string in the format "<basename><tag>-<number><name_suffix>".
     """
     re_group_num = 'num'
 
-    re_expr = u'%s-clone-(?P<%s>\d+)' % (basename, re_group_num)
+    re_expr = u'%s%s-(?P<%s>\d+)' % (basename, clone_tag, re_group_num)
     if name_suffix != '':
         re_expr = u'%s%s' % (re_expr, name_suffix)
 
@@ -346,7 +349,7 @@ def get_next_clone_name(all_names, basename, name_suffix=''):
             max_num = max(max_num, int(match.group(re_group_num)))
 
     # increments the maximum "clone number" found
-    new_name = u'%s-clone-%d' % (basename, max_num + 1)
+    new_name = u'%s%s-%d' % (basename, clone_tag, max_num + 1)
     if name_suffix != '':
         new_name = new_name + name_suffix
 
-- 
1.9.3




More information about the Kimchi-devel mailing list