LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   A script that could figure out whether a file is tracked by slackware pkg system or n (http://www.linuxquestions.org/questions/slackware-14/a-script-that-could-figure-out-whether-a-file-is-tracked-by-slackware-pkg-system-or-n-743093/)

grissiom 07-27-2009 09:42 AM

A script that could figure out whether a file is tracked by slackware pkg system or n
 
Actually it's not _a_ script but a batch of commands to get things done. But there is a script to build up the database and search in it. The source code is:
Code:

#!/usr/bin/env python
import sys, cPickle
import os

def print_help(outfile=sys.stdout):
        outfile.write(
'''slackfiles.py [--use-db <db-name> | [--creat-db | --add-db] [db-name]]

If no arguments given, it eqauls to "slackfile.py --use-db /var/tmp/slackfile/db.pkl"
Default database name is "/var/tmp/slackfile/db.pkl"
''')

def err_exit(errmsg, no):
        sys.stderr.write(errmsg)
        sys.exit(no)

def help_err_exit(errmsg, no):
        print_help()
        err_exit(errmsg, no)

def check_dir(name):
        if not os.path.isdir(name):
                try:
                        os.makedirs(name)
                except:
                        err_exit('Could not make dir: ' + name + '\n', 1)

def load_set(name):
        try:
                return cPickle.load(open(name, 'rb'))
        except IOError:
                err_exit(name + ' does not exist or \
you don\'t have a read permission.\n\
Please creat the datebase first\n', 1)

def build_set(se, infile):
        for i in infile:
                se.add(i)

def build_db(filename, instream=sys.stdin, add=False):
        check_dir(os.path.split(filename)[0])

        if add == False:
                se = set()
        else:
                se = load_set(filename)
        build_set(se, instream)

        try:
                dbfile = open(filename, 'wb')
        except IOError:
                err_exit('Could not write to ' + filename + '\n', 1)
        cPickle.dump(se, dbfile, -1)
        dbfile.close()

def search_set(se, infile, ostream=sys.stdout):
        for i in infile:
                if i not in se:
                        ostream.write(i)

def search_db(filename, instream=sys.stdin):
        se = load_set(filename)
        search_set(se, instream)

def main(args):
        def_db = "/var/tmp/slackfile/db.pkl"
        if len(args) == 0:
                search_db(def_db)
        elif args[0] == '--use-db':
                if len(args) < 2:
                        help_err_exit('\nPlease specify the database name.\n', 2)
                elif len(args) > 2:
                        help_err_exit('\nMore than one database name found.\n', 2)
                search_db(args[1])
        elif args[0] == '--creat-db':
                if len(args) == 2:
                        name = args[1]
                elif len(args) == 1:
                        name = def_db
                else:
                        help_err_exit('\nInvalid arguments: '
                                      + ' '.join(args[2:]) + '\n', 2)
                build_db(name)
        elif args[0] == '--add-db':
                if len(args) == 2:
                        name = args[1]
                elif len(args) == 1:
                        name = def_db
                else:
                        help_err_exit('\nInvalid arguments: '
                                      + ' '.join(args[2:]) + '\n', 2)
                build_db(name, add=True)

if __name__ == '__main__':
        main(sys.argv[1:])

Save it to one of the PATH you have write permission(not necessarily root) as slackfiles.py and chmod +x. Then you can use it to build up database and search:
Code:

# build up database in "/var/tmp/slackfile/db.pkl"
tail -n+19 /var/log/packages/* | slackfiles.py --creat-db
# build up database in ~/db
tail -n+19 /var/log/packages/* | slackfiles.py --creat-db ~/db
# according to "/var/tmp/slackfile/db.pkl" to find file not got tracked in /usr/lib64
cd /
find usr/lib64 -type f | slackfiles.py
# according to ~/db to find file not got tracked in /usr/lib64
cd /
find usr/lib64 -type f | slackfiles.py --use-db ~/db

All of the above didn't consider symlinks.

NOTE:Critical files or even some files tracked by pkg system may not recognize by above commands. Take your own risk if you really want to delete them.

Any comments or ideas is welcome ;)

rg3 07-27-2009 09:47 AM

slackroll does that (orphan-search operation). It's written in Python too, but completely.

Edit: This is an example of what it looks like. As you can see, the results usually have a good amount of false positives. This is explained in the slackroll documentation properly.

Code:

# slackroll orphan-search /usr/lib
WARNING: This operation may take a long time to complete
WARNING: Results should be interpreted carefully
Press Ctrl+C to cancel or Enter to continue...

Reading normalized contents of /var/log/packages ...
Examining specified paths...
Finding orphan files...
Orphan files:
    /usr/lib/perl5/5.10.0/CPAN/Config.pm
    /usr/lib/python2.5/site-packages/wicd
    /usr/lib/python2.5/site-packages/wicd/__init__.pyo
    /usr/lib/python2.5/site-packages/wicd/gui.pyo
    /usr/lib/python2.5/site-packages/wicd/misc.pyo
    /usr/lib/python2.5/site-packages/wicd/networking.pyo
    /usr/lib/python2.5/site-packages/wicd/wnettools.pyo
    /usr/lib/python2.5/site-packages/wicd/wpath.pyo
    /usr/lib/python2.6/site-packages/wicd/__init__.pyo
    /usr/lib/python2.6/site-packages/wicd/gui.pyo
    /usr/lib/python2.6/site-packages/wicd/misc.pyo
    /usr/lib/python2.6/site-packages/wicd/networking.pyo
    /usr/lib/python2.6/site-packages/wicd/wnettools.pyo
    /usr/lib/python2.6/site-packages/wicd/wpath.pyo
End of list

From the previous list, I could safely delete the Python 2.5 files. The other ones should probably be kept (they will be regenerated if you run wicd).

grissiom 07-28-2009 03:34 AM

Thanks rg3. I found slackroll is an amazing tool ;)

But my purpose is to write a tool _just_ to check whether a file is get track or not and keep the tool small and tiny. If I want pkg management, I'll choose slackpkg.

rg3 07-28-2009 09:37 AM

A suggestion for improvement is: store the database in a known place (e.g. as a hidden file or in a hidden directory in the home directory of the running user) and make the tool receive a search path, instead of the user properly using find. If the database is outdated (file modification time prior to modification time of /var/log/packages), you regenerate it. If not, you use the available database. Or maybe have an option to generate the database explicitly.

Code:

slackfiles -r /usr/lib64 # regenerates the database due to the -r option being present
slackfiles /usr/lib64    # does not regenerate the database unless it does not exist

That way it would be much easier to use.

grissiom 07-28-2009 11:04 PM

Yeah, I updated the slackfiles.py. And I wrote two shell scripts to make life easier:
This one to create the database: (it can resolve symlinks ;))
Code:

#!/bin/sh
# create-db.sh

tail -n+19 -q /var/log/packages/* | slackfiles.py --creat-db

cat /var/log/scripts/* | \
sed -n 's,^( *cd \([^ ;][^ ;]*\) *; *rm -rf \([^ )][^ )]*\) *) *$,\1/\2,p' | \
slackfiles.py --add-db

This one the check files recursively:
Code:

#!/bin/sh
# checkfiles.sh

cd /
find $* | slackfiles.py

So you could simply do "checkfiles.sh usr/bin" now ;)


All times are GMT -5. The time now is 03:14 AM.