OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Generate fake repositories for testing.""" |
| 7 |
| 8 import os |
| 9 import shutil |
| 10 import subprocess |
| 11 import sys |
| 12 |
| 13 |
| 14 def addKill(): |
| 15 """Add kill() method to subprocess.Popen for python <2.6""" |
| 16 if getattr(subprocess.Popen, 'kill', None): |
| 17 return |
| 18 if sys.platform.startswith('win'): |
| 19 def kill_win(process): |
| 20 import win32process |
| 21 return win32process.TerminateProcess(process._handle, -1) |
| 22 subprocess.kill = kill_win |
| 23 else: |
| 24 def kill_nix(process): |
| 25 import signal |
| 26 return os.kill(process.pid, signal.SIGKILL) |
| 27 subprocess.kill = kill_nix |
| 28 |
| 29 |
| 30 def rmtree(path): |
| 31 """Delete a directory.""" |
| 32 if os.path.exists(path): |
| 33 shutil.rmtree(path) |
| 34 |
| 35 |
| 36 def write(path, content): |
| 37 f = open(path, 'wb') |
| 38 f.write(content) |
| 39 f.close() |
| 40 |
| 41 |
| 42 class FakeRepos(object): |
| 43 def __init__(self, trial_dir, leak, local_only): |
| 44 self.trial_dir = trial_dir |
| 45 self.repos_dir = os.path.join(self.trial_dir, 'repos') |
| 46 self.leak = leak |
| 47 self.local_only = local_only |
| 48 self.svnserve = [] |
| 49 self.gitdaemon = [] |
| 50 addKill() |
| 51 rmtree(self.trial_dir) |
| 52 os.mkdir(self.trial_dir) |
| 53 os.mkdir(self.repos_dir) |
| 54 |
| 55 def setUp(self): |
| 56 self.setUpSVN() |
| 57 self.setUpGIT() |
| 58 |
| 59 def tearDown(self): |
| 60 for i in self.svnserve: |
| 61 i.kill() |
| 62 for i in self.gitdaemon: |
| 63 i.kill() |
| 64 if not self.leak: |
| 65 rmtree(self.trial_dir) |
| 66 |
| 67 def setUpSVN(self): |
| 68 """Creates subversion repositories and start the servers.""" |
| 69 assert not self.svnserve |
| 70 join = os.path.join |
| 71 root = join(self.repos_dir, 'svn') |
| 72 rmtree(root) |
| 73 subprocess.check_call(['svnadmin', 'create', root]) |
| 74 write(join(root, 'conf', 'svnserve.conf'), |
| 75 '[general]\n' |
| 76 'anon-access = read\n' |
| 77 'auth-access = write\n' |
| 78 'password-db = passwd\n') |
| 79 write(join(root, 'conf', 'passwd'), |
| 80 '[users]\n' |
| 81 'user1 = foo\n' |
| 82 'user2 = bar\n') |
| 83 |
| 84 # Repos |
| 85 repo = join(self.repos_dir, 'svn_import') |
| 86 rmtree(repo) |
| 87 os.mkdir(repo) |
| 88 os.mkdir(join(repo, 'trunk')) |
| 89 os.mkdir(join(repo, 'trunk', 'src')) |
| 90 write(join(repo, 'trunk', 'src', 'DEPS'), """ |
| 91 # Smoke test DEPS file. |
| 92 # Many DEPS functionalities need to be tested: |
| 93 # Var |
| 94 # File |
| 95 # From |
| 96 # deps_os |
| 97 # hooks |
| 98 # use_relative_paths |
| 99 # |
| 100 # Types of dependencies: |
| 101 # Relative urls |
| 102 # Full urls |
| 103 # svn |
| 104 # git |
| 105 |
| 106 deps = { |
| 107 'src/other': 'svn://%(host)s/svn/trunk/other', |
| 108 'src/third_party': '/trunk/third_party', |
| 109 } |
| 110 |
| 111 deps_os = { |
| 112 'mac': 'repo_4' |
| 113 } |
| 114 """ % { |
| 115 'host': 'localhost', |
| 116 }) |
| 117 write(join(repo, 'trunk', 'src', 'origin'), "svn/trunk/src") |
| 118 os.mkdir(join(repo, 'trunk', 'other')) |
| 119 write(join(repo, 'trunk', 'other', 'origin'), "svn/trunk/other") |
| 120 os.mkdir(join(repo, 'trunk', 'third_party')) |
| 121 write(join(repo, 'trunk', 'third_party', 'origin'), "svn/trunk/third_party") |
| 122 |
| 123 # Start the daemon. |
| 124 cmd = ['svnserve', '-d', '--foreground', '-r', self.repos_dir] |
| 125 if self.local_only: |
| 126 cmd.append('--listen-host=127.0.0.1') |
| 127 self.svnserve.append(subprocess.Popen(cmd, cwd=root)) |
| 128 |
| 129 # Import the repo. |
| 130 subprocess.check_call(['svn', 'import', repo, |
| 131 'svn://127.0.0.1/svn', '-m', 'foo', '-q', |
| 132 '--no-auto-props', '--non-interactive', '--no-auth-cache', |
| 133 '--username', 'user1', '--password', 'foo']) |
| 134 |
| 135 def setUpGIT(self): |
| 136 """Creates git repositories and start the servers.""" |
| 137 assert not self.gitdaemon |
| 138 join = os.path.join |
| 139 root = join(self.repos_dir, 'git') |
| 140 rmtree(root) |
| 141 os.mkdir(root) |
| 142 # Repo 1 |
| 143 repo = join(root, 'repo_1') |
| 144 subprocess.check_call(['git', 'init', '-q', repo]) |
| 145 write(join(repo, 'DEPS'), """ |
| 146 # Smoke test DEPS file. |
| 147 # Many DEPS functionalities need to be tested: |
| 148 # Var |
| 149 # File |
| 150 # From |
| 151 # deps_os |
| 152 # hooks |
| 153 # use_relative_paths |
| 154 # |
| 155 # Types of dependencies: |
| 156 # Relative urls |
| 157 # Full urls |
| 158 # svn |
| 159 # git |
| 160 |
| 161 deps = { |
| 162 'repo2': 'git://%(host)s/git/repo_2', |
| 163 'repo2/repo3': '/repo_3', |
| 164 } |
| 165 |
| 166 deps_os = { |
| 167 'mac': 'repo_4' |
| 168 } |
| 169 """ % { |
| 170 'host': 'localhost', |
| 171 }) |
| 172 write(join(repo, 'origin'), "git/repo_1") |
| 173 subprocess.check_call(['git', 'add', '-A', '-f'], cwd=repo) |
| 174 subprocess.check_call(['git', 'commit', '-q', '-m', 'foo'], cwd=repo) |
| 175 |
| 176 # Repo 2 |
| 177 repo = join(root, 'repo_2') |
| 178 subprocess.check_call(['git', 'init', '-q', repo]) |
| 179 write(join(repo, 'origin'), "git/repo_2") |
| 180 subprocess.check_call(['git', 'add', '-A', '-f'], cwd=repo) |
| 181 subprocess.check_call(['git', 'commit', '-q', '-m', 'foo'], cwd=repo) |
| 182 |
| 183 # Repo 3 |
| 184 repo = join(root, 'repo_3') |
| 185 subprocess.check_call(['git', 'init', '-q', repo]) |
| 186 write(join(repo, 'origin'), "git/repo_3") |
| 187 subprocess.check_call(['git', 'add', '-A', '-f'], cwd=repo) |
| 188 subprocess.check_call(['git', 'commit', '-q', '-m', 'foo'], cwd=repo) |
| 189 |
| 190 # Start the daemon. |
| 191 cmd = ['git', 'daemon', '--export-all', '--base-path=' + self.repos_dir] |
| 192 if self.local_only: |
| 193 cmd.append('--listen=127.0.0.1') |
| 194 self.gitdaemon.append(subprocess.Popen(cmd, cwd=self.repos_dir, |
| 195 stderr=subprocess.PIPE)) |
| 196 |
| 197 if __name__ == '__main__': |
| 198 fake = FakeRepos(os.path.dirname(os.path.abspath(__file__)), False) |
| 199 try: |
| 200 fake.setUp() |
| 201 sys.stdin.readline() |
| 202 finally: |
| 203 fake.tearDown() |
OLD | NEW |