【问题标题】:Run Python script when Directory is updated更新目录时运行 Python 脚本
【发布时间】:2016-06-20 08:31:33
【问题描述】:

我有一个脚本,我想在更新特定目录时执行该脚本。更具体地说:开发团队有 4 个目录(比如说“a”、“b”、“c”和“d”),它们会不时更新。我有一个脚本,它作为目录的参数名称。当目录“a”更新时,我想用参数“a”执行这个脚本。有可能与詹金斯有关吗?如果是这样,我可以使用 SVN 做同样的事情吗?

【问题讨论】:

  • 与python无关。 Jenkins 必须有一个 SVN 插件才能做到这一点。

标签: python jenkins jenkins-plugins


【解决方案1】:

您可以使用 python 本身和watchdog 库来做到这一点。

from watchdog.observers import Observer  
from watchdog.events import PatternMatchingEventHandler 

class FileHandler(PatternMatchingEventHandler):
    def process(self, event):
        print event.src_path, event.event_type  # print now only for degug

    def on_modified(self, event):
        self.process(event)

    def on_created(self, event):
        self.process(event)

if __name__ == '__main__':
    args = sys.argv[1:]
    observer = Observer()
    observer.schedule(MyHandler(), path=args[0] if args else '.')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

【讨论】:

  • 感谢您的回答,这可能真的很有帮助!但是我不确定它是否比使用 Jenkins 更好。这些目录位于服务器中,所以我可能应该在服务器端运行我不想做的这个脚本。更重要的是我不希望它一直工作,我想要简单的开关来运行或停止它
  • Jenkins 有一个插件来完成这项工作,它可以查找文件系统更改并触发构建。但是支持到此为止,我不确定它是否可以将更改的目录名称传递给脚本。 wiki.jenkins-ci.org/display/JENKINS/FSTrigger+Plugin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2014-09-06
  • 1970-01-01
  • 2023-01-30
  • 2017-09-03
  • 1970-01-01
相关资源
最近更新 更多