OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 datetime | 9 import datetime |
10 import errno | 10 import errno |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 cwd=repo) | 81 cwd=repo) |
82 match = re.search(r'(\d+)', out) | 82 match = re.search(r'(\d+)', out) |
83 if not match: | 83 if not match: |
84 raise Exception('Commit failed', out) | 84 raise Exception('Commit failed', out) |
85 rev = match.group(1) | 85 rev = match.group(1) |
86 status = subprocess2.check_output(['svn', 'status'], cwd=repo) | 86 status = subprocess2.check_output(['svn', 'status'], cwd=repo) |
87 assert len(status) == 0, status | 87 assert len(status) == 0, status |
88 logging.debug('At revision %s' % rev) | 88 logging.debug('At revision %s' % rev) |
89 return rev | 89 return rev |
90 | 90 |
| 91 def update_ref_git(repo, ref, oldvalue, newvalue=None): |
| 92 args = ['git', 'update-ref', ref, oldvalue] |
| 93 if newvalue is not None: |
| 94 args.append(newvalue) |
| 95 subprocess2.check_call(args, cwd=repo) |
| 96 |
| 97 def checkout_git(repo, branch=None): |
| 98 """Checks out (creating, if necessary) the target branch.""" |
| 99 if branch is None: |
| 100 branch = 'master' |
| 101 returncode = subprocess2.call(['git', 'show-ref', '-q', branch], cwd=repo) |
| 102 if returncode == 0: |
| 103 subprocess2.check_call(['git', 'checkout', '-q', branch], cwd=repo) |
| 104 else: |
| 105 subprocess2.check_call(['git', 'checkout', '-q', '-B', branch], cwd=repo) |
| 106 return branch |
91 | 107 |
92 def commit_git(repo): | 108 def commit_git(repo): |
93 """Commits the changes and returns the new hash.""" | 109 """Commits the changes and returns the new hash.""" |
94 subprocess2.check_call(['git', 'add', '-A', '-f'], cwd=repo) | 110 subprocess2.check_call(['git', 'add', '-A', '-f'], cwd=repo) |
95 subprocess2.check_call(['git', 'commit', '-q', '--message', 'foo'], cwd=repo) | 111 subprocess2.check_call(['git', 'commit', '-q', '--message', 'foo'], cwd=repo) |
96 rev = subprocess2.check_output( | 112 rev = subprocess2.check_output( |
97 ['git', 'show-ref', '--head', 'HEAD'], cwd=repo).split(' ', 1)[0] | 113 ['git', 'show-ref', '--head', 'HEAD'], cwd=repo).split(' ', 1)[0] |
98 logging.debug('At revision %s' % rev) | 114 logging.debug('At revision %s' % rev) |
99 return rev | 115 return rev |
100 | 116 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 self.svn_revs.append(new_tree) | 417 self.svn_revs.append(new_tree) |
402 | 418 |
403 def _set_svn_commit_date(self, revision, date): | 419 def _set_svn_commit_date(self, revision, date): |
404 subprocess2.check_output( | 420 subprocess2.check_output( |
405 ['svn', 'propset', 'svn:date', '--revprop', '-r', revision, date, | 421 ['svn', 'propset', 'svn:date', '--revprop', '-r', revision, date, |
406 self.svn_base, | 422 self.svn_base, |
407 '--username', self.USERS[0][0], | 423 '--username', self.USERS[0][0], |
408 '--password', self.USERS[0][1], | 424 '--password', self.USERS[0][1], |
409 '--non-interactive']) | 425 '--non-interactive']) |
410 | 426 |
411 def _commit_git(self, repo, tree): | 427 def _update_ref_git(self, repo, ref, oldvalue, newvalue=None): |
412 repo_root = join(self.git_root, repo) | 428 repo_root = join(self.git_root, repo) |
| 429 update_ref_git(repo_root, ref, oldvalue, newvalue=newvalue) |
| 430 |
| 431 def _commit_git(self, repo, tree, branch=None): |
| 432 repo_root = join(self.git_root, repo) |
| 433 checkout_git(repo_root, branch) |
413 self._genTree(repo_root, tree) | 434 self._genTree(repo_root, tree) |
414 commit_hash = commit_git(repo_root) | 435 commit_hash = commit_git(repo_root) |
| 436 checkout_git(repo_root) |
415 if self.git_hashes[repo][-1]: | 437 if self.git_hashes[repo][-1]: |
416 new_tree = self.git_hashes[repo][-1][1].copy() | 438 new_tree = self.git_hashes[repo][-1][1].copy() |
417 new_tree.update(tree) | 439 new_tree.update(tree) |
418 else: | 440 else: |
419 new_tree = tree.copy() | 441 new_tree = tree.copy() |
420 self.git_hashes[repo].append((commit_hash, new_tree)) | 442 self.git_hashes[repo].append((commit_hash, new_tree)) |
421 | 443 |
422 def check_port_is_free(self, port): | 444 def check_port_is_free(self, port): |
423 sock = socket.socket() | 445 sock = socket.socket() |
424 try: | 446 try: |
425 sock.connect((self.host, port)) | 447 sock.connect((self.host, port)) |
426 # It worked, throw. | 448 # It worked, throw. |
427 assert False, '%d shouldn\'t be bound' % port | 449 assert False, '%d shouldn\'t be bound' % port |
428 except (socket.error, EnvironmentError): | 450 except (socket.error, EnvironmentError): |
429 pass | 451 pass |
430 finally: | 452 finally: |
431 sock.close() | 453 sock.close() |
432 | 454 |
433 def populateSvn(self): | 455 def populateSvn(self): |
434 raise NotImplementedError() | 456 raise NotImplementedError() |
435 | 457 |
436 def populateGit(self): | 458 def populateGit(self): |
437 raise NotImplementedError() | 459 raise NotImplementedError() |
438 | 460 |
439 | 461 |
440 class FakeRepos(FakeReposBase): | 462 class FakeRepos(FakeReposBase): |
441 """Implements populateSvn() and populateGit().""" | 463 """Implements populateSvn() and populateGit().""" |
442 NB_GIT_REPOS = 5 | 464 NB_GIT_REPOS = 6 |
443 | 465 |
444 def populateSvn(self): | 466 def populateSvn(self): |
445 """Creates a few revisions of changes including DEPS files.""" | 467 """Creates a few revisions of changes including DEPS files.""" |
446 # Repos | 468 # Repos |
447 subprocess2.check_call( | 469 subprocess2.check_call( |
448 ['svn', 'checkout', self.svn_base, self.svn_checkout, | 470 ['svn', 'checkout', self.svn_base, self.svn_checkout, |
449 '-q', '--non-interactive', '--no-auth-cache', | 471 '-q', '--non-interactive', '--no-auth-cache', |
450 '--username', self.USERS[0][0], '--password', self.USERS[0][1]]) | 472 '--username', self.USERS[0][0], '--password', self.USERS[0][1]]) |
451 assert os.path.isdir(join(self.svn_checkout, '.svn')) | 473 assert os.path.isdir(join(self.svn_checkout, '.svn')) |
452 def file_system(rev, DEPS, DEPS_ALT=None): | 474 def file_system(rev, DEPS, DEPS_ALT=None): |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 } | 721 } |
700 ] | 722 ] |
701 """ % { | 723 """ % { |
702 'git_base': self.git_base, | 724 'git_base': self.git_base, |
703 'hash1': self.git_hashes['repo_1'][2][0][:7], | 725 'hash1': self.git_hashes['repo_1'][2][0][:7], |
704 'hash2': self.git_hashes['repo_2'][1][0][:7], | 726 'hash2': self.git_hashes['repo_2'][1][0][:7], |
705 }, | 727 }, |
706 'origin': 'git/repo_5@3\n', | 728 'origin': 'git/repo_5@3\n', |
707 }) | 729 }) |
708 | 730 |
| 731 # repo_6: basic repository that exists in a non-default Gerrit-like |
| 732 # location on remote |
| 733 self._commit_git('repo_6', {'origin': 'git/repo_6@1\n'}) |
| 734 self._commit_git('repo_6', {'origin': 'git/repo_6@alternate\n'}, |
| 735 branch='alternate') |
| 736 self._update_ref_git('repo_6', 'refs/changes/12/1', 'alternate') |
| 737 self._commit_git('repo_6', {'origin': 'git/repo_6@2\n'}) |
| 738 |
709 | 739 |
710 class FakeRepoTransitive(FakeReposBase): | 740 class FakeRepoTransitive(FakeReposBase): |
711 """Implements populateSvn()""" | 741 """Implements populateSvn()""" |
712 | 742 |
713 def populateSvn(self): | 743 def populateSvn(self): |
714 """Creates a few revisions of changes including a DEPS file.""" | 744 """Creates a few revisions of changes including a DEPS file.""" |
715 # Repos | 745 # Repos |
716 subprocess2.check_call( | 746 subprocess2.check_call( |
717 ['svn', 'checkout', self.svn_base, self.svn_checkout, | 747 ['svn', 'checkout', self.svn_base, self.svn_checkout, |
718 '-q', '--non-interactive', '--no-auth-cache', | 748 '-q', '--non-interactive', '--no-auth-cache', |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 fake.set_up_git() | 959 fake.set_up_git() |
930 print('Fake setup, press enter to quit or Ctrl-C to keep the checkouts.') | 960 print('Fake setup, press enter to quit or Ctrl-C to keep the checkouts.') |
931 sys.stdin.readline() | 961 sys.stdin.readline() |
932 except KeyboardInterrupt: | 962 except KeyboardInterrupt: |
933 trial_dir.TrialDir.SHOULD_LEAK.leak = True | 963 trial_dir.TrialDir.SHOULD_LEAK.leak = True |
934 return 0 | 964 return 0 |
935 | 965 |
936 | 966 |
937 if __name__ == '__main__': | 967 if __name__ == '__main__': |
938 sys.exit(main(sys.argv)) | 968 sys.exit(main(sys.argv)) |
OLD | NEW |