整合了python和bash的神器 — xonsh

xonsh

Python-powered, cross-platform, Unix-gazing shell http://xon.sh

演示代码

比如我最近写了这样一个python+bash的脚本。

#!/usr/bin/env xonsh

import re
from os.path import dirname, join, abspath, exists, basename
from json import load, dump
import toml
import sys

\_\_dirname = dirname(abspath(\_\_file__))
sys.path.append(__dirname)
from dnspod import Dnspod
from config import CONFIG

VERSION_INCR = 1

def main():

    root = dirname(dirname(__dirname))
    version\_toml\_path = join(root, '.release.version.toml')
    package_path = join(
        root,
        "package.json"
    )

    release_path = join(dirname(root), "release/srv")

    if not exists(release_path):
        print(release_path, '不存在')
        return

    cd @(root)
    git add -u

    try:
        version\_toml = toml.load(version\_toml_path)
    except:
        version_toml = {}

    if re.findall("nothing.* to commit", $(git st), re.S):
        last_commit = $(git log -1).split("\\n",1)\[0\].split(" ",1)\[-1\]
        if last\_commit in version\_toml:
            print(f"commit {last\_commit} released version {version\_toml\[last_commit\]}")
            return

    with open(package_path) as f:
        package = load(f)
        version = package\['version'\]
        version = list(map(int, version.split(".")))
        version\[-1\]+=VERSION_INCR
        version = ".".join(map(str, version))
        package\['version'\] = version

    with open(package_path,"w") as f:
        dump(package, f, indent=4, sort_keys=True)

    comment = f"PUBLISH VERSION {version}"

    print(comment)


    cd @(release_path)
    find . | grep -vE '\\.git/|.git$|\\.$|\\.\\.#39; | xargs rm -rf

    cd @(root)
    print(f">> cd {root}")
    git add -u
    git commit -m @(comment)
    last_commit = $(git log -1).split("\\n",1)\[0\].split(" ",1)\[-1\]

    git archive master | tar -x -C @(release_path)

    print(f">> cd {release_path}")
    cd @(release_path)

    rm -rf nodemon.json .gitignore dev py *.md test LICENSE release.sh
    git add .

    gitignore = join(root, ".gitignore")
    cp @(gitignore) .
    git add .gitignore

    git commit -m @(comment)
    git push origin master
    git push github master

    version\_toml\[last\_commit\] = version

    with open(version\_toml\_path, "w") as f:
        toml.dump(version_toml, f)

    dnspod_update(version)

def dnspod_update(version):
    domain = CONFIG.HOST.TXT
    dnspod = Dnspod(*CONFIG.DNSPOD)
    dnspod.update(domain, "cli-v", version)
    dnspod.update(domain, "cli-git", " ".join(CONFIG.GIT))


if \_\_name\_\_ == "\_\_main\_\_":
    main()