A script that could figure out whether a file is tracked by slackware pkg system or n
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 10:58 PM.
Reason: update slackfiles.py
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).
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.