svn.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import re
  28. from bb import data
  29. from bb.fetch2 import FetchMethod
  30. from bb.fetch2 import FetchError
  31. from bb.fetch2 import MissingParameterError
  32. from bb.fetch2 import runfetchcmd
  33. from bb.fetch2 import logger
  34. class Svn(FetchMethod):
  35. """Class to fetch a module or modules from svn repositories"""
  36. def supports(self, ud, d):
  37. """
  38. Check to see if a given url can be fetched with svn.
  39. """
  40. return ud.type in ['svn']
  41. def urldata_init(self, ud, d):
  42. """
  43. init svn specific variable within url data
  44. """
  45. if not "module" in ud.parm:
  46. raise MissingParameterError('module', ud.url)
  47. ud.basecmd = d.getVar('FETCHCMD_svn', True)
  48. ud.module = ud.parm["module"]
  49. # Create paths to svn checkouts
  50. relpath = self._strip_leading_slashes(ud.path)
  51. ud.pkgdir = os.path.join(data.expand('${SVNDIR}', d), ud.host, relpath)
  52. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  53. ud.setup_revisons(d)
  54. if 'rev' in ud.parm:
  55. ud.revision = ud.parm['rev']
  56. ud.localfile = data.expand('%s_%s_%s_%s_.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision), d)
  57. def _buildsvncommand(self, ud, d, command):
  58. """
  59. Build up an svn commandline based on ud
  60. command is "fetch", "update", "info"
  61. """
  62. proto = ud.parm.get('protocol', 'svn')
  63. svn_rsh = None
  64. if proto == "svn+ssh" and "rsh" in ud.parm:
  65. svn_rsh = ud.parm["rsh"]
  66. svnroot = ud.host + ud.path
  67. options = []
  68. options.append("--no-auth-cache")
  69. if ud.user:
  70. options.append("--username %s" % ud.user)
  71. if ud.pswd:
  72. options.append("--password %s" % ud.pswd)
  73. if command == "info":
  74. svncmd = "%s info %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  75. elif command == "log1":
  76. svncmd = "%s log --limit 1 %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  77. else:
  78. suffix = ""
  79. if ud.revision:
  80. options.append("-r %s" % ud.revision)
  81. suffix = "@%s" % (ud.revision)
  82. if command == "fetch":
  83. svncmd = "%s co %s %s://%s/%s%s %s" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module, suffix, ud.module)
  84. elif command == "update":
  85. svncmd = "%s update %s" % (ud.basecmd, " ".join(options))
  86. else:
  87. raise FetchError("Invalid svn command %s" % command, ud.url)
  88. if svn_rsh:
  89. svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
  90. return svncmd
  91. def download(self, ud, d):
  92. """Fetch url"""
  93. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  94. if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK):
  95. svnupdatecmd = self._buildsvncommand(ud, d, "update")
  96. logger.info("Update " + ud.url)
  97. # update sources there
  98. os.chdir(ud.moddir)
  99. # We need to attempt to run svn upgrade first in case its an older working format
  100. try:
  101. runfetchcmd(ud.basecmd + " upgrade", d)
  102. except FetchError:
  103. pass
  104. logger.debug(1, "Running %s", svnupdatecmd)
  105. bb.fetch2.check_network_access(d, svnupdatecmd, ud.url)
  106. runfetchcmd(svnupdatecmd, d)
  107. else:
  108. svnfetchcmd = self._buildsvncommand(ud, d, "fetch")
  109. logger.info("Fetch " + ud.url)
  110. # check out sources there
  111. bb.utils.mkdirhier(ud.pkgdir)
  112. os.chdir(ud.pkgdir)
  113. logger.debug(1, "Running %s", svnfetchcmd)
  114. bb.fetch2.check_network_access(d, svnfetchcmd, ud.url)
  115. runfetchcmd(svnfetchcmd, d)
  116. scmdata = ud.parm.get("scmdata", "")
  117. if scmdata == "keep":
  118. tar_flags = ""
  119. else:
  120. tar_flags = "--exclude '.svn'"
  121. os.chdir(ud.pkgdir)
  122. # tar them up to a defined filename
  123. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d, cleanup = [ud.localpath])
  124. def clean(self, ud, d):
  125. """ Clean SVN specific files and dirs """
  126. bb.utils.remove(ud.localpath)
  127. bb.utils.remove(ud.moddir, True)
  128. def supports_srcrev(self):
  129. return True
  130. def _revision_key(self, ud, d, name):
  131. """
  132. Return a unique key for the url
  133. """
  134. return "svn:" + ud.moddir
  135. def _latest_revision(self, ud, d, name):
  136. """
  137. Return the latest upstream revision number
  138. """
  139. bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "log1"))
  140. output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "log1"), d, True)
  141. # skip the first line, as per output of svn log
  142. # then we expect the revision on the 2nd line
  143. revision = re.search('^r([0-9]*)', output.splitlines()[1]).group(1)
  144. return revision
  145. def sortable_revision(self, ud, d, name):
  146. """
  147. Return a sortable revision number which in our case is the revision number
  148. """
  149. return False, self._build_revision(ud, d)
  150. def _build_revision(self, ud, d):
  151. return ud.revision