def mock_svn_repository(tmpdir_factory): """Creates a very simple svn repository with two commits.""" svn = spack.util.executable.which('svn', required=True) svnadmin = spack.util.executable.which('svnadmin', required=True) tmpdir = tmpdir_factory.mktemp('mock-svn-stage') repo_name = 'mock-svn-repo' tmpdir.ensure(repo_name, dir=True) repodir = tmpdir.join(repo_name) url = 'file://' + str(repodir) # Initialize the repository with repodir.as_cwd(): # NOTE: Adding --pre-1.5-compatible works for NERSC # Unknown if this is also an issue at other sites. svnadmin('create', '--pre-1.5-compatible', str(repodir)) # Import a structure (first commit) r0_file = 'r0_file' tmpdir.ensure('tmp-path', r0_file) tmp_path = tmpdir.join('tmp-path') svn('import', str(tmp_path), url, '-m', 'Initial import r0') tmp_path.remove() # Second commit r1_file = 'r1_file' svn('checkout', url, str(tmp_path)) tmpdir.ensure('tmp-path', r1_file) with tmp_path.as_cwd(): svn('add', str(tmpdir.ensure('tmp-path', r1_file))) svn('ci', '-m', 'second revision r1') tmp_path.remove() r0 = '1' r1 = '2' checks = { 'default': Bunch( revision=r1, file=r1_file, args={'svn': url}), 'rev0': Bunch( revision=r0, file=r0_file, args={ 'svn': url, 'revision': r0}) } def get_rev(): output = svn('info', output=str) assert "Revision" in output for line in output.split('\n'): match = re.match(r'Revision: (\d+)', line) if match: return match.group(1) t = Bunch(checks=checks, url=url, hash=get_rev, path=str(repodir)) yield t
def mock_svn_repository(tmpdir_factory): """Creates a very simple svn repository with two commits.""" svn = spack.util.executable.which('svn', required=True) svnadmin = spack.util.executable.which('svnadmin', required=True) tmpdir = tmpdir_factory.mktemp('mock-svn-stage') tmpdir.ensure(spack.stage._source_path_subdir, dir=True) repodir = tmpdir.join(spack.stage._source_path_subdir) url = 'file://' + str(repodir) # Initialize the repository with repodir.as_cwd(): # NOTE: Adding --pre-1.5-compatible works for NERSC # Unknown if this is also an issue at other sites. svnadmin('create', '--pre-1.5-compatible', str(repodir)) # Import a structure (first commit) r0_file = 'r0_file' tmpdir.ensure('tmp-path', r0_file) tmp_path = tmpdir.join('tmp-path') svn('import', str(tmp_path), url, '-m', 'Initial import r0') tmp_path.remove() # Second commit r1_file = 'r1_file' svn('checkout', url, str(tmp_path)) tmpdir.ensure('tmp-path', r1_file) with tmp_path.as_cwd(): svn('add', str(tmpdir.ensure('tmp-path', r1_file))) svn('ci', '-m', 'second revision r1') tmp_path.remove() r0 = '1' r1 = '2' checks = { 'default': Bunch( revision=r1, file=r1_file, args={'svn': url}), 'rev0': Bunch( revision=r0, file=r0_file, args={ 'svn': url, 'revision': r0}) } def get_rev(): output = svn('info', '--xml', output=str) info = xml.etree.ElementTree.fromstring(output) return info.find('entry/commit').get('revision') t = Bunch(checks=checks, url=url, hash=get_rev, path=str(repodir)) yield t
def test_query_arguments(): query_arguments = spack.cmd.find.query_arguments # Default arguments args = Bunch(only_missing=False, missing=False, unknown=False, explicit=False, implicit=False, start_date="2018-02-23", end_date=None) q_args = query_arguments(args) assert 'installed' in q_args assert 'known' in q_args assert 'explicit' in q_args assert q_args['installed'] is True assert q_args['known'] is any assert q_args['explicit'] is any assert 'start_date' in q_args assert 'end_date' not in q_args # Check that explicit works correctly args.explicit = True q_args = query_arguments(args) assert q_args['explicit'] is True args.explicit = False args.implicit = True q_args = query_arguments(args) assert q_args['explicit'] is False
def mock_hg_repository(tmpdir_factory): """Creates a very simple hg repository with two commits.""" hg = spack.util.executable.which('hg', required=True) tmpdir = tmpdir_factory.mktemp('mock-hg-repo-dir') repo_name = 'mock-hg-repo' tmpdir.ensure(repo_name, dir=True) repodir = tmpdir.join(repo_name) get_rev = lambda: hg('id', '-i', output=str).strip() # Initialize the repository with repodir.as_cwd(): url = 'file://' + str(repodir) hg('init') # Commit file r0 r0_file = 'r0_file' repodir.ensure(r0_file) hg('add', r0_file) hg('commit', '-m', 'revision 0', '-u', 'test') r0 = get_rev() # Commit file r1 r1_file = 'r1_file' repodir.ensure(r1_file) hg('add', r1_file) hg('commit', '-m' 'revision 1', '-u', 'test') r1 = get_rev() checks = { 'default': Bunch(revision=r1, file=r1_file, args={'hg': str(repodir)}), 'rev0': Bunch(revision=r0, file=r0_file, args={ 'hg': str(repodir), 'revision': r0 }) } t = Bunch(checks=checks, url=url, hash=get_rev, path=str(repodir)) yield t
def test_query_arguments(self): query_arguments = spack.cmd.find.query_arguments # Default arguments args = Bunch(only_missing=False, missing=False, unknown=False, explicit=False, implicit=False) q_args = query_arguments(args) self.assertTrue('installed' in q_args) self.assertTrue('known' in q_args) self.assertTrue('explicit' in q_args) self.assertEqual(q_args['installed'], True) self.assertEqual(q_args['known'], any) self.assertEqual(q_args['explicit'], any) # Check that explicit works correctly args.explicit = True q_args = query_arguments(args) self.assertEqual(q_args['explicit'], True) args.explicit = False args.implicit = True q_args = query_arguments(args) self.assertEqual(q_args['explicit'], False)
def mock_git_repository(tmpdir_factory): """Creates a very simple git repository with two branches and two commits. """ git = spack.util.executable.which('git', required=True) tmpdir = tmpdir_factory.mktemp('mock-git-repo-dir') repo_name = 'mock-git-repo' tmpdir.ensure(repo_name, dir=True) repodir = tmpdir.join(repo_name) # Initialize the repository with repodir.as_cwd(): git('init') git('config', 'user.name', 'Spack') git('config', 'user.email', '*****@*****.**') url = 'file://' + str(repodir) # r0 is just the first commit r0_file = 'r0_file' repodir.ensure(r0_file) git('add', r0_file) git('commit', '-m', 'mock-git-repo r0') branch = 'test-branch' branch_file = 'branch_file' git('branch', branch) tag_branch = 'tag-branch' tag_file = 'tag_file' git('branch', tag_branch) # Check out first branch git('checkout', branch) repodir.ensure(branch_file) git('add', branch_file) git('commit', '-m' 'r1 test branch') # Check out a second branch and tag it git('checkout', tag_branch) repodir.ensure(tag_file) git('add', tag_file) git('commit', '-m' 'tag test branch') tag = 'test-tag' git('tag', tag) git('checkout', 'master') # R1 test is the same as test for branch rev_hash = lambda x: git('rev-parse', x, output=str).strip() r1 = rev_hash(branch) r1_file = branch_file checks = { 'master': Bunch(revision='master', file=r0_file, args={'git': str(repodir)}), 'branch': Bunch(revision=branch, file=branch_file, args={ 'git': str(repodir), 'branch': branch }), 'tag': Bunch(revision=tag, file=tag_file, args={ 'git': str(repodir), 'tag': tag }), 'commit': Bunch(revision=r1, file=r1_file, args={ 'git': str(repodir), 'commit': r1 }) } t = Bunch(checks=checks, url=url, hash=rev_hash, path=str(repodir)) yield t
def mock_git_repository(tmpdir_factory): """Creates a simple git repository with two branches, two commits and two submodules. Each submodule has one commit. """ git = spack.util.executable.which('git', required=True) suburls = [] for submodule_count in range(2): tmpdir = tmpdir_factory.mktemp( 'mock-git-repo-submodule-dir-{0}'.format(submodule_count)) tmpdir.ensure(spack.stage._source_path_subdir, dir=True) repodir = tmpdir.join(spack.stage._source_path_subdir) suburls.append((submodule_count, 'file://' + str(repodir))) # Initialize the repository with repodir.as_cwd(): git('init') git('config', 'user.name', 'Spack') git('config', 'user.email', '*****@*****.**') # r0 is just the first commit submodule_file = 'r0_file_{0}'.format(submodule_count) repodir.ensure(submodule_file) git('add', submodule_file) git('commit', '-m', 'mock-git-repo r0 {0}'.format(submodule_count)) tmpdir = tmpdir_factory.mktemp('mock-git-repo-dir') tmpdir.ensure(spack.stage._source_path_subdir, dir=True) repodir = tmpdir.join(spack.stage._source_path_subdir) # Initialize the repository with repodir.as_cwd(): git('init') git('config', 'user.name', 'Spack') git('config', 'user.email', '*****@*****.**') url = 'file://' + str(repodir) for number, suburl in suburls: git('submodule', 'add', suburl, 'third_party/submodule{0}'.format(number)) # r0 is just the first commit r0_file = 'r0_file' repodir.ensure(r0_file) git('add', r0_file) git('commit', '-m', 'mock-git-repo r0') branch = 'test-branch' branch_file = 'branch_file' git('branch', branch) tag_branch = 'tag-branch' tag_file = 'tag_file' git('branch', tag_branch) # Check out first branch git('checkout', branch) repodir.ensure(branch_file) git('add', branch_file) git('commit', '-m' 'r1 test branch') # Check out a second branch and tag it git('checkout', tag_branch) repodir.ensure(tag_file) git('add', tag_file) git('commit', '-m' 'tag test branch') tag = 'test-tag' git('tag', tag) git('checkout', 'master') # R1 test is the same as test for branch rev_hash = lambda x: git('rev-parse', x, output=str).strip() r1 = rev_hash(branch) r1_file = branch_file checks = { 'master': Bunch(revision='master', file=r0_file, args={'git': url}), 'branch': Bunch(revision=branch, file=branch_file, args={ 'git': url, 'branch': branch }), 'tag-branch': Bunch(revision=tag_branch, file=tag_file, args={ 'git': url, 'branch': tag_branch }), 'tag': Bunch(revision=tag, file=tag_file, args={ 'git': url, 'tag': tag }), 'commit': Bunch(revision=r1, file=r1_file, args={ 'git': url, 'commit': r1 }) } t = Bunch(checks=checks, url=url, hash=rev_hash, path=str(repodir), git_exe=git) yield t
def mock_cvs_repository(tmpdir_factory): """Creates a very simple CVS repository with two commits and a branch.""" cvs = spack.util.executable.which('cvs', required=True) tmpdir = tmpdir_factory.mktemp('mock-cvs-repo-dir') tmpdir.ensure(spack.stage._source_path_subdir, dir=True) repodir = tmpdir.join(spack.stage._source_path_subdir) cvsroot = str(repodir) # The CVS repository and source tree need to live in a different directories sourcedirparent = tmpdir_factory.mktemp('mock-cvs-source-dir') module = spack.stage._source_path_subdir url = cvsroot + "%module=" + module sourcedirparent.ensure(module, dir=True) sourcedir = sourcedirparent.join(module) def format_date(date): if date is None: return None return date.strftime('%Y-%m-%d %H:%M:%S') def get_cvs_timestamp(output): """Find the most recent CVS time stamp in a `cvs log` output""" latest_timestamp = None for line in output.splitlines(): timestamp = _parse_cvs_date(line) if timestamp: if latest_timestamp is None: latest_timestamp = timestamp else: latest_timestamp = max(latest_timestamp, timestamp) return latest_timestamp # We use this to record the time stamps for when we create CVS revisions, # so that we can later check that we retrieve the proper commits when # specifying a date. (CVS guarantees checking out the lastest revision # before or on the specified date). As we create each revision, we # separately record the time by querying CVS. revision_date = {} # Initialize the repository with sourcedir.as_cwd(): cvs('-d', cvsroot, 'init') cvs('-d', cvsroot, 'import', '-m', 'initial mock repo commit', module, 'mockvendor', 'mockrelease') with sourcedirparent.as_cwd(): cvs('-d', cvsroot, 'checkout', module) # Commit file r0 r0_file = 'r0_file' sourcedir.ensure(r0_file) cvs('-d', cvsroot, 'add', r0_file) cvs('-d', cvsroot, 'commit', '-m', 'revision 0', r0_file) output = cvs('log', '-N', r0_file, output=str) revision_date['1.1'] = format_date(get_cvs_timestamp(output)) # Commit file r1 r1_file = 'r1_file' sourcedir.ensure(r1_file) cvs('-d', cvsroot, 'add', r1_file) cvs('-d', cvsroot, 'commit', '-m' 'revision 1', r1_file) output = cvs('log', '-N', r0_file, output=str) revision_date['1.2'] = format_date(get_cvs_timestamp(output)) # Create branch 'mock-branch' cvs('-d', cvsroot, 'tag', 'mock-branch-root') cvs('-d', cvsroot, 'tag', '-b', 'mock-branch') # CVS does not have the notion of a unique branch; branches and revisions # are managed separately for every file def get_branch(): """Return the branch name if all files are on the same branch, else return None. Also return None if all files are on the trunk.""" lines = cvs('-d', cvsroot, 'status', '-v', output=str).splitlines() branch = None for line in lines: m = re.search(r'(\S+)\s+[(]branch:', line) if m: tag = m.group(1) if branch is None: # First branch name found branch = tag elif tag == branch: # Later branch name found; all branch names found so far # agree pass else: # Later branch name found; branch names differ branch = None break return branch # CVS does not have the notion of a unique revision; usually, one uses # commit dates instead def get_date(): """Return latest date of the revisions of all files""" output = cvs('log', '-N', r0_file, output=str) timestamp = get_cvs_timestamp(output) if timestamp is None: return None return format_date(timestamp) checks = { 'default': Bunch( file=r1_file, branch=None, date=None, args={'cvs': url}, ), 'branch': Bunch( file=r1_file, branch='mock-branch', date=None, args={ 'cvs': url, 'branch': 'mock-branch' }, ), 'date': Bunch( file=r0_file, branch=None, date=revision_date['1.1'], args={ 'cvs': url, 'date': revision_date['1.1'] }, ), } test = Bunch( checks=checks, url=url, get_branch=get_branch, get_date=get_date, path=str(repodir), ) yield test