Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 138 lines (114 sloc) 4.274 kb
f139bda @rtv added doc importing script for conveniemce
authored
1 #! /usr/bin/env python
2 #
3 # This file is part of the ghp-import package released under
4 # the Tumbolia Public License. See the LICENSE file for more
5 # information.
6
7 import optparse as op
8 import os
9 import subprocess as sp
10 import time
11
12 __usage__ = "%prog [OPTIONS] DIRECTORY"
13
14 def is_repo(d):
15 if not os.path.isdir(d):
16 return False
17 if not os.path.isdir(os.path.join(d, 'objects')):
18 return False
19 if not os.path.isdir(os.path.join(d, 'refs')):
20 return False
21
22 headref = os.path.join(d, 'HEAD')
23 if os.path.isfile(headref):
24 return True
25 if os.path.islinke(headref) and os.readlink(headref).startswith("refs"):
26 return True
27 return False
28
29 def find_repo(path):
30 if is_repo(path):
31 return True
32 if is_repo(os.path.join(path, '.git')):
33 return True
34 (parent, ignore) = os.path.split(path)
35 if parent == path:
36 return False
37 return find_repo(parent)
38
39 def try_rebase(remote):
40 cmd = ['git', 'rev-list', '--max-count=1', 'origin/gh-pages']
41 p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
42 (rev, ignore) = p.communicate()
43 if p.wait() != 0:
44 return True
45 cmd = ['git', 'update-ref', 'refs/heads/gh-pages', rev.strip()]
46 if sp.call(cmd) != 0:
47 return False
48 return True
49
50 def get_config(key):
51 p = sp.Popen(['git', 'config', key], stdin=sp.PIPE, stdout=sp.PIPE)
52 (value, stderr) = p.communicate()
53 return value.strip()
54
55 def get_prev_commit():
56 cmd = ['git', 'rev-list', '--max-count=1', 'gh-pages']
57 p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
58 (rev, ignore) = p.communicate()
59 if p.wait() != 0:
60 return None
61 return rev.strip()
62
63 def make_when(timestamp=None):
64 if timestamp is None:
65 timestamp = int(time.time())
66 currtz = "%+05d" % (time.timezone / 36) # / 3600 * 100
67 return "%s %s" % (timestamp, currtz)
68
69 def start_commit(pipe, message):
70 username = get_config("user.name")
71 email = get_config("user.email")
72 pipe.stdin.write('commit refs/heads/gh-pages\n')
73 pipe.stdin.write('committer %s <%s> %s\n' % (username, email, make_when()))
74 pipe.stdin.write('data %d\n%s\n' % (len(message), message))
75 head = get_prev_commit()
76 if head:
77 pipe.stdin.write('from %s\n' % head)
78 pipe.stdin.write('deleteall\n')
79
80 def add_file(pipe, srcpath, tgtpath):
81 pipe.stdin.write('M 100644 inline %s\n' % tgtpath)
82 with open(srcpath) as handle:
83 data = handle.read()
84 pipe.stdin.write('data %d\n' % len(data))
85 pipe.stdin.write(data)
86 pipe.stdin.write('\n')
87
88 def run_import(srcdir, message):
89 cmd = ['git', 'fast-import', '--date-format=raw', '--quiet']
90 pipe = sp.Popen(cmd, stdin=sp.PIPE)
91 start_commit(pipe, message)
92 for path, dnames, fnames in os.walk(srcdir):
93 for fn in fnames:
94 fpath = os.path.join(path, fn)
95 add_file(pipe, fpath, os.path.relpath(fpath, start=srcdir))
96 pipe.stdin.write('\n')
97 pipe.stdin.close()
98 if pipe.wait() != 0:
99 print "Failed to process commit."
100
101 def options():
102 return [
103 op.make_option('-m', dest='mesg', default='Update documentation',
104 help='The commit message to use on the gh-pages branch.'),
105 op.make_option('-p', dest='push', default=False, action='store_true',
106 help='Push the branch to origin/gh-pages after committing.'),
107 op.make_option('-r', dest='remote', default='origin',
108 help='The name of the remote to push to. [%default]')
109 ]
110
111 def main():
112 parser = op.OptionParser(usage=__usage__, option_list=options())
113 opts, args = parser.parse_args()
114
115 if len(args) == 0:
116 parser.error("No import directory specified.")
117
118 if len(args) > 1:
119 parser.error("Unknown arguments specified: %s" % ', '.join(args[1:]))
120
121 if not os.path.isdir(args[0]):
122 parser.error("Not a directory: %s" % args[0])
123
124 if not find_repo(os.getcwd()):
125 parser.error("No Git repository found.")
126
127 if not try_rebase(opts.remote):
128 parser.error("Failed to rebase gh-pages branch.")
129
130 run_import(args[0], opts.mesg)
131
132 if opts.push:
133 sp.check_call(['git', 'push', opts.remote, 'gh-pages'])
134
135 if __name__ == '__main__':
136 main()
137
Something went wrong with that request. Please try again.