osc.py 4.3 KB

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