svn.py 6.5 KB

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