Skip to content
Newer
Older
100644 133 lines (103 sloc) 4.26 KB
899b680 initial commit
Hilary Mason authored Dec 17, 2010
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 gitmark.py
5
6 Created by Hilary Mason on 2010-09-24.
7 Copyright (c) 2010 Hilary Mason. All rights reserved.
8 """
9
10 import sys, os
11 import urllib
12 import re
13 import hashlib
14 import csv
15 import subprocess
16 from optparse import OptionParser
17
18 from settings import *
19
d8f9b8b @ashearer Restore Windows compatibility, which my shell escaping fix would have…
ashearer authored Dec 17, 2010
20 # Arguments are passed directly to git, not through the shell, to avoid the
21 # need for shell escaping. On Windows, however, commands need to go through the
22 # shell for git to be found on the PATH, but escaping is automatic there. So
23 # send git commands through the shell on Windows, and directly everywhere else.
24 USE_SHELL = os.name == 'nt'
25
899b680 initial commit
Hilary Mason authored Dec 17, 2010
26 class gitMark(object):
27
28 def __init__(self, options, args):
29 modified = [] # track files we need to add - a hack, because it will add files that are already tracked by git
30
58fcb59 @cwarden make sure a url is passed as an argument
cwarden authored Dec 22, 2010
31 try:
32 url = args[0].strip('/')
33 except IndexError, e:
34 print >>sys.stderr, ("Error: No url found")
35 return
36
899b680 initial commit
Hilary Mason authored Dec 17, 2010
37 content = self.getContent(url)
38 title = self.parseTitle(content)
39 content_filename = self.generateHash(url)
40
41 modified.append(self.saveContent(content_filename, content))
42 tags = ['all']
43 tags.extend(options['tags'].split(','))
44 for tag in tags:
45 t = tag.strip()
87280c6 deal with odd chars in tags
Hilary Mason authored Dec 17, 2010
46 if not t:
47 continue
48 if '/' in t:
49 t = ''.join(t.split('/'))
899b680 initial commit
Hilary Mason authored Dec 17, 2010
50 modified.append(self.saveTagData(t, url, title, content_filename))
51
52 self.gitAdd(modified)
53
54 commit_msg = options['msg']
55 if not commit_msg:
56 commit_msg = 'adding %s' % url
57
58 self.gitCommit(commit_msg)
59
60 if options['push']:
61 self.gitPush()
62
63 def gitAdd(self, files):
d8f9b8b @ashearer Restore Windows compatibility, which my shell escaping fix would have…
ashearer authored Dec 17, 2010
64 subprocess.call(['git', 'add'] + files, shell=USE_SHELL)
e8dd604 @ashearer Add each list of files in a single git command
ashearer authored Dec 17, 2010
65
899b680 initial commit
Hilary Mason authored Dec 17, 2010
66 def gitCommit(self, msg):
d8f9b8b @ashearer Restore Windows compatibility, which my shell escaping fix would have…
ashearer authored Dec 17, 2010
67 subprocess.call(['git', 'commit', '-m', msg], shell=USE_SHELL)
899b680 initial commit
Hilary Mason authored Dec 17, 2010
68
69 def gitPush(self):
70 pipe = subprocess.Popen("git push origin master", shell=True)
71 pipe.wait()
72
73 def saveContent(self, filename, content):
756fe27 content and tags directories will be created if they don't already ex…
Hilary Mason authored Dec 20, 2010
74 try:
75 f = open('%s%s' % (CONTENT_PATH, filename), 'w')
76 except IOError: #likely the dir doesn't exist
77 os.mkdir(CONTENT_PATH,0755)
78 f = open('%s%s' % (CONTENT_PATH, filename), 'w')
79
899b680 initial commit
Hilary Mason authored Dec 17, 2010
80 f.write(content)
81 f.close()
82 return '%s%s' % (CONTENT_PATH, filename)
83
84 def saveTagData(self, tag, url, title, content_filename):
756fe27 content and tags directories will be created if they don't already ex…
Hilary Mason authored Dec 20, 2010
85 try:
86 tag_writer = csv.writer(open('%s%s' % (TAG_PATH, tag), 'a'))
87 except IOError:
88 os.mkdir(TAG_PATH,0755)
89 tag_writer = csv.writer(open('%s%s' % (TAG_PATH, tag), 'a'))
90
899b680 initial commit
Hilary Mason authored Dec 17, 2010
91 tag_writer.writerow([url, title, content_filename])
92 return '%s%s' % (TAG_PATH, tag)
93
94 def parseTitle(self, content):
95 re_htmltitle = re.compile(".*<title>(.*)</title>.*")
96 t = re_htmltitle.search(content)
97 try:
98 title = t.group(1)
99 except AttributeError:
100 title = '[No Title]'
101
102 return title
103
104 def generateHash(self, text):
105 m = hashlib.md5()
106 m.update(text)
107 return m.hexdigest()
108
109 def getContent(self, url):
91e0667 @ashearer Log an error but save the bookmark and continue if downloading conten…
ashearer authored Dec 17, 2010
110 try:
111 h = urllib.urlopen(url)
112 content = h.read()
113 h.close()
114 except IOError, e:
115 print >>sys.stderr, ("Error: could not retrieve the content of a"
116 " URL. The bookmark will be saved, but its content won't be"
117 " searchable. URL: <%s>. Error: %s" % (url, e))
118 content = ''
899b680 initial commit
Hilary Mason authored Dec 17, 2010
119 return content
120
121
122
123 if __name__ == '__main__':
58fcb59 @cwarden make sure a url is passed as an argument
cwarden authored Dec 22, 2010
124 parser = OptionParser("usage: %prog [options] <url>")
899b680 initial commit
Hilary Mason authored Dec 17, 2010
125 parser.add_option("-p", "--push", dest="push", action="store_false", default=True, help="don't push to origin.")
126 parser.add_option("-t", "--tags", dest="tags", action="store", default='notag', help="comma seperated list of tags")
127 parser.add_option("-m", "--message", dest="msg", action="store", default=None, help="specify a commit message (default is 'adding [url]')")
128 (options, args) = parser.parse_args()
129
130 opts = {'push': options.push, 'tags': options.tags, 'msg': options.msg}
131
0a1a493 @ashearer Fix errors due to quotes or other shell-recognized characters in desc…
ashearer authored Dec 17, 2010
132 g = gitMark(opts, args)
Something went wrong with that request. Please try again.