Skip to content

Commit

Permalink
Merge branch 'master' into gitmark_web
Browse files Browse the repository at this point in the history
* master:
  Preserve bookmark timestamps during Delicious import.
  Restore Windows compatibility, which my shell escaping fix would have broken.
  Log an error but save the bookmark and continue if downloading content fails
  Add each list of files in a single git command
  Fix errors due to quotes or other shell-recognized characters in description
  Show progress with percent complete during import from Delicious
  • Loading branch information
jcn committed Dec 17, 2010
2 parents a9556fe + b3912ba commit 51a81eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
16 changes: 14 additions & 2 deletions delicious_import.py
Expand Up @@ -25,7 +25,8 @@ def __init__(self, username, password=''):
x = minidom.parseString(content)

# sample post: <post href="http://www.pixelbeat.org/cmdline.html" hash="e3ac1d1e4403d077ee7e65f62a55c406" description="Linux Commands - A practical reference" tag="linux tutorial reference" time="2010-11-29T01:07:35Z" extended="" meta="c79362665abb0303d577b6b9aa341599" />
for post in x.getElementsByTagName("post"):
post_list = x.getElementsByTagName('post')
for post_index, post in enumerate(post_list):
url = post.getAttribute('href')
desc = post.getAttribute('description')
tags = ",".join([t for t in post.getAttribute('tag').split()])
Expand All @@ -36,9 +37,20 @@ def __init__(self, username, password=''):
options['push'] = False
options['msg'] = desc

args = [url]
# 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'] = timestamp

args = [url]
g = gitMark(options, args)

# Reset authoring timestamp (abundance of caution)
del os.environ['GIT_AUTHOR_DATE']

if post_list.length > 1:
print '%d of %d bookmarks imported (%d%%)' % (
post_index + 1, post_list.length,
(post_index + 1.0) / post_list.length * 100)


if __name__ == '__main__':
Expand Down
30 changes: 19 additions & 11 deletions gitmark.py
Expand Up @@ -17,6 +17,12 @@

from settings import *

# Arguments are passed directly to git, not through the shell, to avoid the
# need for shell escaping. On Windows, however, commands need to go through the
# shell for git to be found on the PATH, but escaping is automatic there. So
# send git commands through the shell on Windows, and directly everywhere else.
USE_SHELL = os.name == 'nt'

class gitMark(object):

def __init__(self, options, args):
Expand Down Expand Up @@ -50,14 +56,10 @@ def __init__(self, options, args):
self.gitPush()

def gitAdd(self, files):
for f in files:
cmd = 'git add %s' % f
pipe = subprocess.Popen(cmd, shell=True)
pipe.wait()

subprocess.call(['git', 'add'] + files, shell=USE_SHELL)

def gitCommit(self, msg):
pipe = subprocess.Popen("git commit -m '%s'" % msg, shell=True)
pipe.wait()
subprocess.call(['git', 'commit', '-m', msg], shell=USE_SHELL)

def gitPush(self):
pipe = subprocess.Popen("git push origin master", shell=True)
Expand Down Expand Up @@ -90,9 +92,15 @@ def generateHash(self, text):
return m.hexdigest()

def getContent(self, url):
h = urllib.urlopen(url)
content = h.read()
h.close()
try:
h = urllib.urlopen(url)
content = h.read()
h.close()
except IOError, e:
print >>sys.stderr, ("Error: could not retrieve the content of a"
" URL. The bookmark will be saved, but its content won't be"
" searchable. URL: <%s>. Error: %s" % (url, e))
content = ''
return content


Expand All @@ -106,4 +114,4 @@ def getContent(self, url):

opts = {'push': options.push, 'tags': options.tags, 'msg': options.msg}

g = gitMark(opts, args)
g = gitMark(opts, args)

0 comments on commit 51a81eb

Please sign in to comment.