Python中用MacFSEvents模块监视MacOS文件系统改变一例
最近一个项目中用gulp-watch不能满足需求,于是想到了用Python来解决问题。在安装了MacFSEvents模块后,写了下面一个小程序。
#!/usr/bin/env python2
#-*- coding: utf-8 -*- import os,sys,signal
from fsevents import Observer
from fsevents import Stream def callback(FileEvent):
# attributes of FileEvent:mask, cookie and name.
# mask: 512-delete;256-create;2-changed
if FileEvent.name.endswith("index.html") and FileEvent.mask == 256:
print "site rebuild. redeploy extra assets"
os.system("gulp")
elif FileEvent.name.endswith(".scss") and FileEvent.mask == 2:
print "scss changed! compile and redeploy extra assets"
os.system("gulp sass") if __name__ == '__main__':
observer = Observer()
stream = Stream(callback, ".", file_events=True)
observer.schedule(stream)
observer.start()
#按Control+\强制结束
但是运行时发现只能用Control+\强杀进程,而不能用Control+C结束。
经google搜索及自行研究结果,得到下面这段程序。用fork子进程的方法,使得进程响应Control+C 退出。
#!/usr/bin/env python2
#-*- coding: utf-8 -*- import os,sys,signal
from fsevents import Observer
from fsevents import Stream def callback(FileEvent):
# attributes of FileEvent:mask, cookie and name.
# mask: 512-delete;256-create;2-changed;...
if FileEvent.name.endswith("index.html") and FileEvent.mask == 256:
print "site rebuild. redeploy extra assets"
os.system("gulp")
elif FileEvent.name.endswith(".scss") and FileEvent.mask == 2:
print "scss changed! compile and redeploy extra assets"
os.system("gulp sass") def child():
observer = Observer()
stream = Stream(callback, ".", file_events=True)
observer.schedule(stream)
observer.start() class Watcher:
"""
创建一个做苦工的子进程。然后父进程等待KeyboardInterrupt并杀掉子进程。
27
""" def __init__(self):
self.child = os.fork()
if self.child == 0:
child()
else:
self.watch() def watch(self):
try:
os.wait()
except KeyboardInterrupt:
#捕获 Control+C,杀掉子进程
print 'KEYBOARDINTERRUPT\n'
self.kill()
sys.exit() def kill(self):
try:
os.kill(self.child, signal.SIGKILL)
except OSError: pass if __name__ == '__main__':
Watcher()
Python中用MacFSEvents模块监视MacOS文件系统改变一例的更多相关文章
- python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
import yagmailimport psutildef sendmail(subject,contents): #连接邮箱服务器 yag = yagmail.SMTP(user='邮箱名称@16 ...
- python的pymysql模块简介
一.介绍 在python中用pymysql模块来对mysql进行操作,该模块本质就是一个套接字客户端软件,使用前需要事先安装 pip3 install pymysql 二.操作简介 import py ...
- 【转】Python 3的pathlib模块:驯服文件系统
[转]Python 3的pathlib模块:驯服文件系统 https://python.freelycode.com/contribution/detail/1248 Python部落(python. ...
- python中os模块中文帮助
python中os模块中文帮助 python中os模块中文帮助文档文章分类:Python编程 python中os模块中文帮助文档 翻译者:butalnd 翻译于2010.1.7——2010.1.8 ...
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- Python中的模块介绍和使用
在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...
- 周末班:Python基础之模块
什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...
- 第四章:4.0 python常用的模块
1.模块.包和相关语法 使用模块好处: 最大的好处是大大提高了代码的可维护性.其次,编写代码不必从零开始.当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Pyt ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
随机推荐
- 【会装】kylin的安装(填坑)和简单使用
1.简介 kylin的设计思想是空间换时间,将hive上的大表的维度全部排列组合计算也将度量提前计算然后存入HBase库,这个步骤在kylin中称之为build cube. 在查询的时候已经建立cu ...
- angular项目中使用jQWidgets
Angular CLI with jQWidgets In this tutorial, we will show you how to use https://cli.angular.io/ alo ...
- Git提交记住用户名和密码
https://www.baidu.com/link?url=R14MHMloypfAfIeiQwCINfY1AZlcoSU7-tYdnqC1PxfmFKs4TWzLOPdtyJbWVfqMqOkRx ...
- Django实现文章按年月归档、点赞和评论、发送邮件
文章归档的实现 我们在创建文章时,会在数据库中存储文章创建的时间这样的字段,一般用的都是datetime类型,记录文章创建的年月日和时分秒,所以我们直接使用文章的创建时间分类是无法实现文章的按年月归档 ...
- html5多媒体Video/Audio
video: 1.常见的视频格式 视频的组成部分:画面.音频.编码格式 视频编码:H.264.theora.VP8(google开源) 2.常见的音频格式 编码:AAC.MP3 ...
- Linux平台上SQLite数据库教程(一)——终端使用篇
https://blog.csdn.net/u011192270/article/details/48031763 https://blog.csdn.net/fml1997/article/deta ...
- (转)HttpClient 模拟登陆,保持会话并进行后续操作
转自:http://unmi.cc/httpclient-login-session android实现session保持 SessionID的本质
- JavaScript中的数据类型总结
Javascript是一种弱类型语言,没有明确的类型分类:网上分类的方式比较多,个人感觉不比去特别的追究细分是什么什么类型,若是能够明确的分出类型的话,javascript就不是弱类型语言,又由于大家 ...
- 【DEV C++】 Error: ld returned 1 exit status
一般出现“ld returned 1 exit status”错误都是由于函数名称拼写错误造成的,或者在一个工程中不同的函数使用了同一个函数名,暂时还未遇到其他情况.
- CoreOS 添加用户并赋予sudo权限
使用root账号登录CoreOS username 为你要添加的用户登录名 添加用户到root组 useradd -g rot username 添加 sudo 权限 visudo -f /etc/s ...