From e3d1f10ec3069b06f7f5e34d048b9a7e0d19df2c Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 14:37:22 -0700 Subject: [PATCH 1/5] added create repo --- Makefile | 2 +- pre_commit/git.py | 15 ++++++++++++--- tests/git_test.py | 27 ++++++++++++++------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 161f6d0f..9aedace0 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ coverage: py_env coverage run `which py.test` tests $(TEST_TARGETS) && \ coverage report -m' -py_env: requirements.txt +py_env: requirements.txt setup.py rm -rf py_env virtualenv py_env bash -c 'source py_env/bin/activate && \ diff --git a/pre_commit/git.py b/pre_commit/git.py index 7f206125..e5bb2132 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -11,6 +11,12 @@ def get_pre_commit_path(): return os.path.join(get_root(), '.git/hooks/pre-commit') +def get_env_path(): + return os.path.join(get_root(), '.pre-commit') + +def create_pre_commit_package_dir(): + local.path(get_root() + '/.pre-commit').mkdir() + def create_pre_commit(): path = get_pre_commit_path() pre_commit_file = pkg_resources.resource_filename('pre_commit', 'resources/pre-commit.sh') @@ -20,8 +26,11 @@ def create_pre_commit(): def remove_pre_commit(): local.path(get_pre_commit_path()).delete() +def create_repo_in_env(name, git_repo_path): + create_pre_commit_package_dir() + env_path = get_env_path() - - - + with local.cwd(env_path): + local['git']['clone', git_repo_path, name]() + print local.cwd.getpath() diff --git a/tests/git_test.py b/tests/git_test.py index c566b1f3..11c0454a 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -1,5 +1,4 @@ -import contextlib import os import pytest @@ -7,30 +6,28 @@ from plumbum import local from pre_commit import git - -@contextlib.contextmanager -def in_dir(dir): - old_path = local.cwd.getpath() - local.cwd.chdir(dir) - try: - yield - finally: - local.cwd.chdir(old_path) - @pytest.yield_fixture def empty_git_dir(tmpdir): - with in_dir(tmpdir.strpath): + with local.cwd(tmpdir.strpath): local['git']['init']() yield tmpdir.strpath +@pytest.yield_fixture +def dummy_git_repo(empty_git_dir): + local['touch']['dummy']() + local['git']['add', 'dummy']() + local['git']['commit', '-m', 'dummy commit']() + + yield empty_git_dir + def test_get_root(empty_git_dir): assert git.get_root() == empty_git_dir foo = local.path('foo') foo.mkdir() - with in_dir(foo): + with local.cwd(foo): assert git.get_root() == empty_git_dir @@ -52,3 +49,7 @@ def test_remove_pre_commit(empty_git_dir): git.remove_pre_commit() assert not os.path.exists(git.get_pre_commit_path()) + + +def test_create_repo_in_env(empty_git_dir, dummy_git_repo): + git.create_repo_in_env('pre-commit', dummy_git_repo) \ No newline at end of file From 6498321e1ebbf63793faa5e05bfe9f3a91183573 Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 16:29:59 -0700 Subject: [PATCH 2/5] added more integration with git repo creation --- pre_commit/git.py | 50 ++++++++++++++++++---- tests/git_test.py | 104 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 142 insertions(+), 12 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index e5bb2132..47323436 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -1,5 +1,8 @@ import os import pkg_resources +import contextlib + +import pre_commit.constants as C from plumbum import local @@ -11,11 +14,11 @@ def get_pre_commit_path(): return os.path.join(get_root(), '.git/hooks/pre-commit') -def get_env_path(): - return os.path.join(get_root(), '.pre-commit') +def get_pre_commit_dir_path(): + return os.path.join(get_root(), C.PRE_COMMIT_DIR) def create_pre_commit_package_dir(): - local.path(get_root() + '/.pre-commit').mkdir() + local.path(get_pre_commit_dir_path()).mkdir() def create_pre_commit(): path = get_pre_commit_path() @@ -26,11 +29,40 @@ def create_pre_commit(): def remove_pre_commit(): local.path(get_pre_commit_path()).delete() -def create_repo_in_env(name, git_repo_path): - create_pre_commit_package_dir() - env_path = get_env_path() +class PreCommitProject(object): + + def __init__(self, git_repo_path, sha): + self.git_repo_path = git_repo_path + self.sha = sha + + @contextlib.contextmanager + def in_checkout(self): + with local.cwd(get_pre_commit_dir_path()): + with local.cwd(self.sha): + yield + + def create(self): + create_pre_commit_package_dir() + + with local.cwd(get_pre_commit_dir_path()): + local['git']['clone', self.git_repo_path, self.sha]() + with self.in_checkout(): + local['git']['checkout', self.sha]() + + def install(self): + with self.in_checkout(): + if local.path('setup.py').exists(): + local['virtualenv']['py_env']() + local['bash'][local['pip']['install', '.']] + +def create_repo_in_env(git_repo_path, sha): + project = PreCommitProject(git_repo_path, sha) + project.create() + +def install_pre_commit(git_repo_path, sha): + project = PreCommitProject(git_repo_path, sha) + project.create() + project.install() + - with local.cwd(env_path): - local['git']['clone', git_repo_path, name]() - print local.cwd.getpath() diff --git a/tests/git_test.py b/tests/git_test.py index 11c0454a..5cfd5e79 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -5,6 +5,17 @@ import pytest from plumbum import local from pre_commit import git +import pre_commit.constants as C + +def add_and_commit(): + local['git']['add', '.']() + local['git']['commit', '-m', 'random commit']() + + +def get_sha(git_repo): + with local.cwd(git_repo): + return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n') + @pytest.yield_fixture def empty_git_dir(tmpdir): @@ -16,11 +27,66 @@ def empty_git_dir(tmpdir): @pytest.yield_fixture def dummy_git_repo(empty_git_dir): local['touch']['dummy']() - local['git']['add', 'dummy']() - local['git']['commit', '-m', 'dummy commit']() + add_and_commit() yield empty_git_dir + +@pytest.yield_fixture +def dummy_pre_commit_hooks_git_repo(dummy_git_repo): + local.path(C.MANIFEST_FILE).write(""" +hooks: + - + id: foo + name: Foo + entry: foo + language: python>2.6 + """) + + add_and_commit() + + yield dummy_git_repo + +@pytest.yield_fixture +def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo): + local.path('setup.py').write( +""" +from setuptools import find_packages +from setuptools import setup + +setup( + name='Foo', + version='0.0.0', + packages=find_packages('.'), + entry_points={ + 'console_scripts': [ + 'entry = foo.main:func' + ], + } +) +""" + ) + + foo_module = local.path('foo') + + foo_module.mkdir() + + with local.cwd(foo_module): + local.path('__init__.py').write('') + local.path('main.py').write( +""" + +def func(): + return 0 + +""" + ) + + add_and_commit() + + yield dummy_pre_commit_hooks_git_repo + + def test_get_root(empty_git_dir): assert git.get_root() == empty_git_dir @@ -52,4 +118,36 @@ def test_remove_pre_commit(empty_git_dir): def test_create_repo_in_env(empty_git_dir, dummy_git_repo): - git.create_repo_in_env('pre-commit', dummy_git_repo) \ No newline at end of file + sha = get_sha(dummy_git_repo) + git.create_repo_in_env(dummy_git_repo, sha) + assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha)) + + +def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo): + sha = get_sha(python_pre_commit_git_repo) + git.install_pre_commit(python_pre_commit_git_repo, sha) + assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env')) + + +# def install_config(): +# config = [ +# { +# 'repo': 'repo1', +# 'sha': 'dsfjksljfslkf', +# 'hooks': [ +# { +# 'id': 'script', +# 'args': [ +# { +# 'type': 'files', +# 'opt': '*.py' +# }, +# ] +# } +# ], +# }, +# ] +# for repo in config: +# clone(repo) +# for + From 320bb0a679d4a337ee925b5f20bf81c15bcbd807 Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 17:06:23 -0700 Subject: [PATCH 3/5] hopefully unbreaking the build --- tests/git_test.py | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/git_test.py b/tests/git_test.py index b4245f78..bd00a6b7 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -9,6 +9,8 @@ import pre_commit.constants as C def add_and_commit(): local['git']['add', '.']() + local['git']['config', 'user.email', 'ken@struys.ca'] + local['git']['config', 'user.name', 'Ken Struys'] local['git']['commit', '-m', 'random commit']() @@ -115,31 +117,34 @@ def test_create_repo_in_env(empty_git_dir, dummy_git_repo): assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha)) +@pytest.mark.integration def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo): sha = get_sha(python_pre_commit_git_repo) git.install_pre_commit(python_pre_commit_git_repo, sha) assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env')) +@pytest.mark.integration +def test_install_config(empty_git_dir, python_pre_commit_git_repo): -# def install_config(): -# config = [ -# { -# 'repo': 'repo1', -# 'sha': 'dsfjksljfslkf', -# 'hooks': [ -# { -# 'id': 'script', -# 'args': [ -# { -# 'type': 'files', -# 'opt': '*.py' -# }, -# ] -# } -# ], -# }, -# ] -# for repo in config: -# clone(repo) -# for + config = [ + { + 'repo': python_pre_commit_git_repo, + 'sha': get_sha(python_pre_commit_git_repo), + 'hooks': [ + { + 'id': 'foo', + 'args': [ + { + 'type': 'files', + 'opt': '*.py' + }, + ] + } + ], + }, + ] + for repo in config: + git.install_pre_commit(repo['repo'], repo['sha']) + + print python_pre_commit_git_repo \ No newline at end of file From 1d8af9ce501b046f68a2d43d5927816c159ec0a9 Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 17:31:58 -0700 Subject: [PATCH 4/5] moved the installing code to its own package --- pre_commit/git.py | 38 --------- pre_commit/installer/__init__.py | 0 pre_commit/installer/repo_installer.py | 44 ++++++++++ tests/conftest.py | 72 +++++++++++++++- tests/git_test.py | 113 ------------------------- tests/installer/__init__.py | 0 tests/installer/repo_installer_test.py | 52 ++++++++++++ 7 files changed, 167 insertions(+), 152 deletions(-) create mode 100644 pre_commit/installer/__init__.py create mode 100644 pre_commit/installer/repo_installer.py create mode 100644 tests/installer/__init__.py create mode 100644 tests/installer/repo_installer_test.py diff --git a/pre_commit/git.py b/pre_commit/git.py index 169d9394..d72008d0 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -1,6 +1,5 @@ import os import pkg_resources -import contextlib import pre_commit.constants as C from plumbum import local @@ -9,7 +8,6 @@ from plumbum import local def get_root(): return local['git']['rev-parse', '--show-toplevel']().strip() - def get_pre_commit_path(): return os.path.join(get_root(), '.git/hooks/pre-commit') @@ -30,40 +28,4 @@ def remove_pre_commit(): local.path(get_pre_commit_path()).delete() -class PreCommitProject(object): - - def __init__(self, git_repo_path, sha): - self.git_repo_path = git_repo_path - self.sha = sha - - @contextlib.contextmanager - def in_checkout(self): - with local.cwd(get_pre_commit_dir_path()): - with local.cwd(self.sha): - yield - - def create(self): - create_pre_commit_package_dir() - - with local.cwd(get_pre_commit_dir_path()): - local['git']['clone', self.git_repo_path, self.sha]() - with self.in_checkout(): - local['git']['checkout', self.sha]() - - def install(self): - with self.in_checkout(): - if local.path('setup.py').exists(): - local['virtualenv']['py_env']() - local['bash']['-c', 'source py_env/bin/activate && pip install .']() - print local.cwd.getpath() - -def create_repo_in_env(git_repo_path, sha): - project = PreCommitProject(git_repo_path, sha) - project.create() - -def install_pre_commit(git_repo_path, sha): - project = PreCommitProject(git_repo_path, sha) - project.create() - project.install() - diff --git a/pre_commit/installer/__init__.py b/pre_commit/installer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pre_commit/installer/repo_installer.py b/pre_commit/installer/repo_installer.py new file mode 100644 index 00000000..c8695b1a --- /dev/null +++ b/pre_commit/installer/repo_installer.py @@ -0,0 +1,44 @@ +import contextlib + +from plumbum import local +from pre_commit import git + +class RepoInstaller(object): + + def __init__(self, git_repo_path, sha): + self.git_repo_path = git_repo_path + self.sha = sha + + @contextlib.contextmanager + def in_checkout(self): + with local.cwd(git.get_pre_commit_dir_path()): + with local.cwd(self.sha): + yield + + def create(self): + git.create_pre_commit_package_dir() + + with local.cwd(git.get_pre_commit_dir_path()): + if local.path(self.sha).exists(): + # Project already exists, no reason to re-create it + return + + local['git']['clone', self.git_repo_path, self.sha]() + with self.in_checkout(): + local['git']['checkout', self.sha]() + + def install(self): + with self.in_checkout(): + if local.path('setup.py').exists(): + local['virtualenv']['py_env']() + local['bash']['-c', 'source py_env/bin/activate && pip install .']() + + +def create_repo_in_env(git_repo_path, sha): + project = RepoInstaller(git_repo_path, sha) + project.create() + +def install_pre_commit(git_repo_path, sha): + project = RepoInstaller(git_repo_path, sha) + project.create() + project.install() \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 68613e1a..30cdf8ab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest +import pre_commit.constants as C from plumbum import local @@ -7,4 +8,73 @@ from plumbum import local def empty_git_dir(tmpdir): with local.cwd(tmpdir.strpath): local['git']['init']() - yield tmpdir.strpath \ No newline at end of file + yield tmpdir.strpath + + + +def add_and_commit(): + local['git']['add', '.']() + local['git']['commit', '-m', 'random commit', '--author', 'A U Thor ']() + + +@pytest.yield_fixture +def dummy_git_repo(empty_git_dir): + local['touch']['dummy']() + add_and_commit() + + yield empty_git_dir + + +@pytest.yield_fixture +def dummy_pre_commit_hooks_git_repo(dummy_git_repo): + local.path(C.MANIFEST_FILE).write(""" +hooks: + - + id: foo + name: Foo + entry: foo + language: python>2.6 + """) + + add_and_commit() + + yield dummy_git_repo + +@pytest.yield_fixture +def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo): + local.path('setup.py').write( +""" +from setuptools import find_packages +from setuptools import setup + +setup( + name='Foo', + version='0.0.0', + packages=find_packages('.'), + entry_points={ + 'console_scripts': [ + 'entry = foo.main:func' + ], + } +) +""" + ) + + foo_module = local.path('foo') + + foo_module.mkdir() + + with local.cwd(foo_module): + local.path('__init__.py').write('') + local.path('main.py').write( +""" + +def func(): + return 0 + +""" + ) + + add_and_commit() + + yield dummy_pre_commit_hooks_git_repo diff --git a/tests/git_test.py b/tests/git_test.py index bd00a6b7..9f413294 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -1,85 +1,9 @@ import os -import pytest from plumbum import local from pre_commit import git -import pre_commit.constants as C - -def add_and_commit(): - local['git']['add', '.']() - local['git']['config', 'user.email', 'ken@struys.ca'] - local['git']['config', 'user.name', 'Ken Struys'] - local['git']['commit', '-m', 'random commit']() - - -def get_sha(git_repo): - with local.cwd(git_repo): - return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n') - -@pytest.yield_fixture -def dummy_git_repo(empty_git_dir): - local['touch']['dummy']() - add_and_commit() - - yield empty_git_dir - - -@pytest.yield_fixture -def dummy_pre_commit_hooks_git_repo(dummy_git_repo): - local.path(C.MANIFEST_FILE).write(""" -hooks: - - - id: foo - name: Foo - entry: foo - language: python>2.6 - """) - - add_and_commit() - - yield dummy_git_repo - -@pytest.yield_fixture -def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo): - local.path('setup.py').write( -""" -from setuptools import find_packages -from setuptools import setup - -setup( - name='Foo', - version='0.0.0', - packages=find_packages('.'), - entry_points={ - 'console_scripts': [ - 'entry = foo.main:func' - ], - } -) -""" - ) - - foo_module = local.path('foo') - - foo_module.mkdir() - - with local.cwd(foo_module): - local.path('__init__.py').write('') - local.path('main.py').write( -""" - -def func(): - return 0 - -""" - ) - - add_and_commit() - - yield dummy_pre_commit_hooks_git_repo - def test_get_root(empty_git_dir): assert git.get_root() == empty_git_dir @@ -111,40 +35,3 @@ def test_remove_pre_commit(empty_git_dir): assert not os.path.exists(git.get_pre_commit_path()) -def test_create_repo_in_env(empty_git_dir, dummy_git_repo): - sha = get_sha(dummy_git_repo) - git.create_repo_in_env(dummy_git_repo, sha) - assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha)) - - -@pytest.mark.integration -def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo): - sha = get_sha(python_pre_commit_git_repo) - git.install_pre_commit(python_pre_commit_git_repo, sha) - assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env')) - -@pytest.mark.integration -def test_install_config(empty_git_dir, python_pre_commit_git_repo): - - - config = [ - { - 'repo': python_pre_commit_git_repo, - 'sha': get_sha(python_pre_commit_git_repo), - 'hooks': [ - { - 'id': 'foo', - 'args': [ - { - 'type': 'files', - 'opt': '*.py' - }, - ] - } - ], - }, - ] - for repo in config: - git.install_pre_commit(repo['repo'], repo['sha']) - - print python_pre_commit_git_repo \ No newline at end of file diff --git a/tests/installer/__init__.py b/tests/installer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/installer/repo_installer_test.py b/tests/installer/repo_installer_test.py new file mode 100644 index 00000000..bfa304dd --- /dev/null +++ b/tests/installer/repo_installer_test.py @@ -0,0 +1,52 @@ +import pytest +import os + +import pre_commit.constants as C +from plumbum import local +from pre_commit.installer.repo_installer import create_repo_in_env +from pre_commit.installer.repo_installer import install_pre_commit + + +def get_sha(git_repo): + with local.cwd(git_repo): + return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n') + +@pytest.mark.integration +def test_create_repo_in_env(empty_git_dir, dummy_git_repo): + sha = get_sha(dummy_git_repo) + create_repo_in_env(dummy_git_repo, sha) + + assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha)) + +@pytest.mark.integration +def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo): + sha = get_sha(python_pre_commit_git_repo) + install_pre_commit(python_pre_commit_git_repo, sha) + + assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env')) + + +@pytest.mark.integration +def test_install_config(empty_git_dir, python_pre_commit_git_repo): + + config = [ + { + 'repo': python_pre_commit_git_repo, + 'sha': get_sha(python_pre_commit_git_repo), + 'hooks': [ + { + 'id': 'foo', + 'args': [ + { + 'type': 'files', + 'opt': '*.py' + }, + ] + } + ], + }, + ] + for repo in config: + install_pre_commit(repo['repo'], repo['sha']) + + assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, config[0]['sha'], 'py_env')) \ No newline at end of file From 6cf21ec53319607351b83b824f8cf63c81ebec1a Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 17:38:16 -0700 Subject: [PATCH 5/5] fixing build --- .travis.yml | 5 +++++ tests/conftest.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6e98e9d5..976fde03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,8 @@ python: install: pip install virtualenv script: make + + +before_install: + - git config --global user.name "Travis CI" + - git config --global user.email "user@example.com" diff --git a/tests/conftest.py b/tests/conftest.py index 30cdf8ab..377a7bb5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,7 @@ def empty_git_dir(tmpdir): def add_and_commit(): local['git']['add', '.']() - local['git']['commit', '-m', 'random commit', '--author', 'A U Thor ']() + local['git']['commit', '-m', 'random commit']() @pytest.yield_fixture