aboutsummaryrefslogtreecommitdiffstats
path: root/auto/util/util.py
blob: 003390015f2aba88a3bcb6e6abe40f4af0bc0e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 <huangxiangyu5@huawei.com>"


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'))