Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
8d7a2f31
Commit
8d7a2f31
authored
Jun 03, 2013
by
Nate Hardison
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from edx/nate/asset-preprocessing-mgmt-cmd
Preprocess assets as Django management command
parents
4724a35f
03a9765b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
130 additions
and
48 deletions
+130
-48
cms/envs/common.py
+1
-0
common/djangoapps/mitxmako/management/__init__.py
+0
-0
common/djangoapps/mitxmako/management/commands/__init__.py
+0
-0
common/djangoapps/mitxmako/management/commands/preprocess_assets.py
+65
-0
lms/envs/common.py
+1
-0
rakefiles/assets.rake
+21
-36
rakefiles/django.rake
+10
-2
rakefiles/jasmine.rake
+32
-10
No files found.
cms/envs/common.py
View file @
8d7a2f31
...
@@ -323,6 +323,7 @@ INSTALLED_APPS = (
...
@@ -323,6 +323,7 @@ INSTALLED_APPS = (
'track'
,
'track'
,
# For asset pipelining
# For asset pipelining
'mitxmako'
,
'pipeline'
,
'pipeline'
,
'staticfiles'
,
'staticfiles'
,
'static_replace'
,
'static_replace'
,
...
...
common/djangoapps/mitxmako/management/__init__.py
0 → 100644
View file @
8d7a2f31
common/djangoapps/mitxmako/management/commands/__init__.py
0 → 100644
View file @
8d7a2f31
common/djangoapps/mitxmako/management/commands/preprocess_assets.py
0 → 100644
View file @
8d7a2f31
"""
Preprocess templatized asset files, enabling asset authors to use
Python/Django inside of Sass and CoffeeScript. This preprocessing
will happen before the invocation of the asset compiler (currently
handled by the asset Rakefile).
For this to work, assets need to be named with the appropriate
template extension (e.g., .mako for Mako templates). Currently Mako
is the only template engine supported.
"""
import
os
from
django.core.management.base
import
NoArgsCommand
from
django.conf
import
settings
from
mako.template
import
Template
class
Command
(
NoArgsCommand
):
"""
Basic management command to preprocess asset template files.
"""
help
=
"Preprocess asset template files to ready them for compilation."
def
handle_noargs
(
self
,
**
options
):
"""
Walk over all of the static files directories specified in the
settings file, looking for asset template files (indicated by
a file extension like .mako).
"""
for
staticfiles_dir
in
getattr
(
settings
,
"STATICFILES_DIRS"
,
[]):
# Cribbed from the django-staticfiles app at:
# https://github.com/jezdez/django-staticfiles/blob/develop/staticfiles/finders.py#L52
if
isinstance
(
staticfiles_dir
,
(
list
,
tuple
)):
prefix
,
staticfiles_dir
=
staticfiles_dir
# Walk over the current static files directory tree,
# preprocessing files that have a template extension.
for
root
,
dirs
,
files
in
os
.
walk
(
staticfiles_dir
):
for
filename
in
files
:
outfile
,
extension
=
os
.
path
.
splitext
(
filename
)
# We currently only handle Mako templates
if
extension
==
".mako"
:
self
.
__preprocess
(
os
.
path
.
join
(
root
,
filename
),
os
.
path
.
join
(
root
,
outfile
))
def
__context
(
self
):
"""
Return a dict that contains all of the available context
variables to the asset template.
"""
# TODO: do we need to include anything else?
# TODO: do this with the django-settings-context-processor
return
{
"THEME_NAME"
:
getattr
(
settings
,
"THEME_NAME"
,
None
)
}
def
__preprocess
(
self
,
infile
,
outfile
):
"""
Run `infile` through the Mako template engine, storing the
result in `outfile`.
"""
with
open
(
outfile
,
"w"
)
as
_outfile
:
_outfile
.
write
(
Template
(
filename
=
str
(
infile
))
.
render
(
env
=
self
.
__context
()))
lms/envs/common.py
View file @
8d7a2f31
...
@@ -662,6 +662,7 @@ INSTALLED_APPS = (
...
@@ -662,6 +662,7 @@ INSTALLED_APPS = (
'service_status'
,
'service_status'
,
# For asset pipelining
# For asset pipelining
'mitxmako'
,
'pipeline'
,
'pipeline'
,
'staticfiles'
,
'staticfiles'
,
'static_replace'
,
'static_replace'
,
...
...
rakefiles/assets.rake
View file @
8d7a2f31
...
@@ -6,30 +6,6 @@ if USE_CUSTOM_THEME
...
@@ -6,30 +6,6 @@ if USE_CUSTOM_THEME
THEME_SASS
=
File
.
join
(
THEME_ROOT
,
"static"
,
"sass"
)
THEME_SASS
=
File
.
join
(
THEME_ROOT
,
"static"
,
"sass"
)
end
end
# Run the specified file through the Mako templating engine, providing
# the ENV_TOKENS to the templating context.
def
preprocess_with_mako
(
filename
)
# simple command-line invocation of Mako engine
# cdodge: the .gsub() are used to translate true->True and false->False to make the generated
# python actually valid python. This is just a short term hack to unblock the release train
# until a real fix can be made by people who know this better
mako
=
"from mako.template import Template;"
+
"print Template(filename=
\"
#{
filename
}
\"
)"
+
# Total hack. It works because a Python dict literal has
# the same format as a JSON object.
".render(env=
#{
ENV_TOKENS
.
to_json
.
gsub
(
"true"
,
"True"
).
gsub
(
"false"
,
"False"
)
}
);"
# strip off the .mako extension
output_filename
=
filename
.
chomp
(
File
.
extname
(
filename
))
# just pipe from stdout into the new file, exiting on failure
File
.
open
(
output_filename
,
'w'
)
do
|
file
|
file
.
write
(
`python -c '
#{
mako
}
'`
)
exit_code
=
$?
.
to_i
abort
"
#{
mako
}
failed with
#{
exit_code
}
"
if
exit_code
.
to_i
!=
0
end
end
def
xmodule_cmd
(
watch
=
false
,
debug
=
false
)
def
xmodule_cmd
(
watch
=
false
,
debug
=
false
)
xmodule_cmd
=
'xmodule_assets common/static/xmodule'
xmodule_cmd
=
'xmodule_assets common/static/xmodule'
if
watch
if
watch
...
@@ -84,11 +60,12 @@ namespace :assets do
...
@@ -84,11 +60,12 @@ namespace :assets do
desc
"Compile all assets in debug mode"
desc
"Compile all assets in debug mode"
multitask
:debug
multitask
:debug
desc
"Preprocess all static assets that have the .mako extension"
desc
"Preprocess all templatized static asset files"
task
:preprocess
do
task
:preprocess
,
[
:system
,
:env
]
do
|
t
,
args
|
# Run assets through the Mako templating engine. Right now we
args
.
with_defaults
(
:system
=>
"lms"
,
:env
=>
"dev"
)
# just hardcode the asset filenames.
sh
(
django_admin
(
args
.
system
,
args
.
env
,
"preprocess_assets"
))
do
|
ok
,
status
|
preprocess_with_mako
(
"lms/static/sass/application.scss.mako"
)
abort
"asset preprocessing failed!"
if
!
ok
end
end
end
desc
"Watch all assets for changes and automatically recompile"
desc
"Watch all assets for changes and automatically recompile"
...
@@ -138,7 +115,6 @@ namespace :assets do
...
@@ -138,7 +115,6 @@ namespace :assets do
end
end
end
end
multitask
:sass
=>
'assets:xmodule'
multitask
:sass
=>
'assets:xmodule'
namespace
:sass
do
namespace
:sass
do
# In watch mode, sass doesn't immediately compile out of date files,
# In watch mode, sass doesn't immediately compile out of date files,
...
@@ -153,16 +129,25 @@ namespace :assets do
...
@@ -153,16 +129,25 @@ namespace :assets do
end
end
end
end
# This task does the real heavy lifting to gather all of the static
# assets. We want people to call it via the wrapper below, so we
# don't provide a description so that it won't show up in rake -T.
task
:gather_assets
,
[
:system
,
:env
]
=>
:assets
do
|
t
,
args
|
sh
(
"
#{
django_admin
(
args
.
system
,
args
.
env
,
'collectstatic'
,
'--noinput'
)
}
> /dev/null"
)
do
|
ok
,
status
|
if
!
ok
abort
"collectstatic failed!"
end
end
end
[
:lms
,
:cms
].
each
do
|
system
|
[
:lms
,
:cms
].
each
do
|
system
|
# Per environment tasks
# Per environment tasks
environments
(
system
).
each
do
|
env
|
environments
(
system
).
each
do
|
env
|
# This task wraps the one above, since we need the system and
# env arguments to be passed to all dependent tasks.
desc
"Compile coffeescript and sass, and then run collectstatic in the specified environment"
desc
"Compile coffeescript and sass, and then run collectstatic in the specified environment"
task
"
#{
system
}
:gather_assets:
#{
env
}
"
=>
:assets
do
task
"
#{
system
}
:gather_assets:
#{
env
}
"
do
sh
(
"
#{
django_admin
(
system
,
env
,
'collectstatic'
,
'--noinput'
)
}
> /dev/null"
)
do
|
ok
,
status
|
Rake
::
Task
[
:gather_assets
].
invoke
(
system
,
env
)
if
!
ok
abort
"collectstatic failed!"
end
end
end
end
end
end
end
end
rakefiles/django.rake
View file @
8d7a2f31
...
@@ -15,14 +15,22 @@ task :fastlms do
...
@@ -15,14 +15,22 @@ task :fastlms do
sh
(
"
#{
django_admin
}
runserver --traceback --settings=lms.envs.dev --pythonpath=."
)
sh
(
"
#{
django_admin
}
runserver --traceback --settings=lms.envs.dev --pythonpath=."
)
end
end
# Start :system locally with the specified :env and :options.
#
# This task should be invoked via the wrapper below, so we don't
# include a description to keep it from showing up in rake -T.
task
:runserver
,
[
:system
,
:env
,
:options
]
=>
[
:install_prereqs
,
'assets:_watch'
,
:predjango
]
do
|
t
,
args
|
sh
(
django_admin
(
args
.
system
,
args
.
env
,
'runserver'
,
args
.
options
))
end
[
:lms
,
:cms
].
each
do
|
system
|
[
:lms
,
:cms
].
each
do
|
system
|
desc
<<-
desc
desc
<<-
desc
Start the
#{
system
}
locally with the specified environment (defaults to dev).
Start the
#{
system
}
locally with the specified environment (defaults to dev).
Other useful environments are devplus (for dev testing with a real local database)
Other useful environments are devplus (for dev testing with a real local database)
desc
desc
task
system
,
[
:env
,
:options
]
=>
[
:install_prereqs
,
'assets:_watch'
,
:predjango
]
do
|
t
,
args
|
task
system
,
[
:env
,
:options
]
do
|
t
,
args
|
args
.
with_defaults
(
:env
=>
'dev'
,
:options
=>
default_options
[
system
])
args
.
with_defaults
(
:env
=>
'dev'
,
:options
=>
default_options
[
system
])
sh
(
django_admin
(
system
,
args
.
env
,
'runserver'
,
args
.
options
)
)
Rake
::
Task
[
:runserver
].
invoke
(
system
,
args
.
env
,
args
.
options
)
end
end
desc
"Start
#{
system
}
Celery worker"
desc
"Start
#{
system
}
Celery worker"
...
...
rakefiles/jasmine.rake
View file @
8d7a2f31
...
@@ -73,21 +73,43 @@ def run_phantom_js(url)
...
@@ -73,21 +73,43 @@ def run_phantom_js(url)
sh
(
"
#{
phantomjs
}
node_modules/jasmine-reporters/test/phantomjs-testrunner.js
#{
url
}
"
)
sh
(
"
#{
phantomjs
}
node_modules/jasmine-reporters/test/phantomjs-testrunner.js
#{
url
}
"
)
end
end
# Open jasmine tests for :system in the default browser. The :env
# should (always?) be 'jasmine', but it's passed as an arg so that
# the :assets dependency gets it.
#
# This task should be invoked via the wrapper below, so we don't
# include a description to keep it from showing up in rake -T.
task
:browse_jasmine
,
[
:system
,
:env
]
=>
:assets
do
|
t
,
args
|
django_for_jasmine
(
args
.
system
,
true
)
do
|
jasmine_url
|
Launchy
.
open
(
jasmine_url
)
puts
"Press ENTER to terminate"
.
red
$stdin
.
gets
end
end
# Use phantomjs to run jasmine tests from the console. The :env
# should (always?) be 'jasmine', but it's passed as an arg so that
# the :assets dependency gets it.
#
# This task should be invoked via the wrapper below, so we don't
# include a description to keep it from showing up in rake -T.
task
:phantomjs_jasmine
,
[
:system
,
:env
]
=>
:assets
do
|
t
,
args
|
django_for_jasmine
(
args
.
system
,
false
)
do
|
jasmine_url
|
run_phantom_js
(
jasmine_url
)
end
end
# Wrapper tasks for the real browse_jasmine and phantomjs_jasmine
# tasks above. These have a nicer UI since there's no arg passing.
[
:lms
,
:cms
].
each
do
|
system
|
[
:lms
,
:cms
].
each
do
|
system
|
desc
"Open jasmine tests for
#{
system
}
in your default browser"
desc
"Open jasmine tests for
#{
system
}
in your default browser"
task
"browse_jasmine_
#{
system
}
"
=>
:assets
do
task
"browse_jasmine_
#{
system
}
"
do
django_for_jasmine
(
system
,
true
)
do
|
jasmine_url
|
Rake
::
Task
[
:browse_jasmine
].
invoke
(
system
,
'jasmine'
)
Launchy
.
open
(
jasmine_url
)
puts
"Press ENTER to terminate"
.
red
$stdin
.
gets
end
end
end
desc
"Use phantomjs to run jasmine tests for
#{
system
}
from the console"
desc
"Use phantomjs to run jasmine tests for
#{
system
}
from the console"
task
"phantomjs_jasmine_
#{
system
}
"
=>
:assets
do
task
"phantomjs_jasmine_
#{
system
}
"
do
django_for_jasmine
(
system
,
false
)
do
|
jasmine_url
|
Rake
::
Task
[
:phantomjs_jasmine
].
invoke
(
system
,
'jasmine'
)
run_phantom_js
(
jasmine_url
)
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment