rakefile 5.28 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
def select_executable(*cmds)
    cmds.find_all{ |cmd| system("which #{cmd} > /dev/null 2>&1") }[0] || fail("No executables found from #{cmds.join(', ')}")
end

37 38 39 40
def django_admin(system, env, command, *args)
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
    return "#{django_admin} #{command} --settings=#{system}.envs.#{env} --pythonpath=. #{args.join(' ')}"
end
41

42 43 44
task :default => [:pep8, :pylint, :test]

directory REPORT_DIR
45
directory LMS_REPORT_DIR
46

47 48 49
desc "Run pep8 on all libraries"
task :pep8 => REPORT_DIR do
    sh("pep8 --ignore=E501 lms/djangoapps common/lib/* | tee #{REPORT_DIR}/pep8.report")
50 51
end

52 53 54 55
desc "Run pylint on all libraries"
task :pylint => REPORT_DIR do
    Dir["lms/djangoapps/*", "common/lib/*"].each do |app|
        ENV['PYTHONPATH'] = File.dirname(app)
56
        app = File.basename(app)
57
        sh("pylint --rcfile=.pylintrc -f parseable #{app} | tee #{REPORT_DIR}/#{app}.pylint.report")
Calen Pennington committed
58 59 60
    end
end

61
[:lms, :cms].each do |system|
62 63 64 65 66 67 68
    task_name = "test_#{system}"
    report_dir = File.join(REPORT_DIR, task_name)
    directory report_dir

    desc "Run all django tests on our djangoapps for the #{system}"
    task task_name => report_dir do
        ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
69
        ENV['NOSE_COVER_HTML_DIR'] = File.join(report_dir, "cover")
70
        sh(django_admin(system, :test, 'test', *Dir["#{system}/djangoapps/*"].each))
71 72
    end
    task :test => task_name
73 74 75 76 77 78 79 80 81

    desc <<-desc
        Start the #{system} locally with the specified environment (defaults to dev).
        Other useful environments are devplus (for dev testing with a real local database)
        desc
    task system, [:env, :options] => [] do |t, args|
        args.with_defaults(:env => 'dev', :options => '')
        sh(django_admin(system, args.env, 'runserver', args.options))
    end
82 83 84 85 86
end

Dir["common/lib/*"].each do |lib|
    task_name = "test_#{lib}"

87
    report_dir = File.join(REPORT_DIR, task_name.gsub('/', '_'))
88 89 90
    directory report_dir

    desc "Run tests for common lib #{lib}"
91
    task task_name => report_dir do
92
        ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
93
        sh("nosetests #{lib} --cover-erase --with-xunit --with-xcoverage --cover-html --cover-inclusive --cover-package #{File.basename(lib)} --cover-html-dir #{File.join(report_dir, "cover")}")
94 95
    end
    task :test => task_name
96
end
97

98 99
task :runserver => :lms

100 101 102 103 104 105
desc "Run django-admin <action> against the specified system and environment"
task "django-admin", [:action, :system, :env, :options] do |t, args|
    args.with_defaults(:env => 'dev', :system => 'lms', :options => '')
    sh(django_admin(args.system, args.env, args.action, args.options))
end

106
task :package do
107 108 109
    FileUtils.mkdir_p(BUILD_DIR)
    
    Dir.chdir(BUILD_DIR) do
110 111 112 113 114 115

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

117
        chown -R makeitso:makeitso #{INSTALL_DIR_PATH}
118
        chmod +x #{INSTALL_DIR_PATH}/collect_static_resources
119

120
        service gunicorn stop || echo "Unable to stop gunicorn. Continuing"
121
        rm -f #{LINK_PATH}
122
        ln -s #{INSTALL_DIR_PATH} #{LINK_PATH}
123
        chown makeitso:makeitso #{LINK_PATH}
124 125 126

        /opt/wwc/mitx/collect_static_resources

127
        # Delete mako temp files
128
        rm -rf /tmp/tmp*mako
129

130 131 132 133 134
        service gunicorn start
        POSTINSTALL
        postinstall.close()
        FileUtils.chmod(0755, postinstall.path)

135
        args = ["fakeroot", "fpm", "-s", "dir", "-t", "deb",
136
            "--verbose",
137 138
            "--after-install=#{postinstall.path}",
            "--prefix=#{INSTALL_DIR_PATH}",
139 140 141
            "--exclude=**/build/**",
            "--exclude=**/rakefile",
            "--exclude=**/.git/**",
142
            "--exclude=**/*.pyc",
143
            "--exclude=**/reports/**",
144
            "-C", "#{REPO_ROOT}",
145
            "--provides=#{PACKAGE_NAME}",
146
            "--name=#{NORMALIZED_DEPLOY_NAME}",
147
            "--version=#{PKG_VERSION}",
148
            "-a", "all",
149
            "."]
150 151 152
        system(*args) || raise("fpm failed to build the .deb")
    end
end
153 154

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