geekbooksync.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:geekbook 作者: mmagnus 项目源码 文件源码
def fetch(remote_file, local_tmp_file, local_file, diff_tool):
    cmd = 'scp %s %s' % (remote_file, local_tmp_file)
    print(cmd)
    os.system(cmd)
    with open(local_tmp_file) as f:
        txt = f.read()

    ntxt = re.sub('Table of Contents.+markdown-toc.go\)', '[tableofcontent]', txt, flags=re.S)
    with open(local_tmp_file, 'w') as f:
        f.write(ntxt)

    cmd = 'diff ' + local_tmp_file + ' ' + local_file
    o = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if o.stdout.read().strip():  # if diff empty, file are the same
        cmd = diff_tool + ' ' + local_tmp_file + ' ' + local_file
        o = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        o.communicate()
        o.wait()
    else:
        print('The files are the same')


# main
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号