2016年9月16日金曜日

開発環境

Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 14.(Files)のExercises 14-3、1、2、3.(No. 3391)を取り組んでみる。

Exercises 14-3、1、2、3.(No. 3391)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os

l = len(sys.argv)
if l == 2:
    top = sys.argv[1]
else:
    top = os.path.pardir

if l == 3:
    suffix = sys.argv[2]
else:
    suffix = '.py'

files = {}
for dirpath, _, filenames in os.walk(top):
    for filename in filenames:
        if filename.endswith(suffix):
            path = os.path.join(dirpath, filename)
            cmd = 'md5 {0}'.format(path)
            fp = os.popen(cmd)
            res = fp.read()
            fp.close()
            md5 = res.split('=')[-1].strip()
            files[md5] = files.get(md5, []) + [path]

for k, v in files.items():
    if len(v) > 1:
        print('{0}: {1}'.format(k, v))
        path = v[0]
        for path0 in v[1:]:
            cmd = 'diff {0} {1}'.format(path, path0)
            print(cmd)
            fp = os.popen(cmd)
            res = fp.read()
            fp.close()
            print(res, end='')

入出力結果(Terminal, IPython)

$ ./sample3.py
4190755ee61f36ed7c5a3c9ca66e7972: ['../ch13/analyze_book1.py', '../ThinkPython2/code/analyze_book1.py']
diff ../ch13/analyze_book1.py ../ThinkPython2/code/analyze_book1.py
656f8ee8019f36c0c83359912c3d4155: ['../ch12/anagram_sets.py', '../ch14/anagram_sets.py', '../ThinkPython2/code/anagram_sets.py']
diff ../ch12/anagram_sets.py ../ch14/anagram_sets.py
diff ../ch12/anagram_sets.py ../ThinkPython2/code/anagram_sets.py
45197171b30969902ba5ea73b20c5a86: ['../ch11/pronounce.py', '../ThinkPython2/code/pronounce.py']
diff ../ch11/pronounce.py ../ThinkPython2/code/pronounce.py
7cf44bd606527ed88c520a7dc81e1a62: ['../ch13/zipf.py', '../ThinkPython2/code/zipf.py']
diff ../ch13/zipf.py ../ThinkPython2/code/zipf.py
e43eb36aad20e41cabf6fd72af01f60e: ['../ThinkPython2/code/find_duplicates.py', '../ThinkPython2/code/find_duplicates_copy.py']
diff ../ThinkPython2/code/find_duplicates.py ../ThinkPython2/code/find_duplicates_copy.py
6c9167470eea39824dd85239422d4594: ['../ch12/sample.py', '../ch12/sample1.py', '../ch13/sample.py']
diff ../ch12/sample.py ../ch12/sample1.py
diff ../ch12/sample.py ../ch13/sample.py
77030b5f1a37473ab260b81ab704e297: ['../ch12/reducible.py', '../ThinkPython2/code/reducible.py']
diff ../ch12/reducible.py ../ThinkPython2/code/reducible.py
$

0 コメント:

コメントを投稿