osc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. """
  5. Bitbake "Fetch" implementation for osc (Opensuse build service client).
  6. Based on the svn "Fetch" implementation.
  7. """
  8. import logging
  9. import bb
  10. from bb.fetch2 import FetchMethod
  11. from bb.fetch2 import FetchError
  12. from bb.fetch2 import MissingParameterError
  13. from bb.fetch2 import runfetchcmd
  14. class Osc(FetchMethod):
  15. """Class to fetch a module or modules from Opensuse build server
  16. repositories."""
  17. def supports(self, ud, d):
  18. """
  19. Check to see if a given url can be fetched with osc.
  20. """
  21. return ud.type in ['osc']
  22. def urldata_init(self, ud, d):
  23. if not "module" in ud.parm:
  24. raise MissingParameterError('module', ud.url)
  25. ud.module = ud.parm["module"]
  26. # Create paths to osc checkouts
  27. oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
  28. relpath = self._strip_leading_slashes(ud.path)
  29. ud.pkgdir = os.path.join(oscdir, ud.host)
  30. ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
  31. if 'rev' in ud.parm:
  32. ud.revision = ud.parm['rev']
  33. else:
  34. pv = d.getVar("PV", False)
  35. rev = bb.fetch2.srcrev_internal_helper(ud, d)
  36. if rev:
  37. ud.revision = rev
  38. else:
  39. ud.revision = ""
  40. ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision))
  41. def _buildosccommand(self, ud, d, command):
  42. """
  43. Build up an ocs commandline based on ud
  44. command is "fetch", "update", "info"
  45. """
  46. basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
  47. proto = ud.parm.get('protocol', 'ocs')
  48. options = []
  49. config = "-c %s" % self.generate_config(ud, d)
  50. if ud.revision:
  51. options.append("-r %s" % ud.revision)
  52. coroot = self._strip_leading_slashes(ud.path)
  53. if command == "fetch":
  54. osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
  55. elif command == "update":
  56. osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
  57. else:
  58. raise FetchError("Invalid osc command %s" % command, ud.url)
  59. return osccmd
  60. def download(self, ud, d):
  61. """
  62. Fetch url
  63. """
  64. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  65. if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK):
  66. oscupdatecmd = self._buildosccommand(ud, d, "update")
  67. logger.info("Update "+ ud.url)
  68. # update sources there
  69. logger.debug(1, "Running %s", oscupdatecmd)
  70. bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
  71. runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
  72. else:
  73. oscfetchcmd = self._buildosccommand(ud, d, "fetch")
  74. logger.info("Fetch " + ud.url)
  75. # check out sources there
  76. bb.utils.mkdirhier(ud.pkgdir)
  77. logger.debug(1, "Running %s", oscfetchcmd)
  78. bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
  79. runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
  80. # tar them up to a defined filename
  81. runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
  82. cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
  83. def supports_srcrev(self):
  84. return False
  85. def generate_config(self, ud, d):
  86. """
  87. Generate a .oscrc to be used for this run.
  88. """
  89. config_path = os.path.join(d.getVar('OSCDIR'), "oscrc")
  90. if (os.path.exists(config_path)):
  91. os.remove(config_path)
  92. f = open(config_path, 'w')
  93. f.write("[general]\n")
  94. f.write("apisrv = %s\n" % ud.host)
  95. f.write("scheme = http\n")
  96. f.write("su-wrapper = su -c\n")
  97. f.write("build-root = %s\n" % d.getVar('WORKDIR'))
  98. f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
  99. f.write("extra-pkgs = gzip\n")
  100. f.write("\n")
  101. f.write("[%s]\n" % ud.host)
  102. f.write("user = %s\n" % ud.parm["user"])
  103. f.write("pass = %s\n" % ud.parm["pswd"])
  104. f.close()
  105. return config_path