python Django项目通过编译为.pyc 形式发布


一、批量编译代码

创建compile_pyc.py,代码如下:

import os
import sys
import shutil
from py_compile import compile

def clean(path):
    for parent, dirname, filename in os.walk(path):
        for dir in dirname:
            if dir == '__pycache__':
                try:
                    fullname = os.path.join(parent, dir)
                    shutil.rmtree(fullname)
                    print("Success clean Folder:%s" % fullname)
                except Exception as e:
                    print("Can't clean Folder:%s, reason:%s" % (fullname, e))

def compile_pyc(path):
    for parent, dirname, filename in os.walk(path):
        for cfile in filename:
            fullname = os.path.join(parent, cfile)
            if cfile[-3:] == '.py':
                try:
                    if compile(fullname):
                        if cfile != 'settings.py' and cfile != 'wsgi.py':
                            os.remove(fullname) #删除原文件,保留settings.py和wsgi.py
                        print("Success compile and remove file:%s" % fullname)
                    else:
                        print("Can't compile file:%s,The original file has been retained" % fullname)
                except Exception as e:
                    print("Can't compile file:%s, reason:%s" % (fullname, e))

def move(path):
    for parent, dirname, filename in os.walk(path):
        for c_file in filename:
            fullname = os.path.join(parent, c_file)
            if c_file[-4:] == '.pyc':
                try:
                    parent_path = os.path.dirname(parent)
                    shutil.move(fullname, parent_path)
                    print('update the dir of file successfully')
                except Exception as e:
                    print("Can't move file:%s, reason:%s" % (fullname, e))

def replace_name(path):
    for parent, dirname, filename in os.walk(path):
        for c_file in filename:
            fullname = os.path.join(parent, c_file)
            if c_file[-4:] == '.pyc':
                try:
                    cfile_name = ''
                    cfile_list = c_file.split('.')
                    version = sys.version_info
                    replace_name = 'cpython-' + str(version[0])+str(version[1])
                    for i in range(len(cfile_list)):
                        if cfile_list[i] == replace_name:
                            continue
                        cfile_name += cfile_list[i]
                        if i == len(cfile_list) - 1:
                            continue
                        cfile_name += '.'
                    shutil.move(fullname, os.path.join(parent, cfile_name))
                    print('update the name of the file successfully')
                except Exception as e:
                    print("Can't remove file:%s, reason:%s" % (fullname, e))

if __name__ == '__main__':
    if len(sys.argv) == 3:
        cmd = sys.argv[1]
        path = sys.argv[2]
        if os.path.exists(path) and os.path.isdir(path):
            if cmd == 'clean':
                clean(path)
            elif cmd == 'move':
                move(path)
            elif cmd == 'replace_name':
                replace_name(path)
            elif cmd == 'compile_pyc':
                compile_pyc(path)
            elif cmd == 'compile':
                clean(path)
                compile_pyc(path)
                move(path)
                replace_name(path)
                clean(path)
            else:
                print('没有该命令')
        else:
            print("Not an directory or Direcotry doesn't exist!")
    else:
        print("在命令行中使用以下命令:")
        print("\tpython3 compile_pyc.py compile_pyc PATH\t\t#生成pyc文件")
        print("\tpython3 compile_pyc.py move PATH\t\t#移动所有pyc文件至原位置")
        print("\tpython3 compile_pyc.py replace_name PATH\t\t#修改文件名文件")
        print("\tpython3 compile_pyc.py clean PATH\t\t#清除当前项目中所有的__pycache__文件夹及以内文件")
        print("\tpython3 compile_pyc.py compile PATH\t\t#一键编译pyc")

二、运行命令

逐步运行以下命令

python3 compile_pyc.py clean PATH #清除当前项目中所有的__pycache__文件夹及以内文件
python3 compile_pyc.py compile_pyc PATH #生成.pyc文件,并删除源文件
python3 compile_pyc.py move PATH #将.pyc文件移动到原py文件目录下
python3 compile_pyc.py replace_name PATH #去掉.pyc文件名中的版本号
python3 compile_pyc.py clean PATH

或者直接运行以下一条命令就可以

python3 compile_pyc.py compile PATH

三、注意

保留了settings.py和wsgi.py这两个文件,方便改变配置,以及之后的uwsgi部署


多次运行,.pyc会移动错乱,保证只移动在__pycache__下的.pyc文件,修改move代码

def move(path):
    for parent, dirname, filename in os.walk(path):
        for c_file in filename:
            fullname = os.path.join(parent, c_file)
            if os.path.basename(parent) == '__pycache__':
                if c_file[-4:] == '.pyc':
                    try:
                        parent_path = os.path.dirname(parent)
                        shutil.move(fullname, parent_path)
                        print('update the dir of file successfully')
                    except Exception as e:
                        print("Can't move file:%s, reason:%s" % (fullname, e))
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python Django 项目源码是指使用Python编写的基于Django框架的Web应用程序的全部代码。 Django是一个开源的Web框架,使用Python语言编写。它提供了一套完善的工具和库,用于快速开发高品质的Web应用程序。Python Django项目源码通常由多个文件组成,包含了项目的核心逻辑和各个功能模块的代码。 在Python Django项目源码中,最重要的文件是项目的配置文件和应用程序文件。配置文件通常命名为`settings.py`,它包含了项目的全局配置信息,如数据库连接、静态文件路径等。应用程序文件则是各个功能模块的代码文件,通常被组织在一个名为`apps`或`modules`的文件夹中,每个应用程序都有自己的模型、视图、模板等文件。 在一个典型的Python Django项目源码中,还可能包含其他类型的文件,如静态文件(如CSS、JavaScript)、模板文件、数据库迁移文件、测试文件等,这些文件用于支持和完善项目的功能。 Python Django项目源码的组织结构可以根据个人的喜好和项目规模进行调整,但在一般情况下,源码会遵循MVC(Model-View-Controller)或MTV(Model-Template-View)的设计模式,便于代码的管理和维护。 总之,Python Django项目源码是一个基于Django框架的Web应用程序的全部代码,包含了项目的配置信息、功能模块的代码文件和其他支持文件。通过阅读和理解源码,开发人员可以更好地了解项目的结构和实现细节,从而进行二次开发和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值