svn.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 svn.
  5. """
  6. # Copyright (C) 2003, 2004 Chris Larson
  7. # Copyright (C) 2004 Marcin Juszkiewicz
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  23. import os
  24. import sys
  25. import logging
  26. import bb
  27. from bb import data
  28. from bb.fetch2 import Fetch
  29. from bb.fetch2 import FetchError
  30. from bb.fetch2 import MissingParameterError
  31. from bb.fetch2 import runfetchcmd
  32. from bb.fetch2 import logger
  33. class Svn(Fetch):
  34. """Class to fetch a module or modules from svn repositories"""
  35. def supports(self, url, ud, d):
  36. """
  37. Check to see if a given url can be fetched with svn.
  38. """
  39. return ud.type in ['svn']
  40. def urldata_init(self, ud, d):
  41. """
  42. init svn specific variable within url data
  43. """
  44. if not "module" in ud.parm:
  45. raise MissingParameterError("svn method needs a 'module' parameter")
  46. ud.module = ud.parm["module"]
  47. # Create paths to svn checkouts
  48. relpath = self._strip_leading_slashes(ud.path)
  49. ud.pkgdir = os.path.join(data.expand('${SVNDIR}', d), ud.host, relpath)
  50. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  51. if 'rev' in ud.parm:
  52. ud.date = ""
  53. ud.revision = ud.parm['rev']
  54. elif 'date' in ud.date:
  55. ud.date = ud.parm['date']
  56. ud.revision = ""
  57. else:
  58. #
  59. # ***Nasty hack***
  60. # If DATE in unexpanded PV, use ud.date (which is set from SRCDATE)
  61. # Should warn people to switch to SRCREV here
  62. #
  63. pv = data.getVar("PV", d, 0)
  64. if "DATE" in pv:
  65. ud.revision = ""
  66. else:
  67. # use the initizlied revision
  68. if ud.revision:
  69. ud.date = ""
  70. ud.localfile = data.expand('%s_%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision, ud.date), d)
  71. def localpath(self, url, ud, d):
  72. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  73. def _buildsvncommand(self, ud, d, command):
  74. """
  75. Build up an svn commandline based on ud
  76. command is "fetch", "update", "info"
  77. """
  78. basecmd = data.expand('${FETCHCMD_svn}', d)
  79. proto = ud.parm.get('proto', 'svn')
  80. svn_rsh = None
  81. if proto == "svn+ssh" and "rsh" in ud.parm:
  82. svn_rsh = ud.parm["rsh"]
  83. svnroot = ud.host + ud.path
  84. # either use the revision, or SRCDATE in braces,
  85. options = []
  86. if ud.user:
  87. options.append("--username %s" % ud.user)
  88. if ud.pswd:
  89. options.append("--password %s" % ud.pswd)
  90. if command is "info":
  91. svncmd = "%s info %s %s://%s/%s/" % (basecmd, " ".join(options), proto, svnroot, ud.module)
  92. else:
  93. suffix = ""
  94. if ud.revision:
  95. options.append("-r %s" % ud.revision)
  96. suffix = "@%s" % (ud.revision)
  97. elif ud.date:
  98. options.append("-r {%s}" % ud.date)
  99. if command is "fetch":
  100. svncmd = "%s co %s %s://%s/%s%s %s" % (basecmd, " ".join(options), proto, svnroot, ud.module, suffix, ud.module)
  101. elif command is "update":
  102. svncmd = "%s update %s" % (basecmd, " ".join(options))
  103. else:
  104. raise FetchError("Invalid svn command %s" % command)
  105. if svn_rsh:
  106. svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
  107. return svncmd
  108. def download(self, loc, ud, d):
  109. """Fetch url"""
  110. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  111. if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK):
  112. svnupdatecmd = self._buildsvncommand(ud, d, "update")
  113. logger.info("Update " + loc)
  114. # update sources there
  115. os.chdir(ud.moddir)
  116. logger.debug(1, "Running %s", svnupdatecmd)
  117. bb.fetch2.check_network_access(d, svnupdatecmd)
  118. runfetchcmd(svnupdatecmd, d)
  119. else:
  120. svnfetchcmd = self._buildsvncommand(ud, d, "fetch")
  121. logger.info("Fetch " + loc)
  122. # check out sources there
  123. bb.mkdirhier(ud.pkgdir)
  124. os.chdir(ud.pkgdir)
  125. logger.debug(1, "Running %s", svnfetchcmd)
  126. bb.fetch2.check_network_access(d, svnfetchcmd)
  127. runfetchcmd(svnfetchcmd, d)
  128. scmdata = ud.parm.get("scmdata", "")
  129. if scmdata == "keep":
  130. tar_flags = ""
  131. else:
  132. tar_flags = "--exclude '.svn'"
  133. os.chdir(ud.pkgdir)
  134. # tar them up to a defined filename
  135. try:
  136. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d)
  137. except:
  138. t, v, tb = sys.exc_info()
  139. try:
  140. os.unlink(ud.localpath)
  141. except OSError:
  142. pass
  143. raise t, v, tb
  144. def supports_srcrev(self):
  145. return True
  146. def _revision_key(self, url, ud, d, name):
  147. """
  148. Return a unique key for the url
  149. """
  150. return "svn:" + ud.moddir
  151. def _latest_revision(self, url, ud, d, name):
  152. """
  153. Return the latest upstream revision number
  154. """
  155. bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "info"))
  156. output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "info"), d, True)
  157. revision = None
  158. for line in output.splitlines():
  159. if "Last Changed Rev" in line:
  160. revision = line.split(":")[1].strip()
  161. return revision
  162. def _sortable_revision(self, url, ud, d):
  163. """
  164. Return a sortable revision number which in our case is the revision number
  165. """
  166. return self._build_revision(url, ud, d)
  167. def _build_revision(self, url, ud, d):
  168. return ud.revision