From 4ec65db3c1730778ae3212fc713918e9919a1da6 Mon Sep 17 00:00:00 2001
From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Date: Wed, 17 Nov 2010 14:44:40 +0100
Subject: [PATCH 2/4] fetch: add common helper _strip_leading_slashes()

Several fetcher need a way to strip leading slashes off a local path.
This helper-function consolidates all such occurances.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
---
 lib/bb/fetch/__init__.py |    9 ++++++++-
 lib/bb/fetch/bzr.py      |    5 +----
 lib/bb/fetch/hg.py       |    6 ++----
 lib/bb/fetch/osc.py      |   10 ++--------
 lib/bb/fetch/perforce.py |    3 +--
 lib/bb/fetch/svn.py      |    5 +----
 6 files changed, 15 insertions(+), 23 deletions(-)

diff --git a/lib/bb/fetch/__init__.py b/lib/bb/fetch/__init__.py
index 0d0fa35..170bbde 100644
--- a/lib/bb/fetch/__init__.py
+++ b/lib/bb/fetch/__init__.py
@@ -26,7 +26,7 @@ BitBake build tools.
 
 from __future__ import absolute_import
 from __future__ import print_function
-import os, re
+import os, os.path, re
 import logging
 import bb
 from   bb import data
@@ -545,6 +545,13 @@ class Fetch(object):
         and duplicate code execution)
         """
         return url
+    def _strip_leading_slashes(self, relpath):
+        """
+        Remove leading slash as os.path.join can't cope
+        """
+        while os.path.isabs(relpath):
+            relpath = relpath[1:]
+        return relpath
 
     def setUrls(self, urls):
         self.__urls = urls
diff --git a/lib/bb/fetch/bzr.py b/lib/bb/fetch/bzr.py
index 4cd51cb..92fff74 100644
--- a/lib/bb/fetch/bzr.py
+++ b/lib/bb/fetch/bzr.py
@@ -37,10 +37,7 @@ class Bzr(Fetch):
     def localpath (self, url, ud, d):
 
         # Create paths to bzr checkouts
-        relpath = ud.path
-        if relpath.startswith('/'):
-            # Remove leading slash as os.path.join can't cope
-            relpath = relpath[1:]
+        relpath = self._strip_leading_slashes(ud.path)
         ud.pkgdir = os.path.join(data.expand('${BZRDIR}', d), ud.host, relpath)
 
         revision = Fetch.srcrev_internal_helper(ud, d)
diff --git a/lib/bb/fetch/hg.py b/lib/bb/fetch/hg.py
index 264a52d..86c58f8 100644
--- a/lib/bb/fetch/hg.py
+++ b/lib/bb/fetch/hg.py
@@ -25,6 +25,7 @@ BitBake 'Fetch' implementation for mercurial DRCS (hg).
 # Based on functions from the base bb module, Copyright 2003 Holger Schurig
 
 import os
+import os.path
 import sys
 import logging
 import bb
@@ -57,10 +58,7 @@ class Hg(Fetch):
         ud.module = ud.parm["module"]
 
         # Create paths to mercurial checkouts
-        relpath = ud.path
-        if relpath.startswith('/'):
-            # Remove leading slash as os.path.join can't cope
-            relpath = relpath[1:]
+        relpath = self._strip_leading_slashes(ud.path)
         ud.pkgdir = os.path.join(data.expand('${HGDIR}', d), ud.host, relpath)
         ud.moddir = os.path.join(ud.pkgdir, ud.module)
 
diff --git a/lib/bb/fetch/osc.py b/lib/bb/fetch/osc.py
index 6fcb344..a32d0b0 100644
--- a/lib/bb/fetch/osc.py
+++ b/lib/bb/fetch/osc.py
@@ -33,10 +33,7 @@ class Osc(Fetch):
         ud.module = ud.parm["module"]
 
         # Create paths to osc checkouts
-        relpath = ud.path
-        if relpath.startswith('/'):
-            # Remove leading slash as os.path.join can't cope
-            relpath = relpath[1:]
+        relpath = self._strip_leading_slashes(ud.path)
         ud.pkgdir = os.path.join(data.expand('${OSCDIR}', d), ud.host)
         ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
 
@@ -73,10 +70,7 @@ class Osc(Fetch):
         if ud.revision:
             options.append("-r %s" % ud.revision)
 
-        coroot = ud.path
-        if coroot.startswith('/'):
-            # Remove leading slash as os.path.join can't cope
-            coroot= coroot[1:]
+        coroot = self._strip_leading_slashes(ud.path)
 
         if command is "fetch":
             osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
diff --git a/lib/bb/fetch/perforce.py b/lib/bb/fetch/perforce.py
index 6f68d85..bdd23de 100644
--- a/lib/bb/fetch/perforce.py
+++ b/lib/bb/fetch/perforce.py
@@ -113,8 +113,7 @@ class Perforce(Fetch):
         if which != -1:
             base = path[:which]
 
-        if base[0] == "/":
-            base = base[1:]
+        base = self._strip_leading_slashes(base)
 
         cset = Perforce.getcset(d, path, host, user, pswd, parm)
 
diff --git a/lib/bb/fetch/svn.py b/lib/bb/fetch/svn.py
index 538b4c2..c46ace4 100644
--- a/lib/bb/fetch/svn.py
+++ b/lib/bb/fetch/svn.py
@@ -49,10 +49,7 @@ class Svn(Fetch):
         ud.module = ud.parm["module"]
 
         # Create paths to svn checkouts
-        relpath = ud.path
-        if relpath.startswith('/'):
-            # Remove leading slash as os.path.join can't cope
-            relpath = relpath[1:]
+        relpath = self._strip_leading_slashes(ud.path)
         ud.pkgdir = os.path.join(data.expand('${SVNDIR}', d), ud.host, relpath)
         ud.moddir = os.path.join(ud.pkgdir, ud.module)
 
-- 
1.7.2.3

