#!/usr/bin/env python3
# package-tracker - Compare and track package versions in debian repositories
# Copyright (C) 2017 Andreas Kreuzer <andreas.kreuzer@open-infrastructure.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import os.path
import apt
import apt_pkg
import re

software = u"package-tracker"
program = sys.argv[0]
install_path = "/"
apt_root_path = "var/lib/" + software
config_path = "etc/" + software

# --------------------------------
# Function definitions
#
def relink_sources_file(filename):
    """ Creates a symlink of the desired input file to sources.list """
    global apt_root_path, config_path, install_path

    symlink_dir = os.path.join(
                        install_path,
                        apt_root_path,
                        "etc/apt/sources.list"
                  )

    if (os.path.exists(symlink_dir)):
        os.remove(symlink_dir)

    os.symlink(
        os.path.join(install_path, filename),
        symlink_dir
    )

# --------------------------------
# Main: load apt cache objects
#
relink_sources_file("etc/package-tracker/progress-linux.list")

query_cache = apt.Cache(rootdir=os.path.join(install_path, apt_root_path))
query_cache.update(None)
query_cache.open()

relink_sources_file("etc/package-tracker/debian.list")

compare_cache = apt.Cache(rootdir=os.path.join(install_path, apt_root_path))
compare_cache.update(None)
compare_cache.open()


print("size of query: ", len(query_cache))

data = {}
for package in query_cache:
    archive = package.versions[0].origins[0].archive
    if (not archive in data):
        data[archive] = {}
    if (not package.versions[0].source_name in data[archive]):
        data[archive][package.versions[0].source_name] = []
    data[archive][package.versions[0].source_name].append(package.shortname)

for archive in data:
    print("archive: ", archive)
    print("  number of source packages: ", len(data[archive]))

print("size of query: ", len(compare_cache))

data2 = {}
for package in compare_cache:
    archive = package.versions[0].origins[0].archive
    if (not archive in data2):
        data2[archive] = {}
    if (not package.versions[0].source_name in data2[archive]):
        data2[archive][package.versions[0].source_name] = []
    data2[archive][package.versions[0].source_name].append(package.shortname)

for archive in data2:
    print("archive: ", archive)
    print("  number of source packages: ", len(data2[archive]))


sys.exit(0)
