osc.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. Bitbake "Fetch" implementation for osc (Opensuse build service client).
  5. Based on the svn "Fetch" implementation.
  6. """
  7. import os
  8. import sys
  9. import logging
  10. import bb
  11. from bb import data
  12. from bb.fetch2 import Fetch
  13. from bb.fetch2 import FetchError
  14. from bb.fetch2 import MissingParameterError
  15. from bb.fetch2 import runfetchcmd
  16. class Osc(Fetch):
  17. """Class to fetch a module or modules from Opensuse build server
  18. repositories."""
  19. def supports(self, url, 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("osc method needs a 'module' parameter.")
  27. ud.module = ud.parm["module"]
  28. # Create paths to osc checkouts
  29. relpath = self._strip_leading_slashes(ud.path)
  30. ud.pkgdir = os.path.join(data.expand('${OSCDIR}', d), ud.host)
  31. ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
  32. if 'rev' in ud.parm:
  33. ud.revision = ud.parm['rev']
  34. else:
  35. pv = data.getVar("PV", d, 0)
  36. rev = Fetch.srcrev_internal_helper(ud, d)
  37. if rev and rev != True:
  38. ud.revision = rev
  39. else:
  40. ud.revision = ""
  41. ud.localfile = data.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision), d)
  42. def localpath(self, url, ud, d):
  43. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  44. def _buildosccommand(self, ud, d, command):
  45. """
  46. Build up an ocs commandline based on ud
  47. command is "fetch", "update", "info"
  48. """
  49. basecmd = data.expand('${FETCHCMD_osc}', d)
  50. proto = ud.parm.get('proto', 'ocs')
  51. options = []
  52. config = "-c %s" % self.generate_config(ud, d)
  53. if ud.revision:
  54. options.append("-r %s" % ud.revision)
  55. coroot = self._strip_leading_slashes(ud.path)
  56. if command is "fetch":
  57. osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
  58. elif command is "update":
  59. osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
  60. else:
  61. raise FetchError("Invalid osc command %s" % command)
  62. return osccmd
  63. def download(self, loc, ud, d):
  64. """
  65. Fetch url
  66. """
  67. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  68. if os.access(os.path.join(data.expand('${OSCDIR}', d), ud.path, ud.module), os.R_OK):
  69. oscupdatecmd = self._buildosccommand(ud, d, "update")
  70. logger.info("Update "+ loc)
  71. # update sources there
  72. os.chdir(ud.moddir)
  73. logger.debug(1, "Running %s", oscupdatecmd)
  74. bb.fetch2.check_network_access(d, oscupdatecmd)
  75. runfetchcmd(oscupdatecmd, d)
  76. else:
  77. oscfetchcmd = self._buildosccommand(ud, d, "fetch")
  78. logger.info("Fetch " + loc)
  79. # check out sources there
  80. bb.mkdirhier(ud.pkgdir)
  81. os.chdir(ud.pkgdir)
  82. logger.debug(1, "Running %s", oscfetchcmd)
  83. bb.fetch2.check_network_access(d, oscfetchcmd)
  84. runfetchcmd(oscfetchcmd, d)
  85. os.chdir(os.path.join(ud.pkgdir + ud.path))
  86. # tar them up to a defined filename
  87. try:
  88. runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d)
  89. except:
  90. t, v, tb = sys.exc_info()
  91. try:
  92. os.unlink(ud.localpath)
  93. except OSError:
  94. pass
  95. raise t, v, tb
  96. def supports_srcrev(self):
  97. return False
  98. def generate_config(self, ud, d):
  99. """
  100. Generate a .oscrc to be used for this run.
  101. """
  102. config_path = os.path.join(data.expand('${OSCDIR}', d), "oscrc")
  103. if (os.path.exists(config_path)):
  104. os.remove(config_path)
  105. f = open(config_path, 'w')
  106. f.write("[general]\n")
  107. f.write("apisrv = %s\n" % ud.host)
  108. f.write("scheme = http\n")
  109. f.write("su-wrapper = su -c\n")
  110. f.write("build-root = %s\n" % data.expand('${WORKDIR}', d))
  111. f.write("urllist = http://moblin-obs.jf.intel.com:8888/build/%(project)s/%(repository)s/%(buildarch)s/:full/%(name)s.rpm\n")
  112. f.write("extra-pkgs = gzip\n")
  113. f.write("\n")
  114. f.write("[%s]\n" % ud.host)
  115. f.write("user = %s\n" % ud.parm["user"])
  116. f.write("pass = %s\n" % ud.parm["pswd"])
  117. f.close()
  118. return config_path