【python第11日】自动生成项目目录 安全插入本文件夹下文件,即使别人引用也可以

自动生成项目目录 

#/usr/bin/env python3
# encoding : utf-8
import getopt
import os
import sys


def usage():
    "帮助信息"
    print("帮助信息:")
    print("\t获取帮助:%s [-h|--help]" % os.path.basename(__file__))
    print("\t添加目录: %s [-a|--append] "% os.path.basename(__file__))
    print("\t添加文件: %s [-f|--file]" % os.path.basename(__file__))
    print("\t添加模式已经废弃默认存在不删除: %s [-m|--mode]" % os.path.basename(__file__))
    print("如果有要添加的第一级别目录,请用-a或者--append添加即可")
    print("如果有要添加的文件,请用/分开,例如:\"bin/script/create_dir_structure.py\",请用-f或者--file添加即可")
    print("模式0表示如果目录或者文件存在,则不变,1表示强制删除添加")
    return


def create_dir(dir_name, mode):
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)


def create_file(file_name, mode):
    dir_tmp = ""
    file = ""
    #先判断是否有目录,先创建目录再创建文件
    if "/" in file_name:
        dir_tmp = file_name.rsplit("/", 1)[0]
        file = file_name.rsplit("/", 1)[-1]
    else:
        file = file_name

    if dir_tmp:
        create_dir(dir_tmp, mode)
    if file and not os.path.exists(file_name) and file_name.endswith(".py"):
        with open(file_name, "w+", encoding="utf-8") as f:
            f.write("#/usr/bin/env python3\n")
            f.write("# encoding = utf-8\n")
            f.write("__author__ = \"周广露\" \n")


def create_structure(dir_list, file_list, mode):
    "创建文件夹"
    for dir_one in dir_list:
        if dir_one in file_list:
            create_file(dir_one, mode)
        else:
            create_dir(dir_one, mode)
            create_file(dir_one + "/__init__.py", mode)

    #为conf/setting.py 提供配置信息
    confpath = "conf/setting.py"
    if os.path.exists(confpath):
        with open(confpath, "a", encoding="utf8") as f:
            writelines = [
                "\n",
                "import  os\n"
                "\n",
                "#Path setting\n",
                "BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n",
                "BINDIR = os.path.join(BASEDIR, \"bin\")\n",
                "CONFDIR = os.path.join(BASEDIR, \"conf\")\n",
                "LIBDIR = os.path.join(BASEDIR, \"lib\")\n",
                "DBDIR = os.path.join(BASEDIR, \"db\")\n",
                "LOGDIR = os.path.join(BASEDIR, \"log\")\n",
                "PLUGINDIR = os.path.join(BASEDIR, \"plugin\")\n",
                "\n",
                "\n",
                "#配置信息\n"
            ]
            f.writelines(writelines)
if __name__ == '__main__':
    mode = 0
    dir_list = ["__init__.py", "bin/__init__.py", "conf/__init__.py","conf/setting.py", "db/__init__.py", "plugin/", "lib", "log", "setup.py"]
    file_list = ["__init__.py", "bin/__init__.py","conf/__init__.py", "conf/setting.py", "db/__init__.py", "setup.py"]
    opts, args = getopt.getopt(sys.argv[1:],"hf:m:a:", ["help", "file=", "mode=", "append="])

    for opt in opts :
        if "-h" in opt or "--help" in opt:
            usage()

        if "-m" in opt or "--mode" in opt:
            opt1, opt2 = opt
            if opt2 == "1" :
                mode = 1
            elif opt2 != "0":
                print("参数m设置错误")
                usage()
                sys.exit(0)

        if "-a" in opt or "--append" in opt:
            opt1, opt2 = opt
            dir_list.append(opt2)

        if "-f" in opt or "--file" in opt:
            opt1, opt2 = opt
            # tmplist = opt2.split("/")
            dir_list.append(opt2)
            file_list.append(opt2)

    #把剩余的东西放到目录
    for arg in args:
        print("有多余参数")
        usage()
        sys.exit(0)

    create_structure(dir_list , file_list , mode)

安全import

import os
import sys


def safe_insert(f_path):
    """
    把文件的路径添加到sys中去
    :param f_path:传入的是文件的绝对路径
    :return:
    """
    BASE_DIR = f_path
    if not os.path.isdir(f_path):
        BASE_DIR=os.path.dirname(f_path)

    if BASE_DIR in sys.path:
        sys.path.remove(BASE_DIR)
    sys.path.insert(0,BASE_DIR)

if __name__ == '__main__':
    safe_insert(os.path.abspath(__file__))
    print(sys.path)

 

posted @ 2019-01-15 22:25  周sir搞人工  阅读(371)  评论(0编辑  收藏  举报