Browse Source

Merge pull request #24 from Akrog/fix/autoalias

Fix/autoalias
Miguel Ángel Ajo 3 years ago
parent
commit
a202c68891
1 changed files with 26 additions and 12 deletions
  1. 26
    12
      oslogmerger/oslogmerger.py

+ 26
- 12
oslogmerger/oslogmerger.py View File

@@ -272,10 +272,17 @@ def reduce_strings(strings):
272 272
 
273 273
 
274 274
 def reduce_tree(tree):
275
-    if not len(tree):
275
+    # If there are no subdirectories we have finished with this subtree
276
+    if not len(tree[1]):
276 277
         return tree
278
+    # Reduce the names of all subdirectories in this directory
277 279
     reduced = reduce_strings(tree[1].keys())
278
-    return {k: (reduced[k], reduce_tree(v)) for k, v in tree[1].items()}
280
+    # For each of those subdirectories reduce subtrees using the reduced name
281
+    # but we still use the original diretory's name as the directory key.
282
+    return (tree[0],
283
+            {k: reduce_tree((reduced[k], v[1], v[2]))
284
+             for k, v in tree[1].items()},
285
+            tree[2])
279 286
 
280 287
 
281 288
 def generate_aliases(aliases, cfg):
@@ -327,6 +334,11 @@ def generate_aliases(aliases, cfg):
327 334
     # Split the path, and mark it all as not processed
328 335
     for k, v in non_aliased.items():
329 336
         split = v.split('/')
337
+        # If it's an absolute path we remove the split string and add / to the
338
+        # first directory
339
+        if split and not split[0]:
340
+            del split[0]
341
+            split[0] = '/' + split[0]
330 342
         non_aliased[k] = (split, '')
331 343
 
332 344
     non_aliased = process(aliased, non_aliased, map_file)
@@ -334,27 +346,29 @@ def generate_aliases(aliases, cfg):
334 346
 
335 347
     # Remove all paths that are not unique within their respective directories
336 348
     # For convenience we'll use a dictionary as a tree
337
-    tree = {}
349
+    tree = (None, {}, [])
338 350
     for k, v in non_aliased.items():
339 351
         last_tree = tree
340 352
         for directory in v[0]:
341
-            last_tree[directory] = (directory, {})
342
-            last_tree = last_tree[directory][1]
353
+            last_tree[1].setdefault(directory, (directory, {}, []))
354
+            last_tree = last_tree[1][directory]
355
+        # We have to store the filename in the last directory we visited
356
+        last_tree[2].append(v[1])
343 357
 
344 358
     # Reduce all paths as much as possible if alias level is 3
345 359
     if level == 3:
346
-        tree = reduce_tree(('', tree))
360
+        tree = reduce_tree(tree)
347 361
 
348
-    # Cleanup directorios from the non processed part
362
+    # Cleanup directories from the non processed part
349 363
     for k, v in non_aliased.items():
350 364
         last_tree = tree
351 365
         path = []
352 366
         for directory in v[0]:
353
-            # If a directory is non relevant (there is only 1 directory) we
354
-            # can remove it from the resulting path.
355
-            if len(last_tree) > 1:
356
-                path.append(last_tree[directory][0])
357
-            last_tree = last_tree[directory][1]
367
+            # If a directory is non relevant (there is only 1 directory and no
368
+            # files in it) we can remove it from the resulting path.
369
+            if len(last_tree[1]) > 1 or last_tree[2]:
370
+                path.append(last_tree[1][directory][0])
371
+            last_tree = last_tree[1][directory]
358 372
         non_aliased[k] = (path, v[1])
359 373
 
360 374
     # Add aliased items back

Loading…
Cancel
Save