summaryrefslogtreecommitdiff
path: root/setup.py
blob: f7794211642e352af52a0da62be9fdbc2b203d0e (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python

# Copyright (c) 2009 Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""psutil is a cross-platform library for retrieving information on
running processes and system utilization (CPU, memory, disks, network)
in Python.
"""

import atexit
import contextlib
import io
import os
import sys
import tempfile
import platform
try:
    from setuptools import setup, Extension
except ImportError:
    from distutils.core import setup, Extension


HERE = os.path.abspath(os.path.dirname(__file__))


def get_version():
    INIT = os.path.join(HERE, 'psutil/__init__.py')
    with open(INIT, 'r') as f:
        for line in f:
            if line.startswith('__version__'):
                ret = eval(line.strip().split(' = ')[1])
                assert ret.count('.') == 2, ret
                for num in ret.split('.'):
                    assert num.isdigit(), ret
                return ret
        else:
            raise ValueError("couldn't find version string")


def get_description():
    README = os.path.join(HERE, 'README.rst')
    with open(README, 'r') as f:
        return f.read()


@contextlib.contextmanager
def silenced_output(stream_name):
    class DummyFile(io.BytesIO):
        # see: https://github.com/giampaolo/psutil/issues/678
        errors = "ignore"

        def write(self, s):
            pass

    orig = getattr(sys, stream_name)
    try:
        setattr(sys, stream_name, DummyFile())
        yield
    finally:
        setattr(sys, stream_name, orig)


VERSION = get_version()
VERSION_MACRO = ('PSUTIL_VERSION', int(VERSION.replace('.', '')))


# POSIX
if os.name == 'posix':
    posix_extension = Extension(
        'psutil._psutil_posix',
        sources=['psutil/_psutil_posix.c'])
    if sys.platform.startswith("sunos") or sys.platform.startswith("solaris"):
        posix_extension.libraries.append('socket')
        if platform.release() == '5.10':
            posix_extension.sources.append('psutil/arch/solaris/v10/ifaddrs.c')
            posix_extension.define_macros.append(('PSUTIL_SUNOS10', 1))

# Windows
if sys.platform.startswith("win32"):

    def get_winver():
        maj, min = sys.getwindowsversion()[0:2]
        return '0x0%s' % ((maj * 100) + min)

    ext = Extension(
        'psutil._psutil_windows',
        sources=[
            'psutil/_psutil_windows.c',
            'psutil/_psutil_common.c',
            'psutil/arch/windows/process_info.c',
            'psutil/arch/windows/process_handles.c',
            'psutil/arch/windows/security.c',
            'psutil/arch/windows/inet_ntop.c',
        ],
        define_macros=[
            VERSION_MACRO,
            # be nice to mingw, see:
            # http://www.mingw.org/wiki/Use_more_recent_defined_functions
            ('_WIN32_WINNT', get_winver()),
            ('_AVAIL_WINVER_', get_winver()),
            ('_CRT_SECURE_NO_WARNINGS', None),
            # see: https://github.com/giampaolo/psutil/issues/348
            ('PSAPI_VERSION', 1),
        ],
        libraries=[
            "psapi", "kernel32", "advapi32", "shell32", "netapi32",
            "iphlpapi", "wtsapi32", "ws2_32",
        ],
        # extra_compile_args=["/Z7"],
        # extra_link_args=["/DEBUG"]
    )
    extensions = [ext]
# OS X
elif sys.platform.startswith("darwin"):
    ext = Extension(
        'psutil._psutil_osx',
        sources=[
            'psutil/_psutil_osx.c',
            'psutil/_psutil_common.c',
            'psutil/arch/osx/process_info.c'
        ],
        define_macros=[VERSION_MACRO],
        extra_link_args=[
            '-framework', 'CoreFoundation', '-framework', 'IOKit'
        ])
    extensions = [ext, posix_extension]
# FreeBSD
elif sys.platform.startswith("freebsd"):
    ext = Extension(
        'psutil._psutil_bsd',
        sources=[
            'psutil/_psutil_bsd.c',
            'psutil/_psutil_common.c',
            'psutil/arch/bsd/freebsd.c',
            'psutil/arch/bsd/freebsd_socks.c',
        ],
        define_macros=[VERSION_MACRO],
        libraries=["devstat"])
    extensions = [ext, posix_extension]
# OpenBSD
elif sys.platform.startswith("openbsd"):
    ext = Extension(
        'psutil._psutil_bsd',
        sources=[
            'psutil/_psutil_bsd.c',
            'psutil/_psutil_common.c',
            'psutil/arch/bsd/openbsd.c',
        ],
        define_macros=[VERSION_MACRO],
        libraries=["kvm"])
    extensions = [ext, posix_extension]
# NetBSD
elif sys.platform.startswith("netbsd"):
    ext = Extension(
        'psutil._psutil_bsd',
        sources=[
            'psutil/_psutil_bsd.c',
            'psutil/_psutil_common.c',
            'psutil/arch/bsd/netbsd.c',
            'psutil/arch/bsd/netbsd_socks.c',
        ],
        define_macros=[VERSION_MACRO],
        libraries=["kvm"])
    extensions = [ext, posix_extension]
# Linux
elif sys.platform.startswith("linux"):
    def get_ethtool_macro():
        # see: https://github.com/giampaolo/psutil/issues/659
        from distutils.unixccompiler import UnixCCompiler
        from distutils.errors import CompileError

        with tempfile.NamedTemporaryFile(
                suffix='.c', delete=False, mode="wt") as f:
            f.write("#include <linux/ethtool.h>")

        @atexit.register
        def on_exit():
            try:
                os.remove(f.name)
            except OSError:
                pass

        compiler = UnixCCompiler()
        try:
            with silenced_output('stderr'):
                with silenced_output('stdout'):
                    compiler.compile([f.name])
        except CompileError:
            return ("PSUTIL_ETHTOOL_MISSING_TYPES", 1)
        else:
            return None

    ETHTOOL_MACRO = get_ethtool_macro()
    macros = [VERSION_MACRO]
    if ETHTOOL_MACRO is not None:
        macros.append(ETHTOOL_MACRO)
    ext = Extension(
        'psutil._psutil_linux',
        sources=['psutil/_psutil_linux.c'],
        define_macros=macros)
    extensions = [ext, posix_extension]
# Solaris
elif sys.platform.lower().startswith('sunos') or \
        sys.platform.lower().startswith('solaris'):
    ext = Extension(
        'psutil._psutil_sunos',
        sources=['psutil/_psutil_sunos.c'],
        define_macros=[VERSION_MACRO],
        libraries=['kstat', 'nsl', 'socket'])
    extensions = [ext, posix_extension]
else:
    sys.exit('platform %s is not supported' % sys.platform)


def main():
    setup_args = dict(
        name='psutil',
        version=VERSION,
        description=__doc__.replace('\n', '').strip(),
        long_description=get_description(),
        keywords=[
            'ps', 'top', 'kill', 'free', 'lsof', 'netstat', 'nice', 'tty',
            'ionice', 'uptime', 'taskmgr', 'process', 'df', 'iotop', 'iostat',
            'ifconfig', 'taskset', 'who', 'pidof', 'pmap', 'smem', 'pstree',
            'monitoring', 'ulimit', 'prlimit',
        ],
        author='Giampaolo Rodola',
        author_email='g.rodola <at> gmail <dot> com',
        url='https://github.com/giampaolo/psutil',
        platforms='Platform Independent',
        license='BSD',
        packages=['psutil'],
        # see: python setup.py register --list-classifiers
        classifiers=[
            'Development Status :: 5 - Production/Stable',
            'Environment :: Console',
            'Environment :: Win32 (MS Windows)',
            'Intended Audience :: Developers',
            'Intended Audience :: Information Technology',
            'Intended Audience :: System Administrators',
            'License :: OSI Approved :: BSD License',
            'Operating System :: MacOS :: MacOS X',
            'Operating System :: Microsoft :: Windows :: Windows NT/2000',
            'Operating System :: Microsoft',
            'Operating System :: OS Independent',
            'Operating System :: POSIX :: BSD :: FreeBSD',
            'Operating System :: POSIX :: BSD :: NetBSD',
            'Operating System :: POSIX :: BSD :: OpenBSD',
            'Operating System :: POSIX :: BSD',
            'Operating System :: POSIX :: Linux',
            'Operating System :: POSIX :: SunOS/Solaris',
            'Operating System :: POSIX',
            'Programming Language :: C',
            'Programming Language :: Python :: 2',
            'Programming Language :: Python :: 2.6',
            'Programming Language :: Python :: 2.7',
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.0',
            'Programming Language :: Python :: 3.1',
            'Programming Language :: Python :: 3.2',
            'Programming Language :: Python :: 3.3',
            'Programming Language :: Python :: 3.4',
            'Programming Language :: Python :: 3.5',
            'Programming Language :: Python :: Implementation :: CPython',
            'Programming Language :: Python :: Implementation :: PyPy',
            'Programming Language :: Python',
            'Topic :: Software Development :: Libraries :: Python Modules',
            'Topic :: Software Development :: Libraries',
            'Topic :: System :: Benchmark',
            'Topic :: System :: Hardware',
            'Topic :: System :: Monitoring',
            'Topic :: System :: Networking :: Monitoring',
            'Topic :: System :: Networking',
            'Topic :: System :: Operating System',
            'Topic :: System :: Systems Administration',
            'Topic :: Utilities',
        ],
    )
    if extensions is not None:
        setup_args["ext_modules"] = extensions
    setup(**setup_args)

if __name__ == '__main__':
    main()