Package Synchronizer [Arch]

This is a small script that I wrote yesterday in order to synchronize my packages that are stored in my cache directory (/var/cache/pacman/). It creates a directory defined by user where it stores all the latest version of the installed packages available in your cache directory.
This script first gets the list of all packages installed in your computer. Just running the “pacman -Q” command and then searches for .pkg file for each package installed, in your cache directory.
If found it then checks whether a newer version is already present in the user directory, if not then transfers the package to the user directory and if an older version is already present in the user directory then it transfers it back to the cache directory

#!/usr/bin/python
#A simple Package Synchronizer
"""
Author: Mohit Kothari <mohitrajkothari@gmail.com>
"""
import os
import shutil

pkg_dir = "/var/cache/pacman/pkg"
lat_dir = "/var/cache/pacman/bck_pkg"

if not os.access(lat_dir,os.F_OK):
	os.system("sudo mkdir "+lat_dir)

os.system("pacman -Q > list")
list_fl = open("./list","r")
pkg_list = list_fl.readlines()
length = len(pkg_list)
list_fl.close()
ctr = int (1)
print"Completed [",
for l in pkg_list:
	perc = float(ctr)/float(length)*100
	print"\rCompleted["+"-"*int(perc/4) + "] "+str(int(perc))+"%",
	
	spli = l.split(" ")
#	print spli[0]
	com = "ls {1} | grep ^{0}-[1,2,3,4,5,6,7,8,9,0] > pkg".format(spli[0],pkg_dir)
	os.system(com)
	pkg_fl = open("./pkg","r")
	pkg_list = pkg_fl.readlines()
	if len(pkg_list) is not 0:
		size = len(pkg_list)
		pkg_name = pkg_list[size-1].split("\n")
#		print pkg_name[0]
		com = "ls {1} | grep ^{0}-[1,2,3,4,5,6,7,8,9,0] > pkg".format(spli[0],lat_dir)
		os.system(com)
		pkg_fl = open("./pkg","r")
		pkg = pkg_fl.readlines()
#		print pkg
		if len(pkg) is 0:
			com = "sudo mv {1}/{0} {2}/".format(pkg_name[0],pkg_dir,lat_dir)
#			print com
			os.system(com)
		else:
#			print "IN VEEVR"
			pack2 = pkg[0].split("\n")
			if pack2[0] < pkg_name[0]:
#				print "MOVING"
				com = "sudo mv {1}/{0} {2}/".format(pkg_name[0],pkg_dir,lat_dir)
				os.system(com)
				com = "sudo mv {1}/{0} {2}/".format(pack2[0],lat_dir,pkg_dir)
#				print com
				os.system(com)
	ctr = ctr+1

Usage:
Just run the script as root as it makes changes in var directory which requires root privileges.
It will show the status of completion as follows

[zarthon@zarkost ~]$ sudo ./control.py
Completed[-----------------------] 92%

By default the user directory is specified as “/var/cache/pacman/bck_pkg”
You can change it in the script.

Advertisements
    • shadyabhi
    • August 15th, 2010

    sudo rsync -v /var/cache/pacman/pkg/* /destination/folder/

    does the job in one line.. 😛

    • No it doesn’t….when u create for the first time it copies all the data, doesn’t even check for versions.
      I tried it.
      SO it doesnt work for me.

    • shadyabhi
    • August 15th, 2010

    Oh. i missed the fact that it takes only the latest versions.. Then try this one liner..

    for pkgs in `pacman -Q | sed ‘s/ /-/g’`; do cp -v /var/cache/pacman/pkg/$pkgs* /media/misc2/Softwares/PKGS/ 2>/dev/null; done

    • shadyabhi
    • August 15th, 2010

    Add -u option to cp so that it only updates the destination folder..

  1. Yours one liner do accomplish half of the task…

    1. It copies the version which are installed in your computer, but if in cache dir theres a newer version present but not installed, it wont copy

    2. It doesn’t removes the older version, suppose amarok 2.3.2 is there than i will copy 2.3.2 and in dest folder there will be 2.3.1 present, hence redundancy

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: