点击跳转到个人博客查看原文python
正常咱们要同步博客内容时都须要先打开命令窗口(Windows),而后调转到博客目录,而后输入hexo命令清除以前生成的缓存,而后生成静态文件,最后在部署到repository,很繁琐,这时候若是有个图标点击几下就部署到respository多舒服。。。因此就心血来潮。。。嗯。。。 PS:脚本只有在windows 10下1080p分辨率屏幕的电脑测试过,Python版本为3.6.3git
脚本运行后会打开一个界面和一个命令行窗口 windows
Generator 执行'hexo clean'和'hexo g',清理以前生成的静态文件再从新生成静态文件缓存
Deploy 执行'hexo d',将生成的静态文件部署到云端hexo
GeneratorAndDeploy 直接自动执行前面两个命令,清理静态文件-->从新生成静态文件-->部署到云端oop
Server 执行'hexo s',启动本地模拟(本地预览博客),默认端口为博客配置设置的,没有设置则为:localhost:4000 PS:使用该按钮以前记得先使用Gnerator
按钮测试
ServerWithCustomPort 若是默认端口被占用,则可在下面的输入另外的端口,端口位4位数字,而后点击‘ServerWithCustomPort’便可开始自定义端口的本地模拟 PS:使用该按钮以前记得先使用Gnerator
按钮this
最后一个表情按钮 你猜spa
安装Python3.6.3 点击跳转到官网下载命令行
安装 下载完,一路点击安装便可,最后的完成界面有个选项将python的目录添加到PATH
环境变量记得勾选
获取脚本 下载脚本(密码:s3r2)或者将下面的脚本内容复制,而后新建一个.py
结尾的文件保存进去便可
执行脚本 右键脚本-->选择打开方式
-->选择其余应用
-->找到Python的安装目录,选择python.exe
双击便可,选定打开方式后记勾选设置为默认打开方式,下次直接双击打开便可。PS:脚本要放在博客目录里才可以使用,建立一个脚本的快捷方式到桌面就不用每次都到博客目录运行脚本了
注意 本脚本没有使用子线程去执行耗时操做,因此在执行一项操做时不要再点击其余按钮,否则windows会提示程序无响应可能致使脚本退出,请等待DOS窗口提示操做完成,或者是按下的按钮浮起来界面显示正常在进行其余操做 使用Server
或者ServerWithCustomPort
功能时,要退出本地模拟只要点击DOS窗口按CTRL+c
快捷键组合就会提示是否退出,输入y
便可退出,这时候会发现程序界面按钮恢复默认状态,能够继续进行其余操做,从新开启本地模拟或者部署等
#!/usr/bin/env python
# encoding: utf-8
''' @author: smileorigin @license: (C) Copyright 2017 @contact: smileorigin@foxmail.com @file: hexo_utils.py @time: 12/3/2017 7:02 PM @desc: '''
# import
import os
from tkinter import *
from tkinter import messagebox
class MainView(Frame):
bg = 'white'
bt_bg = '#E91E63'
text_color = '#fff'
about_msg = 'Author: smileorigin\nBlog: smileorigin.site\nEmail: smileorigin@foxmail.com'
def __init__(self, generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback, master=None):
Frame.__init__(self, master, bg=self.bg)
# expand扩展frame背景充满整窗口
self.pack(expand=YES, fill='both')
self.createWidgets(generatorCallback, deployCallback, serverCallback,
serverCustomPortCallback, generatorAndDeployCallback)
def createWidgets(self, generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback):
# four function button
self.generator_bt = Button(self, command=generatorCallback, text='Generator', width=20,
bg=self.bt_bg, fg=self.text_color)
self.generator_bt.pack(pady=10, padx=20)
self.deploy_bt = Button(self, command=deployCallback, text='Deploy', width=20,
bg=self.bt_bg, fg=self.text_color)
self.deploy_bt.pack()
self.generator_and_deploy_bt = Button(self, command=generatorAndDeployCallback,
text='GeneratorAndDeploy', width=20,
bg=self.bt_bg, fg=self.text_color)
self.generator_and_deploy_bt.pack(pady=10)
self.server_bt = Button(self, command=serverCallback, text='Server', width=20,
bg=self.bt_bg, fg=self.text_color)
self.server_bt.pack()
self.server_custom_port = Button(self, command=serverCustomPortCallback,
text='ServerWithCustomPort', width=20, bg=self.bt_bg,
fg=self.text_color)
self.server_custom_port.pack(pady=8)
self.custom_port_label = Label(self, text="Port:", bg=self.bg)
self.custom_port_label.pack(fill='x')
hint = StringVar()
hint.set('5000')
self.custom_port_entry = Entry(self, textvariable=hint, bg=self.bg)
self.custom_port_entry.pack()
self.about_bt = Button(self, text='(⓿_⓿)', command=self.showMessage, bg=self.bt_bg,
fg=self.text_color)
self.about_bt.pack(pady=10)
def showMessage(self):
messagebox.showinfo('About', self.about_msg)
# --------------------------------------------------------------------------------------------------
# constant values
# --------------------------------------------------------------------------------------------------
# clean
cmd_clean = 'hexo clean'
# generator
cmd_generator = 'hexo g'
# deploy
cmd_deploy = 'hexo d'
# server
cmd_server = 'hexo s'
# windows width height
width = 230
height = 280
# star
star = '*'
# star num
star_num = 84
# icon path
# icon_path = '\\favicon.ico'
# out
generator_start_text = ' generator start '
generator_done_text = ' generator done '
deploy_start_text = ' deploy start '
deploy_done_text = ' deploy done '
server_start_text = ' server start '
server_done_text = ' server done'
welcome_text = ' welcome '
# --------------------------------------------------------------------------------------------------
# method
# --------------------------------------------------------------------------------------------------
# server with another port
# port string what's your port do you want to server
def serverWithAnotherPort(port):
return cmd_server + ' -p ' + port
def executeCommand(cmd):
os.system(command=cmd)
def regexFourNum(str):
p = re.compile('^[0-9]{4}')
return p.match(str)
def printStar(num):
print(star * num)
def printStarNotEnter(num):
print(star * num, end='')
def printStringWithStar(num, string):
printStar(star_num)
string_len = len(string)
half_star_num = (int)((num - string_len) / 2)
remainder = num - half_star_num * 2 - string_len
printStarNotEnter(half_star_num + remainder)
print(string, end='')
printStar(half_star_num)
printStar(star_num)
def generatorCallback():
printStringWithStar(star_num, generator_start_text)
executeCommand(cmd_clean)
executeCommand(cmd_generator)
printStringWithStar(star_num, generator_done_text)
def deployCallback():
printStringWithStar(star_num, deploy_start_text)
executeCommand(cmd_deploy)
printStringWithStar(star_num, deploy_done_text)
def serverCallback():
printStringWithStar(star_num, server_start_text)
try:
executeCommand(cmd_server)
except KeyboardInterrupt:
printStringWithStar(star_num, server_done_text)
def serverCustomPortCallback():
custom_port = str(main_view.custom_port_entry.get())
if custom_port:
# port: just 4 number
if regexFourNum and custom_port.__len__() == 4:
printStringWithStar(star_num, server_start_text)
try:
executeCommand(serverWithAnotherPort(custom_port))
except KeyboardInterrupt:
printStringWithStar(star_num, server_done_text)
else:
messagebox.showinfo('Input error', 'Port needs 4 digits.Example:5000')
else:
# error hint input port
messagebox.showinfo('Error', 'Please input port.Example:5000')
def generatorAndDeployCallback():
generatorCallback()
deployCallback()
# --------------------------------------------------------------------------------------------------
# run code
# --------------------------------------------------------------------------------------------------
printStringWithStar(star_num, welcome_text)
root = Tk()
main_view = MainView(generatorCallback, deployCallback, serverCallback, serverCustomPortCallback,
generatorAndDeployCallback, master=root)
root.title("")
root.resizable(0, 0)
size = '{}x{}+{}+{}'.format(
width, height, (int)((root.winfo_screenwidth() - width) / 2),
(int)((root.winfo_screenheight() - height) / 2))
root.geometry(size)
# root.iconbitmap(sys.path[0] + icon_path)
root.mainloop()
复制代码