From f516cdb29d89027d267b3a09c98e518e7177251a Mon Sep 17 00:00:00 2001 From: Nicolás Reynolds Date: Sat, 13 Oct 2012 11:13:25 -0300 Subject: Deprecating old python scripts --- __init__.py | 0 clean_repo.py | 99 ---------------------------- config.py | 68 -------------------- filter.py | 204 ---------------------------------------------------------- 4 files changed, 371 deletions(-) delete mode 100644 __init__.py delete mode 100755 clean_repo.py delete mode 100755 config.py delete mode 100755 filter.py diff --git a/__init__.py b/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/clean_repo.py b/clean_repo.py deleted file mode 100755 index 076de60..0000000 --- a/clean_repo.py +++ /dev/null @@ -1,99 +0,0 @@ -#! /usr/bin/python2 -#-*- encoding: utf-8 -*- -from filter import * -import argparse - -def mkpending(packages_iterable, pending_file, blacklisted_names, - whitelisted_names): - """ Determine wich packages are pending for license auditing.""" - search = tuple(blacklisted_names + - whitelisted_names) - - try: - fsock=open(pending_file, "r") - pkgs=[pkg for pkg in packages_iterable - if pkg["name"] not in listado(pending_file)] - for line in fsock.readlines(): - if line: - pkg=Package() - pkg["name"]=line.split(":")[0] - pkg["license"]=":".join(line.split(":")[1:]) - pkgs.append(pkg) - pkgs=[pkg for pkg in pkgs if pkg["name"] not in search - and "custom" in pkg["license"]] - fsock=open(pending_file, "w") - fsock.write("\n".join([pkg["name"] + ":" + pkg["location"] + - ":" + pkg["license"] - for pkg in pkgs]) + "\n") - fsock.close() - except(IOError): - printf("Can't read or write %s" % pending_file) - return pkgs - -def remove_from_blacklist(path_to_db, blacklisted_names): - """ Check the blacklist and remove packages on the db""" - if "~" in path_to_db: - path_to_db=(os.path.expanduser(path_to_db)) - - pkgs=[pkg for pkg in pkginfo_from_db(path_to_db) if - pkg["name"] in blacklisted_names] - if pkgs: - lista=" ".join(pkgs) - cmd = "repo-remove " + path_to_db + " " + lista - printf(cmd) - a = check_output(cmd) - return pkgs - -def cleanup_nonfree_in_dir(directory, blacklisted_names): - if "~" in directory: - directory=(os.path.expanduser(directory)) - pkglist=list() - pkgs=pkginfo_from_files_in_dir(directory) - for package in pkgs: - if package["name"] in blacklisted_names: - os.remove(package["location"]) - pkglist.append(package) - return pkglist - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - prog="clean_repo", - description="Clean a repo db and packages",) - - parser.add_argument("-k", "--blacklist-file", type=str, - help="File containing blacklisted names", - required=True,) - - group_dir=parser.add_argument_group("Clean non-free packages in dir") - group_dir.add_argument("-d", "--directory", type=str, - help="directory to clean") - - group_db=parser.add_argument_group("Clean non-free packages in db", - "All these arguments need to be specified for db cleaning:") - group_db.add_argument("-b", "--database", type=str, - help="dabatase to clean") - group_db.add_argument("-p", "--pending-file", type=str, - help="File in which to write pending list") - group_db.add_argument("-w", "--whitelist-file", type=str, - help="File containing whitelisted names") - - args=parser.parse_args() - - if args.database and not (args.pending_file and args.whitelist_file): - parser.print_help() - exit(1) - - blacklisted=listado(args.blacklist_file) - - if args.database: - whitelisted=listado(args.whitelist_file) - pkgs=pkginfo_from_db(args.database) - pending_names=[pkg["name"] for pkg in - mkpending(pkgs, args.pending_file, - blacklisted, whitelisted)] - - if args.directory and args.database: - cleanup_nonfree_in_dir(args.directory, (blacklisted + pending_names)) - elif args.directory: - cleanup_nonfree_in_dir(args.directory, blacklisted) - diff --git a/config.py b/config.py deleted file mode 100755 index 0ffc475..0000000 --- a/config.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/python2 -# -*- coding: utf-8 -*- -try: - from subprocess import check_output -except(ImportError): - from commands import getoutput - def check_output(*popenargs,**kwargs): - cmd=" ".join(*popenargs) - return getoutput(cmd) -import os - - -# Rsync commands - -def printf(text, logfile=False): - """Guarda el texto en la variable log y puede imprimir en pantalla.""" - print (str(text) + "\n") - if logfile: - try: - log = open(logfile, 'a') - log.write("\n" + str(text) + "\n") - except: - print("Can't open %s" % logfile) - finally: - log.close() - - -# Classes and Exceptions -class NonValidFile(ValueError): pass -class NonValidDir(ValueError): pass -class NonValidCommand(ValueError): pass - -class Package: - """ An object that has information about a package. """ - package_info=dict() - - def __init__(self): - self.package_info={ "name" : False, - "version" : False, - "release" : False, - "arch" : False, - "license" : False, - "location": False, - "depends" : False,} - - def __setitem__(self, key, item): - if key in self.package_info.keys(): - return self.package_info.__setitem__(key, item) - else: - raise ValueError("Package has no %s attribute" % key) - - def __getitem__(self, key): - return self.package_info.__getitem__(key) - - def __unicode__(self): - return str(self.package_info) - - def __repr__(self): - return str(self.package_info) - - def __eq__(self,x): - if not isinstance(x, Package): - return False - for key in self.package_info.keys(): - if x[key] != self.package_info[key]: - return False - else: - return True diff --git a/filter.py b/filter.py deleted file mode 100755 index 70d5e29..0000000 --- a/filter.py +++ /dev/null @@ -1,204 +0,0 @@ -#! /usr/bin/python2 -#-*- encoding: utf-8 -*- -from glob import glob -from config import * -import tarfile - -def listado(filename, start=0, end=None): - """Obtiene una lista de paquetes de un archivo.""" - fsock = open(filename, "r") - lista = fsock.read().split("\n") - fsock.close() - if end is not None: - return [pkg.split(":")[start:end].rstrip() - for pkg in lista if pkg] - else: - return [pkg.split(":")[start].rstrip() - for pkg in lista if pkg] - -def pkginfo_from_filename(filename): - """ Generates a Package object with info from a filename, - filename can be relative or absolute - - Parameters: - ---------- - filename -> str Must contain .pkg.tar. - - Returns: - ---------- - pkg -> Package object""" - if ".pkg.tar." not in filename: - raise NonValidFile("File is not a pacman package") - pkg = Package() - pkg["location"] = filename - fileattrs = os.path.basename(filename).split("-") - pkg["arch"] = fileattrs.pop(-1).split(".")[0] - pkg["release"] = fileattrs.pop(-1) - pkg["version"] = fileattrs.pop(-1) - pkg["name"] = "-".join(fileattrs) - return pkg - -def pkginfo_from_desc(info_from_desc, pkg=Package()): - """ Returns pkginfo from desc file. - - Parameters: - ---------- - filename -> str File must exist - - Returns: - ---------- - pkg -> Package object""" - info=info_from_desc.rsplit() - info_map={"name" :("%NAME%" , None), - "version" :("%VERSION%" , 0 ), - "release" :("%VERSION%" , 1 ), - "arch" :("%ARCH%" , None), - "license" :("%LICENSE%" , None), - "location":("%FILENAME%", None),} - - for key in info_map.keys(): - field,pos=info_map[key] - pkg[key]=info[info.index(field)+1] - if pos is not None: - pkg[key]=pkg[key].split("-")[pos] - return pkg - -def pkginfo_from_rsync_output(rsync_output): - """ Generates a list of packages and versions from an rsync output - wich uses --list-only and --no-motd options. - - Parameters: - ---------- - rsync_output -> str Contains output from rsync - - Returns: - ---------- - package_list -> tuple Contains Package objects. """ - - def package_or_link(line): - """ Take info out of filename """ - location_field = 4 - return pkginfo_from_filename(line.rsplit()[location_field]) - - def do_nothing(): - pass - - options = { "d": do_nothing, - "l": package_or_link, - "-": package_or_link, - " ": do_nothing} - - package_list=list() - - lines=[x for x in rsync_output.split("\n") if ".pkg.tar" in x] - - for line in lines: - pkginfo=options[line[0]](line) - if pkginfo: - package_list.append(pkginfo) - - return tuple(package_list) - -def pkginfo_from_files_in_dir(directory): - """ Returns pkginfo from filenames of packages in dir - wich has .pkg.tar. on them - - Parameters: - ---------- - directory -> str Directory must exist - - Returns: - ---------- - package_list -> tuple Contains Package objects """ - package_list=list() - - if not os.path.isdir(directory): - raise NonValidDir - - for filename in glob(os.path.join(directory,"*")): - if ".pkg.tar." in filename: - package_list.append(pkginfo_from_filename(filename)) - return tuple(package_list) - -def pkginfo_from_db(path_to_db): - """ Get pkginfo from db. - - Parameters: - ---------- - path_to_db -> str Path to file - - Output: - ---------- - package_list -> tuple of Package objects""" - package_list=list() - - if not os.path.isfile(path_to_db): - raise NonValidFile(path_to_db + " is not a file") - - try: - dbsock = tarfile.open(path_to_db, 'r:gz') - desc_files=[desc for desc in dbsock.getnames() - if "/desc" in desc] - for name in desc_files: - desc=dbsock.extractfile(name).read().decode("UTF-8") - package_list.append(pkginfo_from_desc(desc)) - except tarfile.ReadError: - raise NonValidFile("No valid db_file %s or not readable" - % path_to_db) - finally: - dbsock.close() - return package_list - -def rsyncBlacklist_from_blacklist(packages_iterable, - blacklisted_names, - exclude_file): - """ Generate an exclude list for rsync - - Parameters: - ---------- - package_iterable -> list or tuple Contains Package objects - blacklisted_names-> list or tuple Contains blacklisted names - exclude_file -> str Path to file - debug -> bool If True, file list gets logged - - Output: - ---------- - None """ - pkgs=[pkg["location"] for pkg in packages_iterable - if isinstance(pkg, Package) - and pkg["name"] in blacklisted_names] - if exclude_file: - try: - fsock = open(exclude_file,"w") - fsock.write("\n".join(pkgs) + "\n") - except IOError: - printf("%s wasnt written" % exclude_file) - exit(1) - finally: - fsock.close() - return pkgs - - -if __name__ == "__main__": - import argparse - parser=argparse.ArgumentParser() - parser.add_argument("-r", "--rsync-exclude-file", type=str, - help="File in which to generate exclude list", - required=True,) - parser.add_argument("-k", "--blacklist-file", type=str, - help="File containing blacklisted names", - required=True,) - parser.add_argument("-f", "--rsout-file", type=str, - help="This file will be read to get a pkg list", - required=True,) - args=parser.parse_args() - try: - fsock=open(args.rsout_file, "r") - rsout=fsock.read() - except IOError: - print("%s is not readable" % args.rsout_file) - finally: - fsock.close() - packages=pkginfo_from_rsync_output(rsout) - rsyncBlacklist_from_blacklist(packages, listado(args.blacklist_file), - args.rsync_exclude_file) -- cgit v1.2.3-54-g00ecf