Sam's Blog

For the Obsessive Data Hog: Logging Your Launchpad PPA

Posted on 15 Aug 2013

Since I operate a few Personal Package Archives over on Launchpad I wanted a way to log the number of downloads for each package and file them by date. Fortunately there’s Launchpad’s API and Python.

Scripting FTW

Below is a generic version of the Python script I use to log the download count (of a particular system architecture) of each package for one PPA. And because of my need for organization it does it all by date.

If you were to use it yourself you need only make a few alterations:

That’s it (and you may feel the need to run it daily as a cron job). Here’s hoping it’s useful to some of you! :)

#!/usr/bin/python
#
# Authors:
#   Sam Hewitt <sam@snwh.org>
#
# Description:
#   A python script for PPA logging script based on time.
#
# Legal Stuff:
#
# This script 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; version 3.
#
# This script 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 <https://www.gnu.org/licenses/gpl-3.0.txt>

import sys, os
import time, datetime
from launchpadlib.launchpad import Launchpad

# For reference: "ppa:ownername/archivename" or "https://launchpad.net/~ownername/+archive/archive-name"
PPAOWNER = 'ownername' # Name of the owner of the ppa you would like the stats for.
PPANAME = 'archive-name' # Name of the PPA you would like the stats for.
ARCH = 'amd64' # System CPU architecture you would like the stats for. E.g. amd64, i386, etc.

lp_login = Launchpad.login_anonymously('ppastats', 'edge', "~/.launchpadlib/cache/", version='devel') # Login into Launchpad Anoymously
owner = lp_login.people[PPAOWNER] # PPA owner
archive = owner.getPPAByName(name=PPANAME) # PPA name

# Be sure to COMMENT OUT OR DELETE LINES NOT SUPPORTED BY THE PPA in question. For example if the PPA doesn't
# have Precise (12.04) or i386 support than you wouldn't use that version.

precise_amd64 = 'https://api.launchpad.net/devel/ubuntu/' + 'precise' + '/' + ARCH # Ubuntu 12.04
quantal_amd64 = 'https://api.launchpad.net/devel/ubuntu/' + 'quantal' + '/' + ARCH # Ubuntu 12.10 
raring_amd64 = 'https://api.launchpad.net/devel/ubuntu/' + 'raring' + '/' + ARCH # Ubuntu 13.04 
saucy_amd64 = 'https://api.launchpad.net/devel/ubuntu/' + 'saucy' + '/' + ARCH # Ubuntu 13.10 

# Declare a desination in home folder for the log files based on date
home = os.getenv("HOME") 
dirname = "/logs/"+PPANAME+'/'
year = time.strftime("%Y")+'/'
month = time.strftime("%m")+'/'
destination = home+dirname+year+month

# For loop to write the log files with download statistics from the PPA
for individual_archive in archive.getPublishedBinaries(status='Published',distro_arch_series=precise_amd64):

    x = individual_archive.getDownloadCount() # Get download count
    packagevers='~'.join(individual_archive.binary_package_version.split('~')[:-1]) # Get the package version
    sys.stdout = open(destination+time.strftime("%Y-%m-%d-%I:%M-")+packagevers+'.txt', 'w+') # Open the log file
    # Print heading
    print 'Download stats for '+PPANAME+' PPA'  
    print '----------------------------------------------'
    print ''
    print 'Precise builds:'
    print '---------------'
    if x > 0:
        # Write the package name, version and download count to the log file
        print individual_archive.binary_package_name + "\t" + individual_archive.binary_package_version + "\t" + ARCH + "\t" + str(individual_archive.getDownloadCount())
    elif x < 1:
        # If no stats are found print 'No data'
        print 'No data'
    print ''

for individual_archive in archive.getPublishedBinaries(status='Published',distro_arch_series=quantal_amd64):

    x = individual_archive.getDownloadCount()
    print 'Quantal builds:'
    print '---------------'
    if x > 0:
        print individual_archive.binary_package_name + "\t" + individual_archive.binary_package_version + "\t" + ARCH + "\t" + str(individual_archive.getDownloadCount())
    elif x < 1:
        print 'No data'
    print ''

for individual_archive in archive.getPublishedBinaries(status='Published',distro_arch_series=raring_amd64):

    x = individual_archive.getDownloadCount()
    print 'Raring builds:'
    print '--------------'
    if x > 0:
        print individual_archive.binary_package_name + "\t" + individual_archive.binary_package_version + "\t" + ARCH + "\t" + str(individual_archive.getDownloadCount())
    elif x < 1:
        print 'No data'
    print ''

for individual_archive in archive.getPublishedBinaries(status='Published',distro_arch_series=saucy_amd64):

    x = individual_archive.getDownloadCount()
    print 'Saucy builds:'
    print '-------------'
    if x > 0:
        print individual_archive.binary_package_name + "\t" + individual_archive.binary_package_version + "\t" + ARCH + "\t" + str(individual_archive.getDownloadCount())
    elif x < 1:
        print 'No data'

Recent Posts

How to Run a Usability Test27 Aug 2019
Joining Purism!30 Jul 2019
Taking the "User" out of Design20 Feb 2019
Basic Linux Virtualization with KVM16 Feb 2019
Moving Beyond Themes05 Aug 2018