##// END OF EJS Templates
Merge pull request #6658 from Carreau/less-175...
Min RK -
r18241:2ebd2f86 merge
parent child Browse files
Show More
@@ -1,85 +1,85 b''
1 """ fabfile to prepare the notebook """
1 """ fabfile to prepare the notebook """
2
2
3 from fabric.api import local,lcd
3 from fabric.api import local,lcd
4 from fabric.utils import abort
4 from fabric.utils import abort
5 import os
5 import os
6 from distutils.version import LooseVersion as V
6 from distutils.version import LooseVersion as V
7 from subprocess import check_output
7 from subprocess import check_output
8
8
9 pjoin = os.path.join
9 pjoin = os.path.join
10 static_dir = 'static'
10 static_dir = 'static'
11 components_dir = pjoin(static_dir, 'components')
11 components_dir = pjoin(static_dir, 'components')
12 here = os.path.dirname(__file__)
12 here = os.path.dirname(__file__)
13
13
14 min_less_version = '1.7.0'
14 min_less_version = '1.7.0'
15 max_less_version = '1.8.0' # exclusive
15 max_less_version = '1.7.5' # exclusive
16
16
17 def _need_css_update():
17 def _need_css_update():
18 """Does less need to run?"""
18 """Does less need to run?"""
19
19
20 static_path = pjoin(here, static_dir)
20 static_path = pjoin(here, static_dir)
21 css_targets = [
21 css_targets = [
22 pjoin(static_path, 'style', '%s.min.css' % name)
22 pjoin(static_path, 'style', '%s.min.css' % name)
23 for name in ('style', 'ipython')
23 for name in ('style', 'ipython')
24 ]
24 ]
25 css_maps = [t + '.map' for t in css_targets]
25 css_maps = [t + '.map' for t in css_targets]
26 targets = css_targets + css_maps
26 targets = css_targets + css_maps
27 if not all(os.path.exists(t) for t in targets):
27 if not all(os.path.exists(t) for t in targets):
28 # some generated files don't exist
28 # some generated files don't exist
29 return True
29 return True
30 earliest_target = sorted(os.stat(t).st_mtime for t in targets)[0]
30 earliest_target = sorted(os.stat(t).st_mtime for t in targets)[0]
31
31
32 # check if any .less files are newer than the generated targets
32 # check if any .less files are newer than the generated targets
33 for (dirpath, dirnames, filenames) in os.walk(static_path):
33 for (dirpath, dirnames, filenames) in os.walk(static_path):
34 for f in filenames:
34 for f in filenames:
35 if f.endswith('.less'):
35 if f.endswith('.less'):
36 path = pjoin(static_path, dirpath, f)
36 path = pjoin(static_path, dirpath, f)
37 timestamp = os.stat(path).st_mtime
37 timestamp = os.stat(path).st_mtime
38 if timestamp > earliest_target:
38 if timestamp > earliest_target:
39 return True
39 return True
40
40
41 return False
41 return False
42
42
43 def css(minify=False, verbose=False, force=False):
43 def css(minify=False, verbose=False, force=False):
44 """generate the css from less files"""
44 """generate the css from less files"""
45 minify = _to_bool(minify)
45 minify = _to_bool(minify)
46 verbose = _to_bool(verbose)
46 verbose = _to_bool(verbose)
47 force = _to_bool(force)
47 force = _to_bool(force)
48 # minify implies force because it's not the default behavior
48 # minify implies force because it's not the default behavior
49 if not force and not minify and not _need_css_update():
49 if not force and not minify and not _need_css_update():
50 print("css up-to-date")
50 print("css up-to-date")
51 return
51 return
52
52
53 for name in ('style', 'ipython'):
53 for name in ('style', 'ipython'):
54 source = pjoin('style', "%s.less" % name)
54 source = pjoin('style', "%s.less" % name)
55 target = pjoin('style', "%s.min.css" % name)
55 target = pjoin('style', "%s.min.css" % name)
56 sourcemap = pjoin('style', "%s.min.css.map" % name)
56 sourcemap = pjoin('style', "%s.min.css.map" % name)
57 _compile_less(source, target, sourcemap, minify, verbose)
57 _compile_less(source, target, sourcemap, minify, verbose)
58
58
59 def _to_bool(b):
59 def _to_bool(b):
60 if not b in ['True', 'False', True, False]:
60 if not b in ['True', 'False', True, False]:
61 abort('boolean expected, got: %s' % b)
61 abort('boolean expected, got: %s' % b)
62 return (b in ['True', True])
62 return (b in ['True', True])
63
63
64 def _compile_less(source, target, sourcemap, minify=True, verbose=False):
64 def _compile_less(source, target, sourcemap, minify=True, verbose=False):
65 """Compile a less file by source and target relative to static_dir"""
65 """Compile a less file by source and target relative to static_dir"""
66 min_flag = '-x' if minify is True else ''
66 min_flag = '-x' if minify is True else ''
67 ver_flag = '--verbose' if verbose is True else ''
67 ver_flag = '--verbose' if verbose is True else ''
68
68
69 # pin less to version number from above
69 # pin less to version number from above
70 try:
70 try:
71 out = check_output(['lessc', '--version'])
71 out = check_output(['lessc', '--version'])
72 except OSError as err:
72 except OSError as err:
73 raise ValueError("Unable to find lessc. Please install lessc >= %s and < %s " \
73 raise ValueError("Unable to find lessc. Please install lessc >= %s and < %s " \
74 % (min_less_version, max_less_version))
74 % (min_less_version, max_less_version))
75 out = out.decode('utf8', 'replace')
75 out = out.decode('utf8', 'replace')
76 less_version = out.split()[1]
76 less_version = out.split()[1]
77 if V(less_version) < V(min_less_version):
77 if V(less_version) < V(min_less_version):
78 raise ValueError("lessc too old: %s < %s. Use `$ npm install lesscss@X.Y.Z` to install a specific version of less" % (less_version, min_less_version))
78 raise ValueError("lessc too old: %s < %s. Use `$ npm install lesscss@X.Y.Z` to install a specific version of less" % (less_version, min_less_version))
79 if V(less_version) >= V(max_less_version):
79 if V(less_version) >= V(max_less_version):
80 raise ValueError("lessc too new: %s >= %s. Use `$ npm install lesscss@X.Y.Z` to install a specific version of less" % (less_version, max_less_version))
80 raise ValueError("lessc too new: %s >= %s. Use `$ npm install lesscss@X.Y.Z` to install a specific version of less" % (less_version, max_less_version))
81
81
82 static_path = pjoin(here, static_dir)
82 static_path = pjoin(here, static_dir)
83 with lcd(static_dir):
83 with lcd(static_dir):
84 local('lessc {min_flag} {ver_flag} --source-map={sourcemap} --source-map-basepath={static_path} --source-map-rootpath="../" {source} {target}'.format(**locals()))
84 local('lessc {min_flag} {ver_flag} --source-map={sourcemap} --source-map-basepath={static_path} --source-map-rootpath="../" {source} {target}'.format(**locals()))
85
85
General Comments 0
You need to be logged in to leave comments. Login now