Skip to content

Commit

Permalink
Fix install-local.py Resolves pre-commit#52
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Feb 21, 2016
1 parent 00df256 commit 0fc0497
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 93 deletions.
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
.idea
.project
.pydevproject
all-hooks.json
bower_components
build
install-local.py
node_env
py_env
/all-hooks.json
/bower_components
/build
/nenv
/venv
9 changes: 7 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
- repo: git@github.com:pre-commit/mirrors-scss-lint
sha: 3eb13b9647543ad4d6a62c8be8a9131e3b99b96a
sha: f840ac610d8d9d76b666062416241f2156b3f388
hooks:
- id: scss-lint
language_version: 1.9.3-p547
- repo: git@github.com:pre-commit/pre-commit-hooks
sha: 516cc9fa72ad09699f2c03ffbd0aa7f60d75b59a
sha: 97b88d9610bcc03982ddac33caba98bb2b751f5f
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: autopep8-wrapper
- id: check-yaml
- id: debug-statements
- id: flake8
- repo: https://github.com/asottile/reorder_python_imports.git
sha: 50e0be95e292cac913cc3c6fd44b3d6b51d104c5
hooks:
- id: reorder-python-imports
language_version: python2.7
42 changes: 19 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
# To build scss continuously I use `watch -n 0.1 make build/main.css`

all: install_pre_commit build/main.css all-hooks.json index.html hooks.html install-local.py
all: install-hooks build/main.css all-hooks.json index.html hooks.html

.PHONY: install_pre_commit
install_pre_commit: py_env
. py_env/bin/activate && pre-commit install
.PHONY: install-hooks
install-hooks: venv
venv/bin/pre-commit install

build/main.css: node_env build scss/main.scss scss/_variables.scss
. py_env/bin/activate && sassc -s compressed scss/main.scss build/main.css
build/main.css: nenv build scss/main.scss scss/_variables.scss
venv/bin/sassc -s compressed scss/main.scss build/main.css

all-hooks.json: py_env make_all_hooks.py all-repos.yaml
. py_env/bin/activate && python make_all_hooks.py
all-hooks.json: venv make_all_hooks.py all-repos.yaml
venv/bin/python make_all_hooks.py

index.html hooks.html: py_env all-hooks.json base.mako index.mako hooks.mako make_templates.py
. py_env/bin/activate && python make_templates.py
index.html hooks.html: venv all-hooks.json base.mako index.mako hooks.mako make_templates.py
venv/bin/python make_templates.py

install-local.py: py_env make_bootstrap.py
. py_env/bin/activate && python make_bootstrap.py
venv: requirements-dev.txt
rm -rf venv
virtualenv venv
venv/bin/pip install -r requirements-dev.txt

py_env: requirements-dev.txt
rm -rf py_env
virtualenv py_env
. py_env/bin/activate && pip install -r requirements-dev.txt

node_env: py_env
rm -rf node_env
. py_env/bin/activate && \
nodeenv node_env --prebuilt && \
. node_env/bin/activate && \
nenv: venv
rm -rf nenv
venv/bin/nodeenv nenv --prebuilt && \
. nenv/bin/activate && \
npm install -g bower && \
bower install

clean:
rm -rf py_env node_env build bower_components *.html install-local.py all-hooks.json
rm -rf venv nenv build bower_components *.html all-hooks.json

build:
mkdir -p build
Expand Down
92 changes: 92 additions & 0 deletions install-local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import unicode_literals

import contextlib
import distutils.spawn
import io
import os.path
import shutil
import subprocess
import sys
import tarfile


if str is bytes:
from urllib import urlopen
else:
from urllib.request import urlopen


TGZ = (
'https://pypi.python.org/packages/source/v/virtualenv/'
'virtualenv-1.11.6.tar.gz'
)
PKG_PATH = '/tmp/.virtualenv-pkg'


def clean():
if os.path.exists(PKG_PATH):
shutil.rmtree(PKG_PATH)


@contextlib.contextmanager
def clean_path():
try:
yield
finally:
clean()


def virtualenv(path):
clean()

print('Downloading ' + TGZ)
tar_contents = io.BytesIO(urlopen(TGZ).read())
with contextlib.closing(tarfile.open(fileobj=tar_contents)) as tarfile_obj:
# Chop off the first path segment to avoid having the version in
# the path
for member in tarfile_obj.getmembers():
_, _, member.name = member.name.partition('/')
if member.name:
tarfile_obj.extract(member, PKG_PATH)
print('Done.')

with clean_path():
return subprocess.call((
sys.executable, os.path.join(PKG_PATH, 'virtualenv.py'), path,
))


def main():
venv_path = os.path.join(os.environ['HOME'], '.pre-commit-venv')
virtualenv(venv_path)

subprocess.check_call((
os.path.join(venv_path, 'bin', 'pip'), 'install', 'pre-commit',
))

bin_dir = os.path.join(os.environ['HOME'], 'bin')
script_src = os.path.join(venv_path, 'bin', 'pre-commit')
script_dest = os.path.join(bin_dir, 'pre-commit')
print('*' * 79)
print('Installing pre-commit to {0}'.format(script_dest))
print('*' * 79)

if not os.path.exists(bin_dir):
os.mkdir(bin_dir)

# os.symlink is not idempotent
if os.path.exists(script_dest):
os.remove(script_dest)

os.symlink(script_src, script_dest)

if not distutils.spawn.find_executable('pre-commit'):
print('It looks like {0} is not on your path'.format(bin_dir))
print('You may want to add it.')
print('Often this does the trick: source ~/.profile')


if __name__ == '__main__':
exit(main())
57 changes: 0 additions & 57 deletions make_bootstrap.py

This file was deleted.

2 changes: 0 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
aspy.yaml
flake8
mako
libsass
nodeenv
pre-commit
virtualenv
3 changes: 0 additions & 3 deletions setup.cfg

This file was deleted.

0 comments on commit 0fc0497

Please sign in to comment.