| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generate fake repositories for testing.""" | 6 """Generate fake repositories for testing.""" |
| 7 | 7 |
| 8 import atexit | 8 import atexit |
| 9 import errno | 9 import errno |
| 10 import logging | 10 import logging |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 # i.e. revisions are 1-based. | 239 # i.e. revisions are 1-based. |
| 240 self.svn_revs = [None] | 240 self.svn_revs = [None] |
| 241 # Format is { repo: [ None, (hash, tree), (hash, tree), ... ], ... } | 241 # Format is { repo: [ None, (hash, tree), (hash, tree), ... ], ... } |
| 242 # so reference looks like self.git_hashes[repo][rev][0] for hash and | 242 # so reference looks like self.git_hashes[repo][rev][0] for hash and |
| 243 # self.git_hashes[repo][rev][1] for it's tree snapshot. | 243 # self.git_hashes[repo][rev][1] for it's tree snapshot. |
| 244 # For consistency with self.svn_revs, it is 1-based too. | 244 # For consistency with self.svn_revs, it is 1-based too. |
| 245 self.git_hashes = {} | 245 self.git_hashes = {} |
| 246 self.svnserve = None | 246 self.svnserve = None |
| 247 self.gitdaemon = None | 247 self.gitdaemon = None |
| 248 self.common_init = False | 248 self.common_init = False |
| 249 self.repos_dir = None |
| 250 self.git_root = None |
| 251 self.svn_checkout = None |
| 252 self.svn_repo = None |
| 253 self.git_dirty = False |
| 254 self.svn_dirty = False |
| 249 | 255 |
| 250 def trial_dir(self): | 256 def trial_dir(self): |
| 251 if not self.TRIAL_DIR: | 257 if not self.TRIAL_DIR: |
| 252 self.TRIAL_DIR = os.path.join( | 258 self.TRIAL_DIR = os.path.join( |
| 253 os.path.dirname(os.path.abspath(__file__)), '_trial') | 259 os.path.dirname(os.path.abspath(__file__)), '_trial') |
| 254 return self.TRIAL_DIR | 260 return self.TRIAL_DIR |
| 255 | 261 |
| 256 def setUp(self): | 262 def setUp(self): |
| 257 """All late initialization comes here. | 263 """All late initialization comes here. |
| 258 | 264 |
| 259 Note that it deletes all trial_dir() and not only repos_dir.""" | 265 Note that it deletes all trial_dir() and not only repos_dir. |
| 266 """ |
| 267 self.cleanup_dirt() |
| 260 if not self.common_init: | 268 if not self.common_init: |
| 261 self.common_init = True | 269 self.common_init = True |
| 262 self.repos_dir = os.path.join(self.trial_dir(), 'repos') | 270 self.repos_dir = os.path.join(self.trial_dir(), 'repos') |
| 263 self.git_root = join(self.repos_dir, 'git') | 271 self.git_root = join(self.repos_dir, 'git') |
| 264 self.svn_root = join(self.repos_dir, 'svn_checkout') | 272 self.svn_checkout = join(self.repos_dir, 'svn_checkout') |
| 273 self.svn_repo = join(self.repos_dir, 'svn') |
| 265 addKill() | 274 addKill() |
| 266 rmtree(self.trial_dir()) | 275 rmtree(self.trial_dir()) |
| 267 os.makedirs(self.repos_dir) | 276 os.makedirs(self.repos_dir) |
| 268 atexit.register(self.tearDown) | 277 atexit.register(self.tearDown) |
| 269 | 278 |
| 279 def cleanup_dirt(self): |
| 280 """For each dirty repository, regenerate it.""" |
| 281 if self.svnserve and self.svn_dirty: |
| 282 logging.debug('Killing svnserve pid %s' % self.svnserve.pid) |
| 283 self.svnserve.kill() |
| 284 self.svnserve = None |
| 285 if not self.SHOULD_LEAK: |
| 286 logging.debug('Removing dirty %s' % self.svn_repo) |
| 287 rmtree(self.svn_repo) |
| 288 logging.debug('Removing dirty %s' % self.svn_checkout) |
| 289 rmtree(self.svn_checkout) |
| 290 else: |
| 291 logging.warning('Using both leaking checkout and dirty checkout') |
| 292 if self.gitdaemon and self.git_dirty: |
| 293 logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) |
| 294 self.gitdaemon.kill() |
| 295 self.gitdaemon = None |
| 296 if not self.SHOULD_LEAK: |
| 297 logging.debug('Removing dirty %s' % self.git_root) |
| 298 rmtree(self.git_root) |
| 299 else: |
| 300 logging.warning('Using both leaking checkout and dirty checkout') |
| 301 |
| 270 def tearDown(self): | 302 def tearDown(self): |
| 271 if self.svnserve: | 303 if self.svnserve: |
| 272 logging.debug('Killing svnserve pid %s' % self.svnserve.pid) | 304 logging.debug('Killing svnserve pid %s' % self.svnserve.pid) |
| 273 self.svnserve.kill() | 305 self.svnserve.kill() |
| 274 self.svnserve = None | 306 self.svnserve = None |
| 275 if self.gitdaemon: | 307 if self.gitdaemon: |
| 276 logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) | 308 logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) |
| 277 self.gitdaemon.kill() | 309 self.gitdaemon.kill() |
| 278 self.gitdaemon = None | 310 self.gitdaemon = None |
| 279 if not self.SHOULD_LEAK: | 311 if not self.SHOULD_LEAK: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 292 p = os.sep.join([root] + k_arr[:-1]) | 324 p = os.sep.join([root] + k_arr[:-1]) |
| 293 if not os.path.isdir(p): | 325 if not os.path.isdir(p): |
| 294 os.makedirs(p) | 326 os.makedirs(p) |
| 295 if v is None: | 327 if v is None: |
| 296 os.remove(join(root, k)) | 328 os.remove(join(root, k)) |
| 297 else: | 329 else: |
| 298 write(join(root, k), v) | 330 write(join(root, k), v) |
| 299 | 331 |
| 300 def setUpSVN(self): | 332 def setUpSVN(self): |
| 301 """Creates subversion repositories and start the servers.""" | 333 """Creates subversion repositories and start the servers.""" |
| 334 self.setUp() |
| 302 if self.svnserve: | 335 if self.svnserve: |
| 303 return True | 336 return True |
| 304 self.setUp() | |
| 305 root = join(self.repos_dir, 'svn') | |
| 306 try: | 337 try: |
| 307 check_call(['svnadmin', 'create', root]) | 338 check_call(['svnadmin', 'create', self.svn_repo]) |
| 308 except OSError: | 339 except OSError: |
| 309 return False | 340 return False |
| 310 write(join(root, 'conf', 'svnserve.conf'), | 341 write(join(self.svn_repo, 'conf', 'svnserve.conf'), |
| 311 '[general]\n' | 342 '[general]\n' |
| 312 'anon-access = read\n' | 343 'anon-access = read\n' |
| 313 'auth-access = write\n' | 344 'auth-access = write\n' |
| 314 'password-db = passwd\n') | 345 'password-db = passwd\n') |
| 315 text = '[users]\n' | 346 text = '[users]\n' |
| 316 text += ''.join('%s = %s\n' % (usr, pwd) for usr, pwd in self.USERS) | 347 text += ''.join('%s = %s\n' % (usr, pwd) for usr, pwd in self.USERS) |
| 317 write(join(root, 'conf', 'passwd'), text) | 348 write(join(self.svn_repo, 'conf', 'passwd'), text) |
| 318 | 349 |
| 319 # Start the daemon. | 350 # Start the daemon. |
| 320 cmd = ['svnserve', '-d', '--foreground', '-r', self.repos_dir] | 351 cmd = ['svnserve', '-d', '--foreground', '-r', self.repos_dir] |
| 321 if self.HOST == '127.0.0.1': | 352 if self.HOST == '127.0.0.1': |
| 322 cmd.append('--listen-host=127.0.0.1') | 353 cmd.append('--listen-host=127.0.0.1') |
| 323 self.svnserve = Popen(cmd, cwd=root) | 354 self.svnserve = Popen(cmd, cwd=self.svn_repo) |
| 324 self.populateSvn() | 355 self.populateSvn() |
| 356 self.svn_dirty = False |
| 325 return True | 357 return True |
| 326 | 358 |
| 327 def setUpGIT(self): | 359 def setUpGIT(self): |
| 328 """Creates git repositories and start the servers.""" | 360 """Creates git repositories and start the servers.""" |
| 361 self.setUp() |
| 329 if self.gitdaemon: | 362 if self.gitdaemon: |
| 330 return True | 363 return True |
| 331 self.setUp() | |
| 332 if sys.platform == 'win32': | 364 if sys.platform == 'win32': |
| 333 return False | 365 return False |
| 334 for repo in ['repo_%d' % r for r in range(1, self.NB_GIT_REPOS + 1)]: | 366 for repo in ['repo_%d' % r for r in range(1, self.NB_GIT_REPOS + 1)]: |
| 335 check_call(['git', 'init', '-q', join(self.git_root, repo)]) | 367 check_call(['git', 'init', '-q', join(self.git_root, repo)]) |
| 336 self.git_hashes[repo] = [None] | 368 self.git_hashes[repo] = [None] |
| 337 self.populateGit() | 369 self.populateGit() |
| 338 # Start the daemon. | 370 # Start the daemon. |
| 339 cmd = ['git', 'daemon', '--export-all', '--base-path=' + self.repos_dir] | 371 cmd = ['git', 'daemon', '--export-all', '--base-path=' + self.repos_dir] |
| 340 if self.HOST == '127.0.0.1': | 372 if self.HOST == '127.0.0.1': |
| 341 cmd.append('--listen=127.0.0.1') | 373 cmd.append('--listen=127.0.0.1') |
| 342 logging.debug(cmd) | 374 logging.debug(cmd) |
| 343 self.gitdaemon = Popen(cmd, cwd=self.repos_dir) | 375 self.gitdaemon = Popen(cmd, cwd=self.repos_dir) |
| 376 self.git_dirty = False |
| 344 return True | 377 return True |
| 345 | 378 |
| 346 def _commit_svn(self, tree): | 379 def _commit_svn(self, tree): |
| 347 self._genTree(self.svn_root, tree) | 380 self._genTree(self.svn_checkout, tree) |
| 348 commit_svn(self.svn_root, self.USERS[0][0], self.USERS[0][1]) | 381 commit_svn(self.svn_checkout, self.USERS[0][0], self.USERS[0][1]) |
| 349 if self.svn_revs and self.svn_revs[-1]: | 382 if self.svn_revs and self.svn_revs[-1]: |
| 350 new_tree = self.svn_revs[-1].copy() | 383 new_tree = self.svn_revs[-1].copy() |
| 351 new_tree.update(tree) | 384 new_tree.update(tree) |
| 352 else: | 385 else: |
| 353 new_tree = tree.copy() | 386 new_tree = tree.copy() |
| 354 self.svn_revs.append(new_tree) | 387 self.svn_revs.append(new_tree) |
| 355 | 388 |
| 356 def _commit_git(self, repo, tree): | 389 def _commit_git(self, repo, tree): |
| 357 repo_root = join(self.git_root, repo) | 390 repo_root = join(self.git_root, repo) |
| 358 self._genTree(repo_root, tree) | 391 self._genTree(repo_root, tree) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 371 raise NotImplementedError() | 404 raise NotImplementedError() |
| 372 | 405 |
| 373 | 406 |
| 374 class FakeRepos(FakeReposBase): | 407 class FakeRepos(FakeReposBase): |
| 375 """Implements populateSvn() and populateGit().""" | 408 """Implements populateSvn() and populateGit().""" |
| 376 NB_GIT_REPOS = 4 | 409 NB_GIT_REPOS = 4 |
| 377 | 410 |
| 378 def populateSvn(self): | 411 def populateSvn(self): |
| 379 """Creates a few revisions of changes including DEPS files.""" | 412 """Creates a few revisions of changes including DEPS files.""" |
| 380 # Repos | 413 # Repos |
| 381 check_call(['svn', 'checkout', 'svn://127.0.0.1/svn', self.svn_root, '-q', | 414 check_call(['svn', 'checkout', 'svn://127.0.0.1/svn', self.svn_checkout, |
| 382 '--non-interactive', '--no-auth-cache', | 415 '-q', '--non-interactive', '--no-auth-cache', |
| 383 '--username', self.USERS[0][0], '--password', self.USERS[0][1]]) | 416 '--username', self.USERS[0][0], '--password', self.USERS[0][1]]) |
| 384 assert os.path.isdir(join(self.svn_root, '.svn')) | 417 assert os.path.isdir(join(self.svn_checkout, '.svn')) |
| 385 def file_system(rev, DEPS): | 418 def file_system(rev, DEPS): |
| 386 fs = { | 419 fs = { |
| 387 'origin': 'svn@%(rev)d\n', | 420 'origin': 'svn@%(rev)d\n', |
| 388 'trunk/origin': 'svn/trunk@%(rev)d\n', | 421 'trunk/origin': 'svn/trunk@%(rev)d\n', |
| 389 'trunk/src/origin': 'svn/trunk/src@%(rev)d\n', | 422 'trunk/src/origin': 'svn/trunk/src@%(rev)d\n', |
| 390 'trunk/src/third_party/origin': 'svn/trunk/src/third_party@%(rev)d\n', | 423 'trunk/src/third_party/origin': 'svn/trunk/src/third_party@%(rev)d\n', |
| 391 'trunk/other/origin': 'src/trunk/other@%(rev)d\n', | 424 'trunk/other/origin': 'src/trunk/other@%(rev)d\n', |
| 392 'trunk/third_party/origin': 'svn/trunk/third_party@%(rev)d\n', | 425 'trunk/third_party/origin': 'svn/trunk/third_party@%(rev)d\n', |
| 393 'trunk/third_party/foo/origin': 'svn/trunk/third_party/foo@%(rev)d\n', | 426 'trunk/third_party/foo/origin': 'svn/trunk/third_party/foo@%(rev)d\n', |
| 394 'trunk/third_party/prout/origin': 'svn/trunk/third_party/foo@%(rev)d\n', | 427 'trunk/third_party/prout/origin': 'svn/trunk/third_party/foo@%(rev)d\n', |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 fake.setUp() | 720 fake.setUp() |
| 688 print('Fake setup, press enter to quit or Ctrl-C to keep the checkouts.') | 721 print('Fake setup, press enter to quit or Ctrl-C to keep the checkouts.') |
| 689 sys.stdin.readline() | 722 sys.stdin.readline() |
| 690 except KeyboardInterrupt: | 723 except KeyboardInterrupt: |
| 691 fake.SHOULD_LEAK = True | 724 fake.SHOULD_LEAK = True |
| 692 return 0 | 725 return 0 |
| 693 | 726 |
| 694 | 727 |
| 695 # Kind of hack. | 728 # Kind of hack. |
| 696 if '-l' in sys.argv: | 729 if '-l' in sys.argv: |
| 697 FakeRepos.SHOULD_LEAK = True | 730 FakeReposBase.SHOULD_LEAK = True |
| 731 print 'Leaking!' |
| 698 sys.argv.remove('-l') | 732 sys.argv.remove('-l') |
| 699 | 733 |
| 700 | 734 |
| 701 if __name__ == '__main__': | 735 if __name__ == '__main__': |
| 702 sys.exit(main(sys.argv)) | 736 sys.exit(main(sys.argv)) |
| OLD | NEW |