osc.py 4.3 KB

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