aboutsummaryrefslogtreecommitdiff
path: root/cbc/cli/recipe.py
blob: b9a58b5e54fd0ce34df1f90ee2d1f94ad4d8eee8 (plain) (blame)
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
import argparse
import os
from cbc.parsers import CBCConfigParser, ExtendedInterpolation


RECIPE_TYPES = ['python',
               'make',
               'gnu',
               'scons',
               'cmake']

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--style', default='python')
    parser.add_argument('--name', default='')
    parser.add_argument('--version', default='')
    parser.add_argument('--build-number', default='0')
    parser.add_argument('--license', default='')
    parser.add_argument('--homepage', default='http://')
    parser.add_argument('--d2to1-hack', action='store_true', default=False)
    parser.add_argument('--use-git', action='store_true', default=False)
    parser.add_argument('--meta-package', action='store_true', default=False)
    parser.add_argument('recipe', default='generic.ini')
    args = parser.parse_args()

    NAME = args.name
    VERSION = args.version
    BUILD_NUMBER = args.build_number
    HOMEPAGE = args.homepage
    LICENSE = args.license
    D2TO1_HACK = {
        'active': False,
        'linux': '',
        'windows': ''
    }
    METAPACKAGE = args.meta_package
    STYLE = args.style
    RECIPE = args.recipe

    if args.use_git:
        VERSION = '{{ environ.get("GIT_DESCRIBE_TAG", "0.0.0") }}.git'
        BUILD_NUMBER = '{{ environ.get("GIT_DESCRIBE_NUMBER", 0) }}'

    if args.d2to1_hack:
        D2TO1_HACK['active'] = True


    if D2TO1_HACK['active']:
        D2TO1_HACK['linux'] = '''
#d2to1 hack active
pip install --no-deps --upgrade --force d2to1 || exit 1
    '''
        D2TO1_HACK['windows'] = '''
# d2to1 hack active
pip install --no-deps --upgrade --force d2to1
if errorlevel 1 exit 1
    '''

    config = CBCConfigParser(interpolation=ExtendedInterpolation(),
                                allow_no_value=True, delimiters=(':','='))

    config['package'] = {}
    config['package']['name'] = NAME
    config['package']['version'] = VERSION

    config['about'] = {}
    config['about']['home'] = HOMEPAGE
    config['about']['license'] = LICENSE
    config['about']['summary'] = '${package:name}'

    if not METAPACKAGE:
        config['source'] = {}
        config['source']['fn'] = '${package:name}-${package:version}.tar.gz'
        config['source']['url'] = '${package:home}/${fn}'
        if args.use_git:
            config['source']['git_url'] = ''
            config['source']['git_tag'] = ''

    config['build'] = {}
    config['build']['number'] = BUILD_NUMBER

    config['requirements'] = {}
    config['requirements']['build'] = ''
    config['requirements']['run'] = ''

    build_req = ['\n']
    run_req = ['\n']

    if STYLE in RECIPE_TYPES and STYLE == 'python':
        build_req.append('setuptools')
        build_req.append('python')
        run_req.append('python')

        if D2TO1_HACK['active']:
            build_req.append('d2to1')
    # Other requirement styles don't exist, I think...?
    config['requirements']['build'] = '\n'.join(build_req)
    config['requirements']['run'] = '\n'.join(run_req)

    if not METAPACKAGE:
        config['test'] = {}
        config['test']['imports'] = '\n'
        config['test']['commands'] = '\n'

    config['cbc_build'] = {}
    config['cbc_build']['linux'] = ''
    config['cbc_build']['windows'] = ''

    build_linux = ['\n']
    build_windows = ['\n']

    if STYLE in RECIPE_TYPES and STYLE == 'python':
        if D2TO1_HACK['active']:
            build_linux.append(D2TO1_HACK['linux'])
            build_windows.append(D2TO1_HACK['windows'])

        build_linux.append('python setup.py install || exit 1')
        build_windows.append('python setup.py install')
        build_windows.append('if errorlevel 1 exit 1')

    elif STYLE in RECIPE_TYPES and STYLE == 'gnu':
        build_linux.append('./configure --prefix=$$PREFIX')
        build_linux.append('make -j$$CPU_COUNT')
        build_linux.append('make install')

        build_windows.append('# Probably unsupported...')
        build_windows.append('#./configure --prefix=%PREFIX%')
        build_windows.append('# make -j%CPU_COUNT%')
        build_windows.append('# make install')

    elif STYLE in RECIPE_TYPES and STYLE == 'make':
        build_linux.append('make -j$$CPU_COUNT PREFIX=$$PREFIX')
        build_linux.append('make install')

        build_windows.append('make -j%CPU_COUNT% PREFIX=%PREFIX%')
        build_windows.append('make install')

    elif STYLE in RECIPE_TYPES and STYLE == 'cmake':
        build_linux.append('mkdir BUILD_DIR && cd BUILD_DIR')
        build_linux.append('cd BUILD_DIR')
        build_linux.append('cmake -DCMAKE_INSTALL_PREFIX=$$PREFIX ..')

        build_windows.append('mkdir BUILD_DIR && cd BUILD_DIR')
        build_windows.append('cd BUILD_DIR')
        build_windows.append('cmake -DCMAKE_INSTALL_PREFIX=%PREFIX% ..')

    elif STYLE in RECIPE_TYPES and STYLE == 'scons':
        build_linux.append('scons')
        build_windows.append('scons')

    else:
        pass

    config['cbc_build']['linux'] = '\n'.join(build_linux)
    config['cbc_build']['windows'] = '\n'.join(build_windows)

    if not os.path.splitext(RECIPE)[1]:
        RECIPE += '.ini'

    with open(RECIPE, 'w+') as recipe:
        config.write(recipe)