rakefile 4.08 KB
Newer Older
1 2 3
require 'rake/clean'
require 'tempfile'

4
# Build Constants
5 6
REPO_ROOT = File.dirname(__FILE__)
BUILD_DIR = File.join(REPO_ROOT, "build")
7
REPORT_DIR = File.join(REPO_ROOT, "reports")
8
LMS_REPORT_DIR = File.join(REPORT_DIR, "lms")
9 10

# Packaging constants
11
DEPLOY_DIR = "/opt/wwc"
12
PACKAGE_NAME = "mitx"
13
LINK_PATH = "/opt/wwc/mitx"
14
PKG_VERSION = "0.1"
15
COMMIT = (ENV["GIT_COMMIT"] || `git rev-parse HEAD`).chomp()[0, 10]
16
BRANCH = (ENV["GIT_BRANCH"] || `git symbolic-ref -q HEAD`).chomp().gsub('refs/heads/', '').gsub('origin/', '')
17 18 19
BUILD_NUMBER = (ENV["BUILD_NUMBER"] || "dev").chomp()

if BRANCH == "master"
20
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BUILD_NUMBER}-#{COMMIT}"
21
else
22
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BRANCH}-#{BUILD_NUMBER}-#{COMMIT}"
23 24 25
end
PACKAGE_REPO = "packages@gp.mitx.mit.edu:/opt/pkgrepo.incoming"

26 27
NORMALIZED_DEPLOY_NAME = DEPLOY_NAME.downcase().gsub(/[_\/]/, '-')
INSTALL_DIR_PATH = File.join(DEPLOY_DIR, NORMALIZED_DEPLOY_NAME)
28 29

# Set up the clean and clobber tasks
30
CLOBBER.include(BUILD_DIR, REPORT_DIR, 'cover*', '.coverage')
31
CLEAN.include("#{BUILD_DIR}/*.deb", "#{BUILD_DIR}/util")
32

33 34 35 36 37
def select_executable(*cmds)
    cmds.find_all{ |cmd| system("which #{cmd} > /dev/null 2>&1") }[0] || fail("No executables found from #{cmds.join(', ')}")
end


38 39 40
task :default => [:pep8, :pylint, :test]

directory REPORT_DIR
41
directory LMS_REPORT_DIR
42

43
desc "Run pep8 on all of djangoapps"
44 45
task :pep8 => LMS_REPORT_DIR do
    sh("pep8 --ignore=E501 lms/djangoapps | tee #{LMS_REPORT_DIR}/pep8.report")
46 47
end

48
desc "Run pylint on all of djangoapps"
49 50 51 52 53
task :pylint => LMS_REPORT_DIR do
    ENV['PYTHONPATH'] = 'lms/djangoapps'
    Dir["lms/djangoapps/*"].each do |app|
        app = File.basename(app)
        sh("pylint -f parseable #{app} | tee #{LMS_REPORT_DIR}/#{app}.pylint.report")
Calen Pennington committed
54 55 56
    end
end

57
desc "Run all django tests on our djangoapps"
58 59
task :test => LMS_REPORT_DIR do
    ENV['NOSE_XUNIT_FILE'] = File.join(LMS_REPORT_DIR, "nosetests.xml")
60
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
61
    sh("#{django_admin} test --settings=lms.envs.test --pythonpath=. $(ls lms/djangoapps)")
62
end
63

64 65 66 67 68
desc <<-desc
    Start the lms locally with the specified environment (defaults to dev).
    Other useful environments are devplus (for dev testing with a real local database)
    desc
task :lms, [:env] => [] do |t, args|
69 70
    args.with_defaults(:env => 'dev')
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
71
    sh("#{django_admin} runserver --settings=lms.envs.#{args.env} --pythonpath=.")
72 73
end

74 75
task :runserver => :lms

76
task :package do
77 78 79
    FileUtils.mkdir_p(BUILD_DIR)
    
    Dir.chdir(BUILD_DIR) do
80 81 82 83 84 85

        postinstall = Tempfile.new('postinstall')
        postinstall.write <<-POSTINSTALL.gsub(/^\s*/, '')
        #! /bin/sh
        set -e
        set -x
86

87
        chown -R makeitso:makeitso #{INSTALL_DIR_PATH}
88
        chmod +x #{INSTALL_DIR_PATH}/collect_static_resources
89

90
        service gunicorn stop || echo "Unable to stop gunicorn. Continuing"
91
        rm -f #{LINK_PATH}
92
        ln -s #{INSTALL_DIR_PATH} #{LINK_PATH}
93
        chown makeitso:makeitso #{LINK_PATH}
94 95 96

        /opt/wwc/mitx/collect_static_resources

97
        # Delete mako temp files
98
        rm -rf /tmp/tmp*mako
99

100 101 102 103 104
        service gunicorn start
        POSTINSTALL
        postinstall.close()
        FileUtils.chmod(0755, postinstall.path)

105
        args = ["fakeroot", "fpm", "-s", "dir", "-t", "deb",
106
            "--verbose",
107 108
            "--after-install=#{postinstall.path}",
            "--prefix=#{INSTALL_DIR_PATH}",
109 110 111
            "--exclude=**/build/**",
            "--exclude=**/rakefile",
            "--exclude=**/.git/**",
112
            "--exclude=**/*.pyc",
113
            "--exclude=**/reports/**",
114
            "-C", "#{REPO_ROOT}",
115
            "--provides=#{PACKAGE_NAME}",
116
            "--name=#{NORMALIZED_DEPLOY_NAME}",
117
            "--version=#{PKG_VERSION}",
118
            "-a", "all",
119
            "."]
120 121 122
        system(*args) || raise("fpm failed to build the .deb")
    end
end
123 124

task :publish => :package do
125
    sh("scp #{BUILD_DIR}/#{NORMALIZED_DEPLOY_NAME}_#{PKG_VERSION}*.deb #{PACKAGE_REPO}")
126
end