Mercurial > piecrust2
comparison piecrust/admin/siteinfo.py @ 960:8101692fdc11
admin: Add a "rebake preview assets" button to the dashboard.
Baking preview assets is needed for showing the site preview.
Also, add flashed messages.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 07 Oct 2017 12:13:57 -0700 |
parents | a4f1eafd1964 |
children | 68593928298d |
comparison
equal
deleted
inserted
replaced
959:4c69935ca415 | 960:8101692fdc11 |
---|---|
4 import copy | 4 import copy |
5 import logging | 5 import logging |
6 import threading | 6 import threading |
7 import subprocess | 7 import subprocess |
8 from flask import request, flash | 8 from flask import request, flash |
9 from piecrust import CACHE_DIR | |
9 from piecrust.app import PieCrustFactory | 10 from piecrust.app import PieCrustFactory |
10 | 11 |
11 | 12 |
12 logger = logging.getLogger(__name__) | 13 logger = logging.getLogger(__name__) |
13 | 14 |
78 | 79 |
79 @property | 80 @property |
80 def publish_log_file(self): | 81 def publish_log_file(self): |
81 return os.path.join(self.piecrust_app.cache_dir, 'publish.log') | 82 return os.path.join(self.piecrust_app.cache_dir, 'publish.log') |
82 | 83 |
84 def rebakeAssets(self): | |
85 out_dir = os.path.join( | |
86 self.root_dir, | |
87 CACHE_DIR, | |
88 self.piecrust_factory.cache_key, | |
89 'server') | |
90 args = [ | |
91 '--no-color', | |
92 'bake', | |
93 '-o', out_dir, | |
94 '--assets-only'] | |
95 proc = self._runChef(args) | |
96 try: | |
97 proc.wait(timeout=2) | |
98 if proc.returncode == 0: | |
99 flash("Assets baked successfully!") | |
100 else: | |
101 flash("Asset baking process returned '%s'... check the log." % | |
102 proc.returncode) | |
103 except subprocess.TimeoutExpired: | |
104 flash("Asset baking process is still running... check the log later.") | |
105 | |
83 def getPublishTargetLogFile(self, target): | 106 def getPublishTargetLogFile(self, target): |
84 target = target.replace(' ', '_').lower() | 107 target = target.replace(' ', '_').lower() |
85 return os.path.join(self.piecrust_app.cache_dir, | 108 return os.path.join(self.piecrust_app.cache_dir, |
86 'publish.%s.log' % target) | 109 'publish.%s.log' % target) |
87 | 110 |
88 def publish(self, target): | 111 def publish(self, target): |
89 chef_path = os.path.realpath(os.path.join( | |
90 os.path.dirname(__file__), | |
91 '../../chef.py')) | |
92 args = [ | 112 args = [ |
93 sys.executable, chef_path, | |
94 '--no-color', | 113 '--no-color', |
95 '--pid-file', self.publish_pid_file, | 114 '--pid-file', self.publish_pid_file, |
96 '--log', self.publish_log_file, | 115 '--log', self.publish_log_file, |
97 'publish', | 116 'publish', |
98 '--log-publisher', self.getPublishTargetLogFile(target), | 117 '--log-publisher', self.getPublishTargetLogFile(target), |
99 '--log-debug-info', | 118 '--log-debug-info', |
100 target] | 119 target] |
101 env = {} | 120 proc = self._runChef(args) |
102 for k, v in os.environ.items(): | |
103 env[k] = v | |
104 env['PYTHONHOME'] = sys.prefix | |
105 logger.info("Running publishing command: %s" % args) | |
106 proc = subprocess.Popen(args, cwd=self.root_dir, env=env) | |
107 logger.info("Publishing process ID: %s" % proc.pid) | |
108 try: | 121 try: |
109 proc.wait(timeout=2) | 122 proc.wait(timeout=2) |
110 if proc.returncode == 0: | 123 if proc.returncode == 0: |
111 flash("Publish process ran successfully!") | 124 flash("Publish process ran successfully!") |
112 else: | 125 else: |
113 flash("Publish process returned '%s'... check the log." % | 126 flash("Publish process returned '%s'... check the log." % |
114 proc.returncode) | 127 proc.returncode) |
115 except subprocess.TimeoutExpired: | 128 except subprocess.TimeoutExpired: |
116 flash("Publish process is still running... check the log later.") | 129 flash("Publish process is still running... check the log later.") |
117 | 130 |
131 def _runChef(self, args): | |
132 chef_path = os.path.realpath(os.path.join( | |
133 os.path.dirname(__file__), | |
134 '../../chef.py')) | |
135 args = [sys.executable, chef_path] + args | |
136 | |
137 env = {} | |
138 for k, v in os.environ.items(): | |
139 env[k] = v | |
140 env['PYTHONHOME'] = sys.prefix | |
141 | |
142 logger.info("Running chef command: %s" % args) | |
143 proc = subprocess.Popen(args, cwd=self.root_dir, env=env) | |
144 logger.info("Chef process ID: %s" % proc.pid) | |
145 return proc |