tasks.py 3.87 KB
Newer Older
Pietro Albini's avatar
Pietro Albini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# Ubuntu-it's website
# Copyright (C) 2015 Pietro Albini
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import shutil
import glob
import re

import invoke


BASE = os.path.dirname(__file__)
PYTHON = "python3"
PROJECT = "uitwww"


30 31 32 33 34 35
# This is because invoke can't keep backward compatibility...
_invoke_v = invoke.__version__.split(".")
if int(_invoke_v[0]) == 0 and int(_invoke_v[1]) <= 12:
    invoke.task = invoke.ctask


Pietro Albini's avatar
Pietro Albini committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
def create_env(name, requirements=False, self=False, force=False):
    """Create a new virtual environment"""
    path = os.path.join(BASE, "build", "envs", name)

    # Don't re-create the environment if force is False
    if os.path.exists(path):
        if force:
            shutil.rmtree(path)
        else:
            return path

    invoke.run("virtualenv -p %s %s" % (PYTHON, path))
    if requirements:
        invoke.run("%s/bin/pip install -r requirements-%s.txt" % (path, name))
    if self:
        invoke.run("%s/bin/pip install -e ." % path)

    return path


@invoke.task
57
def clean(ctx):
Pietro Albini's avatar
Pietro Albini committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    """Clean all the build things"""
    for dir in "build", "%s.egg-info" % PROJECT:
        path = os.path.join(BASE, dir)
        if not os.path.exists(path):
            continue
        shutil.rmtree(path)

    exclude = ["%s/.git" % BASE, "%s/build" % BASE]
    remove_files = [
        re.compile(r'.py[co]$'), re.compile(r'.mo$'),
        "%s/uitwww/static/website.css" % BASE,
    ]
    remove_dirs = [
        re.compile('__pycache__'),
    ]

    # Remove all the unwanted things
    for root, dirs, files in os.walk(BASE, topdown=False):
        # Skip excluded directories
        skip = False
        for one in exclude:
            if one == root or (root != BASE and one.startswith(root)):
                skip = True
                break
        if skip:
            continue

        # Remove all the unwanted files
        for file in files:
            file = os.path.join(root, file)
            for regex in remove_files:
                if type(regex) == str:
                    if file != regex:
                        continue
                elif not regex.search(file):
                    continue

                os.remove(file)

        # Remove all the unwanted directories
        for dir in dirs:
            dir = os.path.join(root, dir)
            for regex in remove_dirs:
                if type(regex) == str:
                    if dir != regex:
                        continue
                elif not regex.search(dir):
                    continue

                shutil.rmtree(dir)


@invoke.task
111
def assets(ctx):
Pietro Albini's avatar
Pietro Albini committed
112
    """Build the assets"""
113 114 115 116 117
    env = create_env("assets", requirements=True)
    invoke.run(
        "%s/bin/python -mscss < %s/assets/scss/www.scss > %s/uitwww/static/website.css"
        % (env, BASE, BASE)
    )
Pietro Albini's avatar
Pietro Albini committed
118 119 120


@invoke.task(pre=[assets])
121
def devel(ctx):
Pietro Albini's avatar
Pietro Albini committed
122 123 124 125 126
    """Setup the development environment"""
    create_env("devel", self=True, force=True)


@invoke.task(pre=[assets])
127
def build(ctx):
Pietro Albini's avatar
Pietro Albini committed
128 129 130 131 132 133 134 135 136 137
    """Create a new build"""
    env = create_env("build", requirements=True)

    out = os.path.join(BASE, "build", "packages")
    if os.path.exists(out):
        for file in glob.glob(os.path.join(out, "*")):
            os.remove(file)

    for type in "sdist", "bdist_wheel":
        invoke.run("%s/bin/python setup.py %s -d %s" % (env, type, out))