summaryrefslogtreecommitdiff
path: root/clean_repo.py
blob: eccfd0198ff2b96595e9884bfc87ec873faecb26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/python
#-*- encoding: utf-8 -*-
from repm.filter import *
import argparse

def mkpending(path_to_db, repo, prefix=config["pending"]):
    """ Determine wich packages are pending for license auditing."""
    if "~" in path_to_db:
        path_to_db=(os.path.expanduser(path_to_db))

    search = tuple(listado(config["blacklist"]) +
                   listado(config["whitelist"]))
    
    pkgs=list(pkginfo_from_db(path_to_db))

    filename=prefix + "-" + repo + ".txt"
    try:
        fsock=open(filename, "rw")
        pkgs=[pkg for pkg in pkginfo_from_db(path_to_db)
              if pkg["name"] not in listado(filename)]
        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.write("\n".join([pkg["name"] + ":" + pkg["license"]
                               for pkg in pkgs]) + "\n")
    except(IOError):
        raise NonValidFile("Can't read or write %s" % filename)
    finally:
        fsock.close()
    return pkgs

def remove_from_blacklist(path_to_db, blacklisted_names,
                          debug=config["debug"]):
    """ 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)
    if debug:
        printf(a)
        return pkgs, cmd

def cleanup_nonfree_in_dir(directory, blacklisted_names):
    if "~" in directory:
        directory=(os.path.expanduser(directory))
    pkgs=pkginfo_from_files_in_dir(directory)
    for package in pkgs:
        if package["name"] in blacklisted_names:
            os.remove(package["location"])

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Clean a repo db and packages")
    parser.add_argument("-b", "--database", type=str,
                        help="dabatase to clean")
    parser.add_argument("-d", "--directory", type=str,
                        help="directory to clean")
    args=parser.parse_args()

    if args.database:
        repo=os.path.basename(args.database).split(".")[0]
        pkgs=pkginfo_from_db(args.database)
        remove_from_blacklist(args.database, pkgs,
                              tuple(listado(config["blacklist"]) +
                                    listado(config["pending"] + 
                                            "-" + repo + ".txt")))
        mkpending(args.database, args.repo)

    if args.directory:
        cleanup_nonfree_in_dir(args.directory,
                               listado(config["blacklist"]))

    if not args.directory and not args.database:
        parser.print_help()