def poll(schmenkins, job, info): # Checkout (we don't know any other way to find all this info) checkout(schmenkins, job, info) cmd = ['repo', 'manifest', '-o', '-', '-r'] output = run_cmd(cmd, cwd=job.workspace(), dry_run=schmenkins.dry_run) tree = ElementTree.fromstring(output) project_revisions = {project.attrib['name']: project.attrib['revision'] for project in tree.findall('project')} cmd = ['git', 'rev-parse', 'HEAD'] output = run_cmd(cmd, cwd=os.path.join(job.workspace(), '.repo', 'manifests'), dry_run=schmenkins.dry_run) manifest_revision = output.strip() project_revisions['_manifest'] = manifest_revision job.build_revision = project_revisions if not job.state.last_seen_revision: job.should_run = True elif job.state.last_seen_revision != job.build_revision: job.should_run = True
def test_run_cmd(self): with tempfile.NamedTemporaryFile(delete=False) as fp: try: fp.close() utils.run_cmd(['bash', '-c', 'echo -n hello > %s' % (fp.name,)]) self.assertEquals(open(fp.name, 'r').read(), 'hello') finally: os.unlink(fp.name)
def run(schmenkins, job, info, build): with tempfile.NamedTemporaryFile(delete=False) as fp: try: os.chmod(fp.name, 0o0700) fp.write(info) fp.close() maybe_shebang = info.split('\n')[0] if maybe_shebang.startswith('#!'): cmd = maybe_shebang[2:].split(' ') else: cmd = [os.environ.get('SHELL', '/bin/sh'), '-ex'] cmd += [fp.name] run_cmd(cmd, cwd=job.workspace(), dry_run=schmenkins.dry_run, logger=build.logger, additional_environment=build.parameters()) finally: os.unlink(fp.name)
def checkout(schmenkins, job, info, build=None): logger = build and build.logger or LOG cmd = ['repo', 'init', '-u', info['manifest-url']] if 'manifest-file' in info: cmd += ['-m', info['manifest-file']] if 'manifest-branch' in info: cmd += ['-b', info['manifest-branch']] run_cmd(cmd, cwd=job.workspace(), logger=logger, dry_run=schmenkins.dry_run) def do_sync(): cmd = ['repo', 'sync', '-d', '-c', '-q'] run_cmd(cmd, cwd=job.workspace(), logger=logger, dry_run=schmenkins.dry_run) try: do_sync() except SchmenkinsCommandFailed: cmd = ['repo', 'forall', '-c', 'git reset --hard'] run_cmd(cmd, cwd=job.workspace(), logger=logger, dry_run=schmenkins.dry_run) do_sync()
def poll(schmenkins, job, info): url = info['url'] ref = 'refs/heads/%s' % (info.get('branch', 'master'),) cmd = ['git', 'ls-remote', info['url']] output = run_cmd(cmd, dry_run=schmenkins.dry_run) for l in output.split('\n'): if not l: continue parts = re.split('\s', l) if parts[1] == ref: job.build_revision = parts[0] break if job.build_revision is None: LOG.error('Did not find revision for %s for job %s' % (ref, job.name)) return if not job.state.last_seen_revision: job.should_run = True elif job.state.last_seen_revision != job.build_revision: job.should_run = True
def checkout(schmenkins, job, info, build): remote_name = 'origin' # I believe this can be overriden somehow if info.get('wipe-workspace', True): shutil.rmtree(job.workspace()) if not os.path.isdir(os.path.join(job.workspace(), '.git')): run_cmd(['git', 'init'], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run) run_cmd(['git', 'remote', 'add', remote_name, info['url']], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run) run_cmd(['git', 'remote', 'set-url', remote_name, info['url']], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run) run_cmd(['git', 'fetch', remote_name], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run) rev = build.build_revision or '%s/%s' % (remote_name, info.get('branch', 'master')) run_cmd(['git', 'reset', '--hard', rev], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run) commit = run_cmd(['git', 'rev-parse', 'HEAD'], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run).strip() build.state.commit_info = run_cmd(['git', 'log', '-1', '--oneline'], cwd=job.workspace(), logger=build.logger, dry_run=schmenkins.dry_run).strip() if job._job_dict.get('properties', {}).get('github', False): github_url = job._job_dict['properties']['github'] build.state.commit_info_url = '%s/commit/%s' % (github_url, commit) build._parameters['GIT_COMMIT'] = commit
def test_run_cmd_logging(self): mock_logger = mock.MagicMock() output = utils.run_cmd(['bash', '-c', 'echo -n hello'], logger=mock_logger) mock_logger.debug.assert_called_with('hello') self.assertEquals(output, 'hello')
def test_run_cmd_output(self): mock_logger = mock.MagicMock() output = utils.run_cmd(['bash', '-c', 'echo -n hello']) self.assertEquals(output, 'hello')
def do_sync(): cmd = ['repo', 'sync', '-d', '-c', '-q'] run_cmd(cmd, cwd=job.workspace(), logger=logger, dry_run=schmenkins.dry_run)