forked from splunk/splunk-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·245 lines (189 loc) · 7.34 KB
/
setup.py
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
#!/usr/bin/env python
#
# Copyright 2011-2015 Splunk, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from setuptools import setup, Command
from contextlib import closing
from subprocess import check_call, STDOUT
import os
import sys
import shutil
import tarfile
import splunklib
failed = False
def run_test_suite():
try:
import unittest2 as unittest
except ImportError:
import unittest
def mark_failed():
global failed
failed = True
class _TrackingTextTestResult(unittest._TextTestResult):
def addError(self, test, err):
unittest._TextTestResult.addError(self, test, err)
mark_failed()
def addFailure(self, test, err):
unittest._TextTestResult.addFailure(self, test, err)
mark_failed()
class TrackingTextTestRunner(unittest.TextTestRunner):
def _makeResult(self):
return _TrackingTextTestResult(
self.stream, self.descriptions, self.verbosity)
original_cwd = os.path.abspath(os.getcwd())
os.chdir('tests')
suite = unittest.defaultTestLoader.discover('.')
runner = TrackingTextTestRunner(verbosity=2)
runner.run(suite)
os.chdir(original_cwd)
return failed
def run_test_suite_with_junit_output():
try:
import unittest2 as unittest
except ImportError:
import unittest
import xmlrunner
original_cwd = os.path.abspath(os.getcwd())
os.chdir('tests')
suite = unittest.defaultTestLoader.discover('.')
xmlrunner.XMLTestRunner(output='../test-reports').run(suite)
os.chdir(original_cwd)
class CoverageCommand(Command):
"""setup.py command to run code coverage of the test suite."""
description = "Create an HTML coverage report from running the full test suite."
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
try:
import coverage
except ImportError:
print("Could not import coverage. Please install it and try again.")
exit(1)
cov = coverage.coverage(source=['splunklib'])
cov.start()
run_test_suite()
cov.stop()
cov.html_report(directory='coverage_report')
class TestCommand(Command):
"""setup.py command to run the whole test suite."""
description = "Run test full test suite."
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
failed = run_test_suite()
if failed:
sys.exit(1)
class JunitXmlTestCommand(Command):
"""setup.py command to run the whole test suite."""
description = "Run test full test suite with JUnit-formatted output."
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
run_test_suite_with_junit_output()
class DistCommand(Command):
"""setup.py command to create .spl files for modular input and search
command examples"""
description = "Build modular input and search command example tarballs."
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
@staticmethod
def get_python_files(files):
"""Utility function to get .py files from a list"""
python_files = []
for file_name in files:
if file_name.endswith(".py"):
python_files.append(file_name)
return python_files
def run(self):
# Create random_numbers.spl and github_forks.spl
app_names = ['random_numbers', 'github_forks']
splunklib_arcname = "splunklib"
modinput_dir = os.path.join(splunklib_arcname, "modularinput")
if not os.path.exists("build"):
os.makedirs("build")
for app in app_names:
with closing(tarfile.open(os.path.join("build", app + ".spl"), "w")) as spl:
spl.add(
os.path.join("examples", app, app + ".py"),
arcname=os.path.join(app, "bin", app + ".py")
)
spl.add(
os.path.join("examples", app, "default", "app.conf"),
arcname=os.path.join(app, "default", "app.conf")
)
spl.add(
os.path.join("examples", app, "README", "inputs.conf.spec"),
arcname=os.path.join(app, "README", "inputs.conf.spec")
)
splunklib_files = self.get_python_files(os.listdir(splunklib_arcname))
for file_name in splunklib_files:
spl.add(
os.path.join(splunklib_arcname, file_name),
arcname=os.path.join(app, "bin", splunklib_arcname, file_name)
)
modinput_files = self.get_python_files(os.listdir(modinput_dir))
for file_name in modinput_files:
spl.add(
os.path.join(modinput_dir, file_name),
arcname=os.path.join(app, "bin", modinput_dir, file_name)
)
spl.close()
# Create searchcommands_app-<three-part-version-number>-private.tar.gz
# but only if we are on 2.7 or later
if sys.version_info >= (2,7):
setup_py = os.path.join('examples', 'searchcommands_app', 'setup.py')
check_call(('python', setup_py, 'build', '--force'), stderr=STDOUT, stdout=sys.stdout)
tarball = 'searchcommands_app-{0}-private.tar.gz'.format(self.distribution.metadata.version)
source = os.path.join('examples', 'searchcommands_app', 'build', tarball)
target = os.path.join('build', tarball)
shutil.copyfile(source, target)
return
setup(
author="Splunk, Inc.",
author_email="[email protected]",
cmdclass={'coverage': CoverageCommand,
'test': TestCommand,
'testjunit': JunitXmlTestCommand,
'dist': DistCommand},
description="The Splunk Software Development Kit for Python.",
license="http://www.apache.org/licenses/LICENSE-2.0",
name="splunk-sdk",
packages = ["splunklib",
"splunklib.modularinput",
"splunklib.searchcommands"],
url="http://github.com/splunk/splunk-sdk-python",
version=splunklib.__version__,
classifiers = [
"Programming Language :: Python",
"Development Status :: 3 - Alpha",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Libraries :: Application Frameworks",
],
)