Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Copy Local Dev site by Python script

Recommended Posts

本地开发站点复制。

用途: 从父级目录下的mints001.com递归复制全部文件过来,然后修改 wp-config.php和 wp-content/advanced-cache.php 两个文件。

默认地,域名为当前工作目录的名称。

默认地, 当前站点和 mints001.com 的数据库IP,数据库用户一致。

用例:

python b.py -d [数据库名称]

如果需要单独指定域名,应加参数

python b.py -d [数据库名称] -o [域名]

脚本文件

# -*- coding: utf-8 -*-
# @Author: suifengtec
# @Date:   2018-07-04 06:17:50
# @Last Modified by:    suifengtec
# @Last Modified time: 2018-07-04 06:27:07

import os
import shutil
import glob
import os.path
import fileinput
import sys
from pathlib import Path
import inspect
import getopt
import webbrowser

def recursiveCopyFiles(source_path, destination_path, override=False):
    """
   递归复制源目录下的目录或文件到指定的目录,第三个参数用于指定是否覆盖。
    """

    files_count = 0
    if not os.path.exists(destination_path):
        os.mkdir(destination_path)
    items = glob.glob(source_path + '\\*')
    for item in items:
        if os.path.isdir(item):
            path = os.path.join(destination_path, item.split('\\')[-1])
            files_count += recursiveCopyFiles(
                source_path=item, destination_path=path, override=override)
        else:
            file = os.path.join(destination_path, item.split('\\')[-1])

            if not os.path.exists(file) or override:
                shutil.copyfile(item, file)
                files_count += 1

    return files_count


def strReplace(searchExp, replaceExp, file):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp, replaceExp)
        sys.stdout.write(line)


def copyFiles():
    a = recursiveCopyFiles(os.path.join(os.path.abspath(
        os.path.join(os.getcwd(), os.pardir)), 'mints001.com'),
        os.getcwd())
    print('已复制完成 '+str(a)+' 个文件!')


def modifyDbConfig(dbName=''):
    if len(dbName) < 1:
        print('没有提供数据库名称')
        return 0
    strReplace("define('DB_NAME', 'test001');",
               "define('DB_NAME', '"+dbName+"');", os.path.join(os.getcwd(), 'wp-config.php'))
    print('数据库已修改!')


def modifyCacheConfig(domainName=''):

    # if len(domainName) < 1:
    #     domainName = os.path.basename(
    #         os.path.dirname(os.path.realpath(__file__)))
    wpContentPath = os.path.join(os.getcwd(), 'wp-content')
    strReplace("mints001.com", domainName, os.path.join(
        wpContentPath, 'advanced-cache.php'))
    print('advanced-cache.php 已修改!')


def main(argv):

    dbName = ''
    domainName = ''

    # print('参数数量:', len(sys.argv), 'arguments.')
    # print('参数列表:', str(sys.argv))

    try:
        opts, args = getopt.getopt(argv, "d:o:", ["db=", "domain="])
    except getopt.GetoptError:
        print('python a.py -d <db> -o <domain>')
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-d", "--db"):
            dbName = arg
        elif opt in ("-o", "--domain"):
            domainName = arg
    if len(dbName) < 1:
        dbName = 'ccc'
        sys.exit(2)
    if len(domainName) < 1:
        domainName = os.path.basename(
            os.path.dirname(os.path.realpath(__file__)))

    print('dbName : ', dbName)
    print('domainName : ', domainName)

    copyFiles()
    modifyDbConfig(dbName)
    modifyCacheConfig(domainName)

    # 干完之后,打开Chrome,跳转到当前域名对应的链接

    url = 'http://'+domainName+'/'
    # Windows
    chrome_path = r'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

    # MacOS
    #chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

    # Linux
    # chrome_path = '/usr/bin/google-chrome %s'

    webbrowser.get(chrome_path).open(url)

'''


python b.py -d dev_jwt

[description]
'''
if __name__ == "__main__":
    main(sys.argv[1:])

适用于Windows,Python 3+ ,如果要在 Linux 上使用,应修改下路径间隔符。

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×