comparison piecrust/admin/sites.py @ 778:5e91bc0e3b4d

internal: Move admin panel code into the piecrust package.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Jul 2016 15:02:24 +0200
parents foodtruck/sites.py@e2e955a3bb25
children 82509bce94ca
comparison
equal deleted inserted replaced
777:8d633ca59bc5 778:5e91bc0e3b4d
1 import os
2 import os.path
3 import copy
4 import logging
5 import threading
6 import subprocess
7 from piecrust.app import PieCrust
8 from piecrust.configuration import merge_dicts
9
10
11 logger = logging.getLogger(__name__)
12
13
14 class UnauthorizedSiteAccessError(Exception):
15 pass
16
17
18 class InvalidSiteError(Exception):
19 pass
20
21
22 class Site(object):
23 def __init__(self, name, root_dir, config):
24 self.name = name
25 self.root_dir = root_dir
26 self._global_config = config
27 self._piecrust_app = None
28 self._scm = None
29 logger.debug("Creating site object for %s" % self.name)
30
31 @property
32 def piecrust_app(self):
33 if self._piecrust_app is None:
34 s = PieCrust(self.root_dir)
35 s.config.set('site/root', '/site/%s/' % self.name)
36 self._piecrust_app = s
37 return self._piecrust_app
38
39 @property
40 def scm(self):
41 if self._scm is None:
42 cfg = copy.deepcopy(self._global_config.get('scm', {}))
43 merge_dicts(cfg, self.piecrust_app.config.get('scm', {}))
44
45 if os.path.isdir(os.path.join(self.root_dir, '.hg')):
46 from .scm.mercurial import MercurialSourceControl
47 self._scm = MercurialSourceControl(self.root_dir, cfg)
48 elif os.path.isdir(os.path.join(self.root_dir, '.git')):
49 from .scm.git import GitSourceControl
50 self._scm = GitSourceControl(self.root_dir, cfg)
51 else:
52 self._scm = False
53
54 return self._scm
55
56 @property
57 def publish_pid_file(self):
58 return os.path.join(self.piecrust_app.cache_dir, 'publish.pid')
59
60 @property
61 def publish_log_file(self):
62 return os.path.join(self.piecrust_app.cache_dir, 'publish.log')
63
64 def publish(self, target):
65 args = [
66 'chef',
67 '--pid-file', self.publish_pid_file,
68 'publish', target,
69 '--log-publisher', self.publish_log_file]
70 proc = subprocess.Popen(args, cwd=self.root_dir)
71
72 def _comm():
73 proc.communicate()
74
75 t = threading.Thread(target=_comm, daemon=True)
76 t.start()
77
78
79 class FoodTruckSites():
80 def __init__(self, config, current_site):
81 self._sites = {}
82 self.config = config
83 self.current_site = current_site
84 if current_site is None:
85 raise Exception("No current site was given.")
86
87 def get_root_dir(self, name=None):
88 name = name or self.current_site
89 root_dir = self.config.get('sites/%s' % name)
90 if root_dir is None:
91 raise InvalidSiteError("No such site: %s" % name)
92 if not os.path.isdir(root_dir):
93 raise InvalidSiteError("Site '%s' has an invalid path." % name)
94 return root_dir
95
96 def get(self, name=None):
97 name = name or self.current_site
98 s = self._sites.get(name)
99 if s:
100 return s
101
102 root_dir = self.get_root_dir(name)
103 s = Site(name, root_dir, self.config)
104 self._sites[name] = s
105 return s
106
107 def getall(self):
108 for name in self.config.get('sites'):
109 yield self.get(name)
110