~chuck-bell/mysql-utilities/mysql-utilities

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
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python

# Keep the imports sorted (alphabetically) in each group. Makes
# merging easier.

import distutils.core
import os
import sys

# Setup function to use
from distutils.core import setup

from distutils.command.build_scripts import build_scripts as _build_scripts
from distutils.command.install import install as _install
from distutils.command.install_scripts import install_scripts as _install_scripts
from info import META_INFO, INSTALL

COMMANDS = {
    'cmdclass': {
        },
    }

ARGS = {
}

PROFILE_SCRIPT = '''
prepend_path () (
    IFS=':'
    for D in $PATH; do
        if test x$D != x$1; then
            OUTPATH="${OUTPATH:+$OUTPATH:}$D"
        fi
    done
    echo "$1:$OUTPATH"
)

PATH=`prepend_path %s`
'''

class install_scripts(_install_scripts):
    description = (_install_scripts.description
                   + " and add path to /etc/profile.d")

    user_options = _install_scripts.user_options + [
        ("skip-profile", None, "Skip installing a profile script"),
        ]

    boolean_options = _install_scripts.boolean_options + ['skip-profile']

    profile_file = "/etc/profile.d/mysql-utilities.sh"

    def initialize_options(self):
        _install_scripts.initialize_options(self)
        self.skip_profile = None

    def finalize_options(self):
        _install_scripts.finalize_options(self)
        self.set_undefined_options('install',
                                   ('skip_profile', 'skip_profile'))

    def run(self):
        from distutils import log, dir_util
        _install_scripts.run(self)
        # We should probably use distutils.dist.execute here to allow
        # --dry-run to work properly.
        if not self.skip_profile:
            if os.path.exists(os.path.dirname(self.profile_file)):
                outfile = self.profile_file
                if os.path.isdir(outfile) and not os.path.islink(outfile):
                    dir_util.remove_tree(outfile)
                elif os.path.exists(outfile):
                    log.info("Removing %s", outfile)
                    os.unlink(outfile)
                script = PROFILE_SCRIPT % (self.install_dir,)
                log.info("Writing %s", outfile)
                open(outfile, "w+").write(script)
            else:
                log.info("Not adding %s%s", self.profile_file,
                         " (skipped)" if self.skip_profile else "")

    def get_outputs(self):
        outputs = _install_scripts.get_outputs(self)
        if not self.skip_profile:
            outputs.append(self.profile_file)
        return outputs

class install_man(distutils.core.Command):
    description = "install (Unix) manual pages"

    user_options = [
        ('install-base=', None, "base installation directory"),
        ('force', 'f', 'force installation (overwrite existing files)'),
        ('build-dir=', 'b', 'Build directory'),
        ('skip-build', None, "skip the build steps"),
    ]

    boolean_options = ['force']

    def initialize_options(self):
        self.install_base = None
        self.build_dir = None
        self.force = None
        self.skip_build = None

    def finalize_options(self):
        self.set_undefined_options('install',
                                   ('install_data', 'install_base'),
                                   ('force', 'force'),
                                   )
        self.set_undefined_options('build_sphinx',
                                   ('build_dir', 'build_dir'),
                                   )
        self.target_dir = os.path.join(self.install_base, 'man')
        self.source_dir = os.path.join(self.build_dir, 'man')

    def run(self):
        from glob import glob
        from distutils import log

        outfiles = []
        man_files = glob(os.path.join(self.source_dir, '*.[12345678]'))
        for man_file in man_files:
            man_dir = 'man' + os.path.splitext(man_file)[1][1:]
            man_page = os.path.basename(man_file)
            self.mkpath(man_dir)
            man_install = os.path.join(self.target_dir, man_dir, man_page)
            self.copy_file(man_file, man_install)
            outfiles.append(man_install)
        self.outfiles = outfiles

    def get_outputs(self):
        return self.outfiles or []

# See if we have Sphinx installed, otherwise, just ignore building
# documentation.
class install(_install):
    user_options = _install.user_options + [
        ("skip-profile", None, "Skip installing a profile script"),
        ]

    boolean_options = _install.boolean_options + ['skip-profile']

    def initialize_options(self):
        _install.initialize_options(self)
        self.skip_profile = False

    def finalize_options(self):
        _install.finalize_options(self)

COMMANDS['cmdclass'].update({
        'install': install,
        })

try:
    import sphinx.setup_command

    # Add install_man command if we have Sphinx
    install.sub_commands = _install.sub_commands + [
        ('install_man', lambda self: True),
        ]

    COMMANDS['cmdclass'].update({
            'install_man': install_man,
            'build_sphinx': sphinx.setup_command.BuildDoc,
            'build_man': sphinx.setup_command.BuildDoc,
            })

    COMMANDS.setdefault('options',{}).update({
            'build_man': { 'builder': 'man' },
            })
except ImportError:
    pass

class build_scripts(_build_scripts):
    """Class for providing a customized version of build_scripts.

    When ``run`` is called, this command class will:
    1. Create a copy of all ``.py`` files in the **scripts** option
       that does not have the ``.py`` extension.
    2. Replace the list in the **scripts** attribute with a list
       consisting of the script files with the ``.py`` extension
       removed.
    3. Call run method in `distutils.command.build_scripts`.
    4. Restore the scripts list to the old value, for other commands
       to use."""

    def run(self):
        from distutils import log

        if not self.scripts:
            return

        saved_scripts = self.scripts
        self.scripts = []
        for script in saved_scripts:
            script = distutils.util.convert_path(script)
            script_copy, script_ext = os.path.splitext(script)

            if script_ext != '.py':
                log.debug("Not removing extension from %s since it's not '.py'", script)
            else:
                log.debug("Copying %s -> %s", script, script_copy)
                self.copy_file(script, script_copy)
                self.scripts.append(script_copy)
        # distutils is compatible with 2.1 so we cannot use super() to
        # call it.
        _build_scripts.run(self)
        self.scripts = saved_scripts

if os.name != "nt":
    COMMANDS['cmdclass'].update({
        'build_scripts': build_scripts,
        'install_scripts': install_scripts,
        })

ARGS.update(META_INFO)
ARGS.update(INSTALL)
ARGS.update(COMMANDS)
setup(**ARGS)