def handle(self, *args, **options): publish_target_name = options.get('publish_target_name') if not publish_target_name: publish_target_name = 'default' publish_targets = getattr(settings, 'DISTILL_PUBLISH', {}) publish_target = publish_targets.get(publish_target_name) if type(publish_target) != dict: e = 'Invalid publish target name: "{}"'.format(publish_target_name) e += ', check your settings.DISTILL_PUBLISH values' raise CommandError(e) publish_engine = publish_target.get('ENGINE') if not publish_engine: e = 'Publish target {} has no ENGINE'.format(publish_target_name) raise CommandError(e) self.stdout.write('') self.stdout.write('You have requested to test a publishing target:') self.stdout.write('') self.stdout.write(' Name: {}'.format(publish_target_name)) self.stdout.write(' Engine: {}'.format(publish_engine)) self.stdout.write('') ans = input('Type \'yes\' to continue, or \'no\' to cancel: ') if ans.lower() == 'yes': self.stdout.write('') self.stdout.write('Testing publishing target...') else: raise CommandError('Testing publishing target cancelled.') self.stdout.write('') self.stdout.write('Connecting to backend engine') backend_class = get_backend(publish_engine) random_file = NamedTemporaryFile(delete=False) random_str = hexlify(os.urandom(16)) random_name = '/' + random_str.decode('utf-8') random_file.write(random_str) random_file.close() backend = backend_class(os.path.dirname(random_file.name), publish_target) self.stdout.write('Authenticating') backend.authenticate() remote_file_name = os.path.basename(random_file.name) self.stdout.write('Uploading test file: {}'.format(random_file.name)) backend.upload_file(random_file.name, remote_file_name) url = backend.remote_url(random_file.name) self.stdout.write('Verifying remote test file: {}'.format(url)) if backend.check_file(random_file.name, url): self.stdout.write('File uploaded correctly, file hash is correct') else: msg = 'File error, remote file hash differs from local hash' self.stderr.write(msg) self.stdout.write('Deleting remote test file') backend.delete_remote_file(remote_file_name) if os.path.exists(random_file.name): self.stdout.write('Deleting local test file') os.unlink(random_file.name) self.stdout.write('') self.stdout.write('Backend testing complete.')
def handle(self, *args, **options): publish_target_name = options.get('publish_target_name') if not publish_target_name: publish_target_name = 'default' publish_targets = getattr(settings, 'DISTILL_PUBLISH', {}) publish_target = publish_targets.get(publish_target_name) if type(publish_target) != dict: e = 'Invalid publish target name: "{}"'.format(publish_target_name) e += ', check your settings.DISTILL_PUBLISH values' raise CommandError(e) publish_engine = publish_target.get('ENGINE') if not publish_engine: e = 'Publish target {} has no ENGINE'.format(publish_target_name) raise CommandError(e) collectstatic = options.get('collectstatic') quiet = options.get('quiet') force = options.get('force') if quiet: stdout = self._quiet else: stdout = self.stdout.write static_dir = settings.STATIC_ROOT static_url = settings.STATIC_URL try: output_dir = mkdtemp() if not output_dir.endswith(os.sep): output_dir += os.sep backend_class = get_backend(publish_engine) backend = backend_class(output_dir, publish_target) username = backend.account_username() container = backend.account_container() stdout('') stdout('You have requested to distill and publish this site') stdout('to the following target:') stdout('') stdout(' Settings name: {}'.format(publish_target_name)) stdout(' Engine: {}'.format(publish_engine)) stdout(' Username: {}'.format(username)) stdout(' Container: {}'.format(container)) stdout('') if collectstatic: run_collectstatic(stdout) if not os.path.isdir(settings.STATIC_ROOT): e = 'Static source directory does not exist, run collectstatic' raise CommandError(e) ans = input('Type \'yes\' to continue, or \'no\' to cancel: ') if ans.lower() == 'yes': pass else: raise CommandError('Publishing site cancelled.') self.stdout.write('') static_output_dir = os.path.join(output_dir, static_url[1:]) msg = 'Generating static site into directory: {}' stdout(msg.format(output_dir)) try: render_to_dir(output_dir, urls_to_distill, stdout) except DistillError as err: raise raise_with_traceback(CommandError(str(err))) stdout('') stdout('Publishing site') backend.index_local_files() publish_dir(output_dir, backend, stdout) finally: if os.path.exists(output_dir): stdout('Deleting temporary directory.') rmtree(output_dir) stdout('') stdout('Site generation and publishing complete.')