1"""SCons.Tool.Packaging.ipk
2"""
3
4#
5# __COPYRIGHT__
6#
7# Permission is hereby granted, free of charge, to any person obtaining
8# a copy of this software and associated documentation files (the
9# "Software"), to deal in the Software without restriction, including
10# without limitation the rights to use, copy, modify, merge, publish,
11# distribute, sublicense, and/or sell copies of the Software, and to
12# permit persons to whom the Software is furnished to do so, subject to
13# the following conditions:
14#
15# The above copyright notice and this permission notice shall be included
16# in all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25#
26
27__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
28
29import os
30
31import SCons.Builder
32import SCons.Node.FS
33import SCons.Util
34
35from SCons.Tool.packaging import stripinstallbuilder, putintopackageroot
36
37def package(env, target, source, PACKAGEROOT, NAME, VERSION, DESCRIPTION,
38            SUMMARY, X_IPK_PRIORITY, X_IPK_SECTION, SOURCE_URL,
39            X_IPK_MAINTAINER, X_IPK_DEPENDS, **kw):
40    """ This function prepares the packageroot directory for packaging with the
41    ipkg builder.
42    """
43    SCons.Tool.Tool('ipkg').generate(env)
44
45    # setup the Ipkg builder
46    bld = env['BUILDERS']['Ipkg']
47    target, source = stripinstallbuilder(target, source, env)
48    target, source = putintopackageroot(target, source, env, PACKAGEROOT)
49
50    # This should be overrideable from the construction environment,
51    # which it is by using ARCHITECTURE=.
52    # Guessing based on what os.uname() returns at least allows it
53    # to work for both i386 and x86_64 Linux systems.
54    archmap = {
55        'i686'  : 'i386',
56        'i586'  : 'i386',
57        'i486'  : 'i386',
58    }
59
60    buildarchitecture = os.uname()[4]
61    buildarchitecture = archmap.get(buildarchitecture, buildarchitecture)
62
63    if 'ARCHITECTURE' in kw:
64        buildarchitecture = kw['ARCHITECTURE']
65
66    # setup the kw to contain the mandatory arguments to this function.
67    # do this before calling any builder or setup function
68    loc=locals()
69    del loc['kw']
70    kw.update(loc)
71    del kw['source'], kw['target'], kw['env']
72
73    # generate the specfile
74    specfile = gen_ipk_dir(PACKAGEROOT, source, env, kw)
75
76    # override the default target.
77    if str(target[0])=="%s-%s"%(NAME, VERSION):
78        target=[ "%s_%s_%s.ipk"%(NAME, VERSION, buildarchitecture) ]
79
80    # now apply the Ipkg builder
81    return bld(env, target, specfile, **kw)
82
83def gen_ipk_dir(proot, source, env, kw):
84    # make sure the packageroot is a Dir object.
85    if SCons.Util.is_String(proot): proot=env.Dir(proot)
86
87    #  create the specfile builder
88    s_bld=SCons.Builder.Builder(
89        action  = build_specfiles,
90        )
91
92    # create the specfile targets
93    spec_target=[]
94    control=proot.Dir('CONTROL')
95    spec_target.append(control.File('control'))
96    spec_target.append(control.File('conffiles'))
97    spec_target.append(control.File('postrm'))
98    spec_target.append(control.File('prerm'))
99    spec_target.append(control.File('postinst'))
100    spec_target.append(control.File('preinst'))
101
102    # apply the builder to the specfile targets
103    s_bld(env, spec_target, source, **kw)
104
105    # the packageroot directory does now contain the specfiles.
106    return proot
107
108def build_specfiles(source, target, env):
109    """ Filter the targets for the needed files and use the variables in env
110    to create the specfile.
111    """
112    #
113    # At first we care for the CONTROL/control file, which is the main file for ipk.
114    #
115    # For this we need to open multiple files in random order, so we store into
116    # a dict so they can be easily accessed.
117    #
118    #
119    opened_files={}
120    def open_file(needle, haystack=None):
121        try:
122            return opened_files[needle]
123        except KeyError:
124            files = filter(lambda x: x.get_path().rfind(needle) != -1, haystack)
125            # Py3: filter returns an iterable, not a list
126            file = list(files)[0]
127            opened_files[needle] = open(file.get_abspath(), 'w')
128            return opened_files[needle]
129
130    control_file = open_file('control', target)
131
132    if 'X_IPK_DESCRIPTION' not in env:
133        env['X_IPK_DESCRIPTION']="%s\n %s"%(env['SUMMARY'],
134                                            env['DESCRIPTION'].replace('\n', '\n '))
135
136
137    content = """
138Package: $NAME
139Version: $VERSION
140Priority: $X_IPK_PRIORITY
141Section: $X_IPK_SECTION
142Source: $SOURCE_URL
143Architecture: $ARCHITECTURE
144Maintainer: $X_IPK_MAINTAINER
145Depends: $X_IPK_DEPENDS
146Description: $X_IPK_DESCRIPTION
147"""
148
149    control_file.write(env.subst(content))
150
151    #
152    # now handle the various other files, which purpose it is to set post-,
153    # pre-scripts and mark files as config files.
154    #
155    # We do so by filtering the source files for files which are marked with
156    # the "config" tag and afterwards we do the same for x_ipk_postrm,
157    # x_ipk_prerm, x_ipk_postinst and x_ipk_preinst tags.
158    #
159    # The first one will write the name of the file into the file
160    # CONTROL/configfiles, the latter add the content of the x_ipk_* variable
161    # into the same named file.
162    #
163    for f in [x for x in source if 'PACKAGING_CONFIG' in dir(x)]:
164        config = open_file('conffiles')
165        config.write(f.PACKAGING_INSTALL_LOCATION)
166        config.write('\n')
167
168    for str in 'POSTRM PRERM POSTINST PREINST'.split():
169        name="PACKAGING_X_IPK_%s"%str
170        for f in [x for x in source if name in dir(x)]:
171            file = open_file(name)
172            file.write(env[str])
173
174    #
175    # close all opened files
176    for f in opened_files.values():
177        f.close()
178
179    # call a user specified function
180    if 'CHANGE_SPECFILE' in env:
181        content += env['CHANGE_SPECFILE'](target)
182
183    return 0
184
185# Local Variables:
186# tab-width:4
187# indent-tabs-mode:nil
188# End:
189# vim: set expandtab tabstop=4 shiftwidth=4:
190