def test_readpipe(): x = readpipe(['echo', '42']) WVPASSEQ(x, '42\n') try: readpipe(['bash', '-c', 'exit 42']) except Exception as ex: WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
def test_readpipe(): x = readpipe([b'echo', b'42']) WVPASSEQ(x, b'42\n') try: readpipe([b'bash', b'-c', b'exit 42']) except Exception as ex: rx = '^subprocess b?"bash -c \'exit 42\'" failed with status 42$' if not re.match(rx, str(ex)): WVPASSEQ(str(ex), rx)
def showval(commit, val): return readpipe( ['git', 'show', '-s', '--pretty=format:%s' % val, commit]).strip()
def test_commit_parsing(): def restore_env_var(name, val): if val is None: del os.environ[name] else: os.environ[name] = val def showval(commit, val): return readpipe( ['git', 'show', '-s', '--pretty=format:%s' % val, commit]).strip() initial_failures = wvfailure_count() orig_cwd = os.getcwd() tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-') workdir = tmpdir + "/work" repodir = workdir + '/.git' orig_author_name = os.environ.get('GIT_AUTHOR_NAME') orig_author_email = os.environ.get('GIT_AUTHOR_EMAIL') orig_committer_name = os.environ.get('GIT_COMMITTER_NAME') orig_committer_email = os.environ.get('GIT_COMMITTER_EMAIL') os.environ['GIT_AUTHOR_NAME'] = 'bup test' os.environ['GIT_COMMITTER_NAME'] = os.environ['GIT_AUTHOR_NAME'] os.environ['GIT_AUTHOR_EMAIL'] = 'bup@a425bc70a02811e49bdf73ee56450e6f' os.environ['GIT_COMMITTER_EMAIL'] = os.environ['GIT_AUTHOR_EMAIL'] try: readpipe(['git', 'init', workdir]) os.environ['GIT_DIR'] = os.environ['BUP_DIR'] = repodir git.check_repo_or_die(repodir) os.chdir(workdir) with open('foo', 'w') as f: print >> f, 'bar' readpipe(['git', 'add', '.']) readpipe([ 'git', 'commit', '-am', 'Do something', '--author', 'Someone <someone@somewhere>', '--date', 'Sat Oct 3 19:48:49 2009 -0400' ]) commit = readpipe(['git', 'show-ref', '-s', 'master']).strip() parents = showval(commit, '%P') tree = showval(commit, '%T') cname = showval(commit, '%cn') cmail = showval(commit, '%ce') cdate = showval(commit, '%ct') coffs = showval(commit, '%ci') coffs = coffs[-5:] coff = (int(coffs[-4:-2]) * 60 * 60) + (int(coffs[-2:]) * 60) if coffs[-5] == '-': coff = -coff commit_items = git.get_commit_items(commit, git.cp()) WVPASSEQ(commit_items.parents, []) WVPASSEQ(commit_items.tree, tree) WVPASSEQ(commit_items.author_name, 'Someone') WVPASSEQ(commit_items.author_mail, 'someone@somewhere') WVPASSEQ(commit_items.author_sec, 1254613729) WVPASSEQ(commit_items.author_offset, -(4 * 60 * 60)) WVPASSEQ(commit_items.committer_name, cname) WVPASSEQ(commit_items.committer_mail, cmail) WVPASSEQ(commit_items.committer_sec, int(cdate)) WVPASSEQ(commit_items.committer_offset, coff) WVPASSEQ(commit_items.message, 'Do something\n') with open('bar', 'w') as f: print >> f, 'baz' readpipe(['git', 'add', '.']) readpipe(['git', 'commit', '-am', 'Do something else']) child = readpipe(['git', 'show-ref', '-s', 'master']).strip() parents = showval(child, '%P') commit_items = git.get_commit_items(child, git.cp()) WVPASSEQ(commit_items.parents, [commit]) finally: os.chdir(orig_cwd) restore_env_var('GIT_AUTHOR_NAME', orig_author_name) restore_env_var('GIT_AUTHOR_EMAIL', orig_author_email) restore_env_var('GIT_COMMITTER_NAME', orig_committer_name) restore_env_var('GIT_COMMITTER_EMAIL', orig_committer_email) if wvfailure_count() == initial_failures: subprocess.call(['rm', '-rf', tmpdir])
def exo(*cmd): cmd_str = ' '.join(cmd) print >> sys.stderr, cmd_str return readpipe(cmd)
def test_commit_parsing(): def restore_env_var(name, val): if val is None: del environ[name] else: environ[name] = val def showval(commit, val): return readpipe([b'git', b'show', b'-s', b'--pretty=format:%s' % val, commit]).strip() with no_lingering_errors(): with test_tempdir(b'bup-tgit-') as tmpdir: orig_cwd = os.getcwd() workdir = tmpdir + b'/work' repodir = workdir + b'/.git' orig_author_name = environ.get(b'GIT_AUTHOR_NAME') orig_author_email = environ.get(b'GIT_AUTHOR_EMAIL') orig_committer_name = environ.get(b'GIT_COMMITTER_NAME') orig_committer_email = environ.get(b'GIT_COMMITTER_EMAIL') environ[b'GIT_AUTHOR_NAME'] = b'bup test' environ[b'GIT_COMMITTER_NAME'] = environ[b'GIT_AUTHOR_NAME'] environ[b'GIT_AUTHOR_EMAIL'] = b'bup@a425bc70a02811e49bdf73ee56450e6f' environ[b'GIT_COMMITTER_EMAIL'] = environ[b'GIT_AUTHOR_EMAIL'] try: readpipe([b'git', b'init', workdir]) environ[b'GIT_DIR'] = environ[b'BUP_DIR'] = repodir git.check_repo_or_die(repodir) os.chdir(workdir) with open('foo', 'w') as f: print('bar', file=f) readpipe([b'git', b'add', b'.']) readpipe([b'git', b'commit', b'-am', b'Do something', b'--author', b'Someone <someone@somewhere>', b'--date', b'Sat Oct 3 19:48:49 2009 -0400']) commit = readpipe([b'git', b'show-ref', b'-s', b'master']).strip() parents = showval(commit, b'%P') tree = showval(commit, b'%T') cname = showval(commit, b'%cn') cmail = showval(commit, b'%ce') cdate = showval(commit, b'%ct') coffs = showval(commit, b'%ci') coffs = coffs[-5:] coff = (int(coffs[-4:-2]) * 60 * 60) + (int(coffs[-2:]) * 60) if bytes_from_byte(coffs[-5]) == b'-': coff = - coff commit_items = git.get_commit_items(commit, git.cp()) WVPASSEQ(commit_items.parents, []) WVPASSEQ(commit_items.tree, tree) WVPASSEQ(commit_items.author_name, b'Someone') WVPASSEQ(commit_items.author_mail, b'someone@somewhere') WVPASSEQ(commit_items.author_sec, 1254613729) WVPASSEQ(commit_items.author_offset, -(4 * 60 * 60)) WVPASSEQ(commit_items.committer_name, cname) WVPASSEQ(commit_items.committer_mail, cmail) WVPASSEQ(commit_items.committer_sec, int(cdate)) WVPASSEQ(commit_items.committer_offset, coff) WVPASSEQ(commit_items.message, b'Do something\n') with open(b'bar', 'wb') as f: f.write(b'baz\n') readpipe([b'git', b'add', '.']) readpipe([b'git', b'commit', b'-am', b'Do something else']) child = readpipe([b'git', b'show-ref', b'-s', b'master']).strip() parents = showval(child, b'%P') commit_items = git.get_commit_items(child, git.cp()) WVPASSEQ(commit_items.parents, [commit]) finally: os.chdir(orig_cwd) restore_env_var(b'GIT_AUTHOR_NAME', orig_author_name) restore_env_var(b'GIT_AUTHOR_EMAIL', orig_author_email) restore_env_var(b'GIT_COMMITTER_NAME', orig_committer_name) restore_env_var(b'GIT_COMMITTER_EMAIL', orig_committer_email)
def exo(*cmd): print(repr(cmd), file=sys.stderr) return readpipe(cmd)
def showval(commit, val): return readpipe(['git', 'show', '-s', '--pretty=format:%s' % val, commit]).strip()
def test_commit_parsing(): def restore_env_var(name, val): if val is None: del os.environ[name] else: os.environ[name] = val def showval(commit, val): return readpipe(['git', 'show', '-s', '--pretty=format:%s' % val, commit]).strip() initial_failures = wvfailure_count() orig_cwd = os.getcwd() tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-') workdir = tmpdir + "/work" repodir = workdir + '/.git' orig_author_name = os.environ.get('GIT_AUTHOR_NAME') orig_author_email = os.environ.get('GIT_AUTHOR_EMAIL') orig_committer_name = os.environ.get('GIT_COMMITTER_NAME') orig_committer_email = os.environ.get('GIT_COMMITTER_EMAIL') os.environ['GIT_AUTHOR_NAME'] = 'bup test' os.environ['GIT_COMMITTER_NAME'] = os.environ['GIT_AUTHOR_NAME'] os.environ['GIT_AUTHOR_EMAIL'] = 'bup@a425bc70a02811e49bdf73ee56450e6f' os.environ['GIT_COMMITTER_EMAIL'] = os.environ['GIT_AUTHOR_EMAIL'] try: readpipe(['git', 'init', workdir]) os.environ['GIT_DIR'] = os.environ['BUP_DIR'] = repodir git.check_repo_or_die(repodir) os.chdir(workdir) with open('foo', 'w') as f: print >> f, 'bar' readpipe(['git', 'add', '.']) readpipe(['git', 'commit', '-am', 'Do something', '--author', 'Someone <someone@somewhere>', '--date', 'Sat Oct 3 19:48:49 2009 -0400']) commit = readpipe(['git', 'show-ref', '-s', 'master']).strip() parents = showval(commit, '%P') tree = showval(commit, '%T') cname = showval(commit, '%cn') cmail = showval(commit, '%ce') cdate = showval(commit, '%ct') coffs = showval(commit, '%ci') coffs = coffs[-5:] coff = (int(coffs[-4:-2]) * 60 * 60) + (int(coffs[-2:]) * 60) if coffs[-5] == '-': coff = - coff commit_items = git.get_commit_items(commit, git.cp()) WVPASSEQ(commit_items.parents, []) WVPASSEQ(commit_items.tree, tree) WVPASSEQ(commit_items.author_name, 'Someone') WVPASSEQ(commit_items.author_mail, 'someone@somewhere') WVPASSEQ(commit_items.author_sec, 1254613729) WVPASSEQ(commit_items.author_offset, -(4 * 60 * 60)) WVPASSEQ(commit_items.committer_name, cname) WVPASSEQ(commit_items.committer_mail, cmail) WVPASSEQ(commit_items.committer_sec, int(cdate)) WVPASSEQ(commit_items.committer_offset, coff) WVPASSEQ(commit_items.message, 'Do something\n') with open('bar', 'w') as f: print >> f, 'baz' readpipe(['git', 'add', '.']) readpipe(['git', 'commit', '-am', 'Do something else']) child = readpipe(['git', 'show-ref', '-s', 'master']).strip() parents = showval(child, '%P') commit_items = git.get_commit_items(child, git.cp()) WVPASSEQ(commit_items.parents, [commit]) finally: os.chdir(orig_cwd) restore_env_var('GIT_AUTHOR_NAME', orig_author_name) restore_env_var('GIT_AUTHOR_EMAIL', orig_author_email) restore_env_var('GIT_COMMITTER_NAME', orig_committer_name) restore_env_var('GIT_COMMITTER_EMAIL', orig_committer_email) if wvfailure_count() == initial_failures: subprocess.call(['rm', '-rf', tmpdir])
def exo(cmd, shell=False): global opt logcmd(cmd) if not opt.dry_run: return readpipe(cmd, shell=shell)
def exo(*cmd): cmd_str = ' '.join(cmd) print(cmd_str, file=sys.stderr) return readpipe(cmd)