-
Notifications
You must be signed in to change notification settings - Fork 41
/
host_my_docs.py
125 lines (102 loc) · 3.33 KB
/
host_my_docs.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
"""Run this script to test your Host the Docs server.
It will POST a zip file.
You can also pass in your own settings to test more.
Requires the requests library.
Used also to generate and upload Host the Docs documentation for its demo page.
"""
import argparse
import logging
import os
import sys
import time
import zipfile
import six
import requests
from tests.test_filekeeper import ZIPFILE
from hostthedocs import getconfig as cfg
L = logging.getLogger('host_my_docs')
def parse():
p = argparse.ArgumentParser()
p.add_argument('-n', '--name', default='Test Project')
p.add_argument('-d', '--description', default='Project description.')
p.add_argument('-v', '--version', default='7.8.9')
p.add_argument('-z', '--zippath', default=ZIPFILE)
p.add_argument(
'--hostthedocs', action='store_true',
help='Generates docs for Host the Docs. Ignore all other options if used.')
p.add_argument(
'-H', '--host',
default='%s:%s' % (cfg.host, cfg.port),
help='Host to use.')
p.add_argument('-D', '--delete', action='store_true')
p.add_argument('-A', '--deleteall', action='store_true')
return p.parse_args()
def _makeaddr(host):
return 'http://%s/hmfd' % host.rstrip('/')
def _unlink(path):
try:
os.unlink(path)
except WindowsError:
time.sleep(1)
os.unlink(path)
def post(host, metadata, zippath):
address = _makeaddr(host)
L.info('POSTing to %s\n metadata: %s\n zippath: %s', address, metadata, zippath)
filename = os.path.basename(zippath)
got = requests.post(
address,
data=metadata,
files={"archive": (filename, open(zippath, 'rb'))})
return got
def delete(host, metadata, deleteall=False):
address = _makeaddr(host)
address += '?name=%s&version=%s' % (
metadata['name'], metadata['version'])
if deleteall:
address += '&entire_project=True'
L.info('DELETING to %s', address)
got = requests.delete(address)
return got
def generate_htd_docs():
from docutils.core import publish_string
with open('README.rst') as f:
html = publish_string(f.read(),writer_name='html')
with open('index.html', 'w') as f:
f.write(html)
zippath = 'docstemp.zip'
z = zipfile.ZipFile(zippath, 'w')
z.write('index.html')
z.close()
_unlink('index.html')
metadata = {
'name': 'Host the Docs',
'version': 'latest',
'description': 'Makes documentation hosting easy.'}
host = 'tech-artists.org:5003'
try:
resp = post(host, metadata, zippath)
finally:
_unlink(zippath)
if resp.status_code != 200:
raise RuntimeError(repr(resp))
def main():
opts = parse()
if opts.hostthedocs:
generate_htd_docs()
sys.exit(0)
metadata = {
'name': opts.name,
'version': opts.version,
'description': opts.description}
if opts.delete or opts.deleteall:
got = delete(opts.host, metadata, opts.deleteall)
else:
got = post(opts.host, metadata, opts.zippath)
content = got.content
if six.PY3:
content = str(content)
L.info('Recieved: %s: %s', got.status_code, content.replace('\n', ''))
sys.exit(0 if got.status_code == 200 else got.status_code)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
main()