Conditions | 13 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 setup_pack_virtualenv() 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 |
||
43 | def setup_pack_virtualenv(pack_name, update=False, logger=None, include_pip=True, |
||
44 | include_setuptools=True, include_wheel=True, proxy_config=None): |
||
45 | |||
46 | """ |
||
47 | Setup virtual environment for the provided pack. |
||
48 | |||
49 | :param pack_name: Name of the pack to setup the virtualenv for. |
||
50 | :type pack_name: ``str`` |
||
51 | |||
52 | :param update: True to update dependencies inside the virtual environment. |
||
53 | :type update: ``bool`` |
||
54 | |||
55 | :param logger: Optional logger instance to use. If not provided it defaults to the module |
||
56 | level logger. |
||
57 | """ |
||
58 | logger = logger or LOG |
||
59 | three = False # TODO: Change default Python version to a global setting |
||
60 | if not re.match(PACK_REF_WHITELIST_REGEX, pack_name): |
||
61 | raise ValueError('Invalid pack name "%s"' % (pack_name)) |
||
62 | |||
63 | base_virtualenvs_path = os.path.join(cfg.CONF.system.base_path, 'virtualenvs/') |
||
64 | virtualenv_path = os.path.join(base_virtualenvs_path, quote_unix(pack_name)) |
||
65 | |||
66 | # Ensure pack directory exists in one of the search paths |
||
67 | pack_path = get_pack_directory(pack_name=pack_name) |
||
68 | |||
69 | logger.debug('Setting up virtualenv for pack "%s" (%s)' % (pack_name, pack_path)) |
||
70 | |||
71 | if not pack_path: |
||
72 | packs_base_paths = get_packs_base_paths() |
||
73 | search_paths = ', '.join(packs_base_paths) |
||
74 | msg = 'Pack "%s" is not installed. Looked in: %s' % (pack_name, search_paths) |
||
75 | raise Exception(msg) |
||
76 | |||
77 | try: |
||
78 | pack_meta = get_pack_metadata(pack_path) |
||
79 | has_pack_meta = True |
||
80 | except ValueError: |
||
81 | # Pack is missing meta file |
||
82 | has_pack_meta = False |
||
83 | |||
84 | if has_pack_meta: |
||
85 | logger.debug('Checking pack specific Python version.') |
||
86 | if 'system' in pack_meta.keys() and 'python3' in pack_meta['system'].keys(): |
||
87 | three = bool(pack_meta['system']['python3']) |
||
88 | logger.debug('Using Python %s in virtualenv' % (3 if three else 2)) |
||
89 | |||
90 | # 1. Create virtualenv if it doesn't exist |
||
91 | if not update or not os.path.exists(virtualenv_path): |
||
92 | # 0. Delete virtual environment if it exists |
||
93 | remove_virtualenv(virtualenv_path=virtualenv_path, logger=logger) |
||
94 | |||
95 | # 1. Create virtual environment |
||
96 | logger.debug('Creating virtualenv for pack "%s" in "%s"' % (pack_name, virtualenv_path)) |
||
97 | create_virtualenv(virtualenv_path=virtualenv_path, logger=logger, include_pip=include_pip, |
||
98 | include_setuptools=include_setuptools, include_wheel=include_wheel, |
||
99 | three=three) |
||
100 | |||
101 | # 2. Install base requirements which are common to all the packs |
||
102 | logger.debug('Installing base requirements') |
||
103 | for requirement in BASE_PACK_REQUIREMENTS: |
||
104 | install_requirement(virtualenv_path=virtualenv_path, requirement=requirement, |
||
105 | proxy_config=proxy_config, logger=logger) |
||
106 | |||
107 | # 3. Install pack-specific requirements |
||
108 | requirements_file_path = os.path.join(pack_path, 'requirements.txt') |
||
109 | has_requirements = os.path.isfile(requirements_file_path) |
||
110 | |||
111 | if has_requirements: |
||
112 | logger.debug('Installing pack specific requirements from "%s"' % |
||
113 | (requirements_file_path)) |
||
114 | install_requirements(virtualenv_path=virtualenv_path, |
||
115 | requirements_file_path=requirements_file_path, |
||
116 | proxy_config=proxy_config, |
||
117 | logger=logger) |
||
118 | else: |
||
119 | logger.debug('No pack specific requirements found') |
||
120 | |||
121 | action = 'updated' if update else 'created' |
||
122 | logger.debug('Virtualenv for pack "%s" successfully %s in "%s"' % |
||
123 | (pack_name, action, virtualenv_path)) |
||
124 | |||
298 |