Completed
Pull Request — master (#2982)
by Edward
05:27
created

LookupLocal.run()   F

Complexity

Conditions 11

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
dl 0
loc 44
rs 3.1764
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like LookupLocal.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
18
from git.repo import Repo
19
from gitdb.exc import InvalidGitRepositoryError
20
21
from st2common.runners.base_action import Action
22
from st2common.content.utils import get_packs_base_paths
23
24
MANIFEST_FILE = 'pack.yaml'
25
26
27
class LookupLocal(Action):
28
    def run(self, pack):
29
        packs_base_paths = get_packs_base_paths()
30
31
        metadata_file = None
32
        for packs_base_path in packs_base_paths:
33
            pack_path = os.path.join(packs_base_path, pack)
34
            pack_yaml_path = os.path.join(pack_path, MANIFEST_FILE)
35
36
            if os.path.isfile(pack_yaml_path):
37
                metadata_file = pack_yaml_path
38
                break
39
40
        if not metadata_file:
41
            error = ('Pack "%s" doesn\'t exist or it doesn\'t contain pack.yaml.' % (pack))
42
            raise Exception(error)
43
44
        try:
45
            details = self._parse_yaml_file(metadata_file)
46
        except Exception as e:
47
            error = ('Pack "%s" doesn\'t contain a valid pack.yaml file: %s' % (pack, str(e)))
48
            raise Exception(error)
49
50
        try:
51
            repo = Repo(pack_path)
52
            git_status = "Status:\n%s\n\nRemotes:\n%s" % (
53
                repo.git.status().split('\n')[0],
54
                "\n".join([remote.url for remote in repo.remotes])
55
            )
56
57
            ahead_behind = repo.git.rev_list(
58
                '--left-right', '--count', 'HEAD...origin/master'
59
            ).split()
60
            # Dear god.
61
            if ahead_behind != [u'0', u'0']:
62
                git_status += "\n\n"
63
                git_status += "%s commits ahead " if ahead_behind[0] != u'0' else ""
64
                git_status += "and " if u'0' not in ahead_behind else ""
65
                git_status += "%s commits behind " if ahead_behind[1] != u'0' else ""
66
                git_status += "origin/master."
67
        except InvalidGitRepositoryError:
68
            git_status = None
69
70
        return {'pack': details,
71
                'git_status': git_status}
72
73
    def _parse_yaml_file(self, file_path):
74
        with open(file_path) as data_file:
75
            # details = yaml.load(data_file)
76
            # You know what? We'll just output yaml, it's pretty as it is.
77
            details = data_file.read()
78
        return details
79