#!/usr/bin/env python
# Import necessary libraries
import sys
import gzip
import shutil
import requests
import array as arr
from collections import defaultdict
from zipfile import ZipFile


# This function is to unzip a .gz on source into destination
def unzip(source, destination, block_size=65536, arch="amd64"):
    with gzip.open(source, 'rb') as s_file, \
            open(destination, 'wb') as d_file:
        while True:
            block = s_file.read(65536)
            if not block:
                break
            else:
                d_file.write(block)
        d_file.write(block)
    return destination


def main():
    # lowercase arch
    arch = sys.argv[1].lower()

    # Define Debian mirror URL
    url = 'http://ftp.uk.debian.org/debian/dists/stable/main/'

    # Define filenames to unzip
    filename = "Contents-"+arch+".gz"
    filenameUdeb = "Contents-udeb-"+arch+".gz"

    # Download filename to local folder
    r = requests.get(url+filename)
    with open("tmp/%s" % filename, 'wb') as f:
        f.write(r.content)

    f = gzip.open("tmp/%s" % filename, 'rb')
    file_content = f.read()
    f.close()

    # Unzip .gzip file
    rawFile = unzip("tmp/%s" % filename, "tmp/%s" % filename.replace(".gz", ""), arch)

    # with gzip.open(filename, 'r') as f_in, open(filename.replace(".gz", ""), 'wb') as f_out:
    #     shutil.copyfileobj(f_in, f_out)   

    # Open unzipped file
    topTenUnsorted = defaultdict(int)
    with open(rawFile, "r") as f:
        line = f.readline()

        # Read line by line
        while line != "":
            line = line.strip("\n")
            # Remove whitespaces and parse into filename and packagename
            print(line)
            (fileName, packageName) = ("|".join(line.split())).split("|")

            # Insert into array and increase counter if it exists
            if packageName in topTenUnsorted:
                topTenUnsorted[packageName] += topTenUnsorted[packageName]
            else:
                topTenUnsorted[packageName] = 1
            line = f.readline()

    # Print the top 10 package
    count = 1
    for package in sorted(topTenUnsorted, key=topTenUnsorted.get, reverse=True):
        if count <= 10:
            print(str(count)+". "+package+" "+str(topTenUnsorted[package]))
        else:
            exit
        count += 1

if __name__ == "__main__":
    main()

#!/usr/bin/env python
# Import necessary libraries
import sys
import gzip
import shutil
import requests
import array as arr
from collections import defaultdict
from zipfile import ZipFile


# This function is to unzip a .gz on source into destination
def unzip(source, destination, block_size=65536, arch="amd64"):
    with gzip.open(source, 'rb') as s_file, \
            open(destination, 'wb') as d_file:
        while True:
            block = s_file.read(65536)
            if not block:
                break
            else:
                d_file.write(block)
        d_file.write(block)
    return destination


def main():
    # lowercase arch
    arch = sys.argv[1].lower()

    # Define Debian mirror URL
    url = 'http://ftp.uk.debian.org/debian/dists/stable/main/'

    # Define filenames to unzip
    filename = "Contents-"+arch+".gz"
    filenameUdeb = "Contents-udeb-"+arch+".gz"

    # Download filename to local folder
    r = requests.get(url+filename)
    with open("tmp/%s" % filename, 'wb') as f:
        f.write(r.content)

    f = gzip.open("tmp/%s" % filename, 'rb')
    file_content = f.read()
    f.close()

    # Unzip .gzip file
    rawFile = unzip("tmp/%s" % filename, "tmp/%s" % filename.replace(".gz", ""), arch)

    # with gzip.open(filename, 'r') as f_in, open(filename.replace(".gz", ""), 'wb') as f_out:
    #     shutil.copyfileobj(f_in, f_out)   

    # Open unzipped file
    topTenUnsorted = defaultdict(int)
    with open(rawFile, "r") as f:
        line = f.readline()

        # Read line by line
        while line != "":
            line = line.strip("\n")
            # Remove whitespaces and parse into filename and packagename
            print(line)
            (fileName, packageName) = ("|".join(line.split())).split("|")

            # Insert into array and increase counter if it exists
            if packageName in topTenUnsorted:
                topTenUnsorted[packageName] += topTenUnsorted[packageName]
            else:
                topTenUnsorted[packageName] = 1
            line = f.readline()

    # Print the top 10 package
    count = 1
    for package in sorted(topTenUnsorted, key=topTenUnsorted.get, reverse=True):
        if count <= 10:
            print(str(count)+". "+package+" "+str(topTenUnsorted[package]))
        else:
            exit
        count += 1

if __name__ == "__main__":
    main()

#!/usr/bin/env python # Import necessary libraries # This function is to unzip a .gz on source into destination # lowercase arch # Define Debian mirror URL # Define filenames to unzip # Download filename to local folder # Unzip .gzip file # with gzip.open(filename, 'r') as f_in, open(filename.replace(".gz", ""), 'wb') as f_out: #     shutil.copyfileobj(f_in, f_out) # Open unzipped file # Read line by line # Remove whitespaces and parse into filename and packagename # Insert into array and increase counter if it exists # Print the top 10 package