Commits

Brent Tubbs committed ce75ae9

tweaks to support staticfiles magic

  • Participants
  • Parent commits 5543bc1

Comments (0)

Files changed (2)

 import yaml
 import copy
 import os
+import sys
 
 def get_gunicorn_cmd(site_config, bin_dir=''):
     gconfig = copy.copy(site_config['gunicorn'])
     return blame
 
 def get_config(site_root, role=None):
-    """If given a role, returns merged site and role config,
-    else tries to read config from blame.yaml"""
+    """Returns merged site and role config.
+    Falls back to blame.yaml if no role given.
+    Falls back to just site.yaml if no role given and no blame file found"""
     if role is None:
         try:
             return get_blame(site_root)['config']
         except IOError:
-            raise Exception("No role given, and blame.yaml not found")
+            return get_site_config(site_root)
     else:
         config = get_site_config(site_root)
         config.update(get_role_config(role))
         return config
+
+def get_role():
+    try:
+        #if '-R rolename' found in sys.argv, use that
+        return sys.argv[sys.argv.index('-R')+1]
+    except:
+        #role not found in sys.argv, try env var
+        #return None if no role there either
+        return os.environ.get('SILK_ROLE', None)

File silk/utils.py

 def run_cherrypy(config, root):
     """Mount each of the static dirs in site.yaml as its own app
     in the cherrypy tree.  Then mount the wsgi app as the root, and run."""
-    for static_dir in config['static_dirs']:
-        #mount each of our static dirs as its own app in the cherrypy tree
-        url_path = static_dir['url_path'].rstrip('/')
-        sys_path = os.path.join(root, static_dir['system_path'])
-        cherry_conf = {'/': {'tools.staticdir.on': True,
-                             'tools.staticdir.dir': sys_path,}}
-        cherrypy.tree.mount(None, script_name=url_path, config=cherry_conf)
+    if config['static_dirs']:
+        for static_dir in config['static_dirs']:
+            #mount each of our static dirs as its own app in the cherrypy tree
+            url_path = static_dir['url_path'].rstrip('/')
+            sys_path = os.path.join(root, static_dir['system_path'])
+            cherry_conf = {'/': {'tools.staticdir.on': True,
+                                 'tools.staticdir.dir': sys_path,}}
+            cherrypy.tree.mount(None, script_name=url_path, config=cherry_conf)
     #mount the wsgi app
     sys.path.append(root)
     os.chdir(root)
     if cmd in cmd_map:
         cmd_func = cmd_map[cmd]
         if cmd_func in config_required:
-            occurrences = [i for i,x in enumerate(args) if x == '-R']
-            if not len(occurrences):
-                sys.exit('You need to provide a role with -R rolename')
-            else:
-                index = occurrences[0]
-                role = args[index + 1]
+            role = silk.lib.get_role()
             root = silk.lib.get_site_root(os.getcwd())
             config = silk.lib.get_config(root, role)
             cmd_func(config, root)