Create the static directory if it is missing
[cookbook.git] / bin / cook.py
index 4a872bee0e1e75c817bc1ceaf978f20f0afeef82..bd0286059fc133431a4a573f2001967427861200 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Cookbook.  If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import with_statement
+
+import os
 import os.path
+import uuid
 
 import cookbook
 from cookbook import Cookbook
@@ -81,6 +85,8 @@ if __name__ == '__main__':
         sys.stderr.write('Serving cookbook\n')
         module_dir = os.path.dirname(os.path.abspath(cookbook.__file__))
         static_dir = os.path.join(module_dir, 'static')
+        if not os.path.exists(static_dir):
+            os.mkdir(static_dir)
         template_dir = os.path.join(module_dir, 'template')
         config = os.path.join(module_dir, 'config')
         s = Server(c, template_dir)
@@ -88,12 +94,42 @@ if __name__ == '__main__':
             cherrypy.config.update({ # http://www.cherrypy.org/wiki/ConfigAPI
                     'server.socket_host': options.address,
                     'server.socket_port': int(options.port),
+                    'tools.decode.on': True,
                     'tools.encode.on': True,
                     'tools.encode.encoding': 'utf8',
                     'tools.staticdir.root': static_dir,
                     })
-            app_config = { '/static': { 'tools.staticdir.on': True,
-                                        'tools.staticdir.dir': '', } }
+            if cherrypy.__version__.startswith('3.2'):
+                get_ha1 = cherrypy.lib.auth_digest.get_ha1_file_htdigest(
+                    '.htaccess')
+                digest_auth = {
+                    'tools.auth_digest.on': True,
+                    'tools.auth_digest.realm': 'cookbook',
+                    'tools.auth_digest.get_ha1': get_ha1,
+                    'tools.auth_digest.key': str(uuid.uuid4()),
+                    }
+            else:
+                passwds = {}
+                with open('.htaccess', 'r') as f:
+                    for line in f:
+                        user,realm,ha1 = line.strip().split(':')
+                        passwds[user] = ha1  # use the ha1 as the password
+                digest_auth = {
+                    'tools.digest_auth.on': True,
+                    'tools.digest_auth.realm': 'cookbook',
+                    'tools.digest_auth.users': passwds,
+                    }
+                print passwds
+                del(passwds)
+            app_config = {
+                '/static': {
+                    'tools.staticdir.on': True,
+                    'tools.staticdir.dir': '',
+                    },
+                '/edit': digest_auth,
+                '/add_tag': digest_auth,
+                '/remove_tag': digest_auth,
+                }
             cherrypy.quickstart(root=s, config=app_config)
         elif cherrypy.__version__.startswith('2.'):
             cherrypy.root = s
@@ -101,6 +137,7 @@ if __name__ == '__main__':
                     'server.environment': 'production',
                     'server.socket_host': options.address,
                     'server.socket_port': int(options.port),
+                    'decoding_filter.on': True,
                     'encoding_filter.on': True,
                     'encodinf_filter.encoding': 'utf8',
                     'static_filter.on': True,