~oubiwann/ubuntu-accomplishments-system/946850-twisted-app

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
"""The libaccomplishments daemon

Provides a D-Bus API to record accomplishments as achieved (trophies) and
to enumerate achieved and unachieved accomplishments.
"""
import ConfigParser
import Image, ImageEnhance
from StringIO import StringIO
import datetime
import getpass
import glob
import gobject
import gpgme
import json
import logging
import os
import pwd
import subprocess
import time

import dbus
import dbus.service

from twisted.internet import defer, reactor
from twisted.internet.protocol import ProcessProtocol
from twisted.python import filepath

import xdg.BaseDirectory

try:
    import pynotify
except ImportError:
    pynotify = None

from ubuntuone.platform.tools import SyncDaemonTool
from ubuntuone.couch import auth

import accomplishments
from accomplishments import exceptions
from accomplishments.daemon import dbusapi
from accomplishments.util import get_data_file, SubprocessReturnCodeProtocol


MATRIX_USERNAME = "openiduser155707"
LOCAL_USERNAME = getpass.getuser()
SCRIPT_DELAY = 900


# XXX the source code needs to be updated to use Twisted async calls better:
# grep the source code for any *.asyncapi.* references, and if they return
# deferreds, adjust them to use callbacks
class AsyncAPI(object):
    """
    This class simply organizes all the Twisted calls into a single location
    for better readability and separation of concerns.
    """
    def __init__(self, parent):
        self.parent = parent

    @staticmethod
    def run_a_subprocess(command):
        logging.info("Running subprocess command: " + str(command))
        pprotocol = SubprocessReturnCodeProtocol()
        reactor.spawnProcess(pprotocol, command[0], command, env=os.environ)
        return pprotocol.returnCodeDeferred

    # XXX let's rewrite this to use deferreds explicitly
    @defer.inlineCallbacks
    def wait_until_a_sig_file_arrives(self):
        path, info = yield self.parent.sd.wait_for_signals(
            signal_ok="DownloadFinished",
            success_filter=lambda path,
            info: path.startswith(self.trophies_path)
            and path.endswith(".asc"))
        logging.info("Trophy signature recieved...")
        accomname = os.path.splitext(os.path.splitext(
            os.path.split(path)[1])[0])[0]
        data = self.parent.listAccomplishmentInfo(accomname)
        iconpath = os.path.join(
            self.parent.accomplishments_path,
            data[0]["application"],
            "trophyimages",
            data[0]["icon"])

        item = os.path.split(path)[1][:-11]
        app = os.path.split(os.path.split(path)[0])[1]
        data = self.parent.listAccomplishmentInfo(item)

        if self.parent.scriptrun_total == len(self.parent.scriptrun_results):
            self.parent.show_unlocked_accomplishments()

        if self.parent.show_notifications == True and pynotify and (
        pynotify.is_initted() or pynotify.init("icon-summary-body")):
            self.parent.service.trophy_received("foo")
            trophy_icon_path = "file://%s" % os.path.realpath(
                os.path.join(
                    os.path.split(__file__)[0],
                    "trophy-accomplished.svg"))
            n = pynotify.Notification(
                "You have accomplished something!", data[0]["title"], iconpath)
            n.show()

        self.wait_until_a_sig_file_arrives()
        #reload_trophy_corresponding_to_sig_file(path)

    # XXX let's rewrite this to use deferreds explicitly
    @defer.inlineCallbacks
    # XXX rename using under_scores, not camelCase, since that seems to be the
    # majority usage in this project...
    def registerTrophyDir(self, trophydir):
        """
        Creates the Ubuntu One share for the trophydir and offers it to the
        server. Returns True if the folder was successfully shared, False if
        not.
        """
        timeid = str(time.time())
        logging.info("Registering Ubuntu One share directory: " + trophydir)

        folder_list = yield self.parent.sd.get_folders()
        folder_is_synced = False
        for folder in folder_list:
            if folder["path"] == trophydir:
                folder_is_synced = True
                break
        if not folder_is_synced:
            # XXX let's breack this out into a separate sync'ing method
            logging.info(
                "...the '%s' folder is not synced with the Matrix" % trophydir)
            logging.info("...creating the share folder on Ubuntu One")
            self.parent.sd.create_folder(trophydir)

            success_filter = lambda info: info["path"] == trophydir
            info = yield self.parent.sd.wait_for_signals(
                signal_ok='FolderCreated', success_filter=success_filter)

            self.parent.sd.offer_share(
                trophydir, MATRIX_USERNAME, LOCAL_USERNAME + " Trophies Folder"
                + " (" + timeid + ")", "Modify")
            logging.info(
                "...share has been offered (" + trophydir + "" + ", "
                + MATRIX_USERNAME + ", " + LOCAL_USERNAME + ")")
            return

        logging.info("...the '%s' folder is already synced" % trophydir)
        # XXX put the following logic into a folders (plural) sharing method
        logging.info("... now checking whether it's shared")
        shared_list = yield self.parent.sd.list_shared()
        folder_is_shared = False
        shared_to = []
        for share in shared_list:
            # XXX let's break this out into a separate share-tracking method
            if share["path"] == trophydir:
                logging.info("...the folder is already shared.")
                folder_is_shared = True
                shared_to.append("%s (%s)" % (
                    share["other_visible_name"], share["other_username"]))
        if not folder_is_shared:
            # XXX let's break this out into a separate folder-sharing method
            logging.info("...the '%s' folder is not shared" % trophydir)
            self.parent.sd.offer_share(
                trophydir, MATRIX_USERNAME, LOCAL_USERNAME + " Trophies Folder"
                + " (" + timeid + ")", "Modify")
            logging.info("...share has been offered (" + trophydir + "" + ", "
                + MATRIX_USERNAME + ", " + LOCAL_USERNAME + ")")
            logging.info("...offered the share.")
            return
        else:
            logging.info("The folder is shared, with: %s" % ", ".join(
                shared_to))
            return

    # XXX let's rewrite this to use deferreds explicitly
    @defer.inlineCallbacks
    def run_scripts_for_user(self, uid):
        # XXX let's use logging here instead
        print "--- Starting Running Scripts ---"
        logging.info("--- Starting Running Scripts ---")
        timestart = time.time()
        self.parent.service.scriptrunner_start()

        # Is the user currently logged in and running a gnome session?
        # XXX use deferToThread
        username = pwd.getpwuid(uid).pw_name
        try:
            # XXX since we're using Twisted, let's use it here too and use the
            # deferred-returning call
            proc = subprocess.check_output(
                ["pgrep", "-u", username, "gnome-session"]).strip()
        except subprocess.CalledProcessError:
            # user does not have gnome-session running or isn't logged in at
            # all
            logging.info("No gnome-session process for user %s" % username)
            return
        # XXX this is a blocking call and can't be here if we want to take
        # advantage of deferreds; instead, rewrite this so that the blocking
        # call occurs in a separate thread (e.g., deferToThread)
        fp = open("/proc/%s/environ" % proc)
        try:
            envars = dict(
                [line.split("=", 1) for line in fp.read().split("\0")
                if line.strip()])
        except IOError:
            # user does not have gnome-session running or isn't logged in at
            # all
            logging.info("No gnome-session environment for user %s" % username)
            return
        fp.close()

        # XXX use deferToThread
        os.seteuid(uid)

        required_envars = ['DBUS_SESSION_BUS_ADDRESS']
        env = dict([kv for kv in envars.items() if kv[0] in required_envars])
        # XXX use deferToThread
        oldenviron = os.environ
        os.environ.update(env)
        # XXX note that for many of these deferredToThread changes, we can put
        # them all in a DeferredList and once they're all done and we have the
        # results for all of them, a callback can be fired to continue.

        # XXX this next call, a DBus check, happens in the middle of this
        # method; it would be better if this check was done at a higher level,
        # for instance, where this class is initiated: if the daemon isn't
        # registered at the time of instantiation, simply abort then instead of
        # making all the way here and then aborting. (Note that moving this
        # check to that location will also eliminate an obvious circular
        # import.)
        if not dbusapi.daemon_is_registered():
            return

        # XXX all parent calls should be refactored out of the AsyncAPI class
        # to keep the code cleaner and the logic more limited to one particular
        # task
        accoms = self.parent.listAllAvailableAccomplishmentsWithScripts()
        totalscripts = len(accoms)
        self.parent.scriptrun_total = totalscripts
        logging.info("Need to run (%d) scripts" % totalscripts)
        # XXX let's use logging here instead
        print "Need to run (%d) scripts" % totalscripts

        scriptcount = 1
        for accom in accoms:
            msg = "%s/%s: %s" % (scriptcount, totalscripts, accom["_script"])
            # XXX let's use logging here instead
            print msg
            logging.info(msg)
            exitcode = yield self.run_a_subprocess([accom["_script"]])
            if exitcode == 0:
                self.parent.scriptrun_results.append(
                    str(accom["application"]) + "/"
                    + str(accom["accomplishment"]))
                self.parent.accomplish(
                    accom["application"], accom["accomplishment"])
                # XXX let's use logging here instead
                print "...Accomplished"
                logging.info("...Accomplished")
            elif exitcode == 1:
                self.parent.scriptrun_results.append(None)
                # XXX let's use logging here instead
                print "...Not Accomplished"
                logging.info("...Not Accomplished")
            elif exitcode == 2:
                self.parent.scriptrun_results.append(None)
                # XXX let's use logging here instead
                print "...Error"
                logging.info("....Error")
            else:
                self.parent.scriptrun_results.append(None)
                # XXX let's use logging here instead
                print "...Other error code."
                logging.info("...Other error code")
            scriptcount = scriptcount + 1

        os.environ = oldenviron

        # XXX eventually the code in this method will be rewritten using
        # deferreds; as such, we're going to have to be more clever regarding
        # timing things...
        timeend = time.time()
        timefinal = round((timeend - timestart), 2)

        # XXX let's use logging here instead
        print "--- Completed Running Scripts in %.2f seconds ---" % timefinal
        logging.info(
            "--- Completed Running Scripts in %.2f seconds---" % timefinal)
        self.parent.service.scriptrunner_finish()


class Accomplishments(object):
    """The main accomplishments daemon.

    No D-Bus required, so that it can be used for testing.
    """
    def __init__(self, service, show_notifications=None):
        self.accomplishments_path = None
        self.scripts_path = None
        self.trophies_path = None
        self.has_u1 = None
        self.has_verif = None
        self.lang = "en_us"
        self.service = service
        self.dir_config = None
        self.dir_data = None
        self.dir_cache = None
        self.scriptrun_total = 0
        self.scriptrun_results = []
        self.depends = []
        self.processing_unlocked = False
        self.asyncapi = AsyncAPI(self)

        # create config / data dirs if they don't exist

        self.dir_config = os.path.join(
            xdg.BaseDirectory.xdg_config_home, "accomplishments")
        self.dir_data = os.path.join(
            xdg.BaseDirectory.xdg_data_home, "accomplishments")
        self.dir_cache = os.path.join(
            xdg.BaseDirectory.xdg_cache_home, "accomplishments")

        if not os.path.exists(self.dir_config):
            os.makedirs(self.dir_config)

        if not os.path.exists(self.dir_data):
            os.makedirs(self.dir_data)

        if not os.path.exists(self.dir_cache):
            os.makedirs(self.dir_cache)

        # set up logging
        logdir = os.path.join(self.dir_cache, "logs")

        if not os.path.exists(logdir):
            os.makedirs(logdir)

        #self.logging = logging
        logging.basicConfig(
            filename=(os.path.join(logdir, 'daemon.log')), level=logging.INFO)

        now = datetime.datetime.now()
        logging.info(
            "------------------- Ubuntu Accomplishments Daemon Log - %s "
            "-------------------", str(now))

        self._loadConfigFile()

        # XXX let's use logging here instead
        print "Accomplishments path: " + self.accomplishments_path
        print "Scripts path: " + self.scripts_path
        print "Trophies path: " + self.trophies_path

        self.show_notifications = show_notifications
        logging.info("Connecting to Ubuntu One")
        self.sd = SyncDaemonTool()

        # XXX this wait-until thing should go away; it should be replaced by a
        # deferred-returning function that has a callback which fires off
        # generate_all_trophis and schedule_run_scripts...
        self.asyncapi.wait_until_a_sig_file_arrives()
        self.generate_all_trophies()


    def get_media_file(self, media_file_name):
        media_filename = get_data_file('media', '%s' % (media_file_name,))
        if not os.path.exists(media_filename):
            media_filename = None

        return "file:///" + media_filename

    def show_unlocked_accomplishments(self):
        """
        Determine if accomplishments have been unlocked and display a
        notify-osd bubble.
        """
        unlocked = 0
        it = 0
        for dep in self.depends:
            for res in self.scriptrun_results:
                if dep.values()[0] == res:
                    unlocked = unlocked + 1
            it = it + 1

        if unlocked is not 0:
            if self.show_notifications == True and pynotify and (
            pynotify.is_initted() or pynotify.init("icon-summary-body")):
                #trophy_icon_path = "file://%s" % os.path.realpath(
                #    os.path.join(
                #        os.path.split(__file__)[0], "trophy-accomplished.svg")
                if unlocked == 1:
                    message = "You have unlocked one new accomplishment."
                else:
                    message = "You have unlocked %s new accomplishments." % (
                        str(unlocked))
                n = pynotify.Notification(
                    "Accomplishments Unlocked!", message,
                    self.get_media_file("unlocked.png"))
                n.show()

        self.scriptrun_total = 0
        self.scriptrun_results = []
        self.depends = []

    def _get_accomplishments_files_list(self):
        logging.info("Looking for accomplishments files in "
                     + self.accomplishments_path)
        accom_files = os.path.join(self.accomplishments_path,
            "*", "*.accomplishment")
        return glob.glob(accom_files)

    def _load_accomplishment_file(self, f):
        logging.info("Loading accomplishments file: " + f)
        config = ConfigParser.RawConfigParser()
        config.read(f)
        data = dict(config._sections["accomplishment"])
        data["_filename"] = f
        data["accomplishment"] = os.path.splitext(os.path.split(f)[1])[0]
        data["accomplishment"] = os.path.splitext(os.path.split(f)[1])[0]
        return data

    def validate_trophy(self, filename):
        """
        Validated a trophy file to ensure it has not been tampered with.
        Returns True for valid or False for invalid (missing file, bad sig
        etc).
        """
        logging.info("Validate trophy: " + str(filename))

        if os.path.exists(filename):
            # the .asc signed file exists, so let's verify that it is correctly
            # signed by the Matrix
            trophysigned = open(filename, "r")
            trophy = open(filename[:-4], "r")
            c = gpgme.Context()

            signed = StringIO(trophysigned.read())
            plaintext = StringIO(trophy.read())
            sig = c.verify(signed, None, plaintext)

            if len(sig) != 1:
                logging.info("...No Sig")
                return False

            if sig[0].status is not None:
                logging.info("...Bad Sig")
                return False
            else:
                result = {'timestamp': sig[0].timestamp, 'signer': sig[0].fpr}
                logging.info("...Verified!")
                return True
        else:
            logging.info(".asc does not exist for this trophy")
            return False

        logging.info("Verifying trophy signature")

    def _load_trophy_file(self, f):
        logging.info("Load trophy file: " + f)
        config = ConfigParser.RawConfigParser()
        config.read(f)
        data = dict(config._sections["trophy"])
        data["_filename"] = f
        data["accomplishment"] = os.path.splitext(os.path.split(f)[1])[0]
        return data

    def listAllAccomplishments(self):
        logging.info("List all accomplishments")
        fs = [self._load_accomplishment_file(f) for f in
            self._get_accomplishments_files_list()]
        return fs

    def generate_all_trophies(self):
        paths = []
        final = []
        files = self._get_accomplishments_files_list()

        for f in files:
            paths.append(os.path.split(f)[0])

        paths = list(set(paths))

        for p in paths:
            app = os.path.split(p)[1]
            app_trophyimagespath = os.path.join(p, "trophyimages")
            cache_trophyimagespath = os.path.join(
                self.dir_cache, "trophyimages", app)
            if not os.path.exists(cache_trophyimagespath):
                os.makedirs(cache_trophyimagespath)

            # first delete existing images
            lockedlist=glob.glob(cache_trophyimagespath + "/*locked*")

            opplist=glob.glob(cache_trophyimagespath + "/*opportunity*")

            for l in lockedlist:
                os.remove(l)

            for o in opplist:
                os.remove(o)

            # now generate our trophy images
            lock_image_path = os.path.join(
                os.path.dirname(accomplishments.__path__[0]),
                "data/media/lock.png")
            self.generate_trophy_images(
                app_trophyimagespath, cache_trophyimagespath, lock_image_path)

    def reduce_trophy_opacity(self, im, opacity):
        """Returns an image with reduced opacity."""
        assert opacity >= 0 and opacity <= 1
        if im.mode != 'RGBA':
            im = im.convert('RGBA')
        else:
            im = im.copy()
        alpha = im.split()[3]
        alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
        im.putalpha(alpha)
        return im

    def generate_trophy_images(self, infolder, outfolder, watermark):
        mark = Image.open(watermark)
        for root, dirs, files in os.walk(infolder):
            for name in files:
                try:
                    im = Image.open(os.path.join(root, name))
                    filename = os.path.join(outfolder, name)
                    filecore = os.path.splitext(filename)[0]
                    filetype = os.path.splitext(filename)[1]

                    im.save(filename)

                    # Opacity set to 1.0 until we figure out a better way of
                    # showing opportunities
                    reduced = self.reduce_trophy_opacity(im, 1.0)
                    reduced.save(filecore + "-opportunity" + filetype)

                    if im.mode != 'RGBA':
                        im = im.convert('RGBA')
                    layer = Image.new('RGBA', im.size, (0,0,0,0))
                    position = (
                        im.size[0] - mark.size[0], im.size[1] - mark.size[1])
                    layer.paste(mark, position)
                    img = Image.composite(layer, reduced, layer)
                    img.save(filecore + "-locked" + filetype)
                except Exception, (msg):
                    # XXX let's use logging here instead
                    print msg

    def verifyU1Account(self):
        # check if this machine has an Ubuntu One account
        logging.info("Check if this machine has an Ubuntu One account...")
        u1auth_response = auth.request(
            url='https://one.ubuntu.com/api/account/')
        u1email = None
        if not isinstance(u1auth_response, basestring):
            u1email = json.loads(u1auth_response[1])['email']
        else:
            logging.info("No Ubuntu One account is configured.")

        if u1email is None:
            logging.info("...No.")
            logging.info(u1auth_response)
            self.has_u1 = False
            return False
        else:
            logging.info("...Yes.")
            self.has_u1 = True
            return True

    def getConfigValue(self, section, item):
        """Return a configuration value from the .accomplishments file"""
        logging.info(
            "Returning configuration values for: %s, %s", section, item)
        homedir = os.getenv("HOME")
        config = ConfigParser.RawConfigParser()
        cfile = self.dir_config + "/.accomplishments"
        config.read(cfile)

        if section == "config" and item == "has_u1":
            item = config.getboolean(section, item)
            return item
        elif section == "config" and item == "has_verif":
            item = config.getboolean(section, item)
            return item
        else:
            item = config.get(section, item)
            return item

    def setConfigValue(self, section, item, value):
        """Set a configuration value in the .accomplishments file"""
        logging.info(
            "Set configuration file value in '%s': %s = %s", section, item,
            value)
        homedir = os.getenv("HOME")
        config = ConfigParser.RawConfigParser()
        cfile = self.dir_config + "/.accomplishments"

        config.read(cfile)
        if config.has_section(section) is False:
            config.add_section(section)
        else:
            config.set(section, item, value)

        # Writing our configuration file to 'example.cfg'
        with open(cfile, 'wb') as configfile:
            config.write(configfile)

        self._loadConfigFile()

    def _writeConfigFile(self):
        logging.info("Writing the configuration file")
        homedir = os.getenv("HOME")
        config = ConfigParser.RawConfigParser()
        cfile = self.dir_config + "/.accomplishments"

        config.add_section('config')

        config.set('config', 'has_u1', self.has_u1)
        config.set('config', 'has_verif', self.has_verif)
        config.set('config', 'accompath', self.accomplishments_path)
        config.set('config', 'trophypath', self.trophies_path)

        with open(cfile, 'wb') as configfile:
        # Writing our configuration file to 'example.cfg'
            config.write(configfile)

        self.accomplishments_path = os.path.join(
            self.accomplishments_path, "accomplishments")
        logging.info("...done.")

    def accomplish(self, app, accomplishment_name):
        logging.info(
            "Accomplishing something: %s, %s", app, accomplishment_name)
        accom_file = os.path.join(self.accomplishments_path, app,
            "%s.accomplishment" % accomplishment_name)
        try:
            data = self._load_accomplishment_file(accom_file)
        except KeyError:
            raise exceptions.NoSuchAccomplishment()

        needsinfolist = []

        for k in data:
            if "needs-information" in k:
                needsinfolist.append(data[k])

        for n in needsinfolist:
            values = self.getExtraInformation(app, n)
            data[n] = values[0][n]

        if "depends" in data:
            for dependency in data["depends"].split(","):
                dapp, dname = dependency.split("/")
                dtrophy_file = os.path.join(
                    self.trophies_path, dapp,
                    "%s.trophy.asc" % dname)
                if not os.path.exists(dtrophy_file):
                    raise exceptions.AccomplishmentLocked()

        cp = ConfigParser.RawConfigParser()
        cp.add_section("trophy")
        del data["_filename"]
        cp.set("trophy", "accomplishment", "%s/%s" % (
            app, accomplishment_name))
        for o, v in data.items():
            cp.set("trophy", o, v)
        try:
            os.makedirs(os.path.join(self.trophies_path, app))
        except OSError:
            pass # already exists
        trophy_file = os.path.join(self.trophies_path, app,
            "%s.trophy" % accomplishment_name)
        fp = open(trophy_file, "w")
        cp.write(fp)
        fp.close()

        if not data["needs-signing"] or data["needs-signing"] == False:
            self.trophy_received()
            if self.show_notifications == True and pynotify and (
            pynotify.is_initted() or pynotify.init("icon-summary-body")):
                trophy_icon_path = "file://%s" % os.path.realpath(
                    os.path.join(
                        os.path.split(__file__)[0], "trophy-accomplished.svg"))
                n = pynotify.Notification("You have accomplished something!",
                    data["title"], trophy_icon_path)
                n.show()

        return self._load_trophy_file(trophy_file)

    def _loadConfigFile(self):
        homedir = os.environ["HOME"]
        config = ConfigParser.RawConfigParser()
        cfile = os.path.join(self.dir_config, ".accomplishments")

        u1ver = self.verifyU1Account()

        if u1ver is False:
            self.has_u1 = False
        else:
            self.has_u1 = True

        if config.read(cfile):
            logging.info("Loading configuration file: " + cfile)
            if config.get('config', 'accompath'):
                self.accomplishments_path = os.path.join(
                    config.get('config', 'accompath'), "accomplishments/")
                logging.info(
                    "...setting accomplishments path to: "
                    + self.accomplishments_path)
                self.scripts_path = os.path.split(
                    os.path.split(self.accomplishments_path)[0])[0] + "/scripts"
                logging.info(
                    "...setting scripts path to: " + self.scripts_path)
            if config.get('config', 'trophypath'):
                logging.info(
                    "...setting trophies path to: "
                    + config.get('config', 'trophypath'))
                self.trophies_path = config.get('config', 'trophypath')
            if config.get('config', 'has_u1'):
                self.has_u1 = config.getboolean('config', 'has_u1')
            if config.get('config', 'has_verif'):
                self.has_verif = config.getboolean('config', 'has_verif')
        else:
            accompath = os.path.join(homedir, "accomplishments")
            logging.info("Configuration file not found...creating it!")
            # XXX let's use logging here instead
            print "Configuration file not found...creating it!"

            self.has_verif = False
            self.accomplishments_path = accompath
            logging.info(
                "...setting accomplishments path to: "
                + self.accomplishments_path)
            self.trophies_path = os.path.join(self.dir_data, "trophies")
            logging.info("...setting trophies path to: " + self.trophies_path)
            self.scripts_path = os.path.join(accompath, "scripts")
            logging.info("...setting scripts path to: " + self.scripts_path)

            if not os.path.exists(self.trophies_path):
                os.makedirs(self.trophies_path)

            self._writeConfigFile()

    def getAllExtraInformationRequired(self):
        """
        Return a dictionary of all information required for the accomplishments
        to authticate. Returns {application, needs-information, label,
        description}.
        """
        accomplishments_files = self._get_accomplishments_files_list()
        infoneeded = []
        trophyextrainfo = os.path.join(
            self.trophies_path, ".extrainformation/")

        if not os.path.isdir(trophyextrainfo):
            os.makedirs(trophyextrainfo)

        for f in accomplishments_files:
            accomextrainfo = os.path.join(
                os.path.split(f)[0], "extrainformation")
            d = {}
            accomconfig = ConfigParser.RawConfigParser()
            accomconfig.read(f)
            config_args = ("accomplishment", "needs-information")
            if accomconfig.has_option(*config_args) == True:
                try:
                    open(trophyextrainfo + str(accomconfig.get(*config_args)))
                except IOError as e:
                    infofile = os.path.join(
                        accomextrainfo, accomconfig.get(*config_args))
                    infoconfig = ConfigParser.RawConfigParser()
                    infoconfig.read(infofile)
                    d = {
                        "application" : accomconfig.get(
                            "accomplishment", "application"),
                        "needs-information" : accomconfig.get(*config_args),
                        "label" : infoconfig.get("label", self.lang),
                        "description" : infoconfig.get(
                            "description", self.lang)}
                    infoneeded.append(d)

        # uniqify all the data required
        final = []
        for x in infoneeded:
            if x not in final:
                final.append(x)

        return final

    def listAllAccomplishmentsAndStatus(self):
        """
        Provide a list of all accomplishments and whether they have been
        accomplished or not, including validating the trophies. Returns a list
        of dictionaries.
        """
        if self.depends == []:
            getdepends = True
        else:
            getdepends = False

        logging.info("List all accomplishments and status")
        accomplishments_files = self._get_accomplishments_files_list()
        things = {}
        for accomplishment_file in accomplishments_files:
            path, name = os.path.split(accomplishment_file)
            name = os.path.splitext(name)[0]
            app = os.path.split(path)[1]
            data = self._load_accomplishment_file(accomplishment_file)

            icon = data["icon"]
            #icondir = os.path.join(os.path.split(
            #   accomplishment_file)[0], "trophyimages")
            icondir = os.path.join(
                self.dir_cache, "trophyimages", data["application"])
            iconname = os.path.splitext(icon)[0]
            iconext = os.path.splitext(icon)[1]

            # find the human readable name of the application and add it to the
            # dict
            accompath = os.path.join(
                self.accomplishments_path, data["application"])
            infofile = os.path.join(accompath, "ABOUT")
            config = ConfigParser.RawConfigParser()
            config.read(infofile)
            final = config.get("general", "name")

            data["application-human"] = final

            # If the trophy file exists, this must be accomplished and not
            # locked
            trophy_file = os.path.join(
                self.trophies_path, app, "%s.trophy" % name)
            trophysigned = str(trophy_file) + ".asc"

            # validate all files that have been signed
            if self.validate_trophy(trophysigned) == True:
                data["accomplished"] = True
                data["locked"] = False
                data.update(self._load_trophy_file(trophy_file))
                data["iconpath"] = os.path.join(icondir, icon)
            else:
                if os.path.exists(trophysigned):
                    os.remove(trophysigned)
                    os.remove(trophy_file)
                data["accomplished"] = False
                data["iconpath"] = os.path.join(icondir, (
                    iconname + "-opportunity" + iconext))
                # can't tell if it's locked until we've seen all trophies
            things[accomplishment_file] = data
        # Now go through the list again and check if things are locked
        for accomplishment_file in things:
            item = things[accomplishment_file]
            if item["accomplished"] == False:
                locked = False
                depends_list = item.get("depends")
                if depends_list:
                    dependencies = depends_list.split(",")
                    for dependency in dependencies:
                        dapp, dname = dependency.split("/")
                        daccomplishment_file = os.path.join(
                            self.accomplishments_path, dapp,
                            "%s.accomplishment" % dname)
                        daccomplishment_data = things[daccomplishment_file]
                        if daccomplishment_data["accomplished"] == False:
                            # update the list of dependencies
                            if getdepends == True:
                                depends_key = str(
                                    item["application"]) + "/" + str(
                                        item["accomplishment"])
                                self.depends.append(
                                    {depends_key: depends_list})
                            locked = True
                            itemicon = item["icon"]
                            itemiconname = os.path.splitext(itemicon)[0]
                            itemiconext = os.path.splitext(itemicon)[1]
                            item["iconpath"] = os.path.join(
                                os.path.split(item["iconpath"])[0],
                                (itemiconname + "-locked" + itemiconext))
                item["locked"] = locked
                #item["icon"] = os.path.join(
                #   icondir, (iconname + "-opportunity" + iconext))
        return things.values()

    def listAllAvailableAccomplishmentsWithScripts(self):
        logging.info("List all accomplishments with scripts")
        available = [accom for accom in self.listAllAccomplishmentsAndStatus()
            if not accom["accomplished"] and not accom["locked"]]
        withscripts = []
        for accom in available:
            path, name = os.path.split(accom["_filename"])
            name = os.path.splitext(name)[0]
            app = os.path.split(path)[1]
            scriptglob = glob.glob(os.path.join(
                self.scripts_path, app, "%s.*" % name))
            if scriptglob:
                accom["_script"] = scriptglob[0]
                withscripts.append(accom)
        return withscripts

    def listAccomplishmentInfo(self, accomplishment):
        logging.info("Getting accomplishment info for " + accomplishment)
        search = "/" + accomplishment + ".accomplishment"
        files = self._get_accomplishments_files_list()
        match = None

        data = []

        for i in files:
            if search in i:
                match = i

        config = ConfigParser.RawConfigParser()
        config.read(match)
        data.append(dict(config._sections["accomplishment"]))
        return data

    def run_scripts_for_all_active_users(self):
        for uid in [x.pw_uid for x in pwd.getpwall()
            if x.pw_dir.startswith('/home/') and x.pw_shell != '/bin/false']:
            os.seteuid(0)
            self.asyncapi.run_scripts_for_user(uid)

    def run_scripts(self, run_by_client):
        uid = os.getuid()
        if uid == 0:
            logging.info("Run scripts for all active users")
            self.run_scripts_for_all_active_users()
        else:
            logging.info("Run scripts for user")
            self.asyncapi.run_scripts_for_user(uid)

    def createExtraInformationFile(self, app, item, data):
        logging.info(
            "Creating Extra Information file: %s, %s, %s", app, item, data)
        extrainfodir = os.path.join(self.trophies_path, ".extrainformation/")

        if not os.path.isdir(extrainfodir):
            os.makedirs(extrainfodir)
        try:
            open(os.path.join(extrainfodir, item))
            pass
        except IOError as e:
            f = open(os.path.join(extrainfodir, item), 'w')
            f.write(data)
            f.close()

    def getExtraInformation(self, app, item):
        extrainfopath = os.path.join(self.trophies_path, ".extrainformation/")
        authfile = os.path.join(extrainfopath, item)
        try:
            f = open(authfile, "r")
            data = f.read()
            final = [{item : data}]
        except IOError as e:
            #print "No data."
            final = [{item : False}]
        return final


    # XXX once the other reference to dbusapi is removed from this file, this
    # will be the last one. It doesn't really belong here... we can set a whole
    # slew of dbus api calls on this object when it is instantiated, thus
    # alleviating us from the burden of a hack like this below
    trophy_received = dbusapi.DBusSignals.trophy_received