2016-11-25 15:52:03 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
2020-05-22 19:16:02 +01:00
|
|
|
import sys
|
2016-11-25 15:52:03 +03:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2020-02-08 16:24:53 +01:00
|
|
|
def mergepwd(old, new, final, clean=False):
|
2019-02-09 22:27:50 +01:00
|
|
|
with open(old, "r") as old_file:
|
2016-11-25 15:52:03 +03:00
|
|
|
old_passwords = yaml.safe_load(old_file)
|
|
|
|
|
2019-02-09 22:27:50 +01:00
|
|
|
with open(new, "r") as new_file:
|
2016-11-25 15:52:03 +03:00
|
|
|
new_passwords = yaml.safe_load(new_file)
|
|
|
|
|
2020-05-22 19:16:02 +01:00
|
|
|
if not isinstance(old_passwords, dict):
|
|
|
|
print("ERROR: Old passwords file not in expected key/value format")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not isinstance(new_passwords, dict):
|
|
|
|
print("ERROR: New passwords file not in expected key/value format")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-02-08 16:24:53 +01:00
|
|
|
if clean:
|
|
|
|
# keep only new keys
|
|
|
|
for key in new_passwords:
|
|
|
|
if key in old_passwords:
|
|
|
|
new_passwords[key] = old_passwords[key]
|
|
|
|
else:
|
|
|
|
# old behavior
|
|
|
|
new_passwords.update(old_passwords)
|
2016-11-25 15:52:03 +03:00
|
|
|
|
2019-02-09 22:27:50 +01:00
|
|
|
with open(final, "w") as destination:
|
2017-03-30 16:54:00 +08:00
|
|
|
yaml.safe_dump(new_passwords, destination, default_flow_style=False)
|
2016-11-25 15:52:03 +03:00
|
|
|
|
|
|
|
|
2019-02-09 22:27:50 +01:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--old", help="old password file", required=True)
|
|
|
|
parser.add_argument("--new", help="new password file", required=True)
|
|
|
|
parser.add_argument("--final", help="merged password file", required=True)
|
2020-02-08 16:24:53 +01:00
|
|
|
parser.add_argument("--clean",
|
|
|
|
help="clean (keep only new keys)",
|
|
|
|
action='store_true')
|
2019-02-09 22:27:50 +01:00
|
|
|
args = parser.parse_args()
|
2020-02-08 16:24:53 +01:00
|
|
|
mergepwd(args.old, args.new, args.final, args.clean)
|
2019-02-09 22:27:50 +01:00
|
|
|
|
|
|
|
|
2016-11-25 15:52:03 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|