From 8ab9c1508514680a3add1f1837b1569bc4709eb1 Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Mon, 18 Dec 2017 22:57:16 +1300 Subject: [PATCH] Add partial path macro for vcs URL fields Add a %pathelement[]% macro that lets you extract just the nth element or a slice and substitute it into the URL. This will be used so we can treat multiple repos that appear under the same base URL as belonging to the same comparison layer, e.g. https://github.com/somedistro//... would be handled using the file URL https://github.com/somedistro/%pathelement[0]%/blob/master/%pathelement[1:]% so that the path /abc/fix.patch would generate the URL https://github.com/somedistro/abc/blob/master/fix.patch Signed-off-by: Paul Eggleton --- layerindex/models.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/layerindex/models.py b/layerindex/models.py index cc07a7c..828eb1e 100644 --- a/layerindex/models.py +++ b/layerindex/models.py @@ -236,12 +236,28 @@ class LayerBranch(models.Model): else: branchname = self.branch.name url = base_url.replace('%branch%', branchname) + if path: + splitpath = path.split('/') + for match in re.findall('(%pathelement\[([0-9]*)(:[0-9]*)?\]%)', url): + if match[1] == '': + start = None + else: + start = int(match[1]) + if ':' in match[2]: + stopstr = match[2][1:] + if stopstr == '': + stop = None + else: + stop = int(stopstr) + url = url.replace(match[0], '/'.join(splitpath[start:stop])) + else: + url = url.replace(match[0], splitpath[start]) # If there's a % in the path (e.g. a wildcard bbappend) we need to encode it if extra_path: extra_path = extra_path.replace('%', '%25') - if '%path%' in base_url: + if '%path%' in base_url or '%pathelement[' in base_url: if extra_path: url = re.sub(r'\[([^\]]*%path%[^\]]*)\]', '\\1', url) else: