Skip to content

Commit c7bd197

Browse files
author
Shakeel Mohamed
committed
Fix issues with Python 2.6 support
Fixes issue splunk#141 on GitHub
1 parent f464ae8 commit c7bd197

File tree

9 files changed

+109
-40
lines changed

9 files changed

+109
-40
lines changed

examples/searchcommands_app/setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def symlink(source, link_name):
5757
patch_os()
5858
del locals()['patch_os'] # since this function has done its job
5959

60-
from collections import OrderedDict
60+
try:
61+
from collections import OrderedDict
62+
except:
63+
from splunklib.ordereddict import OrderedDict
6164
from glob import glob
6265
from itertools import chain
6366
from setuptools import setup, Command

splunklib/results.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
import xml.etree.ElementTree as et
3939

4040
try:
41-
from collections import OrderedDict
42-
except:
43-
from ordereddict import OrderedDict
41+
from collections import OrderedDict # must be python 2.7
42+
except ImportError:
43+
from splunklib.ordereddict import OrderedDict
4444

4545
try:
4646
from cStringIO import StringIO

splunklib/searchcommands/decorators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
from __future__ import absolute_import, division, print_function, unicode_literals
1818

19-
from collections import OrderedDict # must be python 2.7
19+
try:
20+
from collections import OrderedDict # must be python 2.7
21+
except ImportError:
22+
from splunklib.ordereddict import OrderedDict
23+
2024
from inspect import getmembers, isclass, isfunction
2125
from itertools import imap
2226

splunklib/searchcommands/internals.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
from __future__ import absolute_import, division, print_function, unicode_literals
1818

19-
from collections import deque, namedtuple, OrderedDict
19+
from collections import deque, namedtuple
20+
try:
21+
from collections import OrderedDict # must be python 2.7
22+
except ImportError:
23+
from splunklib.ordereddict import OrderedDict
2024
from cStringIO import StringIO
2125
from itertools import chain, imap
2226
from json import JSONDecoder, JSONEncoder
@@ -763,7 +767,7 @@ def _clear(self):
763767
def _write_chunk(self, metadata, body):
764768

765769
if metadata:
766-
metadata = str(''.join(self._iterencode_json({n: v for n, v in metadata if v is not None}, 0)))
770+
metadata = str(''.join(self._iterencode_json(dict([(n, v) for n, v in metadata if v is not None]), 0)))
767771
metadata_length = len(metadata)
768772
else:
769773
metadata_length = 0

splunklib/searchcommands/search_command.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@
2020

2121
from splunklib.client import Service
2222

23-
from collections import namedtuple, OrderedDict
23+
try:
24+
__named_tuple_check = namedtuple
25+
except NameError:
26+
from collections import namedtuple
27+
try:
28+
from collections import OrderedDict # must be python 2.7
29+
except ImportError:
30+
from splunklib.ordereddict import OrderedDict
2431
from copy import deepcopy
2532
from cStringIO import StringIO
2633
from itertools import chain, ifilter, imap, islice, izip
2734
from logging import _levelNames, getLevelName, getLogger
28-
from shutil import make_archive
35+
try:
36+
from shutil import make_archive
37+
except ImportError:
38+
# Used for recording, skip on python 2.6
39+
pass
2940
from time import time
3041
from urllib import unquote
3142
from urlparse import urlsplit
@@ -572,8 +583,8 @@ def _process_protocol_v1(self, argv, ifile, ofile):
572583
debug('Writing configuration settings')
573584

574585
ifile = self._prepare_protocol_v1(argv, ifile, ofile)
575-
self._record_writer.write_record({
576-
n: ','.join(v) if isinstance(v, (list, tuple)) else v for n, v in self._configuration.iteritems()})
586+
self._record_writer.write_record(dict(
587+
(n, ','.join(v) if isinstance(v, (list, tuple)) else v) for n, v in self._configuration.iteritems()))
577588
self.finish()
578589

579590
elif argv[1] == '__EXECUTE__':
@@ -884,7 +895,7 @@ def _records_protocol_v1(self, ifile):
884895
except StopIteration:
885896
return
886897

887-
mv_fieldnames = {name: name[len('__mv_'):] for name in fieldnames if name.startswith('__mv_')}
898+
mv_fieldnames = dict([(name, name[len('__mv_'):]) for name in fieldnames if name.startswith('__mv_')])
888899

889900
if len(mv_fieldnames) == 0:
890901
for values in reader:
@@ -926,7 +937,7 @@ def _records_protocol_v2(self, ifile):
926937
except StopIteration:
927938
return
928939

929-
mv_fieldnames = {name: name[len('__mv_'):] for name in fieldnames if name.startswith('__mv_')}
940+
mv_fieldnames = dict([(name, name[len('__mv_'):]) for name in fieldnames if name.startswith('__mv_')])
930941

931942
if len(mv_fieldnames) == 0:
932943
for values in reader:

tests/searchcommands/test_builtin_options.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919

2020
from cStringIO import StringIO
21-
from unittest import main, TestCase
21+
try:
22+
from unittest2 import main, TestCase
23+
except ImportError:
24+
from unittest import main, TestCase
2225

2326
import os
2427
import sys
@@ -28,7 +31,13 @@
2831
from splunklib.searchcommands.decorators import Configuration
2932
from splunklib.searchcommands.search_command import SearchCommand
3033

31-
from tests.searchcommands import package_directory, rebase_environment
34+
try:
35+
from tests.searchcommands import rebase_environment, package_directory
36+
except:
37+
# Skip on Python 2.6
38+
def rebase_environment(ignore):
39+
return
40+
pass
3241

3342

3443
@Configuration()

tests/searchcommands/test_decorators.py

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

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919

20-
from unittest import main, TestCase
20+
try:
21+
from unittest2 import main, TestCase
22+
except ImportError:
23+
from unittest import main, TestCase
2124
import sys
2225

2326
from splunklib.searchcommands import Configuration, Option, environment, validators
2427
from splunklib.searchcommands.decorators import ConfigurationSetting
2528
from splunklib.searchcommands.internals import json_encode_string
2629
from splunklib.searchcommands.search_command import SearchCommand
2730

28-
from tests.searchcommands import rebase_environment
31+
try:
32+
from tests.searchcommands import rebase_environment
33+
except ImportError:
34+
# Skip on Python 2.6
35+
pass
2936

3037

3138
@Configuration()
@@ -227,7 +234,7 @@ def fix_up(cls, command_class):
227234
(True, False),
228235
(None, 'anything other than a bool')),
229236
('required_fields',
230-
(['field_1', 'field_2'], {'field_1', 'field_2'}, ('field_1', 'field_2')),
237+
(['field_1', 'field_2'], set(['field_1', 'field_2']), ('field_1', 'field_2')),
231238
(None, 0xdead, {'foo': 1, 'bar': 2})),
232239
('requires_preop',
233240
(True, False),

tests/searchcommands/test_internals_v2.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919

2020
from splunklib.searchcommands.internals import MetadataDecoder, MetadataEncoder, Recorder, RecordWriterV2
2121
from splunklib.searchcommands import SearchMetric
22-
from collections import deque, namedtuple, OrderedDict
22+
try:
23+
from collections import OrderedDict # must be python 2.7
24+
except ImportError:
25+
from splunklib.ordereddict import OrderedDict
26+
try:
27+
__named_tuple_check = namedtuple
28+
except NameError:
29+
# for Python 2.6
30+
from collections import namedtuple
2331
from cStringIO import StringIO
2432
from functools import wraps
2533
from glob import iglob
@@ -28,7 +36,11 @@
2836
from tempfile import mktemp
2937
from time import time
3038
from types import MethodType
31-
from unittest import main, TestCase
39+
from sys import version_info as python_version
40+
try:
41+
from unittest2 import main, TestCase
42+
except ImportError:
43+
from unittest import main, TestCase
3244

3345
import cPickle as pickle
3446
import gzip
@@ -106,13 +118,18 @@ def test_object_view(self):
106118

107119
def test_recorder(self):
108120

121+
if (python_version[0] == 2 and python_version[1] < 7):
122+
print("Skipping test since we're on {1}".format("".join(python_version)))
123+
pass
124+
109125
# Grab an input/output recording, the results of a prior countmatches run
110126

111127
recording = os.path.join(self._package_path, 'recordings', 'scpv2', 'Splunk-6.3', 'countmatches.')
112128

113-
with gzip.open(recording + 'input.gz', 'rb') as file_1, io.open(recording + 'output', 'rb') as file_2:
114-
ifile = StringIO(file_1.read())
115-
result = StringIO(file_2.read())
129+
with gzip.open(recording + 'input.gz', 'rb') as file_1:
130+
with io.open(recording + 'output', 'rb') as file_2:
131+
ifile = StringIO(file_1.read())
132+
result = StringIO(file_2.read())
116133

117134
# Set up the input/output recorders that are under test
118135

@@ -138,9 +155,10 @@ def test_recorder(self):
138155
ifile._recording.close()
139156
ofile._recording.close()
140157

141-
with gzip.open(ifile._recording.name, 'rb') as file_1, gzip.open(ofile._recording.name, 'rb') as file_2:
142-
self.assertEqual(file_1.read(), ifile._file.getvalue())
143-
self.assertEqual(file_2.read(), ofile._file.getvalue())
158+
with gzip.open(ifile._recording.name, 'rb') as file_1:
159+
with gzip.open(ofile._recording.name, 'rb') as file_2:
160+
self.assertEqual(file_1.read(), ifile._file.getvalue())
161+
self.assertEqual(file_2.read(), ofile._file.getvalue())
144162

145163
finally:
146164
ofile._recording.close()

tests/searchcommands/test_searchcommands_app.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@
3131
from datetime import datetime
3232
from itertools import ifilter, imap, izip
3333
from subprocess import PIPE, Popen
34-
from unittest import main, skipUnless, TestCase
34+
35+
try:
36+
from unittest2 import main, skipUnless, TestCase
37+
except ImportError:
38+
from unittest import main, skipUnless, TestCase
3539

3640
import gzip
3741
import json
3842
import csv
3943
import io
4044
import os
4145

42-
from tests.searchcommands import project_root
46+
try:
47+
from tests.searchcommands import project_root
48+
except ImportError:
49+
# Python 2.6
50+
pass
4351

4452

4553
def pypy():
@@ -134,7 +142,11 @@ def __iter__(self):
134142

135143
class TestSearchCommandsApp(TestCase):
136144

137-
app_root = os.path.join(project_root, 'examples', 'searchcommands_app', 'build', 'searchcommands_app')
145+
try:
146+
app_root = os.path.join(project_root, 'examples', 'searchcommands_app', 'build', 'searchcommands_app')
147+
except NameError:
148+
# SKip if Python 2.6
149+
pass
138150

139151
def setUp(self):
140152
if not os.path.isdir(TestSearchCommandsApp.app_root):
@@ -388,17 +400,18 @@ def _run_command(self, name, action=None, phase=None, protocol=2):
388400
compressed_file = recording.input_file
389401
uncompressed_file = os.path.splitext(recording.input_file)[0]
390402
try:
391-
with gzip.open(compressed_file, 'rb') as ifile, io.open(uncompressed_file, 'wb') as ofile:
392-
b = bytearray(io.DEFAULT_BUFFER_SIZE)
393-
n = len(b)
394-
while True:
395-
count = ifile.readinto(b)
396-
if count == 0:
397-
break
398-
if count < n:
399-
ofile.write(b[:count])
400-
break
401-
ofile.write(b)
403+
with gzip.open(compressed_file, 'rb') as ifile:
404+
with io.open(uncompressed_file, 'wb') as ofile:
405+
b = bytearray(io.DEFAULT_BUFFER_SIZE)
406+
n = len(b)
407+
while True:
408+
count = ifile.readinto(b)
409+
if count == 0:
410+
break
411+
if count < n:
412+
ofile.write(b[:count])
413+
break
414+
ofile.write(b)
402415
with io.open(uncompressed_file, 'rb') as ifile:
403416
process = Popen(recording.get_args(command), stdin=ifile, stderr=PIPE, stdout=PIPE)
404417
output, errors = process.communicate()

0 commit comments

Comments
 (0)