fetch2/osc: Add support to query latest revision

Message ID 20220519183403.468179-1-viraniac@gmail.com
State Accepted, archived
Commit aa4cee1bb7415c498e4dc6af4dbb3d0c841faf2e
Headers show
Series fetch2/osc: Add support to query latest revision | expand

Commit Message

Gunjan Gupta May 19, 2022, 6:34 p.m. UTC
Add support to query latest revision. This makes it possble to use
osc fetcher without specifying the rev parameter.

Signed-off-by: Gunjan Gupta <viraniac@gmail.com>
---
 lib/bb/fetch2/osc.py | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

Comments

Martin Jansa May 22, 2022, 6:58 a.m. UTC | #1
On Thu, May 19, 2022 at 8:36 PM Gunjan Gupta <viraniac@gmail.com> wrote:

> Add support to query latest revision. This makes it possble to use
> osc fetcher without specifying the rev parameter.
>
> Signed-off-by: Gunjan Gupta <viraniac@gmail.com>
> ---
>  lib/bb/fetch2/osc.py | 34 +++++++++++++++++++++++++++++-----
>  1 file changed, 29 insertions(+), 5 deletions(-)
>
> diff --git a/lib/bb/fetch2/osc.py b/lib/bb/fetch2/osc.py
> index eb0f82c8..dd02f037 100644
> --- a/lib/bb/fetch2/osc.py
> +++ b/lib/bb/fetch2/osc.py
> ....



+    def _latest_revision(self, ud, d, name):
> +        """
> +        Fetch latest revision for the given package
> +        """
> +        api_source_cmd = self._buildosccommand(ud, d, "api_source")
> +
> +        output = runfetchcmd(api_source_cmd, d)
> +        match = re.match('<directory ?.* rev="(\d+)".*>', output)
> +        if match is None:
> +            raise FetchError("Unable to parse osc response", ud.url)
> +        return match.groups()[0]
>
...

This causes new warning with new python:
bitbake/lib/bb/fetch2/osc.py:93: DeprecationWarning: invalid escape
sequence '\d'
  match = re.match('<directory ?.* rev="(\d+)".*>', output)

Will send a fix.

Patch

diff --git a/lib/bb/fetch2/osc.py b/lib/bb/fetch2/osc.py
index eb0f82c8..dd02f037 100644
--- a/lib/bb/fetch2/osc.py
+++ b/lib/bb/fetch2/osc.py
@@ -9,6 +9,7 @@  Based on the svn "Fetch" implementation.
 
 import logging
 import os
+import re
 import  bb
 from    bb.fetch2 import FetchMethod
 from    bb.fetch2 import FetchError
@@ -60,26 +61,49 @@  class Osc(FetchMethod):
 
         basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
 
-        proto = ud.parm.get('protocol', 'ocs')
+        proto = ud.parm.get('protocol', 'https')
 
         options = []
 
         config = "-c %s" % self.generate_config(ud, d)
 
-        if ud.revision:
+        if getattr(ud, 'revision', ''):
             options.append("-r %s" % ud.revision)
 
         coroot = self._strip_leading_slashes(ud.path)
 
         if command == "fetch":
-            osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
+            osccmd = "%s %s -A %s://%s co %s/%s %s" % (basecmd, config, proto, ud.host, coroot, ud.module, " ".join(options))
         elif command == "update":
-            osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
+            osccmd = "%s %s -A %s://%s up %s" % (basecmd, config, proto, ud.host, " ".join(options))
+        elif command == "api_source":
+            osccmd = "%s %s -A %s://%s api source/%s/%s" % (basecmd, config, proto, ud.host, coroot, ud.module)
         else:
             raise FetchError("Invalid osc command %s" % command, ud.url)
 
         return osccmd
 
+    def _latest_revision(self, ud, d, name):
+        """
+        Fetch latest revision for the given package
+        """
+        api_source_cmd = self._buildosccommand(ud, d, "api_source")
+
+        output = runfetchcmd(api_source_cmd, d)
+        match = re.match('<directory ?.* rev="(\d+)".*>', output)
+        if match is None:
+            raise FetchError("Unable to parse osc response", ud.url)
+        return match.groups()[0]
+
+    def _revision_key(self, ud, d, name):
+        """
+        Return a unique key for the url
+        """
+        # Collapse adjacent slashes
+        slash_re = re.compile(r"/+")
+        rev = getattr(ud, 'revision', "latest")
+        return "osc:%s%s.%s.%s" % (ud.host, slash_re.sub(".", ud.path), name, rev)
+
     def download(self, ud, d):
         """
         Fetch url
@@ -123,7 +147,7 @@  class Osc(FetchMethod):
             os.remove(config_path)
 
         f = open(config_path, 'w')
-        proto = ud.parm.get('proto', 'https')
+        proto = ud.parm.get('protocol', 'https')
         f.write("[general]\n")
         f.write("apiurl = %s://%s\n" % (proto, ud.host))
         f.write("su-wrapper = su -c\n")