Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devbot/state.py
blob: beba83791b3bade42571e26836bb6e5a2b362255 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import json

from devbot import config

_BUILT_MODULES = "builtmodules"
_SYSTEM_CHECK = "syscheck"

def _get_state_path(name):
    return os.path.join(config.build_state_dir, "%s.json" % name)

def _load_state(name, default=None):
    state = default

    try:
        with open(_get_state_path(name)) as f:
            state = json.load(f)
    except IOError:
        pass

    return state

def _save_state(name, state):
    with open(_get_state_path(name), "w+") as f:
        json.dump(state, f, indent=4)
        f.write('\n')

def touch_built_module(module):
    built_modules = _load_state(_BUILT_MODULES, {})

    info = {"commit": module.get_commit_id()}
    built_modules[module.name] = info

    _save_state(_BUILT_MODULES, built_modules)

def remove_built_module(module):
    built_modules = _load_state(_BUILT_MODULES)

    if built_modules and module.name in built_modules:
        del built_modules[module.name]
        _save_state(_BUILT_MODULES, built_modules)

def check_built_module(module):
    built_modules = _load_state(_BUILT_MODULES, {})
    info = built_modules.get(module.name, {})
    return module.get_commit_id() == info.get("commit", None)

def get_last_system_check():
    system_check = _load_state(_SYSTEM_CHECK, {})
    return system_check.get("commit", None)

def touch_last_system_check():
    system_check = _load_state(_SYSTEM_CHECK, {})

    system_check["commit"] = config.get_commit_id()

    _save_state(_SYSTEM_CHECK, system_check)

def clean():
    _state = None

    print "Deleting state"

    try:
        for name in _BUILT_MODULES, _SYSTEM_CHECK:
            os.unlink(_get_state_path(name))
    except OSError:
        pass