Posts Tagged ‘ pacman ’

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.