Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4908 from pdelboca/4639-migrate-cli-commands
4639 - Migrate CLI commands
- Loading branch information
Showing
5 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# encoding: utf-8 | ||
|
||
import click | ||
import subprocess | ||
import os | ||
|
||
from ckan.common import config | ||
from ckan.cli import error_shout | ||
|
||
|
||
_custom_css = { | ||
u'fuchsia': u''' | ||
@layoutLinkColor: #E73892; | ||
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%); | ||
@footerLinkColor: @footerTextColor; | ||
@mastheadBackgroundColor: @layoutLinkColor; | ||
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%); | ||
@btnPrimaryBackgroundHighlight: @layoutLinkColor; | ||
''', | ||
|
||
u'green': u''' | ||
@layoutLinkColor: #2F9B45; | ||
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%); | ||
@footerLinkColor: @footerTextColor; | ||
@mastheadBackgroundColor: @layoutLinkColor; | ||
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%); | ||
@btnPrimaryBackgroundHighlight: @layoutLinkColor; | ||
''', | ||
|
||
u'red': u''' | ||
@layoutLinkColor: #C14531; | ||
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%); | ||
@footerLinkColor: @footerTextColor; | ||
@mastheadBackgroundColor: @layoutLinkColor; | ||
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%); | ||
@btnPrimaryBackgroundHighlight: @layoutLinkColor; | ||
''', | ||
|
||
u'maroon': u''' | ||
@layoutLinkColor: #810606; | ||
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%); | ||
@footerLinkColor: @footerTextColor; | ||
@mastheadBackgroundColor: @layoutLinkColor; | ||
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%); | ||
@btnPrimaryBackgroundHighlight: @layoutLinkColor; | ||
''', | ||
} | ||
|
||
|
||
@click.command( | ||
name=u'less', | ||
short_help=u'Compile all root less documents into their CSS counterparts') | ||
def less(): | ||
command = (u'npm', u'bin') | ||
process = subprocess.Popen( | ||
command, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
universal_newlines=True) | ||
output = process.communicate() | ||
directory = output[0].strip() | ||
if not directory: | ||
error_shout(u'Command "{}" returned nothing. Check that npm is ' | ||
u'installed.'.format(' '.join(command))) | ||
less_bin = os.path.join(directory, u'lessc') | ||
|
||
public = config.get(u'ckan.base_public_folder') | ||
|
||
root = os.path.join(os.path.dirname(__file__), u'..', public, u'base') | ||
root = os.path.abspath(root) | ||
custom_less = os.path.join(root, u'less', u'custom.less') | ||
for color in _custom_css: | ||
f = open(custom_less, u'w') | ||
f.write(_custom_css[color]) | ||
f.close() | ||
_compile_less(root, less_bin, color) | ||
f = open(custom_less, u'w') | ||
f.write(u'// This file is needed in order for ./bin/less to ' | ||
u'compile in less 1.3.1+\n') | ||
f.close() | ||
_compile_less(root, less_bin, u'main') | ||
|
||
|
||
def _compile_less(root, less_bin, color): | ||
click.echo(u'compile {}.css'.format(color)) | ||
main_less = os.path.join(root, u'less', u'main.less') | ||
main_css = os.path.join(root, u'css', u'{}.css'.format(color)) | ||
command = (less_bin, main_less, main_css) | ||
process = subprocess.Popen( | ||
command, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
universal_newlines=True) | ||
output = process.communicate() | ||
click.echo(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# encoding: utf-8 | ||
|
||
import click | ||
import os | ||
import ckan.include.rjsmin as rjsmin | ||
import ckan.include.rcssmin as rcssmin | ||
|
||
_exclude_dirs = [u'vendor'] | ||
|
||
|
||
@click.command(name=u'minify') | ||
@click.option( | ||
u'--clean', is_flag=True, help=u'remove any minified files in the path.') | ||
@click.argument(u'path', nargs=-1, type=click.Path()) | ||
def minify(clean, path): | ||
u'''Create minified versions of the given Javascript and CSS files.''' | ||
for base_path in path: | ||
if os.path.isfile(base_path): | ||
if clean: | ||
_clear_minifyed(base_path) | ||
else: | ||
_minify_file(base_path) | ||
elif os.path.isdir(base_path): | ||
for root, dirs, files in os.walk(base_path): | ||
dirs[:] = [d for d in dirs if d not in _exclude_dirs] | ||
for filename in files: | ||
path = os.path.join(root, filename) | ||
if clean: | ||
_clear_minifyed(path) | ||
else: | ||
_minify_file(path) | ||
else: | ||
# Path is neither a file or a dir? | ||
continue | ||
|
||
|
||
def _clear_minifyed(path): | ||
u'''Remove the minified version of the file''' | ||
path_only, extension = os.path.splitext(path) | ||
|
||
if extension not in (u'.css', u'.js'): | ||
# This is not a js or css file. | ||
return | ||
|
||
if path_only.endswith(u'.min'): | ||
click.echo(u'removing {}'.format(path)) | ||
os.remove(path) | ||
|
||
|
||
def _minify_file(path): | ||
u'''Create the minified version of the given file. | ||
If the file is not a .js or .css file (e.g. it's a .min.js or .min.css | ||
file, or it's some other type of file entirely) it will not be | ||
minifed. | ||
:param path: The path to the .js or .css file to minify | ||
''' | ||
import ckan.lib.fanstatic_resources as fanstatic_resources | ||
path_only, extension = os.path.splitext(path) | ||
|
||
if path_only.endswith(u'.min'): | ||
# This is already a minified file. | ||
return | ||
|
||
if extension not in (u'.css', u'.js'): | ||
# This is not a js or css file. | ||
return | ||
|
||
path_min = fanstatic_resources.min_path(path) | ||
|
||
source = open(path, u'r').read() | ||
f = open(path_min, u'w') | ||
if path.endswith(u'.css'): | ||
f.write(rcssmin.cssmin(source)) | ||
elif path.endswith(u'.js'): | ||
f.write(rjsmin.jsmin(source)) | ||
f.close() | ||
click.echo(u"Minified file '{}'".format(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# encoding: utf-8 | ||
|
||
import click | ||
from ckan.model import Session, Package, DomainObjectOperation | ||
from ckan.model.modification import DomainObjectModificationExtension | ||
|
||
|
||
@click.group( | ||
name=u'notify', | ||
short_help=u'Send out modification notifications.' | ||
) | ||
def notify(): | ||
pass | ||
|
||
|
||
@notify.command( | ||
name=u'replay', | ||
short_help=u'Send out modification signals.' | ||
) | ||
def replay(): | ||
dome = DomainObjectModificationExtension() | ||
for package in Session.query(Package): | ||
dome.notify(package, DomainObjectOperation.changed) |
Oops, something went wrong.