A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

107 lines
2.9 KiB

  1. """
  2. Contains classes and utilities that help publishing a hyde website to
  3. distributed version control systems.
  4. """
  5. from hyde._compat import str, with_metaclass
  6. from hyde.publisher import Publisher
  7. import abc
  8. from subprocess import Popen, PIPE
  9. class DVCS(with_metaclass(abc.ABCMeta, Publisher)):
  10. def initialize(self, settings):
  11. self.settings = settings
  12. self.path = self.site.sitepath.child_folder(settings.path)
  13. self.url = settings.url
  14. self.branch = getattr(settings, 'branch', 'master')
  15. self.switch(self.branch)
  16. @abc.abstractmethod
  17. def pull(self):
  18. pass
  19. @abc.abstractmethod
  20. def push(self):
  21. pass
  22. @abc.abstractmethod
  23. def commit(self, message):
  24. pass
  25. @abc.abstractmethod
  26. def switch(self, branch):
  27. pass
  28. @abc.abstractmethod
  29. def add(self, path="."):
  30. pass
  31. @abc.abstractmethod
  32. def merge(self, branch):
  33. pass
  34. def publish(self):
  35. super(DVCS, self).publish()
  36. if not self.path.exists:
  37. raise Exception("The destination repository must exist.")
  38. self.site.config.deploy_root_path.copy_contents_to(self.path)
  39. self.add()
  40. self.commit(self.message)
  41. self.push()
  42. class Git(DVCS):
  43. """
  44. Acts as a publisher to a git repository. Can be used to publish to
  45. github pages.
  46. """
  47. def add(self, path="."):
  48. cmd = Popen('git add --'.split() + [ path ],
  49. cwd=str(self.path), stdout=PIPE)
  50. cmdresult = cmd.communicate()[0]
  51. if cmd.returncode:
  52. raise Exception(cmdresult)
  53. def pull(self):
  54. self.switch(self.branch)
  55. cmd = Popen('git pull origin'.split() + [ self.branch ],
  56. cwd=str(self.path),
  57. stdout=PIPE)
  58. cmdresult = cmd.communicate()[0]
  59. if cmd.returncode:
  60. raise Exception(cmdresult)
  61. def push(self):
  62. cmd = Popen('git push origin'.split() + [ self.branch ],
  63. cwd=str(self.path), stdout=PIPE)
  64. cmdresult = cmd.communicate()[0]
  65. if cmd.returncode:
  66. raise Exception(cmdresult)
  67. def commit(self, message):
  68. cmd = Popen('git commit -a'.split() + [ '-m' + str(message) ],
  69. cwd=str(self.path), stdout=PIPE)
  70. cmdresult = cmd.communicate()[0]
  71. if cmd.returncode:
  72. raise Exception(cmdresult)
  73. def switch(self, branch):
  74. self.branch = branch
  75. cmd = Popen('git checkout'.split() + [ branch ],
  76. cwd=str(self.path), stdout=PIPE)
  77. cmdresult = cmd.communicate()[0]
  78. if cmd.returncode:
  79. raise Exception(cmdresult)
  80. def merge(self, branch):
  81. cmd = Popen('git merge'.split() + [ branch ],
  82. cwd=str(self.path), stdout=PIPE)
  83. cmdresult = cmd.communicate()[0]
  84. if cmd.returncode:
  85. raise Exception(cmdresult)