git.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. """
  5. BitBake 'Fetch' git implementation
  6. """
  7. #Copyright (C) 2005 Richard Purdie
  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. import os, re
  22. import bb
  23. from bb import data
  24. from bb.fetch import Fetch
  25. from bb.fetch import FetchError
  26. def prunedir(topdir):
  27. # Delete everything reachable from the directory named in 'topdir'.
  28. # CAUTION: This is dangerous!
  29. for root, dirs, files in os.walk(topdir, topdown=False):
  30. for name in files:
  31. os.remove(os.path.join(root, name))
  32. for name in dirs:
  33. os.rmdir(os.path.join(root, name))
  34. def rungitcmd(cmd,d):
  35. bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % cmd)
  36. # Need to export PATH as git is likely to be in metadata paths
  37. # rather than host provided
  38. pathcmd = 'export PATH=%s; %s' % (data.expand('${PATH}', d), cmd)
  39. myret = os.system(pathcmd)
  40. if myret != 0:
  41. raise FetchError("Git: %s failed" % pathcmd)
  42. class Git(Fetch):
  43. """Class to fetch a module or modules from git repositories"""
  44. def supports(self, url, ud, d):
  45. """
  46. Check to see if a given url can be fetched with git.
  47. """
  48. return ud.type in ['git']
  49. def localpath(self, url, ud, d):
  50. ud.proto = "rsync"
  51. if 'protocol' in ud.parm:
  52. ud.proto = ud.parm['protocol']
  53. ud.tag = "master"
  54. if 'tag' in ud.parm:
  55. ud.tag = ud.parm['tag']
  56. ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.tag), d)
  57. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  58. def forcefetch(self, url, ud, d):
  59. # tag=="master" must always update
  60. if (ud.tag == "master"):
  61. return True
  62. return False
  63. def go(self, loc, ud, d):
  64. """Fetch url"""
  65. if not self.forcefetch(loc, ud, d) and Fetch.try_mirror(d, ud.localfile):
  66. bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % ud.localpath)
  67. return
  68. gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.'))
  69. repofilename = 'git_%s.tar.gz' % (gitsrcname)
  70. repofile = os.path.join(data.getVar("DL_DIR", d, 1), repofilename)
  71. repodir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
  72. coname = '%s' % (ud.tag)
  73. codir = os.path.join(repodir, coname)
  74. if not os.path.exists(repodir):
  75. if Fetch.try_mirror(d, repofilename):
  76. bb.mkdirhier(repodir)
  77. os.chdir(repodir)
  78. rungitcmd("tar -xzf %s" % (repofile),d)
  79. else:
  80. rungitcmd("git clone -n %s://%s%s %s" % (ud.proto, ud.host, ud.path, repodir),d)
  81. os.chdir(repodir)
  82. rungitcmd("git pull %s://%s%s" % (ud.proto, ud.host, ud.path),d)
  83. rungitcmd("git pull --tags %s://%s%s" % (ud.proto, ud.host, ud.path),d)
  84. rungitcmd("git prune-packed", d)
  85. rungitcmd("git pack-redundant --all | xargs -r rm", d)
  86. # Remove all but the .git directory
  87. rungitcmd("rm * -Rf", d)
  88. # old method of downloading tags
  89. #rungitcmd("rsync -a --verbose --stats --progress rsync://%s%s/ %s" % (ud.host, ud.path, os.path.join(repodir, ".git", "")),d)
  90. os.chdir(repodir)
  91. bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
  92. rungitcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
  93. if os.path.exists(codir):
  94. prunedir(codir)
  95. bb.mkdirhier(codir)
  96. os.chdir(repodir)
  97. rungitcmd("git read-tree %s" % (ud.tag),d)
  98. rungitcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")),d)
  99. os.chdir(codir)
  100. bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
  101. rungitcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ),d)