PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 18import os
 19import shutil
 20import time
 21
 22import gio
 23import nautilus
 24
 25import pygtk
 26pygtk.require('2.0')
 27import gtk
 28
 29SPARKLESHARE_PATH = os.path.join (os.path.expanduser ('~'), "SparkleShare")
 30
 31import gettext
 32gettext.bindtextdomain('sparkleshare', '@prefix@/share/locale')
 33gettext.textdomain('sparkleshare')
 34_ = gettext.gettext
 35
 36class SparkleShareExtension (nautilus.MenuProvider):
 37
 38
 39    def __init__ (self):
 40
 41        debug = "Loaded Nautilus SparkleShare Extension."
 42
 43
 44    def checkout_version (self, menu, file_reference, commit_hash, username, timestamp):
 45
 46        file_name = file_reference.get_basename ().replace (" ", "\ ").replace ("(", "\(").replace (")", "\)")
 47        file_path = file_reference.get_path ().replace (" ", "\ ").replace ("(", "\(").replace (")", "\)")
 48        tmp_file_path = os.path.join (SPARKLESHARE_PATH, ".tmp", file_reference.get_basename ())
 49
 50        # Move the current version to a temporary path
 51        shutil.move (file_reference.get_path (), tmp_file_path)
 52
 53        # Check out the earlier version
 54        os.chdir (file_reference.get_parent ().get_path ())
 55        os.popen ("git checkout " + commit_hash + " " + file_name
 56            .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
 57
 58        new_tmp_file_name = file_name + " (" + username + ", "
 59        new_tmp_file_name += time.strftime ("%H:%M %d %b %Y", timestamp).replace (" 0", " ") + ") "
 60
 61        # Rename the checked out file
 62        shutil.move (file_name, new_tmp_file_name)
 63
 64        # Move the original file back
 65        shutil.move (tmp_file_path, file_path)
 66
 67        return True
 68
 69
 70    def copy_web_link (self, menu, file_reference):
 71
 72        path = file_reference.get_path ()
 73
 74        # Get the remote url used for the repo
 75        url_command = os.popen ("git config --get remote.origin.url")
 76        url = url_command.readline ().strip ()
 77
 78        # Strip the unneeded parts
 79        url = url.lstrip ("ssh://git")
 80        url = url.lstrip ("@")
 81        url = url.rstrip (".git")
 82
 83        # Format the right web url depending on the service
 84        relative_path = path.lstrip (SPARKLESHARE_PATH)
 85        repo_name = relative_path [:relative_path.find ("/")]
 86        relative_path = relative_path.lstrip (repo_name)
 87
 88        if "gitorious.org" in url:
 89	        url = "http://" + url + "/blobs/master" + relative_path
 90        if "github.com" in url:
 91            url = "http://" + url + "/raw/master" + relative_path
 92
 93        url = url.replace (" ", "%20");
 94
 95        clipboard = gtk.clipboard_get ()
 96        clipboard.set_text (url)
 97        clipboard.store ()
 98
 99        return
100
101
102    def get_file_items (self, window, files):
103
104		# Only work if one file is selected
105        if len (files) != 1:
106            return
107
108        file_reference = gio.File (files [0].get_uri ())
109
110		# Only work if we're in a SparkleShare repository folder
111        if not (file_reference.get_path ().startswith (SPARKLESHARE_PATH)):
112            return
113
114        web_link_menu_item = nautilus.MenuItem ("Nautilus::CopyWebLink", _("Copy Web Link"),
115                                                _("Copy the web address of this file to the clipboard"))
116
117        web_link_menu_item.connect ("activate", self.copy_web_link, file_reference)
118
119
120        epochs        = ["", "", "", "", "", "", "", "", "", ""]
121        commit_hashes = ["", "", "", "", "", "", "", "", "", ""]
122
123        os.chdir (file_reference.get_parent ().get_path ())
124
125        time_command   = os.popen ("git log -10 --format='%at' " + file_reference.get_basename ()
126            .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
127
128        author_command = os.popen ("git log -10 --format='%an' " + file_reference.get_basename ()
129            .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
130
131        hash_command = os.popen ("git log -10 --format='%H' " + file_reference.get_basename ()
132            .replace (" ", "\ ").replace ("(", "\(").replace (")", "\)"))
133
134        i = 0
135        for line in time_command.readlines ():
136            epochs [i] = line.strip ("\n")
137            i += 1
138
139        # Only work if there is history
140        if i < 2:
141            return web_link_menu_item,
142
143        i = 0
144        for line in hash_command.readlines ():
145            commit_hashes [i] = line.strip ("\n")
146            i += 1
147
148        earlier_version_menu_item = nautilus.MenuItem ("Nautilus::OpenOlderVersion", _("Get Earlier Version"),
149                                                       _("Make a copy of an earlier version in this folder"))
150        submenu = nautilus.Menu ()
151
152        i = 0
153        for line in author_command.readlines ():
154
155            if i > 0:
156
157                timestamp = time.strftime ("%d %b\t%H:%M", time.localtime (float (epochs [i])))
158                username = line.strip ("\n")
159
160                menu_item = nautilus.MenuItem ("Nautilus::Version" + epochs [i],
161                                           timestamp + "\t" + username,
162                                           _("Select to get a copy of this version"))
163
164                menu_item.connect ("activate", self.checkout_version, file_reference, commit_hashes [i],
165                                   username, time.localtime (float (epochs [i])))
166                submenu.append_item (menu_item)
167
168            i += 1
169
170        earlier_version_menu_item.set_submenu (submenu)
171
172
173        return earlier_version_menu_item, web_link_menu_item