def sql(self, sql): """ Perform given sql statement. """ verbose.out('> ' + sql, verbose=self._verbose) c = self.connection.cursor() c.execute(sql)
def handle(self, *args, **options): """ Run command. """ rel_path = os.path.join(settings.FAVICON_PATH, settings.FAVICON_FILENAME) self.original_img_path = find(rel_path) if not self.original_img_path or not os.path.isfile( self.original_img_path): out('favicon file doesn\'t exist please ensure there is a file here: %s' % rel_path) else: out('Processing favicons...Please Wait...') for i, image in enumerate(settings.FAVICON_PNG_SIZES, start=1): self.generate_image(image) out('%-70s [%d%%]' % (image['filename'], int( float(i) / float(len(settings.FAVICON_PNG_SIZES)) * 100.0))) out('%d of %d favicons processed.' % (i, len(settings.FAVICON_PNG_SIZES))) self.generate_ico_file() out('Complete.')
def handle(self, *args, **options): """ Run command. """ from cubane.cms.views import get_cms cms = get_cms() out('Generating redirect urls...Please Wait...') sitemap = cms.get_sitemaps() redirects = {} for k, sitemap in sitemap.items(): print k for item in sitemap.items(): if hasattr(item, 'legacy_url'): legacy_url = item.legacy_url if legacy_url: redirects[legacy_url] = item.get_absolute_url() elif hasattr(item, 'get_legacy_urls'): legacy_urls = item.get_legacy_urls() for legacy_url in legacy_urls: redirects[legacy_url] = item.get_absolute_url() if not settings.TEST: print redirects
def _clear(self, verbose=False): """ Remove all downloaded and cached font resources from the font cache. """ out('Clearing font cache...Please Wait...', verbose=verbose) if os.path.isdir(settings.CUBANE_FONT_ROOT): folder_delete_content(settings.CUBANE_FONT_ROOT) out('Font cache cleared.', verbose=verbose)
def generate_identifier(self): """ Generate new version identifier. """ if settings.TRACK_REVISION: identifier = generate_resource_version_identifier() out('Version: %s.' % identifier) else: identifier = None return identifier
def _update(self, verbose=False): """ Update font cache and download missing font resources. """ out('Updating font cache...Please Wait...', verbose=verbose) # get list of used fonts. font_declarations = self.get_used_font_declarations() if not font_declarations: return # get available backends and raise error, if we do not have at least # one font backend available to us... backends = self.get_backends() if not backends: raise ValueError( 'Unable to update fonts because no font backend is ' + \ 'available. Please make sure that a list of font backends ' + \ 'is defined as \'CUBANE_FONT_BACKENDS\' in settings and ' + \ 'that each module path is correct and the module ' + \ 'can be imported.' ) for font_declaration in font_declarations: if not self.is_font_cached(font_declaration.font_name): if self._cache_font(backends, font_declaration): status = 'UPDATED' else: status = 'NOT FOUND' # raise error on debug, so the developer will instantly # notice that a font could not be found, perhabs because # of a spelling error... if settings.DEBUG: raise ValueError( ('The font \'%s\' could not be found by any ' + \ 'of the installed font backends.') % font_declaration.font_name ) else: status = 'CACHED' if not settings.TEST and verbose: # pragma: no cover print '%-35s [%s]' % ( excerpt_from_text( font_declaration.font_name, length=32, prefix=True ), status )
def ask_confirm(question, interactive=True): """ Ask the given yes|no question. The outcome is True for yes and False for no. The default answer (ENTER) is no. """ if not interactive: return True while True: verbose.out('%s [no]: ' % question, newline=False) response = sys.stdin.readline().strip().lower() # just hitting ENTER is considered as no if response == 'yes': return True elif response == '' or response == 'no': return False
def invalidate(self, verbose=False): # get list of files published previously cache = self.get_index() # invalidate all files, so that we keep the generated content # but such pages are no longer cached as such. n = 0 for filename in cache: path = os.path.join(settings.CACHE_ROOT, filename) if os.path.isfile(path): new_path = self._get_invalidated_cache_filename(path) file_move(path, new_path) cubane_verbose.out('%-70s' % path, verbose=verbose) n += 1 # delete cache index self.clear_index() return n
def print_info(self): """ Print some verbose information about the application and where stuff is written to... """ out('CODE: %s' % settings.BASE_PATH) out('STATIC: %s' % settings.STATIC_ROOT) out('MEDIA: %s' % settings.MEDIA_ROOT)
def handle(self, *args, **options): """ Run command. """ from cubane.cms.views import get_cms cms = get_cms() out('Invalidating cache...Please Wait...') out('CACHE: %s' % settings.CACHE_ROOT) items = cms.invalidate(verbose=True) out('%d files removed.' % items)
def minify_resources(self, identifier): """ Compile and minify all resources. """ # compress resources out('Compressing resources...Please Wait...') for target in get_resource_targets(): out('[%s]' % target) for ext in ['css', 'js']: if ext == 'js': self.minify_resources_for(target, ext, None, identifier) else: for css_media in settings.CSS_MEDIA: self.minify_resources_for(target, ext, css_media, identifier) out('Complete.')
def minify_resources_for(self, target, ext, css_media, identifier): """ Compile and minify all resources for the given target, file extension and css media (for css resources only). """ # build resources resources = [] downloaded_files = [] for r in get_resources(target, ext, css_media): if r.startswith('/media/'): # media resource path = os.path.join(settings.MEDIA_ROOT, r[7:]) elif is_external_resource(r): # download external resource url = get_downloadable_resource_url(r) out('\tDownloading: %s...' % url) path = self.download_external_resource(url) downloaded_files.append(path) else: # local resource if r.startswith('/'): r = r[1:] path = os.path.join(settings.STATIC_ROOT, r) resources.append(path) if resources: filename = get_minified_filename(target, ext, css_media, identifier) out('\tCompressing: %s...' % filename) path = os.path.join(settings.STATIC_ROOT, filename) minify_files(resources, settings.STATIC_ROOT, path, ext, identifier) if downloaded_files: out('\tDeleting downloaded files...') for path in downloaded_files: try: os.remove(path) except: pass
def test_should_print_if_verbose_and_not_under_test_without_newline(self): verbose.out('hello world', newline=False, verbose=True, channel=self.output) self.assertEqual('hello world', self.output.getvalue())
def test_should_print_if_verbose_and_not_under_test(self): verbose.out('hello world', verbose=True, channel=self.output) self.assertEqual('hello world\n', self.output.getvalue())
def test_should_not_print_if_not_verbose(self): verbose.out('hello world', verbose=False, channel=self.output) self.assertEqual('', self.output.getvalue())
def _publish_content(self, verbose=False): """ Publish cms content. """ from cubane.cms.views import fake_request # add all pages start = time.time() for page in self.cms.get_pages(): if self.quit: break # determine type of page is_homepage = self.cms.settings.homepage_id == page.id is_contact_page = self.cms.settings.contact_page_id == page.id # skip contact page if CSRF is enabled, since we would not # be able to generate a dynamic CSRF token if this page is cached if 'django.middleware.csrf.CsrfViewMiddleware' in settings.MIDDLEWARE_CLASSES and is_contact_page: continue # add page self.process_page(page=page, verbose=verbose) # add paginated pages entity_model = page.get_entity_model() if entity_model and self.cms.settings.paging_enabled_for( entity_model): # get all available pages, starting with page number 2 # and continue as long as we do not get 404... i = 2 has_pages = True # adding an upper bound of max_pages child_page_count = entity_model.objects.count() while has_pages and child_page_count > 0: page_part = 'page-%d' % i slug = '/%s/%s/' % (page.slug, page_part) # render page has_pages = self.process_page_by_slug(slug=slug, verbose=verbose) i += 1 child_page_count -= 1 # add all child pages for model in self.cms.get_child_page_models(): if self.quit: break # if a page is assigned to this child_page_model pages = self.cms.get_pages().filter(entity_type=model.__name__) pages = self.cms.filter_out_childpages_on_sitemap(pages) if pages.count() > 0: for child_page in self.cms.filter_out_childpages_on_sitemap( self.cms.get_child_pages_for_model(model)): page = child_page.page # ignore child page with parent reference if page == None: continue # ignore, if parent page does not support entity type if page.entity_type != model.__name__: continue # add child page to cache self.process_page(page=child_page, verbose=verbose) # give other modules a place to do something with the cache self.cms.on_generate_cache(self, verbose) # add non-cms pages for custom_page in self.cms.custom_sitemap.cached_pages(): if self.quit: break local_url = custom_page.local_url request = fake_request(local_url, self.cms) view, args, kwargs = resolve(local_url) kwargs['request'] = request # render page try: response = view(*args, **kwargs) # generate cache entry if response and response.status_code == 200: path = os.path.join(local_url.lstrip('/'), 'index.html') self.add_to_cache(path, custom_page.lastmod, None, response.content, verbose=verbose) except Http404: pass # generate cache index file, even if we are forced to quit... self.cache.write() self.cache.cleanup() # present a message that we got terminated by another process if self.quit: out('- TERMINATED -', verbose=verbose) end = time.time() return (self.cache.items, self.cache.size, end - start)