Browse Source

Python 3.x compatibility

Now the code is python2 and python3 compatible, and they both
generate the same output regardless of subtle differences.

Change-Id: Ic6b908387becf4d0de5d9da72ce6915d3f7c3b1c
Miguel Angel Ajo 1 year ago
parent
commit
809bb6add3
1 changed files with 11 additions and 8 deletions
  1. 11
    8
      oslogmerger/oslogmerger.py

+ 11
- 8
oslogmerger/oslogmerger.py View File

@@ -11,8 +11,8 @@ import re
11 11
 import sys
12 12
 import tempfile
13 13
 import time
14
-import urllib2
15 14
 
15
+from six.moves.urllib.parse import urlparse
16 16
 
17 17
 __version__ = '1.1.0'
18 18
 
@@ -96,7 +96,11 @@ class LogEntry(object):
96 96
         self.data += EXTRALINES_PADDING + line
97 97
 
98 98
     def __cmp__(self, other):
99
-        return cmp(self.dt, other.dt)
99
+        return cmp(self.dt, other.dt) * 2 + cmp(self.alias, other.alias)
100
+
101
+    def __lt__(self, other):
102
+        return self.dt < other.dt or (self.dt == other.dt and
103
+                                      (self.alias < other.alias))
100 104
 
101 105
     def __str__(self):
102 106
         return '%s [%s] %s' % (self.dt_str, self.alias, self.data.rstrip('\n'))
@@ -327,7 +331,7 @@ class TSLogParser(LogParser):
327 331
 class LogFile(object):
328 332
     def _detect_format(self, filename, cfg):
329 333
         parsers = []
330
-        for cls in LOG_TYPES.values() + DETECTED_LOG_TYPES:
334
+        for cls in list(LOG_TYPES.values()) + DETECTED_LOG_TYPES:
331 335
             if cls is None:
332 336
                 continue
333 337
 
@@ -394,7 +398,7 @@ class LogFile(object):
394 398
             print("CACHED: %s at %s" % (url, path), file=sys.stderr)
395 399
             return path
396 400
         print("DOWNLOADING: %s to %s" % (url, path), file=sys.stderr)
397
-        http_in = urllib2.urlopen(url)
401
+        http_in = urlopen(url)
398 402
         file_out = open(path, 'w')
399 403
         file_out.write(http_in.read())
400 404
         file_out.close()
@@ -521,7 +525,7 @@ def reduce_tree(tree):
521 525
     if not len(tree[1]):
522 526
         return tree
523 527
     # Reduce the names of all subdirectories in this directory
524
-    reduced = reduce_strings(tree[1].keys())
528
+    reduced = reduce_strings(list(tree[1].keys()))
525 529
     # For each of those subdirectories reduce subtrees using the reduced name
526 530
     # but we still use the original diretory's name as the directory key.
527 531
     return (tree[0],
@@ -634,7 +638,7 @@ def all_unique_values(*args):
634 638
 
635 639
     values = set()
636 640
     for a in args:
637
-        vals = a.values()
641
+        vals = list(a.values())
638 642
         if vals and isinstance(vals[0], tuple):
639 643
             vals = [reconstruct_path(v) for v in vals]
640 644
         values.update(vals)
@@ -736,8 +740,7 @@ one has not been provided:'
736 740
                 2016-02-01 10:26:34.680 [1/N-API]
737 741
                 2016-02-01 10:27:34.680 [2/N-CPU]
738 742
 """
739
-
740
-    parser = MyParser(description=general_description, version=__version__,
743
+    parser = MyParser(description=general_description,
741 744
                       epilog=general_epilog, argument_default='',
742 745
                       formatter_class=argparse.RawTextHelpFormatter)
743 746
     parser.add_argument('--log-base ', '-b', dest='log_base',

Loading…
Cancel
Save