1"""SCons.Tool.Packaging.ipk
2"""
3
4#
5# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 The SCons Foundation
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__ = "src/engine/SCons/Tool/packaging/ipk.py issue-2856:2676:d23b7a2f45e8 2012/08/05 15:38:28 garyo"
28
29import SCons.Builder
30import SCons.Node.FS
31import os
32
33from SCons.Tool.packaging import stripinstallbuilder, putintopackageroot
34
35def package(env, target, source, PACKAGEROOT, NAME, VERSION, DESCRIPTION,
36            SUMMARY, X_IPK_PRIORITY, X_IPK_SECTION, SOURCE_URL,
37            X_IPK_MAINTAINER, X_IPK_DEPENDS, **kw):
38    """ this function prepares the packageroot directory for packaging with the
39    ipkg builder.
40    """
41    SCons.Tool.Tool('ipkg').generate(env)
42
43    # setup the Ipkg builder
44    bld = env['BUILDERS']['Ipkg']
45    target, source = stripinstallbuilder(target, source, env)
46    target, source = putintopackageroot(target, source, env, PACKAGEROOT)
47
48    # This should be overridable from the construction environment,
49    # which it is by using ARCHITECTURE=.
50    # Guessing based on what os.uname() returns at least allows it
51    # to work for both i386 and x86_64 Linux systems.
52    archmap = {
53        'i686'  : 'i386',
54        'i586'  : 'i386',
55        'i486'  : 'i386',
56    }
57
58    buildarchitecture = os.uname()[4]
59    buildarchitecture = archmap.get(buildarchitecture, buildarchitecture)
60
61    if 'ARCHITECTURE' in kw:
62        buildarchitecture = kw['ARCHITECTURE']
63
64    # setup the kw to contain the mandatory arguments to this fucntion.
65    # do this before calling any builder or setup function
66    loc=locals()
67    del loc['kw']
68    kw.update(loc)
69    del kw['source'], kw['target'], kw['env']
70
71    # generate the specfile
72    specfile = gen_ipk_dir(PACKAGEROOT, source, env, kw)
73
74    # override the default target.
75    if str(target[0])=="%s-%s"%(NAME, VERSION):
76        target=[ "%s_%s_%s.ipk"%(NAME, VERSION, buildarchitecture) ]
77
78    # now apply the Ipkg builder
79    return bld(env, target, specfile, **kw)
80
81def gen_ipk_dir(proot, source, env, kw):
82    # make sure the packageroot is a Dir object.
83    if SCons.Util.is_String(proot): proot=env.Dir(proot)
84
85    #  create the specfile builder
86    s_bld=SCons.Builder.Builder(
87        action  = build_specfiles,
88        )
89
90    # create the specfile targets
91    spec_target=[]
92    control=proot.Dir('CONTROL')
93    spec_target.append(control.File('control'))
94    spec_target.append(control.File('conffiles'))
95    spec_target.append(control.File('postrm'))
96    spec_target.append(control.File('prerm'))
97    spec_target.append(control.File('postinst'))
98    spec_target.append(control.File('preinst'))
99
100    # apply the builder to the specfile targets
101    s_bld(env, spec_target, source, **kw)
102
103    # the packageroot directory does now contain the specfiles.
104    return proot
105
106def build_specfiles(source, target, env):
107    """ filter the targets for the needed files and use the variables in env
108    to create the specfile.
109    """
110    #
111    # At first we care for the CONTROL/control file, which is the main file for ipk.
112    #
113    # For this we need to open multiple files in random order, so we store into
114    # a dict so they can be easily accessed.
115    #
116    #
117    opened_files={}
118    def open_file(needle, haystack):
119        try:
120            return opened_files[needle]
121        except KeyError:
122            file=filter(lambda x: x.get_path().rfind(needle)!=-1, haystack)[0]
123            opened_files[needle]=open(file.abspath, 'w')
124            return opened_files[needle]
125
126    control_file=open_file('control', target)
127
128    if 'X_IPK_DESCRIPTION' not in env:
129        env['X_IPK_DESCRIPTION']="%s\n %s"%(env['SUMMARY'],
130                                            env['DESCRIPTION'].replace('\n', '\n '))
131
132
133    content = """
134Package: $NAME
135Version: $VERSION
136Priority: $X_IPK_PRIORITY
137Section: $X_IPK_SECTION
138Source: $SOURCE_URL
139Architecture: $ARCHITECTURE
140Maintainer: $X_IPK_MAINTAINER
141Depends: $X_IPK_DEPENDS
142Description: $X_IPK_DESCRIPTION
143"""
144
145    control_file.write(env.subst(content))
146
147    #
148    # now handle the various other files, which purpose it is to set post-,
149    # pre-scripts and mark files as config files.
150    #
151    # We do so by filtering the source files for files which are marked with
152    # the "config" tag and afterwards we do the same for x_ipk_postrm,
153    # x_ipk_prerm, x_ipk_postinst and x_ipk_preinst tags.
154    #
155    # The first one will write the name of the file into the file
156    # CONTROL/configfiles, the latter add the content of the x_ipk_* variable
157    # into the same named file.
158    #
159    for f in [x for x in source if 'PACKAGING_CONFIG' in dir(x)]:
160        config=open_file('conffiles')
161        config.write(f.PACKAGING_INSTALL_LOCATION)
162        config.write('\n')
163
164    for str in 'POSTRM PRERM POSTINST PREINST'.split():
165        name="PACKAGING_X_IPK_%s"%str
166        for f in [x for x in source if name in dir(x)]:
167            file=open_file(name)
168            file.write(env[str])
169
170    #
171    # close all opened files
172    for f in opened_files.values():
173        f.close()
174
175    # call a user specified function
176    if 'CHANGE_SPECFILE' in env:
177        content += env['CHANGE_SPECFILE'](target)
178
179    return 0
180
181# Local Variables:
182# tab-width:4
183# indent-tabs-mode:nil
184# End:
185# vim: set expandtab tabstop=4 shiftwidth=4:
186