aboutsummaryrefslogtreecommitdiff
blob: e749178f8853847f99abd4317de4caf09a891c0b (plain)
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
# -*- coding: utf-8 -*-

"""
    installed.py
    ~~~~~~~~~~~~

    This module implements a Python object with the register of g-octave
    installed packages, something like the portage's world file.

    :copyright: (c) 2010 by Rafael Goncalves Martins
    :license: GPL-2, see LICENSE for more details.
"""

import json

class Installed(object):
    
    # this will not be changed by the config file, because the user will
    # lost track of the installed packages if he touch in the location
    # of this file without move it.
    _file = '/var/cache/g-octave.json'
    
    def _load_json(self):
        content = {}
        try:
            with open(self._file) as fp:
                content = json.load(fp)
        except:
            return {}
        return content
    
    def _dump_json(self, content):
        try:
            with open(self._file, 'w') as fp:
                json.dump(content, fp)
        except:
            return False
        return True
            
    def do_install(self, package, version):
        packages = self._load_json()
        packages[package] = version
        if not self._dump_json(packages):
            raise RuntimeError('Failed to save JSON file.')
    
    def is_installed(self, package):
        packages = self._load_json()
        return package in packages
    
    def get_version(self, package):
        packages = self._load_json()
        return packages.get(package, None)