Skip to content

Commit f489b05

Browse files
author
Eric Cheng
authored
Update searchcommands app example (#240)
searchcommands_app example now packages splunklib under lib folder instead of bin folder based on best practice
1 parent c7e8d68 commit f489b05

File tree

11 files changed

+43
-30
lines changed

11 files changed

+43
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.coverage
77
.coverage.*
88
.python-version
9+
.vscode
910
__stdout__
1011
docs/_build
1112
build/
@@ -24,7 +25,7 @@ tests/searchcommands/data/app/app.log
2425
splunk_sdk.egg-info/
2526
dist/
2627
examples/searchcommands_app/package/default/commands.conf
27-
examples/searchcommands_app/package/bin/packages
28+
examples/searchcommands_app/package/lib/splunklib
2829
tests/searchcommands/apps/app_with_logging_configuration/*.log
2930
*.observed
3031
venv/

examples/searchcommands_app/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ The app is tested on Splunk 5 and 6. Here is its manifest:
1515

1616
```
1717
├── bin
18-
│ ├── splunklib
19-
│ │ └── searchcommands ....... splunklib.searchcommands module
2018
│   ├── countmatches.py .......... CountMatchesCommand implementation
2119
│ ├── generatetext.py .......... GenerateTextCommand implementation
2220
│ ├── pypygeneratetext.py ...... Executes generatetext.py with PyPy
2321
│ ├── simulate.py .............. SimulateCommand implementation
24-
│ ├── sum.py ................... SumCommand implementation
25-
│   └──
22+
│ └── sum.py ................... SumCommand implementation
23+
├── lib
24+
| └── splunklib
25+
│ └── searchcommands ....... splunklib.searchcommands module
2626
├── default
2727
│ ├── data
2828
│ │   └── ui

examples/searchcommands_app/package/bin/countmatches.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20+
import os,sys
2021

22+
splunkhome = os.environ['SPLUNK_HOME']
23+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
2124
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration, Option, validators
22-
import sys
2325
from splunklib import six
2426

2527

examples/searchcommands_app/package/bin/filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20+
import os,sys
2021

22+
splunkhome = os.environ['SPLUNK_HOME']
23+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
2124
from splunklib.searchcommands import dispatch, EventingCommand, Configuration, Option
2225
from splunklib.searchcommands.validators import Code
2326

24-
import sys
25-
26-
2727
@Configuration()
2828
class FilterCommand(EventingCommand):
2929
""" Filters, augments, and updates records on the events stream.

examples/searchcommands_app/package/bin/generatehello.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20+
import os,sys
21+
import time
2022

23+
splunkhome = os.environ['SPLUNK_HOME']
24+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
2125
from splunklib.searchcommands import dispatch, GeneratingCommand, Configuration, Option, validators
22-
import sys
23-
import time
24-
from six.moves import range
26+
from splunklib.six.moves import range
2527

2628

2729
@Configuration()

examples/searchcommands_app/package/bin/generatetext.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20+
import os,sys
21+
import time
2022

23+
splunkhome = os.environ['SPLUNK_HOME']
24+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
2125
from splunklib.searchcommands import dispatch, GeneratingCommand, Configuration, Option, validators
22-
import sys
23-
import time
2426
from splunklib import six
2527
from splunklib.six.moves import range
2628

examples/searchcommands_app/package/bin/pypygeneratetext.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@
6363

6464
from __future__ import absolute_import, division, print_function, unicode_literals
6565
import app
66-
67-
from splunklib.searchcommands import app_root, execute
66+
import sys
6867
from os import environ, path
6968

70-
import sys
69+
splunkhome = environ['SPLUNK_HOME']
70+
sys.path.append(path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
71+
from splunklib.searchcommands import app_root, execute
7172

7273
pypy_argv = ['pypy', path.join(app_root, 'bin', 'generatetext.py')] + sys.argv[1:]
7374
pypy_environ = dict(environ)

examples/searchcommands_app/package/bin/simulate.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20-
21-
from splunklib.searchcommands import dispatch, GeneratingCommand, Configuration, Option, validators
2220
import random
2321
import csv
24-
import sys
22+
import os,sys
2523
import time
2624

25+
splunkhome = os.environ['SPLUNK_HOME']
26+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
27+
from splunklib.searchcommands import dispatch, GeneratingCommand, Configuration, Option, validators
28+
2729

2830
@Configuration()
2931
class SimulateCommand(GeneratingCommand):

examples/searchcommands_app/package/bin/sum.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919
import app
20+
import os,sys
2021

22+
splunkhome = os.environ['SPLUNK_HOME']
23+
sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'searchcommands_app', 'lib'))
2124
from splunklib.searchcommands import dispatch, ReportingCommand, Configuration, Option, validators
22-
import sys
2325

2426

2527
@Configuration(requires_preop=True)

examples/searchcommands_app/setup.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def install_packages(app_root, distribution):
8888
if not requires:
8989
return
9090

91-
target = os.path.join(app_root, 'bin', 'packages')
91+
target = os.path.join(app_root, 'lib', 'packages')
9292

9393
if not os.path.isdir(target):
9494
os.mkdir(target)
@@ -323,7 +323,7 @@ def run(self):
323323
message = 'Cannot create a link at "{}" because a file by that name already exists.'.format(target)
324324
raise SystemError(message)
325325

326-
packages = os.path.join(self.app_source, 'bin', 'packages')
326+
packages = os.path.join(self.app_source, 'lib')
327327

328328
if not os.path.isdir(packages):
329329
os.mkdir(packages)
@@ -454,13 +454,12 @@ def run(self):
454454
'Topic :: System :: Logging',
455455
'Topic :: System :: Monitoring'],
456456
packages=[
457-
'bin.packages.splunklib', 'bin.packages.splunklib.searchcommands'
457+
'lib.splunklib', 'lib.splunklib.searchcommands'
458458
],
459459
package_dir={
460-
'bin': os.path.join('package', 'bin'),
461-
'bin.packages': os.path.join('package', 'bin', 'packages'),
462-
'bin.packages.splunklib': os.path.join('..', '..', 'splunklib'),
463-
'bin.packages.splunklib.searchcommands': os.path.join('..', '..', 'splunklib', 'searchcommands')
460+
'lib': os.path.join('package', 'lib'),
461+
'lib.splunklib': os.path.join('..', '..', 'splunklib'),
462+
'lib.splunklib.searchcommands': os.path.join('..', '..', 'splunklib', 'searchcommands')
464463
},
465464
package_data={
466465
'bin': [

0 commit comments

Comments
 (0)