-
Notifications
You must be signed in to change notification settings - Fork 7
/
new_planet.py
executable file
·125 lines (97 loc) · 3.5 KB
/
new_planet.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
#!/usr/bin/python
"""
Planeteria new planet interface
Copyright 2009 James Vasile <[email protected]>
Released under AGPL, version 3 or later <http://www.fsf.org/licensing/licenses/agpl-3.0.html>
"""
__authors__ = [ "James Vasile <[email protected]>"]
__license__ = "AGPLv3"
import os,sys,re, shutil
from util import our_db
import cgi
import cgitb
cgitb.enable()
#from config import *
import config as cfg
from config import opt
log = cfg.logging.getLogger('planeteria')
from util import Msg
err=Msg(web=True)
import templates
class BadSubdirNameError(Exception):
pass
def template_vars(subdir="", form_vals={}):
"Returns a dict with the template vars in it"
doc=dict(form_vals.items() + opt.items())
if not 'turing' in doc:
doc['turing']=''
doc['subdirectory'] = subdir
doc['error'] = err.html()
return doc
def validate_input(subdir):
if subdir == "":
return False
valid = True
if re.search('\\W', subdir):
err.add("Subdirectory can only contain letters, numbers and underscores.")
valid = False
return valid
def make_planet(subdir, output_dir=None,
name="", user="", email=""):
"""
Makes a planet on disk and in the db, copying the skeleton
directory on disk. Does not seed the planet with default values
for owner or email.
"""
if not validate_input(subdir):
raise BadSubdirNameError, subdir
if not output_dir:
output_dir = opt['output_dir']
path = os.path.join(output_dir, subdir)
with our_db('planets') as db:
if os.path.exists(path) and not subdir in db:
log.debug("Exists on disk but not in db, attempting to delete")
shutil.rmtree(path)
try:
shutil.copytree(opt['new_planet_dir'], path, symlinks=True)
except(OSError), errstr:
if os.path.exists(path):
msg = "%s planet already exists. Please choose another subdirectory name." % subdir
err.add(msg)
log.info(msg)
return False
err.add("Couldn't create planet: %s" % errstr)
return False
from planet import Planet
if not name: name = 'Planet %s' % subdir
p = Planet({'direc':subdir,
'name':name,
'user':user,
'email':email,
'password':'passme',
'feeds':{'http://hackervisions.org/?feed=rss2':{'image':'http://www.softwarefreedom.org/img/staff/vasile.jpg','name':'James Vasile', 'feedurl':'http://hackervisions.org/?feed=rss2'}}
})
p.save()
mopt = dict(opt.items()+p.__dict__.items())
templates.Welcome(mopt).write(path, 'index.html')
log.info("Made planet: %s" % path)
return True
def main():
global Form
Form = cgi.FieldStorage()
subdir = Form.getvalue("subdirectory", '').lower()
log.debug("Form keys and vals: %s" % (dict([(k,Form.getvalue(k,'')) for k in Form.keys()])))
if 'submit' in Form.keys():
if Form.getvalue("turing",'').lower() != "yes":
err.add("I can't believe you failed the Turing test. Maybe you're a sociopath?")
log.debug("Turing test failed for %s" % subdir)
elif validate_input(subdir):
if make_planet(subdir):
print "Location: http://%s/%s/admin.py\n\n" % (opt['domain'], subdir)
return
sys.stdout.write("Content-type: text/html\n\n")
doc = template_vars(subdir, dict([(k,Form.getvalue(k,'')) for k in Form.keys()]))
log.debug("doc: %s" % doc)
print templates.Index(doc).render().encode('utf-8', 'ignore')
if __name__ == "__main__":
main()