| Conditions | 12 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like GetInstalled.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 |
||
| 29 | def run(self, pack): |
||
| 30 | """ |
||
| 31 | :param pack: Installed Pack Name to get info about |
||
| 32 | :type pack: ``str`` |
||
| 33 | """ |
||
| 34 | packs_base_paths = get_packs_base_paths() |
||
| 35 | |||
| 36 | pack_path = None |
||
| 37 | metadata_file = None |
||
| 38 | for packs_base_path in packs_base_paths: |
||
| 39 | pack_path = os.path.join(packs_base_path, pack) |
||
| 40 | pack_yaml_path = os.path.join(pack_path, MANIFEST_FILE_NAME) |
||
| 41 | |||
| 42 | if os.path.isfile(pack_yaml_path): |
||
| 43 | metadata_file = pack_yaml_path |
||
| 44 | break |
||
| 45 | |||
| 46 | # Pack doesn't exist, finish execution normally with empty metadata |
||
| 47 | if not os.path.isdir(pack_path): |
||
| 48 | return { |
||
| 49 | 'pack': None, |
||
| 50 | 'git_status': None |
||
| 51 | } |
||
| 52 | |||
| 53 | if not metadata_file: |
||
| 54 | error = ('Pack "%s" doesn\'t contain pack.yaml file.' % (pack)) |
||
| 55 | raise Exception(error) |
||
| 56 | |||
| 57 | try: |
||
| 58 | details = self._parse_yaml_file(metadata_file) |
||
| 59 | except Exception as e: |
||
| 60 | error = ('Pack "%s" doesn\'t contain a valid pack.yaml file: %s' % (pack, str(e))) |
||
| 61 | raise Exception(error) |
||
| 62 | |||
| 63 | try: |
||
| 64 | repo = Repo(pack_path) |
||
| 65 | git_status = "Status:\n%s\n\nRemotes:\n%s" % ( |
||
| 66 | repo.git.status().split('\n')[0], |
||
| 67 | "\n".join([remote.url for remote in repo.remotes]) |
||
| 68 | ) |
||
| 69 | |||
| 70 | ahead_behind = repo.git.rev_list( |
||
| 71 | '--left-right', '--count', 'HEAD...origin/master' |
||
| 72 | ).split() |
||
| 73 | # Dear god. |
||
| 74 | if ahead_behind != [u'0', u'0']: |
||
| 75 | git_status += "\n\n" |
||
| 76 | git_status += "%s commits ahead " if ahead_behind[0] != u'0' else "" |
||
| 77 | git_status += "and " if u'0' not in ahead_behind else "" |
||
| 78 | git_status += "%s commits behind " if ahead_behind[1] != u'0' else "" |
||
| 79 | git_status += "origin/master." |
||
| 80 | except InvalidGitRepositoryError: |
||
| 81 | git_status = None |
||
| 82 | |||
| 83 | return { |
||
| 84 | 'pack': details, |
||
| 85 | 'git_status': git_status |
||
| 86 | } |
||
| 92 |