Scripts

Differences between revisions 6 and 7
Revision 6 as of 2012-05-01 10:26:05
Size: 5357
Comment:
Revision 7 as of 2012-05-01 10:30:31
Size: 5631
Comment:
Deletions are marked like this. Additions are marked like this.
Line 77: Line 77:
    # Connect to the daemon to get extra-information.
Line 84: Line 85:
    # Get count of bugs reported by user from Launchpad, using email to
    #
identify
    # Get count of bugs reported by user from Launchpad, using email to identify user
Line 90: Line 90:
        # User of such e-mail does not exist...
Line 93: Line 94:
        # Search for bugs of any status reported by this user in Ubuntu...
Line 98: Line 100:
            # Success! Accomplishment completed!
Line 100: Line 103:
            # No such bugs were found...
Line 115: Line 119:
Line 124: Line 129:

The Guide to Creating Your Accomplishments

Creating accomplishment scripts

<-- Previous page (Creating .accomplishment files) - - - - - Next page (blah) --->

This page is under construction. This guide is not yet complete!

Every single accomplishment requires a script, that is executed to determine whether the user has completed this accomplishment. Usually these scripts are not larger then the .accomplishment file, so no great programming skills are needed to prepare one.

Currently only Python scripts are supported. We hope to allow developers to prepare scripts in other languages too, but that's not our priority, while writing them in Python is simple and quick.

Returning values

The Accomplishments System will run scripts on it's own. The only thing the script has to do is to check if the accomplishment is complete, and report result by returning a number.

The exit codes are:

  • 0 - The accomplishment has been completed.

  • 1 - The accomplishment has not been completed.

  • 2 - There was an error while checking for completion.

  • 4 - There was an error while getting extra-information.

The easiest way to return such value is to use sys.exit(n).


While this should be enough for you to create scripts, there are some tips and examples for you, to make everything clearer.

Also, if your script has to use some extra-information, it can use Accomplishments Daemon API to get it - details below.

Everything your script prints will land in daemon's log - which is available at ~/.cache/accomplishments/daemon.log

Remember, that if your accomplishment collection uses scripts that import some unusual libraries, you will need to setup appropriate dependencies, when publishing a .deb package.

Local accomplishment scripts

Example

##!/usr/bin/python
import traceback, sys
try:
    try:
        #open the highscores file...
        highscoresfile = open("/var/games/gnomine.Small.scores")
    except IOError as e:
        # it seems that the file does not exist
        sys.exit(1)
    bestscore = highscoresfile.readline()
    if not bestscore:
        #the file is empty
        sys.exit(1)
    #the file is not empty
    sys.exit(0)
except:
    traceback.print_exc()
    sys.exit(2)

This is a working script for Begginer Minesweeper from previous chapter. It is intended to check if the user has ever completed a Small game in Gnomine. This is done by reading the highscores file, and checking whether it contains at least one entry. If so, we return 0, otherwise 1. All that is within a global try block, to react in case of unhandled errors (that does not make much sense here, but may be useful in your scripts, so it's just as an example).

Global accomplishment scripts

Scripts for global accomplishments are slightly more complicated. Also, they may use Accomplishments Daemon API to get extra-information easily.

Example

##!/usr/bin/python
import traceback, sys

from accomplishments.daemon import dbusapi

try:
    import sys, os, pwd, subprocess
    from launchpadlib.launchpad import Launchpad

    # Connect to the daemon to get extra-information.
    api = dbusapi.Accomplishments()
    f = api.get_extra_information("ubuntu-community", "launchpad-email")
    if bool(f[0]["launchpad-email"]) == False:
        sys.exit(4)
    else:
        email = f[0]["launchpad-email"]

    # Get count of bugs reported by user from Launchpad, using email to identify user
    l = Launchpad.login_anonymously(
        'ubuntu-community accomplishments', 'production')
    me = l.people.getByEmail(email=email)
    if me == None:
        # User of such e-mail does not exist...
        sys.exit(1)
    else:
        ubuntu=l.projects['ubuntu']
        # Search for bugs of any status reported by this user in Ubuntu...
        bugs_reported = ubuntu.searchTasks(bug_reporter=me, 
            status=['New', 'Incomplete', 'Invalid', 'Confirmed', 'Triaged', 
                    'In Progress', 'Fix Committed', 'Fix Released', 'Opinion',
                    "Won't Fix"])
        if len(bugs_reported) > 0:
            # Success! Accomplishment completed!
            sys.exit(0)
        else:
            # No such bugs were found...
            sys.exit(1)
except SystemExit, e:
    sys.exit(e.code)
except:
    traceback.print_exc()
    sys.exit(2)

This example is an actual code for Filed First Bug accomplishment from previous chapter. It uses launchpadlib to connect to Launchpad, and check bugs that given person reported in Ubuntu. There are few lines of special interest, that are worth explaining:

    from accomplishments.daemon import dbusapi
    api = dbusapi.Accomplishments()

The above used to connect to Accomplishments Daemon.

Accomplishments/CreatingGuide/Scripts (last edited 2012-05-01 15:34:32 by 99-41-167-234)