comparison foodtruck/sites.py @ 610:efc1dc916e7c

admin: Configuration changes. * Move publish targets to site configuration. * Add direct accessor for the current site.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 28 Jan 2016 22:17:58 -0800
parents c6bc0ef03f82
children e2e955a3bb25
comparison
equal deleted inserted replaced
609:978d8bca9fb3 610:efc1dc916e7c
4 import shlex 4 import shlex
5 import logging 5 import logging
6 import threading 6 import threading
7 import subprocess 7 import subprocess
8 from piecrust.app import PieCrust 8 from piecrust.app import PieCrust
9 from piecrust.configuration import merge_dicts, Configuration 9 from piecrust.configuration import merge_dicts
10 10
11 11
12 logger = logging.getLogger(__name__) 12 logger = logging.getLogger(__name__)
13 13
14 14
22 22
23 class Site(object): 23 class Site(object):
24 def __init__(self, name, root_dir, config): 24 def __init__(self, name, root_dir, config):
25 self.name = name 25 self.name = name
26 self.root_dir = root_dir 26 self.root_dir = root_dir
27 self.config = Configuration(values=config.get('sites/%s' % name, {}))
28 self._global_config = config 27 self._global_config = config
29 self._piecrust_app = None 28 self._piecrust_app = None
30 self._scm = None 29 self._scm = None
31 self._publish_thread = None 30 self._publish_thread = None
32 logger.debug("Creating site object for %s" % self.name) 31 logger.debug("Creating site object for %s" % self.name)
41 40
42 @property 41 @property
43 def scm(self): 42 def scm(self):
44 if self._scm is None: 43 if self._scm is None:
45 cfg = copy.deepcopy(self._global_config.get('scm', {})) 44 cfg = copy.deepcopy(self._global_config.get('scm', {}))
46 merge_dicts(cfg, self.config.get('scm', {})) 45 merge_dicts(cfg, self.piecrust_app.config.get('scm', {}))
47 46
48 if os.path.isdir(os.path.join(self.root_dir, '.hg')): 47 if os.path.isdir(os.path.join(self.root_dir, '.hg')):
49 from .scm.mercurial import MercurialSourceControl 48 from .scm.mercurial import MercurialSourceControl
50 self._scm = MercurialSourceControl(self.root_dir, cfg) 49 self._scm = MercurialSourceControl(self.root_dir, cfg)
51 elif os.path.isdir(os.path.join(self.root_dir, '.git')): 50 elif os.path.isdir(os.path.join(self.root_dir, '.git')):
64 @property 63 @property
65 def publish_thread(self): 64 def publish_thread(self):
66 return self._publish_thread 65 return self._publish_thread
67 66
68 def publish(self, target): 67 def publish(self, target):
69 target_cfg = self.config.get('publish/%s' % target) 68 target_cfg = self.piecrust_app.config.get('publish/%s' % target)
70 if not target_cfg: 69 if not target_cfg:
71 raise Exception("No such publish target: %s" % target) 70 raise Exception("No such publish target: %s" % target)
72 71
73 target_cmd = target_cfg.get('cmd') 72 target_cmd = target_cfg.get('cmd')
74 if not target_cmd: 73 if not target_cmd:
124 123
125 124
126 class FoodTruckSites(): 125 class FoodTruckSites():
127 def __init__(self, config, current_site): 126 def __init__(self, config, current_site):
128 self._sites = {} 127 self._sites = {}
129 self._site_dirs = {}
130 self.config = config 128 self.config = config
131 self.current_site = current_site 129 self.current_site = current_site
132 if current_site is None: 130 if current_site is None:
133 raise Exception("No current site was given.") 131 raise Exception("No current site was given.")
134 132
135 def get_root_dir(self, name=None): 133 def get_root_dir(self, name=None):
136 name = name or self.current_site 134 name = name or self.current_site
137 s = self._site_dirs.get(name) 135 root_dir = self.config.get('sites/%s' % name)
138 if s: 136 if root_dir is None:
139 return s
140
141 scfg = self.config.get('sites/%s' % name)
142 if scfg is None:
143 raise InvalidSiteError("No such site: %s" % name) 137 raise InvalidSiteError("No such site: %s" % name)
144 root_dir = scfg.get('path')
145 if root_dir is None:
146 raise InvalidSiteError("Site '%s' has no path defined." % name)
147 if not os.path.isdir(root_dir): 138 if not os.path.isdir(root_dir):
148 raise InvalidSiteError("Site '%s' has an invalid path." % name) 139 raise InvalidSiteError("Site '%s' has an invalid path." % name)
149 self._site_dirs[name] = root_dir
150 return root_dir 140 return root_dir
151 141
152 def get(self, name=None): 142 def get(self, name=None):
153 name = name or self.current_site 143 name = name or self.current_site
154 s = self._sites.get(name) 144 s = self._sites.get(name)
158 root_dir = self.get_root_dir(name) 148 root_dir = self.get_root_dir(name)
159 s = Site(name, root_dir, self.config) 149 s = Site(name, root_dir, self.config)
160 self._sites[name] = s 150 self._sites[name] = s
161 return s 151 return s
162 152
153 def getall(self):
154 for name in self.config.get('sites'):
155 yield self.get(name)
156