forked from FarMcKon/gitmarks_2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitmark_add.py
executable file
·279 lines (230 loc) · 9.98 KB
/
gitmark_add.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
# encoding: utf-8
"""
gitmark_add.py
Functions and classes to add data to a local gitmarks directory.
Based on gitmarks by Hilary Mason on 2010-09-24.
Copyright 2010 by Far McKon (intermediate while picking a opensource license)
"""
import sys, os
import urllib
import subprocess
import time
from optparse import OptionParser
import logging
# -- Our own gitmarks settings
import settings
from gitmark import gitMark
from gitmark import USE_SHELL
def canHazWebs():
""" Returns true/false if I can't ping google,
which is used as a 'can I reach the internet?' test """
try:
h = urllib.urlopen("http://google.com")
#todo switch this to a smarter ping system, and use other than google.
data = h.read()
h.close()
return True
except :
logging.error("fail to get google url")
return False
def process_gitmarks_cmd(opts, args):
""" processes a gitmarks command opts is list of options.
args is 1 or more URL's to process. """
# -- each arg is a URL.
for arg in args:
g = gitMark(arg,settings.USER_NAME)
if 'tags' in opts.keys(): g.addTags(opts['tags'])
if 'private' in opts.keys(): g.setPrivacy(opts['private'])
# -- get content, and autogen title
if canHazWebs():
g.getContent()
g.parseTitle()
else:
logging.error("no netz! overriding push to false!")
opts['push'] = False
doPush = opts['push'] if 'push' in opts.keys() else 'False'
updateRepoWith(g, doPush)
def updateRepoWith(gitmarksObj, doPush = True):
""" Update a repository with the passed gitmarksObject. This can also be flagged
to push that update to the remote repository."""
# -- see if we need to add or update the mark
if not isInGitmarkPublicRepo(gitmarksObj):
return addToRepo(gitmarksObj, doPush)
else:
logging.warning("This bookmark is already in our repo. update?")
#TODO: write/run/do system to update gitmark
return updateExistingInRepo(gitmarksObj, doPush)
return -1;
def updateExistingInRepo(gitmarksObj, doPush = True):
""" Updates an existing gitmark file(s) on disk. """
if(gitmarksObj.private != True):
updateToPublicRepo(gitmarksObj, doPush)
updateToPrivateRepo(gitmarksObj, doPush)
def updateToPublicRepo(gitmarksObj, doPush):
""" Updates an existing gitmark file(s) in a public repo. """
#TODO: set this a pep8 private function name
logging.info("HACK: Do we want to push/pull before/after doing this operation?" )
# -- TODO: decide if we want to pull before doing this operation,
# and/or push after doing this operation
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR,
settings.BOOKMARK_SUB_PATH, gitmarksObj.hash)
# -- Check for tags differences
oldMark = gitMark.cls_hydrate(filename)
# -- TODO add new tags
# -- TODO remove old tags
# -- TODO check for description differences
# -- TODO check for content md5 differences
# -- TODO update local content if turned on, and content is different
exit(-3)
def updateToPrivateRepo(gitmarksObj, doPush):
#TODO: set this a pep8 private function name
logging.warning("no such thing to update private repo, encryption not yet installed")
exit(-5)
def addToRepo(gitmarksObj, doPush = True):
""" addToRepo function that does all of the heavy lifting"""
if(gitmarksObj.private != True):
return addToPublicRepo(gitmarksObj, doPush)
logging.info("adding mark %s to private repo" %str(gitmarksObj))
return addToPrivateRepo(gitmarksObj, doPush)
def addToPrivateRepo(gitmarksObj, doPush = True):
#TODO: set this a pep8 private function name
""" add to the public repository """
if gitmarksObj.private != True:
logging.error("this is a public mark. Use 'addToPublicRepo for this")
return -1
# -- add to our public 'bookmarks'
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PRIVATE_GITMARK_REPO_DIR,
settings.BOOKMARK_SUB_PATH, gitmarksObj.hash)
filename = os.path.normpath(filename)
filename = os.path.abspath(filename)
# -get our string
gitmarksObj.setTimeIfEmpty()
bm_string = gitmarksObj.JSONBlock()
gitmarksBaseDir = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR)
fh = open(filename,'a')
if(fh):
# TRICKY:Set the authoring date of the commit based on the imported
# timestamp. git reads the GIT_AUTHOR_DATE environment var.
os.environ['GIT_AUTHOR_DATE'] = gitmarksObj.time
logging.info(bm_string)
fh.write(bm_string)
fh.close()
del fh
gitMark.gitAdd([filename,],gitmarksObj.time,gitmarksBaseDir)
# -- add to each of our our public 'tags' listings
tag_info_string = gitmarksObj.miniJSONBlock()
tagFilesWrittenSuccess = []
for tag in gitmarksObj.everyPossibleTagList():
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR,
settings.TAG_SUB_PATH, tag)
filename = os.path.normpath(filename)
filename = os.path.abspath(filename)
logging.info('tags filename' + str(filename))
fh = open(filename,'a')
if(fh):
fh.write(tag_info_string)
fh.close()
tagFilesWrittenSuccess.append(filename)
del fh
gitMark.gitAdd(tagFilesWrittenSuccess,gitmarksObj.time, gitmarksBaseDir)
# -- if we should get content, go get it and store it locally
if settings.GET_CONTENT and gitmarksObj.noContentSet() == False:
logging.info("get content? Don't mind of I do...")
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.CONTENT_GITMARK_DIR,
settings.HTML_SUB_PATH, gitmarksObj.hash)
#check if we have a cache directory
c_dir = os.path.join(settings.GITMARK_BASE_DIR, settings.CONTENT_GITMARK_DIR,
settings.HTML_SUB_PATH)
if os.path.isdir(c_dir) == False:
subprocess.call(['mkdir','-p',c_dir],shell=USE_SHELL)
gitmarksObj.cacheContent(filename)
#TOOD: do something about committing our changes
logging.info("git commit (local)? Don't mind if i do....")
msg = "auto commit from delicious import beta test %s" %time.strftime("%Y-%m-%dT%H:%M:%SZ")
gitMark.gitCommit(msg, gitmarksBaseDir )
if doPush:
logging.info("git push (external)? Don't mind if i do....")
gitMark.gitPush(gitmarksBaseDir )
def addToPublicRepo(gitmarksObj, doPush = True):
#TODO: set this a pep8 private function name
""" Adds a gitmark to the local public repository """
if(gitmarksObj.private != False):
logging.info("this is a private mark. Use 'addToPrivateRepo for this")
return -1
# -- add to our public 'bookmarks'
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR,
settings.BOOKMARK_SUB_PATH, gitmarksObj.hash)
filename = os.path.normpath(filename)
filename = os.path.abspath(filename)
# -get our string
gitmarksObj.setTimeIfEmpty()
bm_string = gitmarksObj.JSONBlock()
gitmarksBaseDir = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR)
fh = open(filename,'a')
if(fh):
# TRICKY:Set the authoring date of the commit based on the imported
# timestamp. git reads the GIT_AUTHOR_DATE environment var.
os.environ['GIT_AUTHOR_DATE'] = gitmarksObj.time
logging.info(bm_string)
fh.write(bm_string)
fh.close()
del fh
gitMark.gitAdd([filename,],gitmarksObj.time,gitmarksBaseDir)
# -- add to each of our our public 'tags' listings
tag_info_string = gitmarksObj.miniJSONBlock()
tagFilesWrittenSuccess = []
for tag in gitmarksObj.everyPossibleTagList():
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR,
settings.TAG_SUB_PATH, tag)
filename = os.path.normpath(filename)
filename = os.path.abspath(filename)
logging.info('tags filename' + str(filename))
fh = open(filename,'a')
if(fh):
fh.write(tag_info_string)
fh.close()
tagFilesWrittenSuccess.append(filename)
del fh
gitMark.gitAdd(tagFilesWrittenSuccess,gitmarksObj.time, gitmarksBaseDir)
# -- if we should get content, go get it and store it locally
if settings.GET_CONTENT and gitmarksObj.noContentSet() == False:
logging.info("get content? Don't mind of I do...")
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.CONTENT_GITMARK_DIR,
settings.HTML_SUB_PATH, gitmarksObj.hash)
#check if we have a cache directory
c_dir = os.path.join(settings.GITMARK_BASE_DIR, settings.CONTENT_GITMARK_DIR,
settings.HTML_SUB_PATH)
if os.path.isdir(c_dir) == False:
subprocess.call(['mkdir','-p',c_dir],shell=USE_SHELL)
gitmarksObj.cacheContent(filename)
#TOOD: do something about committing our changes
logging.info("git commit (local)? Don't mind if i do....")
msg = "auto commit from delicious import beta test %s" %time.strftime("%Y-%m-%dT%H:%M:%SZ")
gitMark.gitCommit(msg, gitmarksBaseDir )
if doPush:
logging.info("git push (external)? Don't mind if i do....")
gitMark.gitPush(gitmarksBaseDir )
def isInGitmarkPublicRepo(gitmarkObj):
""" Checks if a gitmarks object is already in the public repository
by checking for it's' hash in our public bookmarks directory. """
if(gitmarkObj.hash == None):
return False
filename = os.path.join(settings.GITMARK_BASE_DIR, settings.PUBLIC_GITMARK_REPO_DIR,
settings.BOOKMARK_SUB_PATH, gitmarkObj.hash)
return os.path.isfile(filename)
if __name__ == '__main__':
""" Function to run if we are running at the commandline"""
# -- parse command line options
parser = OptionParser("usage: %prog [options] uri")
parser.add_option("-s", "--share", dest="push", action="store_true", default=False, help="push data to remote gitmarks share point if possible")
parser.add_option("-t", "--tags", dest="tags", action="store", default='notag', help="comma seperated list of tags")
parser.add_option("-m", "--message", dest="msg", action="store", default=None, help="specify a commit message (default is 'adding [url]')")
parser.add_option("-c", "--skipcontent", dest='content', action='store_false', default=True, help="do not try to fetch content to store locally for search")
parser.add_option("-p", "--private", dest="private", action="store_true", default=False, help="Mark this as a private tag, not to share except with for:XXX recepiants")
if len(sys.argv) <= 1:
parser.print_usage()
exit(0)
(options, args) = parser.parse_args()
opts = {'push': options.push, 'tags': options.tags, 'msg': options.msg, 'content':options.content, 'private':options.private}
g = process_gitmarks_cmd(opts, args)