/SparkleShare/Nautilus/sparkleshare-nautilus-extension.py.in

http://github.com/hbons/SparkleShare · Autoconf · 173 lines · 93 code · 54 blank · 26 comment · 10 complexity · ccfa527844ecef7265556c26bed42290 MD5 · raw file

  1. #!/usr/bin/python
  2. # SparkleShare, an instant update workflow to Git.
  3. # Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import os
  18. import shutil
  19. import time
  20. import gio
  21. import nautilus
  22. import pygtk
  23. pygtk.require('2.0')
  24. import gtk
  25. SPARKLESHARE_PATH = os.path.join (os.path.expanduser ('~'), "SparkleShare")
  26. import gettext
  27. gettext.bindtextdomain('sparkleshare', '@prefix@/share/locale')
  28. gettext.textdomain('sparkleshare')
  29. _ = gettext.gettext
  30. class SparkleShareExtension (nautilus.MenuProvider):
  31. def __init__ (self):
  32. debug = "Loaded Nautilus SparkleShare Extension."
  33. def checkout_version (self, menu, file_reference, commit_hash, username, timestamp):
  34. file_name = file_reference.get_basename ().replace (" ", "\ ").replace ("(", "\(").replace (")", "\)")
  35. file_path = file_reference.get_path ().replace (" ", "\ ").replace ("(", "\(").replace (")", "\)")
  36. tmp_file_path = os.path.join (SPARKLESHARE_PATH, ".tmp", file_reference.get_basename ())
  37. # Move the current version to a temporary path
  38. shutil.move (file_reference.get_path (), tmp_file_path)
  39. # Check out the earlier version
  40. os.chdir (file_reference.get_parent ().get_path ())
  41. os.popen ("git checkout " + commit_hash + " " + file_name
  42. .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
  43. new_tmp_file_name = file_name + " (" + username + ", "
  44. new_tmp_file_name += time.strftime ("%H:%M %d %b %Y", timestamp).replace (" 0", " ") + ") "
  45. # Rename the checked out file
  46. shutil.move (file_name, new_tmp_file_name)
  47. # Move the original file back
  48. shutil.move (tmp_file_path, file_path)
  49. return True
  50. def copy_web_link (self, menu, file_reference):
  51. path = file_reference.get_path ()
  52. # Get the remote url used for the repo
  53. url_command = os.popen ("git config --get remote.origin.url")
  54. url = url_command.readline ().strip ()
  55. # Strip the unneeded parts
  56. url = url.lstrip ("ssh://git")
  57. url = url.lstrip ("@")
  58. url = url.rstrip (".git")
  59. # Format the right web url depending on the service
  60. relative_path = path.lstrip (SPARKLESHARE_PATH)
  61. repo_name = relative_path [:relative_path.find ("/")]
  62. relative_path = relative_path.lstrip (repo_name)
  63. if "gitorious.org" in url:
  64. url = "http://" + url + "/blobs/master" + relative_path
  65. if "github.com" in url:
  66. url = "http://" + url + "/raw/master" + relative_path
  67. url = url.replace (" ", "%20");
  68. clipboard = gtk.clipboard_get ()
  69. clipboard.set_text (url)
  70. clipboard.store ()
  71. return
  72. def get_file_items (self, window, files):
  73. # Only work if one file is selected
  74. if len (files) != 1:
  75. return
  76. file_reference = gio.File (files [0].get_uri ())
  77. # Only work if we're in a SparkleShare repository folder
  78. if not (file_reference.get_path ().startswith (SPARKLESHARE_PATH)):
  79. return
  80. web_link_menu_item = nautilus.MenuItem ("Nautilus::CopyWebLink", _("Copy Web Link"),
  81. _("Copy the web address of this file to the clipboard"))
  82. web_link_menu_item.connect ("activate", self.copy_web_link, file_reference)
  83. epochs = ["", "", "", "", "", "", "", "", "", ""]
  84. commit_hashes = ["", "", "", "", "", "", "", "", "", ""]
  85. os.chdir (file_reference.get_parent ().get_path ())
  86. time_command = os.popen ("git log -10 --format='%at' " + file_reference.get_basename ()
  87. .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
  88. author_command = os.popen ("git log -10 --format='%an' " + file_reference.get_basename ()
  89. .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
  90. hash_command = os.popen ("git log -10 --format='%H' " + file_reference.get_basename ()
  91. .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
  92. i = 0
  93. for line in time_command.readlines ():
  94. epochs [i] = line.strip ("\n")
  95. i += 1
  96. # Only work if there is history
  97. if i < 2:
  98. return web_link_menu_item,
  99. i = 0
  100. for line in hash_command.readlines ():
  101. commit_hashes [i] = line.strip ("\n")
  102. i += 1
  103. earlier_version_menu_item = nautilus.MenuItem ("Nautilus::OpenOlderVersion", _("Get Earlier Version"),
  104. _("Make a copy of an earlier version in this folder"))
  105. submenu = nautilus.Menu ()
  106. i = 0
  107. for line in author_command.readlines ():
  108. if i > 0:
  109. timestamp = time.strftime ("%d %b\t%H:%M", time.localtime (float (epochs [i])))
  110. username = line.strip ("\n")
  111. menu_item = nautilus.MenuItem ("Nautilus::Version" + epochs [i],
  112. timestamp + "\t" + username,
  113. _("Select to get a copy of this version"))
  114. menu_item.connect ("activate", self.checkout_version, file_reference, commit_hashes [i],
  115. username, time.localtime (float (epochs [i])))
  116. submenu.append_item (menu_item)
  117. i += 1
  118. earlier_version_menu_item.set_submenu (submenu)
  119. return earlier_version_menu_item, web_link_menu_item