0%

fabric

1
pip install fabric3

写在了 pythonlearn 里

fabic 这个包主要是用在服务器交互里面,比如发布项目到 git 后,本机运行一个脚本,会连接服务器执行一堆下载,编译的事情

写了一个方便写模板的 fabric

  • 场景
    敲一个 fab new:test 生成一个 test.md 的文件 ,并把 hexo 模板写入进去,模板把文件名”test”
    写入到模板里面.
1
2
# will generate a test.md  file
fab new:test
  • config file
    fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
自动化添加文件头
参考 https://www.solarck.com/using-netlify-auto-deployed-pelican.html
- 新建
fab new:test,tag

"""

import os
from datetime import datetime
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.utils import abort
from fabric.colors import *

env.hosts = ['']
env.port = 22
env.user = ''
env.password = ''

# Local path configuration (can be absolute or relative to fabfile)
env.deploy_path = './'
env.content_path = './'


META = """---
title: {:20}
Date: {:%Y-%m-%d %H:%M}
categories: # 这里写的分类会自动汇集到 categories 页面上,分类可以多级
- mac
tags:
- {:10}
Authors: Jingzy
---
"""


def new(title='new',tag=''):
os.chdir(env.content_path)
file_name=title+'.md'
with open(file_name, 'w') as f:
f.write(META.format(title,datetime.now(),tag))

def preview():
local('pelican -d -s pelicanconf.py')
os.chdir(env.deploy_path)
local('python -m http.server 8000 -b 127.0.0.1')


def github(comMsg ='' ):
local('git add --all')
local('git commit -m "{} update at {:%Y-%m-%d %H:%M}"'.format(comMsg,datetime.now()))
local('git push origin master')

# fab hexo_deploy
def hexo_deploy():
run('jg posthexo')
# run("git pull origin master")
result =run("pwd")
with cd('/home/_posts'):
run('pwd') # /tmp
run('git pull origin master ') # /tmp
with cd('/home/hexo'):
run('pwd')
run('hexo g')
run('pwd') # /root