🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #28331

Prośba o wytknięcie błędów

#28331
From: hubert depesz lubaczewski
Date: Tue, 15 Nov 2011 02:48
71 lines
2740 bytes
elo
jako pierwsze coś napisałem program wyszukujący duplikaty maili.

funkcjonalność jest relatywnie mało istotna - chodzi mi o to czy
moglisbyście mi powiedzieć co zrobiłem nie po pythonowemu, lub co można
zrobić lepiej/bardziej pythonowo.

dzięki,

depesz


---------
#!/usr/bin/env python
from os.path import walk, join, getsize, isfile, basename
import email
import sys
from hashlib import sha256
from pipes import quote
import pprint

def calculate_hash( filename ):
    f = open( filename, 'r' )
    mail = email.message_from_file( f )
    keep_headers =  ( 'subject', 'from', 'to', 'cc', 'in-reply-to', 'references', 'date', 'message-id', 'content-type', 'mime-version' )
    for h in mail.keys():
        if h.lower() not in keep_headers:
            del mail[ h ]
    return sha256( mail.as_string() ).hexdigest()

def visit_fn( hash_dict, path, files ):
    for i in [ 'new', 'tmp' ]:
        if files.count ( i ) > 0:
            files.remove( i )

    if basename( path ) != 'cur':
        return

    for file in files:
        full_path=join( path, file )

        if not isfile( full_path ):
            continue

        if getsize( full_path ) > 10 * 1024 * 1024:
            continue

        checksum = calculate_hash( full_path )
        if not hash_dict.has_key( checksum ):
            hash_dict[ checksum ] = list()
        hash_dict[ checksum ].append( full_path )


hash_dict = dict()

walk( sys.argv[1], visit_fn, hash_dict )

for i in hash_dict.keys():
    if len( hash_dict[i] ) == 1:
        del hash_dict[i]

for sha in hash_dict.keys():
    print "# %s" % ( sha )
    print "# keep: %s" % ( hash_dict[ sha ][0] )
    print "rm", " ".join( [ quote(i) for i in hash_dict[ sha ][1:] ] )
    print
---------

-- 
The best thing about modern society is how easy it is to avoid contact with it.
                                                             http://depesz.com/
Technical Details
Message-ID: <2h09p8-kgk.ln1@depesz.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.onet.pl!.POSTED!depesz.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.onet.pl!.POSTED!depesz.com!not-for-mail
From: hubert depesz lubaczewski <depesz@depesz.com>
Newsgroups: pl.comp.lang.python
Subject: Prośba o
 wytknięcie błędów
Date: Tue, 15 Nov 2011 03:48:34 +0100
Organization: depesz Software
Lines: 71
Message-ID: <2h09p8-kgk.ln1@depesz.com>
Reply-To: depesz@depesz.com
NNTP-Posting-Host: andy.depesz.com
Mime-Version: 1.0
X-Trace: news.onet.pl 1321325403 22310 88.198.47.100 (15 Nov 2011 02:50:03 GMT)
X-Complaints-To: niusy@onet.pl
NNTP-Posting-Date: Tue, 15 Nov 2011 02:50:03 +0000 (UTC)
User-Agent: slrn/pre1.0.0-18 (Linux)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit