~ballot/mojo/add_tests

« back to all changes in this revision

Viewing changes to mojo/utils.py

  • Committer: mergebot at canonical
  • Author(s): "Tom Haddon"
  • Date: 2020-07-21 08:09:52 UTC
  • mfrom: (563.2.4 bundle-show-1887199)
  • Revision ID: mergebot@juju-139df4-prod-is-toolbox-0.canonical.com-20200721080952-hx2b892rtxvtz8wx
Merge dictionaries as part of bundle show, rather than simply using dict update method



Reviewed-on: https://code.launchpad.net/~mthaddon/mojo/bundle-show-1887199/+merge/387644
Reviewed-by: Stuart Bishop <stuart.bishop@canonical.com>
Reviewed-by: Daniel Manrique <daniel.manrique@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
2
2
# GNU General Public License version 3 (see the file LICENSE).
 
3
import copy
3
4
import errno
4
5
import grp
5
6
import io
6
7
import locale
7
8
import logging
8
9
import os
9
 
from pickle import dump as pickle_dump, load as pickle_load
10
10
import pwd
11
11
import select
12
12
import shlex
14
14
import subprocess
15
15
import sys
16
16
import time
 
17
from collections.abc import Mapping
17
18
from hashlib import sha1
 
19
from pickle import dump as pickle_dump, load as pickle_load
18
20
 
19
21
from .exceptions import Unprivileged
20
22
from .shutil_which import get_command
489
491
    # (see https://github.com/pyinvoke/invoke/blob/93af29d/invoke/runners.py#L881)
490
492
    _encoding = locale.getpreferredencoding(False)
491
493
    return _encoding
 
494
 
 
495
 
 
496
def merge_dictionaries(orig_dict, new_dict):
 
497
    """Merge two dictionaries."""
 
498
    merged_dict = copy.deepcopy(orig_dict)
 
499
    for key, val in new_dict.items():
 
500
        if isinstance(val, Mapping):
 
501
            tmp = merge_dictionaries(orig_dict.get(key, {}), val)
 
502
            merged_dict[key] = tmp
 
503
        elif isinstance(val, list):
 
504
            merged_dict[key] = orig_dict.get(key, []) + val
 
505
        else:
 
506
            merged_dict[key] = new_dict[key]
 
507
    return merged_dict