summaryrefslogtreecommitdiff
path: root/doc/pjsip-book/fetch_trac.py
blob: 3c754624d5e0b4e956c9816c4514f50e02ea77b5 (plain)
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
import urllib2
import sys

def fetch_rst(url):
	print 'Fetching %s..' % url
	req = urllib2.Request(url)
		
	fd = urllib2.urlopen(req, timeout=30)
	body = fd.read()		

	pos = body.find("{{{")
	if pos >= 0:
		body = body[pos+4:]
	
	pos = body.find("}}}")
	if pos >= 0:
		body = body[:pos]

	pos = body.find("#!rst")
	if pos >= 0:
		body = body[pos+6:]

	pos = url.rfind("/")
	if pos >= 0:
		filename = url[pos+1:]
	else:
		filename = url
	
	pos = filename.find('?')
	if pos >= 0:
		filename = filename[:pos]
		
	filename += ".rst"
	f = open(filename, 'w')
	f.write(body)
	f.close()


def process_index(index):
	pages = []
	
	f = open(index + '.rst', 'r')
	line = f.readline()
	while line:
		if line.find('toctree::') >= 0:
			break
		line = f.readline()
		
	if line.find('toctree::') < 0:
		return []
	# Skip directive (or whatever it's called
	line = f.readline().strip()
	while line and line[0] == ':':
		line = f.readline().strip()
	# Skip empty lines
	line = f.readline().strip()
	while not line:
		line = f.readline().strip()
	# Parse names
	while line:
		pages.append(line)
		line = f.readline().strip()
		
	f.close()
	
	return pages


if __name__ == '__main__':
	print "** Warning: This will overwrite ALL RST files in current directory. Continue? [n] ",
	if sys.stdin.readline().strip() != 'y':
		sys.exit(0)
		
	url_format = 'http://trac.pjsip.org/repos/wiki/pjsip-doc/%s?format=txt'
	
	index = url_format % ('index')
	fetch_rst(index)
	
	pages = process_index('index')
	for page in pages:
		url = url_format % (page)
		fetch_rst(url)
		
	print 'Done.'