155def checkout_engine(engine_path, repo_root, recipes_cfg_path):
156 """Checks out the recipe_engine repo pinned in recipes.cfg.
157
158 Returns the path to the recipe engine repo.
159 """
160 dep, recipes_path = parse(repo_root, recipes_cfg_path)
161 if dep is None:
162
163 return os.path.join(repo_root, recipes_path)
164
165 url = dep.url
166
167 if not engine_path and url.startswith('file://'):
168 engine_path = urlparse.urlparse(url).path
169
170 if not engine_path:
171 revision = dep.revision
172 branch = dep.branch
173
174
175 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
176
177
178 _git_check_call(['init', engine_path], stdout=subprocess.DEVNULL)
179
180 try:
181 _git_check_call(['rev-parse', '--verify', f'{revision}^{{commit}}'],
182 cwd=engine_path,
183 stdout=subprocess.DEVNULL,
184 stderr=subprocess.DEVNULL)
185 except subprocess.CalledProcessError:
186 _git_check_call(['fetch', '--quiet', url, branch],
187 cwd=engine_path,
188 stdout=subprocess.DEVNULL)
189
190 try:
191 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
192 except subprocess.CalledProcessError:
193 index_lock = os.path.join(engine_path, '.git', 'index.lock')
194 try:
195 os.remove(index_lock)
196 except OSError as exc:
197 if exc.errno != errno.ENOENT:
198 logging.warning('failed to remove %r, reset will fail: %s',
199 index_lock, exc)
200 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
201
202
203
204 _git_check_call(['clean', '-qxf'], cwd=engine_path)
205
206 return engine_path
207
208