# 删除distfiles里面过期文件的方法

## ruf

以前有人写过一个distclean.py，但是现在已经不适用了，所以我改写了一个，希望对大家有帮助。

```

#!/usr/bin/env python

## filename : edistclean.py

## Copyright (c) 2003, Fredrik Arnerup (e97_far@e.kth.se)

## Copyright (c) 2005, Bob Wolfe (rufeng2000@hotmail.com)

import sys, os, os.path, portage

opt_delete = 0

if len(sys.argv) > 1:

    if sys.argv[1] == 'delete':

        opt_delete = 1

    else:

        print 'usage :', sys.argv[0], '[delete]'

        sys.exit(1)

distdir = portage.settings['DISTDIR']

print 'DISTDIR =', distdir

fetchs = os.popen("emerge -pvef world 2>&1 1>/dev/null").readlines()

print '"emerge -pef world" returns', len(fetchs) / 2, 'files.'

files = {}

for s in fetchs:

    if s == '\n':

        continue

    file = s.split(' ')[0].split('/')[-1]

    files[file] = 1

try:

    list = portage.listdir(distdir)

except os.OSError:

    sys.exit('Failed to read ' + distdir)

size = 0; count = 0

for file in list:

    abs_file = distdir + '/' + file

    if (os.path.isfile(abs_file) and (not os.path.islink(abs_file)) 

        and (not file in files)):

        size += os.stat(abs_file).st_size

        count += 1

        if not opt_delete:

            print 'Would remove', abs_file

        else:

            try:

                os.remove(abs_file)

                print 'Removed', abs_file

            except OSError:

                print 'Failed to remove', abs_file

size /= 1048576  ## MB

print '%i files, total size: %i MB' % (count, size)

if not opt_delete:

    print 'use "', sys.argv[0], 'delete " to remove unused files.'

```

----------

## tecehux

虽然还没试，也先顶一个

----------

## EricHsu

呵呵, 希望多看到这种帖子  :Wink: 

----------

## ts

 *EricHsu wrote:*   

> 呵呵, 希望多看到这种帖子 

 

是啊，顶一下

----------

## Freesnake

好帖，一定要顶

----------

## Zer4tul

这种好帖，不顶对不起党和人民啊……希望以后多见到这样的好帖，呵呵

----------

## druggo

是啊，对不起大哥也对不起大嫂……

----------

## dundas

good, upup

----------

## EG

now there is new tools name "eclean" in "gentoolkit" can do the same thing & is official supported

----------

## EricHsu

 *EG wrote:*   

> now there is new tools name "eclean" in "gentoolkit" can do the same thing & is official supported

 

嗯, 最常用法如下:

```

# eclean-dist -d

```

-d 表示仅保留最少的源码包 - 仅包含目前系统中安装的那些, 以备 rebuild 某个软件时使用.

为了安全, 可以先加 -p 选项看看都有哪些包会被清除:

```

# eclean-dist -p -d

```

----------

