Handle web interfaces with different URL schemes

Some git web interfaces use different paths for files (blobs) and
directories when it comes to browsing; this means we need to have a
separate field for the base URL for files.

Additionally, Gitweb typically puts the path within the URL instead of
at the end (although the parameters can be reordered) and cannot handle
the file parameter (f=) being specified with no value. So, to add some
flexibility, add the ability to use a positional macro %path% to
optionally specify where to put the path. If needed, square brackets
can be used in conjunction with %path% to exclude parts of the string
when there is no path, i.e. we want to browse the root. For example, the
following base URL:

  http://git.example.com/?p=somerepo.git;a=tree;[;f=%path%];hb=HEAD

would be translated to this with a path of "path/to/dir":

  http://git.example.com/?p=somerepo.git;a=tree;f=path/to/dir;hb=HEAD

and this if no path were specified:

  http://git.example.com/?p=somerepo.git;a=tree;hb=HEAD

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2013-02-23 20:05:00 +00:00
parent 4fef5c0c8e
commit 6cd47bdd82
2 changed files with 40 additions and 11 deletions

View File

@ -16,7 +16,7 @@ class SubmitLayerForm(forms.ModelForm):
class Meta:
model = LayerItem
fields = ('name', 'layer_type', 'summary', 'description', 'vcs_url', 'vcs_subdir', 'vcs_web_url', 'vcs_web_tree_base_url', 'usage_url')
fields = ('name', 'layer_type', 'summary', 'description', 'vcs_url', 'vcs_subdir', 'vcs_web_url', 'vcs_web_tree_base_url', 'vcs_web_file_base_url', 'usage_url')
def clean_name(self):
name = self.cleaned_data['name'].strip()
@ -36,6 +36,12 @@ class SubmitLayerForm(forms.ModelForm):
val(url)
return url
def clean_vcs_subdir(self):
subdir = self.cleaned_data['vcs_subdir'].strip()
if subdir.endswith('/'):
subdir = subdir[:-1]
return subdir
def clean_vcs_web_tree_base_url(self):
url = self.cleaned_data['vcs_web_tree_base_url'].strip()
if url:
@ -43,6 +49,13 @@ class SubmitLayerForm(forms.ModelForm):
val(url)
return url
def clean_vcs_web_file_base_url(self):
url = self.cleaned_data['vcs_web_file_base_url'].strip()
if url:
val = URLValidator(verify_exists=False)
val(url)
return url
def clean_maintainers(self):
maintainers = self.cleaned_data['maintainers'].strip()
addrs = re.split(r'"?([^"@$<>]+)"? *<([^<> ]+)>,? *', maintainers)

View File

@ -33,7 +33,8 @@ class LayerItem(models.Model):
vcs_subdir = models.CharField('Repository subdirectory', max_length=40, blank=True)
vcs_url = models.CharField('Repository URL', max_length=200)
vcs_web_url = models.URLField('Repository web interface URL', blank=True)
vcs_web_tree_base_url = models.CharField('Repository web interface tree start URL', max_length=200, blank=True)
vcs_web_tree_base_url = models.CharField('Repository web interface tree base URL', max_length=200, blank=True)
vcs_web_file_base_url = models.CharField('Repository web interface file base URL', max_length=200, blank=True)
usage_url = models.URLField('Usage web page URL', blank=True)
class Meta:
@ -45,11 +46,28 @@ class LayerItem(models.Model):
def change_status(self, newstatus, username):
self.status = newstatus
def tree_url(self):
if self.vcs_subdir and self.vcs_web_tree_base_url:
return self.vcs_web_tree_base_url + self.vcs_subdir
def _handle_url_path(self, base_url, path):
if base_url:
if self.vcs_subdir:
extra_path = self.vcs_subdir + '/' + path
else:
return self.vcs_web_tree_base_url
extra_path = path
if '%path%' in base_url:
if extra_path:
url = re.sub(r'\[([^\]]*%path%[^\]]*)\]', '\\1', base_url)
return url.replace('%path%', extra_path)
else:
url = re.sub(r'\[([^\]]*%path%[^\]]*)\]', '', base_url)
return url
else:
return base_url + extra_path
return None
def tree_url(self, path = ''):
return self._handle_url_path(self.vcs_web_tree_base_url, path)
def file_url(self, path = ''):
return self._handle_url_path(self.vcs_web_file_base_url, path)
def sorted_recipes(self):
return self.recipe_set.order_by('filename')
@ -111,10 +129,8 @@ class Recipe(models.Model):
homepage = models.URLField(blank=True)
def vcs_web_url(self):
if self.layer.tree_url():
return os.path.join(self.layer.tree_url(), self.filepath, self.filename)
else:
return ''
url = self.layer.file_url(os.path.join(self.filepath, self.filename))
return url or ''
def full_path(self):
return os.path.join(self.filepath, self.filename)