LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 07-27-2009, 10:42 AM   #1
grissiom
Member
 
Registered: Apr 2008
Location: China, Beijing
Distribution: Slackware
Posts: 423

Rep: Reputation: 45
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

Last edited by grissiom; 07-28-2009 at 11:58 PM. Reason: update slackfiles.py
 
Old 07-27-2009, 10:47 AM   #2
rg3
Member
 
Registered: Jul 2007
Distribution: Slackware Linux
Posts: 514

Rep: Reputation: Disabled
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).

Last edited by rg3; 07-27-2009 at 10:57 AM.
 
Old 07-28-2009, 04:34 AM   #3
grissiom
Member
 
Registered: Apr 2008
Location: China, Beijing
Distribution: Slackware
Posts: 423

Original Poster
Rep: Reputation: 45
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.
 
Old 07-28-2009, 10:37 AM   #4
rg3
Member
 
Registered: Jul 2007
Distribution: Slackware Linux
Posts: 514

Rep: Reputation: Disabled
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.
 
Old 07-29-2009, 12:04 AM   #5
grissiom
Member
 
Registered: Apr 2008
Location: China, Beijing
Distribution: Slackware
Posts: 423

Original Poster
Rep: Reputation: 45
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My little script for pkg search grissiom Slackware 5 06-18-2009 09:22 AM
Why cant voip cant be tracked ? vibinlakshman Linux - Security 3 12-16-2008 07:51 PM
Trying to figure out error in file system check colmmagoo Linux - General 1 07-31-2004 10:50 PM
Slackware Pkg System GT_Onizuka Slackware 7 11-05-2003 10:21 PM


All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration