aboutsummaryrefslogtreecommitdiffstats
path: root/cms-cli
blob: 6a5612cb13ae3167c4bfdade5f4865fbdbd6d159 (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
#!/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
from cms import *


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("")
	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"

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hd:w:i:D:",
			[ "help", "db=", "www=", "images=", "domain=", ])
	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 len(args) >= 1:
		action = args[0]
	if len(args) >= 2:
		path = args[1]
	if not action:
		print("No action specified")
		return 1
	cms = None
	try:
		cms = CMS(dbPath=opt_db,
			  wwwPath=opt_www,
			  imagesDir=opt_images,
			  domain=opt_domain)
		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()
	except (CMSException) as e:
		if cms:
			data, mime, headers = cms.getErrorPage(e)
		else:
			data, mime = "CMSException", "text/html"
	if mime.startswith("text/html"):
		result = data + b"\n"
	else:
		result = data
	sys.stdout.buffer.write(result)
	sys.stdout.buffer.flush()
	return 0

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