Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/setup.py
blob: 89748bcb48c9381bf85d6efa32042e4ed0758a87 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import shutil
import tempfile
import subprocess
from glob import glob
from ConfigParser import ConfigParser
from os.path import join, dirname, abspath, lexists, islink, isdir, exists
from os.path import basename
from gettext import gettext as _


COMPONENTS = ['Edit', 'Jam', 'Mini', 'SynthLab']

FULL_COMPONENT_NAMES = [
        # TRANS: Full activity name that will be used in Sugar Shell
        _('TamTamEdit'),
        # TRANS: Full activity name that will be used in Sugar Shell
        _('TamTamJam'),
        # TRANS: Full activity name that will be used in Sugar Shell
        _('TamTamMini'),
        # TRANS: Full activity name that will be used in Sugar Shell
        _('TamTamSynthLab')]


def link_activities(dst_root, cp_cmd):
    for component in COMPONENTS:
        activity_dir = join(dst_root, 'TamTam%s.activity' % component)

        if lexists(activity_dir):
            if islink(activity_dir):
                os.unlink(activity_dir)
            else:
                shutil.rmtree(activity_dir)
        os.makedirs(join(activity_dir, 'activity'))

        for i in [component, 'icons', 'common', 'locale', 'po',
                'TamTam%s.py' % component,
                join('activity', 'TamTam%s.svg' % component),
                'AUTHORS', 'COPYING', 'HACKING', 'NEWS', 'README']:
            if exists(join(src_root, i)):
                cp_cmd(join(src_root, i), join(activity_dir, i))

        info = ConfigParser()
        info.read(join(src_root, 'activity', 'activity.info'))
        info.set('DEFAULT', 'component_id', component.lower())
        info.set('DEFAULT', 'component_name', 'TamTam%s' % component)
        for key, value in info.items('Activity'):
            # Replace % expansions since ASLO doesn't understand them
            info.set('Activity', key, value)
        info_file = file(join(activity_dir, 'activity', 'activity.info'), 'w')
        info.write(info_file)
        info_file.close()
        info.read(join(src_root, 'activity', 'activity.info'))

        setup_file = file(join(activity_dir, 'setup.py'), 'w')
        setup_file.write('#!/usr/bin/env python\n')
        setup_file.write('from sugar.activity import bundlebuilder\n')
        setup_file.write('bundlebuilder.start()\n')
        setup_file.close()
        os.chmod(setup_file.name, 0755)


def walk_activities(*commands):
    dst_root = tempfile.mkdtemp(dir=join(src_root, '..'))
    link_activities(dst_root, link_tree)
    if not exists(join(src_root, 'dist')):
        os.makedirs(join(src_root, 'dist'))

    for component in COMPONENTS:
        for cmd in commands:
            print '-- %s %s activity' % (' '.join(cmd), component)

            activity_dir = join(dst_root, 'TamTam%s.activity' % component)
            subprocess.check_call(['python', 'setup.py'] + cmd,
                    cwd=activity_dir)

            for i in glob(join(activity_dir, 'dist', '*.xo')) + \
                    glob(join(activity_dir, 'dist', '*.tar.*')):
                os.rename(i, join(src_root, 'dist', basename(i)))

    shutil.rmtree(dst_root)


def link_tree(src, dst):
    if abspath(src) == abspath(dst):
        return

    do_copy = []

    def link(src, dst):
        if not exists(dirname(dst)):
            os.makedirs(dirname(dst))
        if do_copy:
            shutil.copy(src, dst)
        else:
            try:
                os.link(src, dst)
            except OSError:
                do_copy.append(True)
                shutil.copy(src, dst)

    if isdir(src):
        for root, __, files in os.walk(src):
            dst_root = join(dst, root[len(src):].lstrip(os.sep))
            if not exists(dst_root):
                os.makedirs(dst_root)
            for i in files:
                link(join(root, i), join(dst_root, i))
    else:
        link(src, dst)


if len(sys.argv) == 1:
    print """\
Available commands:

dev [PATH]           Create symlinked activity directories in PATH
                     (default is "..") to run TamTam activities in development
                     environment from ~/Activities directory
dist_xo              Create xo bundles for all TamTam activities
dist_source          Create a tar source bundles for all TamTam activities
genpot               Generate the gettext pot file
install              Install activities in the system; it is mpstly for
                     packagers to use in package spec files; export DESTDIR
                     environment variable to set destination directory
"""
    exit(0)

src_root = abspath(dirname(__file__))

if sys.argv[1] == 'dev':
    if len(sys.argv) > 2:
        dst_root = sys.argv[2]
    else:
        dst_root = join(src_root, '..')
    link_activities(dst_root, os.symlink)
elif sys.argv[1] in ['dist_source', 'genpot']:
    from sugar.activity import bundlebuilder
    bundlebuilder.start()
elif sys.argv[1] == 'dist_xo':
    walk_activities(['build'], ['dist_xo'])
elif sys.argv[1] == 'install':
    destdir = os.environ.get('DESTDIR', '')
    walk_activities(['build'], ['install', '--prefix=%s/usr' % destdir])
else:
    print 'Unknown command %s' % sys.argv[1]
    exit(1)