~brendan-donegan/checkbox-certification/sru_wireless_scanning

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python

import os
import re
import posixpath
from glob import glob

from distutils.core import setup
from distutils.util import change_root, convert_path

from distutils.ccompiler import new_compiler
from distutils.command.build import build
from distutils.command.clean import clean
from distutils.command.install import install
from distutils.command.install_data import install_data
from distutils.command.install_scripts import install_scripts


def changelog_version(changelog="debian/changelog"):
    version = "dev"
    if posixpath.exists(changelog):
        head=open(changelog).readline()
        match = re.compile(".*\((.*)\).*").match(head)
        if match:
            version = match.group(1)

    return version

def expand_data_files(data_files):
    for f in data_files:
        if type(f) != str:
            files = f[1]
            i = 0
            while i < len(files):
                if files[i].find("*") > -1:
                    for e in glob(files[i]):
                        files.append(e)
                    files.pop(i)
                    i -= 1
                i += 1

    return data_files

def extract_sources_from_data_files(data_files):
    all_sources = []
    data_files = expand_data_files(data_files)
    for destination, sources in data_files:
        all_sources.extend([s for s in sources if s.endswith(".c")])

    return all_sources

def extract_executables_from_data_files(data_files):
    sources = extract_sources_from_data_files(data_files)
    return [os.path.splitext(s)[0] for s in sources]

def substitute_variables(infile, outfile, variables={}):
    descriptor_in = open(infile, "r")
    descriptor_out = open(outfile, "w")
    for line in descriptor_in.readlines():
        for key, value in variables.items():
            line = line.replace(key, value)
        descriptor_out.write(line)


class checkbox_build(build, object):

    def initialize_options(self):
        super(checkbox_build, self).initialize_options()

        self.sources = []

    def finalize_options(self):
        super(checkbox_build, self).finalize_options()

        # Initialize sources
        data_files = self.distribution.data_files
        self.sources = extract_sources_from_data_files(data_files)

    def run(self):
        super(checkbox_build, self).run()

        cc = new_compiler()
        for source in self.sources:
            executable = os.path.splitext(source)[0]
            cc.link_executable([source], executable, libraries=["rt", "pthread"])


class checkbox_clean(clean, object):

    def initialize_options(self):
        super(checkbox_clean, self).initialize_options()

        self.executables = None

    def finalize_options(self):
        super(checkbox_clean, self).finalize_options()

        # Initialize sources
        data_files = self.distribution.data_files
        self.executables = extract_executables_from_data_files(data_files)

    def run(self):
        super(checkbox_clean, self).run()

        for executable in self.executables:
            os.unlink(executable)


# Hack to workaround unsupported option in Python << 2.5
class checkbox_install(install, object):

    user_options = install.user_options + [
        ('install-layout=', None,
         "installation layout to choose (known values: deb)")]

    def initialize_options(self):
        super(checkbox_install, self).initialize_options()

        self.install_layout = None


class checkbox_install_data(install_data, object):

    def finalize_options(self):
        """Add wildcard support for filenames."""
        super(checkbox_install_data, self).finalize_options()

        self.data_files = expand_data_files(self.data_files)

    def run(self):
        """Run substitutions on files."""
        super(checkbox_install_data, self).run()

        examplesfiles = [o for o in self.outfiles
            if o.find("examples") != -1]
        if not examplesfiles:
            return

        # Find etc directory
        etcdir = convert_path("/etc/checkbox.d")
        if not posixpath.isabs(etcdir):
            etcdir = posixpath.join(self.install_dir, etcdir)
        elif self.root:
            etcdir = change_root(self.root, etcdir)
        self.mkpath(etcdir)

        # Create configs symbolic link
        dstdir = posixpath.dirname(examplesfiles[0]).replace("examples",
            "configs")
        if not os.path.exists(dstdir):
            os.symlink(etcdir, dstdir)

        # Substitute version in examplesfiles and etcfiles
        version = changelog_version()
        for examplesfile in examplesfiles:
            etcfile = posixpath.join(etcdir,
                posixpath.basename(examplesfile))
            infile = posixpath.join("examples",
                posixpath.basename(examplesfile))
            for outfile in examplesfile, etcfile:
                substitute_variables(infile, outfile, {
                    "version = dev": "version = %s" % version})


class checkbox_install_scripts(install_scripts, object):

    def run(self):
        """Run substitutions on files."""
        super(checkbox_install_scripts, self).run()

        # Substitute directory in defaults.py
        for outfile in self.outfiles:
            infile = posixpath.join("bin", posixpath.basename(outfile))
            substitute_variables(infile, outfile, {
                "CHECKBOX_CERTIFICATION_SHARE:-.": "CHECKBOX_CERTIFICATION_SHARE:-/usr/share/checkbox-certification"})


setup(
    name = "checkbox-certification",
    version = changelog_version(),
    author = "Marc Tardif",
    author_email = "marc.tardif@canonical.com",
    license = "GPL",
    description = "Checkbox Certification Tests",
    long_description = """
This project extends the checkbox-compatibility project with certification
tests.
""",
    data_files=[
        ("/etc/default/", ["cli/default/*"]),
        ("/etc/xdg/autostart/", ["gtk/autostart/*"]),
        ("share/applications/", ["gtk/checkbox-certification-gtk.desktop"]),
        ("share/checkbox-certification/data/", ["data/*"]),
        ("share/checkbox-certification/examples/", ["examples/*"]),
        ("share/checkbox-certification/install/", ["install/*"]),
        ("share/checkbox-certification/plugins/", ["plugins/*.py"])],
    scripts = ["bin/checkbox-certification",
        "bin/checkbox-certification-gtk",
        "bin/checkbox-certification-cli",
        "bin/checkbox-to-certification"],
    cmdclass = {
        "build": checkbox_build,
        "clean": checkbox_clean,
        "install": checkbox_install,
        "install_data": checkbox_install_data,
        "install_scripts": checkbox_install_scripts}
)