vcs.py (1957B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import os 6 import subprocess 7 8 9 class Mercurial: 10 def __init__(self, repo_root): 11 self.root = os.path.abspath(repo_root) 12 self.hg = Mercurial.get_func(repo_root) 13 14 @staticmethod 15 def get_func(repo_root): 16 def hg(cmd, *args): 17 full_cmd = ["hg", cmd] + list(args) 18 return subprocess.check_output(full_cmd, cwd=repo_root) 19 # TODO: Test on Windows. 20 21 return hg 22 23 @staticmethod 24 def is_hg_repo(repo_root): 25 try: 26 with open(os.devnull, "w") as devnull: 27 subprocess.check_call( 28 ["hg", "root"], cwd=repo_root, stdout=devnull, stderr=devnull 29 ) 30 except subprocess.CalledProcessError: 31 return False 32 except OSError: 33 return False 34 # TODO: Test on windows 35 return True 36 37 38 class Git: 39 def __init__(self, repo_root, url_base): 40 self.root = os.path.abspath(repo_root) 41 self.git = Git.get_func(repo_root) 42 43 @staticmethod 44 def get_func(repo_root): 45 def git(cmd, *args): 46 full_cmd = ["git", cmd] + list(args) 47 return subprocess.check_output(full_cmd, cwd=repo_root) 48 # TODO: Test on Windows. 49 50 return git 51 52 @staticmethod 53 def is_git_repo(repo_root): 54 try: 55 with open(os.devnull, "w") as devnull: 56 subprocess.check_call( 57 ["git", "rev-parse", "--show-cdup"], 58 cwd=repo_root, 59 stdout=devnull, 60 stderr=devnull, 61 ) 62 except subprocess.CalledProcessError: 63 return False 64 except OSError: 65 return False 66 # TODO: Test on windows 67 return True