svn.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, re
  24. import sys
  25. import bb
  26. from bb import data
  27. from bb.fetch import Fetch
  28. from bb.fetch import FetchError
  29. from bb.fetch import MissingParameterError
  30. from bb.fetch import runfetchcmd
  31. class Svn(Fetch):
  32. """Class to fetch a module or modules from svn repositories"""
  33. def supports(self, url, ud, d):
  34. """
  35. Check to see if a given url can be fetched with svn.
  36. """
  37. return ud.type in ['svn']
  38. def localpath(self, url, ud, d):
  39. if not "module" in ud.parm:
  40. raise MissingParameterError("svn method needs a 'module' parameter")
  41. ud.module = ud.parm["module"]
  42. # Create paths to svn checkouts
  43. relpath = ud.path
  44. if relpath.startswith('/'):
  45. # Remove leading slash as os.path.join can't cope
  46. relpath = relpath[1:]
  47. ud.pkgdir = os.path.join(data.expand('${SVNDIR}', d), ud.host, relpath)
  48. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  49. if 'rev' in ud.parm:
  50. ud.date = ""
  51. ud.revision = ud.parm['rev']
  52. elif 'date' in ud.date:
  53. ud.date = ud.parm['date']
  54. ud.revision = ""
  55. else:
  56. #
  57. # ***Nasty hacks***
  58. # If DATE in unexpanded PV, use ud.date (which is set from SRCDATE)
  59. # Will warn people to switch to SRCREV here
  60. #
  61. # How can we tell when a user has overriden SRCDATE?
  62. # check for "get_srcdate" in unexpanded SRCREV - ugly
  63. #
  64. pv = data.getVar("PV", d, 0)
  65. if "DATE" in pv:
  66. ud.revision = ""
  67. else:
  68. rev = data.getVar("SRCREV", d, 0)
  69. if rev and "get_srcrev" in rev:
  70. ud.revision = self.latest_revision(url, ud, d)
  71. ud.date = ""
  72. elif rev:
  73. ud.revision = rev
  74. ud.date = ""
  75. else:
  76. ud.revision = ""
  77. ud.localfile = data.expand('%s_%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision, ud.date), d)
  78. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  79. def _buildsvncommand(self, ud, d, command):
  80. """
  81. Build up an svn commandline based on ud
  82. command is "fetch", "update", "info"
  83. """
  84. basecmd = data.expand('${FETCHCMD_svn}', d)
  85. proto = "svn"
  86. if "proto" in ud.parm:
  87. proto = ud.parm["proto"]
  88. svn_rsh = None
  89. if proto == "svn+ssh" and "rsh" in ud.parm:
  90. svn_rsh = ud.parm["rsh"]
  91. svnroot = ud.host + ud.path
  92. # either use the revision, or SRCDATE in braces,
  93. options = []
  94. if ud.user:
  95. options.append("--username %s" % ud.user)
  96. if ud.pswd:
  97. options.append("--password %s" % ud.pswd)
  98. if command is "info":
  99. svncmd = "%s info %s %s://%s/%s/" % (basecmd, " ".join(options), proto, svnroot, ud.module)
  100. else:
  101. if ud.revision:
  102. options.append("-r %s" % ud.revision)
  103. elif ud.date:
  104. options.append("-r {%s}" % ud.date)
  105. if command is "fetch":
  106. svncmd = "%s co %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, svnroot, ud.module, ud.module)
  107. elif command is "update":
  108. svncmd = "%s update %s" % (basecmd, " ".join(options))
  109. else:
  110. raise FetchError("Invalid svn command %s" % command)
  111. if svn_rsh:
  112. svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
  113. return svncmd
  114. def go(self, loc, ud, d):
  115. """Fetch url"""
  116. # try to use the tarball stash
  117. if Fetch.try_mirror(d, ud.localfile):
  118. bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists or was mirrored, skipping svn checkout." % ud.localpath)
  119. return
  120. bb.msg.debug(2, bb.msg.domain.Fetcher, "Fetch: checking for module directory '" + ud.moddir + "'")
  121. if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK):
  122. svnupdatecmd = self._buildsvncommand(ud, d, "update")
  123. bb.msg.note(1, bb.msg.domain.Fetcher, "Update " + loc)
  124. # update sources there
  125. os.chdir(ud.moddir)
  126. bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svnupdatecmd)
  127. runfetchcmd(svnupdatecmd, d)
  128. else:
  129. svnfetchcmd = self._buildsvncommand(ud, d, "fetch")
  130. bb.msg.note(1, bb.msg.domain.Fetcher, "Fetch " + loc)
  131. # check out sources there
  132. bb.mkdirhier(ud.pkgdir)
  133. os.chdir(ud.pkgdir)
  134. bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % svnfetchcmd)
  135. runfetchcmd(svnfetchcmd, d)
  136. os.chdir(ud.pkgdir)
  137. # tar them up to a defined filename
  138. try:
  139. runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d)
  140. except:
  141. t, v, tb = sys.exc_info()
  142. try:
  143. os.unlink(ud.localpath)
  144. except OSError:
  145. pass
  146. raise t, v, tb
  147. def suppports_srcrev(self):
  148. return True
  149. def _revision_key(self, url, ud, d):
  150. """
  151. Return a unique key for the url
  152. """
  153. return "svn:" + ud.moddir
  154. def _latest_revision(self, url, ud, d):
  155. """
  156. Return the latest upstream revision number
  157. """
  158. bb.msg.debug(2, bb.msg.domain.Fetcher, "SVN fetcher hitting network for %s" % url)
  159. output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "info"), d, True)
  160. revision = None
  161. for line in output.splitlines():
  162. if "Last Changed Rev" in line:
  163. revision = line.split(":")[1].strip()
  164. return revision
  165. def _sortable_revision(self, url, ud, d):
  166. """
  167. Return a sortable revision number which in our case is the revision number
  168. (use the cached version to avoid network access)
  169. """
  170. return self.latest_revision(url, ud, d)