#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time           : 17-4-19 上午11:02
# @Author         : Tom.Lee
# @Description    : 
# @File           : helper_os.py
# @Product        : PyCharm
import commands
import os
import sys


def shell():
    command_ls = 'ls -al /opt'
    command_docker = 'docker ps -a'

    # 使用os.system()模块
    ros = os.system(command_ls)
    print '\n\nos.system() : ', ros

    # 使用os.popen()模块
    output = os.popen(command_docker)
    result = output.read()
    print '\n\nos.popen() : ', result

    # 使用commands模块
    (status, output) = commands.getstatusoutput(command_docker)
    print '\n\ncommands : ', status, output


def deep_look_dir(dir_path, deep
ull_path=False):
    """
    deep_look_dir(dir_name, console_full_path=False)

    遍历文件夹下所有文件
    :param dir_path:  os.path.dirname(__file__)
    :param deep:
    :param console_full_path:
    :return:
    """
    if deep == 1:
        print dir_path

    files = os.listdir(dir_path)
    split_symbol = '｜＿' * deep if deep == 1 else '｜' + '　' * (deep - 1) + '｜＿'

    for f in files:
        f_path = os.path.join(dir_path, f)
        console_name = f_path if console_full_path else f

        if not os.path.isfile(f_path):
            print "{sp} {dir_path}/: ".format(
                sp=split_symbol,
                dir_path=console_name)
            num = deep + 1
            deep_look_dir(f_path, num, console_full_path)

        else:
            print split_symbol, console_name


def sys_path():
    print '\n\n'

    print '当前文件路径: '
    print os.path.abspath(__file__)

    print '当前文件所在目录: '
    print os.path.dirname(__file__)

    print '当前文件所在目录的上一级目录: '
    print os.path.dirname(os.path.dirname(__file__))

    print '当前文件所在目录: '
    root_path = os.path.dirname(os.path.abspath(__file__))
    print root_path

    print '当前文件所在目录同一级的bin目录: '
    print os.path.abspath(os.path.join(root_path, '..', 'bin'))

    print '目录拼接: '
    print os.path.join(os.path.dirname(__file__), "templates")
    print os.path.join(os.path.dirname(os.path.dirname(__file__)), "templates")

    print '添加文件路径到系统环境变量: '
    if root_path not in sys.path:
        sys.path.append(root_path)
    print sys.path


if '__main__' == __name__:
    """TEST MAIN"""
    # shell()
    # sys_path()
    # print os.path.abspath(os.path.join('/opt/tom/', '..', 'bin'))  # /opt/bin
    # print os.path.dirname('/opt/tom')
    print '\033[1;31;m os.listdir() ==> \033[0m', os.listdir(os.getcwd())
    print'\033[1;31;m os.getcwd() ==> \033[0m', os.getcwd()
    print '\033[1;31;m os.getcwdu() ==> \033[0m', os.getcwdu()
    print '\033[1;31;m os.getegid() ==> \033[0m', os.getegid()
    print '\033[1;31;m os.getenv() ==> \033[0m', os.getenv('TOM_PATH', '/home/tom')
    print '\033[1;31;m os.geteuid() ==> \033[0m', os.geteuid()
    print '\033[1;31;m os.getgid() ==> \033[0m', os.getgid()
    print '\033[1;31;m os.getgroups() ==> \033[0m', os.getgroups()
    print '\033[1;31;m os.getppid() ==> \033[0m', os.getppid()
    print '\033[1;31;m os.getpgrp() ==> \033[0m', os.getpgrp()
    print '\033[1;31;m os.getresgid() ==> \033[0m', os.getresgid()
    print '\033[1;31;m os.getloadavg() ==> \033[0m', os.getloadavg()
    print '\033[1;31;m os.geteuid() ==> \033[0m', os.geteuid()

=1, console_f