~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/jobs/proof.py

  • Committer: Aaron Bentley
  • Date: 2013-02-12 18:57:02 UTC
  • mfrom: (149 charmworld)
  • mto: This revision was merged to the branch mainline in revision 150.
  • Revision ID: aaron@canonical.com-20130212185702-9gnf40ao17a7uw7v
Merged trunk into mongo-urls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# licensed under the GNU Affero General Public License version 3 (see
3
3
# the file LICENSE).
4
4
 
5
 
import contextlib
6
5
import logging
7
 
import os
8
 
import sys
9
6
 
10
 
import config
11
7
from config import PROOF_IN_QUEUE
12
8
from config import PROOF_OUT_QUEUE
13
9
from utils import get_queues
 
10
from worker import proof_charms
14
11
 
15
12
log = logging.getLogger("charm.proof")
16
13
 
17
14
 
18
 
@contextlib.contextmanager
19
 
def get_proof_lib(new_path):
20
 
    if new_path not in sys.path:
21
 
        sys.path.append(new_path)
22
 
    try:
23
 
        import lib.proof as prooflib
24
 
        yield prooflib
25
 
    except ImportError:
26
 
        yield None
27
 
    finally:
28
 
        if new_path in sys.path:
29
 
            sys.path.remove(new_path)
30
 
 
31
 
 
32
 
def proof_charm(charm, prooflib):
33
 
    proof = {}
34
 
    lint, exit_code = prooflib.run(charm['branch_dir'])
35
 
    for line in lint:
36
 
        if not ':' in line:
37
 
            continue
38
 
        level, msg = line.split(':', 1)
39
 
        if level == "W" and 'name' in msg:
40
 
            continue
41
 
        proof.setdefault(level.lower(), []).append(msg)
42
 
    charm['proof'] = proof
43
 
 
44
 
 
45
 
def run(in_queue, out_queue):
46
 
    if not os.path.isdir(config.CHARM_PROOF_PATH):
47
 
        err_msg = ("proof error before processing began: could not find "
48
 
                   "charm proof path.")
49
 
        log.exception(err_msg)
50
 
        log.exception("CHARM_PROOF_PATH: %s", config.CHARM_PROOF_PATH)
51
 
        log.exception("proof aborted.")
52
 
        return
53
 
    with get_proof_lib(config.CHARM_PROOF_PATH) as prooflib:
54
 
        if not prooflib:
55
 
            err_msg = ("proof error before processing began: could not "
56
 
                       "import charm proof lib.")
57
 
            log.exception(err_msg)
58
 
            log.exception("CHARM_PROOF_PATH: %s", config.CHARM_PROOF_PATH)
59
 
            log.exception("proof aborted.")
60
 
            return
61
 
        while 1:
62
 
            item = in_queue.next()
63
 
            if not item:
64
 
                return
65
 
            try:
66
 
                charm_data = item.payload
67
 
                log.info("Proofing charm %s", charm_data["branch_spec"])
68
 
                proof_charm(charm_data, prooflib)
69
 
            except Exception as e:
70
 
                err = "proof error on %s: %s" % (
71
 
                      charm_data['branch_spec'], str(e))
72
 
                charm_data['proof'] = {'e': [err]}
73
 
                log.exception(err)
74
 
            finally:
75
 
                # Remove the job from the input queue.
76
 
                item.complete()
77
 
            out_queue.put(charm_data)
78
 
 
79
 
 
80
15
if __name__ == '__main__':
81
16
    logging.basicConfig(
82
17
        level=logging.WARNING,
83
18
        format="%(asctime)s: %(name)s@%(levelname)s: %(message)s")
84
19
    in_queue, out_queue = get_queues(PROOF_IN_QUEUE, PROOF_OUT_QUEUE)
85
 
    run(in_queue, out_queue)
 
20
    proof_charms(in_queue, out_queue)