aboutsummaryrefslogtreecommitdiffstats
path: root/cms-cli
blob: d0cbdc91f6951038d300a5b1b37be4fb43036cd4 (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
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
#!/usr/bin/env python3
#
#   simple WSGI/Python based CMS script
#   commandline interface
#
#   Copyright (C) 2012 Michael Buesch <m@bues.ch>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import getopt


def usage():
	print("Usage: %s [OPTIONS] [ACTION]" % sys.argv[0])
	print("")
	print("Options:")
	print("  -d|--db PATH           Path to database. Default: ./db")
	print("  -w|--www PATH          Path to the static data. Default: ./www-data")
	print("  -i|--images SUBPATH    Path to images. Default: /images")
	print("  -D|--domain DOMAIN     The domain name. Default: example.com")
	print("  -C|--cython            Import Cython modules")
	print("  -P|--profile LEVEL     Enable profiling")
	print("  -L|--loop LOOPS        Run LOOPS number of loops. For profiling.")
	print("")
	print("Actions:")
	print("  GET <path>             Do a GET request on 'path'")
	print("  POST <path>            Do a POST request on 'path'")

def main():
	action = None
	path = "/"
	opt_db = "./db"
	opt_www = "./www-data"
	opt_images = "/images"
	opt_domain = "example.com"
	opt_cython = False
	opt_profile = 0
	opt_loop = 1

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hd:w:i:D:CP:L:",
			[ "help", "db=", "www=", "images=", "domain=", "cython",
			  "profile=", "loop=", ])
	except (getopt.GetoptError) as e:
		usage()
		return 1
	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			return 0
		if o in ("-d", "--db"):
			opt_db = v
		if o in ("-w", "--www"):
			opt_www = v
		if o in ("-i", "--images"):
			opt_images = v
		if o in ("-D", "--domain"):
			opt_domain = v
		if o in ("-C", "--cython"):
			opt_cython = True
		if o in ("-P", "--profile"):
			try:
				opt_profile = int(v)
			except ValueError:
				print("Invalid -P|--profile value")
				return 1
		if o in ("-L", "--loop"):
			try:
				opt_loop = int(v)
				if opt_loop < 1:
					raise ValueError
			except ValueError:
				print("Invalid -L|--loop value")
				return 1
	if len(args) >= 1:
		action = args[0]
	if len(args) >= 2:
		path = args[1]
	if not action:
		print("No action specified")
		return 1
	retval = 0
	cms = prof = None

	if opt_cython:
		from cms_cython import CMS, CMSException
		from cms_cython.profiler import Profiler
	else:
		from cms import CMS, CMSException
		from cms.profiler import Profiler
	try:
		if opt_profile >= 1:
			prof = Profiler()
			if opt_profile >= 2:
				prof.start()

		cms = CMS(dbPath=opt_db,
			  wwwPath=opt_www,
			  imagesDir=opt_images,
			  domain=opt_domain,
			  debug=True)

		if opt_profile == 1:
			prof.start()

		for _ in range(opt_loop):
			if action.upper() == "GET":
				data, mime = cms.get(path)
			elif action.upper() == "POST":
				data, mime = cms.post(path)
			else:
				print("Invalid action")
				return 1
		cms.shutdown()

		if opt_profile >= 1:
			prof.stop()

	except (CMSException) as e:
		if cms:
			data, mime, headers = cms.getErrorPage(e)
		else:
			data, mime = "CMSException", "text/html"
		retval = 1
	if mime.startswith("text/html"):
		result = data + b"\n"
	else:
		result = data
	sys.stdout.buffer.write(result)
	sys.stdout.buffer.flush()
	if prof:
		print(prof.getResult(), file=sys.stderr)
	return retval

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface