~mvo/software-center/new-apps-icons

« back to all changes in this revision

Viewing changes to utils/update-software-center-agent

  • Committer: Michael Vogt
  • Date: 2010-08-05 10:29:12 UTC
  • mfrom: (858.2.71 buy-something)
  • Revision ID: michael.vogt@ubuntu.com-20100805102912-0ol8f1qnr4ajx2l4
merged lp:~mvo/software-center/buy-something, currently needs to
be enabled via "--enable-buy" to make it work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Copyright (C) 2010 Canonical
 
3
#
 
4
# Authors:
 
5
#  Michael Vogt
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
 
 
21
import apt
 
22
import locale
 
23
import gettext
 
24
import logging
 
25
import os
 
26
import os.path
 
27
import shutil
 
28
import string
 
29
import sys
 
30
import time
 
31
import xapian
 
32
import xdg.BaseDirectory
 
33
 
 
34
from optparse import OptionParser
 
35
 
 
36
from softwarecenter.enums import *
 
37
from softwarecenter.db.update import update_from_software_center_agent
 
38
 
 
39
if __name__ == "__main__":
 
40
    try:
 
41
        locale.setlocale(locale.LC_ALL, "")
 
42
    except Exception, e:
 
43
        logging.warn("setlocale failed with '%s'" % e) 
 
44
 
 
45
    # parser
 
46
    parser = OptionParser()
 
47
    parser.add_option("--debug", "", action="store_true", default=False,
 
48
                      help="show debug output")
 
49
    (options, args) = parser.parse_args()
 
50
 
 
51
    logging.basicConfig(level=logging.INFO)
 
52
    if options.debug:
 
53
        logging.basicConfig(level=logging.DEBUG)
 
54
 
 
55
    # get a cache
 
56
    cache = apt.Cache(memonly=True)
 
57
 
 
58
    # setup path
 
59
    pathname = XAPIAN_BASE_PATH_SOFTWARE_CENTER_AGENT+".tmp"
 
60
    if not os.path.exists(pathname):
 
61
        os.makedirs(pathname)
 
62
 
 
63
    # check that we can write
 
64
    if not os.access(pathname, os.W_OK):
 
65
        logging.warn("Cannot write to '%s'." % pathname)
 
66
        logging.warn("Please check you have the relevant permissions.")
 
67
        sys.exit(1)
 
68
 
 
69
    # get a writable DB
 
70
    db = xapian.WritableDatabase(pathname, xapian.DB_CREATE_OR_OVERWRITE)
 
71
 
 
72
    # the following requires a http connection, so we do it in a 
 
73
    # seperate database
 
74
    if not update_from_software_center_agent(db, cache):
 
75
        logging.warn("update_from_software_center_agent_failed")
 
76
        sys.exit(1)
 
77
 
 
78
    # flush ...
 
79
    db.flush()
 
80
    del db
 
81
 
 
82
    # and move into place
 
83
    final_pathname = string.rsplit(pathname, ".tmp", 1)[0]
 
84
    if os.path.exists(final_pathname):
 
85
        shutil.rmtree(final_pathname)
 
86
    os.rename(pathname, final_pathname)
 
87