Skip to content

Commit a7133d6

Browse files
committed
Add make archives scripts.
1 parent d02f419 commit a7133d6

4 files changed

Lines changed: 158 additions & 0 deletions

File tree

pre_commit/make_archives.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
from __future__ import unicode_literals
4+
5+
import os.path
6+
import shutil
7+
from plumbum import local
8+
9+
from pre_commit.util import tarfile_open
10+
from pre_commit.util import tmpdir
11+
12+
13+
# This is a script for generating the tarred resources for git repo
14+
# dependencies. Currently it's just for "vendoring" ruby support packages.
15+
16+
17+
REPOS = (
18+
('rbenv', 'git://github.com/sstephenson/rbenv', '13a474c'),
19+
('ruby-build', 'git://github.com/sstephenson/ruby-build', 'd3d5fe0'),
20+
(
21+
'ruby-download',
22+
'git://github.com/garnieretienne/rvm-download',
23+
'f2e9f1e',
24+
),
25+
)
26+
27+
28+
RESOURCES_DIR = os.path.abspath(
29+
os.path.join(os.path.dirname(__file__), 'resources')
30+
)
31+
32+
33+
def make_archive(name, repo, ref, destdir):
34+
"""Makes an archive of a repository in the given destdir.
35+
36+
:param text name: Name to give the archive. For instance foo. The file
37+
that is created will be called foo.tar.gz.
38+
:param text repo: Repository to clone.
39+
:param text ref: Tag/SHA/branch to check out.
40+
:param text destdir: Directory to place archives in.
41+
"""
42+
output_path = os.path.join(destdir, name + '.tar.gz')
43+
with tmpdir() as tempdir:
44+
# Clone the repository to the temporary directory
45+
local['git']('clone', repo, tempdir)
46+
with local.cwd(tempdir):
47+
local['git']('checkout', ref)
48+
49+
# We don't want the '.git' directory
50+
shutil.rmtree(os.path.join(tempdir, '.git'))
51+
52+
with tarfile_open(output_path, 'w|gz') as tf:
53+
tf.add(tempdir, name)
54+
55+
return output_path
56+
57+
58+
def main():
59+
for archive_name, repo, ref in REPOS:
60+
print('Making {0}.tar.gz for {1}@{2}'.format(archive_name, repo, ref))
61+
make_archive(archive_name, repo, ref, RESOURCES_DIR)
62+
63+
64+
if __name__ == '__main__':
65+
exit(main())

pre_commit/util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import os.path
88
import shutil
99
import sys
10+
import tarfile
11+
import tempfile
1012

1113

1214
def memoize_by_cwd(func):
@@ -65,3 +67,25 @@ def hex_md5(s):
6567
:param text s:
6668
"""
6769
return hashlib.md5(s.encode('utf-8')).hexdigest()
70+
71+
72+
@contextlib.contextmanager
73+
def tarfile_open(*args, **kwargs):
74+
"""Compatibility layer because python2.6"""
75+
tf = tarfile.open(*args, **kwargs)
76+
try:
77+
yield tf
78+
finally:
79+
tf.close()
80+
81+
82+
@contextlib.contextmanager
83+
def tmpdir():
84+
"""Contextmanager to create a temporary directory. It will be cleaned up
85+
afterwards.
86+
"""
87+
tempdir = tempfile.mkdtemp()
88+
try:
89+
yield tempdir
90+
finally:
91+
shutil.rmtree(tempdir)

tests/make_archives_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import mock
5+
import os.path
6+
import pytest
7+
from plumbum import local
8+
9+
from pre_commit import make_archives
10+
from pre_commit.util import tarfile_open
11+
from testing.fixtures import git_dir
12+
from testing.util import get_head_sha
13+
from testing.util import skipif_slowtests_false
14+
15+
16+
def test_make_archive(tmpdir_factory):
17+
output_dir = tmpdir_factory.get()
18+
git_path = git_dir(tmpdir_factory)
19+
# Add a files to the git directory
20+
with local.cwd(git_path):
21+
local['touch']('foo')
22+
local['git']('add', '.')
23+
local['git']('commit', '-m', 'foo')
24+
# We'll use this sha
25+
head_sha = get_head_sha('.')
26+
# And check that this file doesn't exist
27+
local['touch']('bar')
28+
local['git']('add', '.')
29+
local['git']('commit', '-m', 'bar')
30+
31+
# Do the thing
32+
archive_path = make_archives.make_archive(
33+
'foo', git_path, head_sha, output_dir,
34+
)
35+
36+
assert archive_path == os.path.join(output_dir, 'foo.tar.gz')
37+
assert os.path.exists(archive_path)
38+
39+
extract_dir = tmpdir_factory.get()
40+
41+
# Extract the tar
42+
with tarfile_open(archive_path) as tf:
43+
tf.extractall(extract_dir)
44+
45+
# Verify the contents of the tar
46+
assert os.path.exists(os.path.join(extract_dir, 'foo'))
47+
assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo'))
48+
assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git'))
49+
assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar'))
50+
51+
52+
@skipif_slowtests_false
53+
@pytest.mark.integration
54+
def test_main(tmpdir_factory):
55+
path = tmpdir_factory.get()
56+
57+
# Don't actually want to make these in the current repo
58+
with mock.patch.object(make_archives, 'RESOURCES_DIR', path):
59+
make_archives.main()
60+
61+
for archive, _, _ in make_archives.REPOS:
62+
assert os.path.exists(os.path.join(path, archive + '.tar.gz'))

tests/util_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pre_commit.util import entry
1313
from pre_commit.util import memoize_by_cwd
1414
from pre_commit.util import shell_escape
15+
from pre_commit.util import tmpdir
1516

1617

1718
@pytest.fixture
@@ -112,3 +113,9 @@ class MySystemExit(SystemExit):
112113
)
113114
def test_shell_escape(input_str, expected):
114115
assert shell_escape(input_str) == expected
116+
117+
118+
def test_tmpdir():
119+
with tmpdir() as tempdir:
120+
assert os.path.exists(tempdir)
121+
assert not os.path.exists(tempdir)

0 commit comments

Comments
 (0)