From 206c5a988ab003af7c04b82044eb565d8b3bcfb7 Mon Sep 17 00:00:00 2001 From: Harry Huang Date: Tue, 13 Mar 2018 10:52:01 +0800 Subject: Modify repo structure JIRA: - 1. keep modules in lib directory instead of auto Change-Id: Ie4c51b28554575bafbaa89c5f57309a786b903e0 Signed-off-by: Harry Huang --- lib/auto/util/util.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 lib/auto/util/util.py (limited to 'lib/auto/util/util.py') diff --git a/lib/auto/util/util.py b/lib/auto/util/util.py new file mode 100644 index 0000000..0033900 --- /dev/null +++ b/lib/auto/util/util.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +######################################################################## +# Copyright (c) 2018 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +######################################################################## + +"""Utility Module""" + +import os +import git +import urllib +import yaml +import traceback +from Crypto.PublicKey import RSA +from yaml_type import literal_unicode + +__author__ = "Harry Huang " + + +def folded_unicode_representer(dumper, data): + return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='>') + + +def literal_unicode_representer(dumper, data): + return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|') + + +def unicode_representer(dumper, uni): + node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni) + return node + + +def mkdir(path): + path = path.strip() + path = path.rstrip("\\") + isExist = os.path.exists(path) + if not isExist: + os.makedirs(path) + return True + else: + return False + + +def download(url, file_path): + if os.path.exists(file_path): + return False + else: + urllib.urlretrieve(url, file_path) + return True + + +def git_clone(git_repo, git_branch, clone_path): + if not os.path.exists(clone_path): + git.Repo.clone_from(git_repo, clone_path, branch=git_branch) + + +def read_file(file_path): + with open(os.path.expanduser(file_path)) as fd: + return fd.read() + + +def read_yaml(yaml_path): + with open(os.path.expanduser(yaml_path)) as fd: + return yaml.safe_load(fd) + + +def write_yaml(yaml_data, yaml_path, default_style=False): + yaml.add_representer(literal_unicode, literal_unicode_representer) + yaml.add_representer(unicode, unicode_representer) + with open(os.path.expanduser(yaml_path), 'w') as fd: + return yaml.dump(yaml_data, fd, + default_flow_style=default_style) + + +def create_keypair(prikey_path, pubkey_path, size=2048): + key = RSA.generate(size) + with open(os.path.expanduser(prikey_path), 'w') as prikey_file: + os.chmod(prikey_path, 0600) + prikey_file.write(key.exportKey('PEM')) + pubkey = key.publickey() + with open(os.path.expanduser(pubkey_path), 'w') as pubkey_file: + pubkey_file.write(pubkey.exportKey('OpenSSH')) -- cgit 1.2.3-korg