Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 98e253f

Browse files
Make slicker pip-installable
Summary: This commit makes it so that slicker can be installed as a package! This required surprisingly many minor changes, in addition to the usual metadata/boilerplates. The main change is to the directory structure. I moved all the code into `slicker/`, because that's how python wants it. Then I moved the tests to a separate directory for consistency. This required updating all the imports, because we're now importing e.g. `slicker.khodemod`. (Actually we use relative imports.) Due to some quirk I don't understand, our circular import of `unicode_util` and `khodemod` no longer works this way, so I switched one to a late-import. I also removed requirements.txt (it's now `install_requires`). I kept the dev-only stuff in a requirements file because I don't really understand how `setup.py` wants to handle those. Then I updated the make rules to match -- `make dev_deps` now installs dev-only requirements whereas `make install` installs the package itself. I made the version 0.9; I think we can do 1.0 soon but I want to be a little clearer about the public API first, and maybe do a few other bits of housekeeping and cleanup. Test Plan: - `make dev_deps install test lint build` - `slicker main main2` in webapp did something plausibly reasonable Reviewers: csilvers Reviewed By: csilvers Differential Revision: https://phabricator.khanacademy.org/D42146
1 parent 57d2cbd commit 98e253f

17 files changed

+67
-34
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.pyc
2+
dist
3+
build
4+
slicker.egg-info

Makefile

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
deps:
2-
pip install -r requirements.txt
1+
dev_deps:
2+
pip install -r requirements.dev.txt
3+
4+
install:
5+
pip install -e .
36

47
test:
5-
python -m unittest discover
8+
python -m unittest discover tests
69

710
lint:
811
flake8
12+
13+
build: dev_deps
14+
python setup.py sdist # builds source distribution
15+
python setup.py bdist_wheel # builds wheel
16+
17+
release: build
18+
twine upload dist/*
19+
20+
.PHONY: deps test lint build release

README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ doing just that!
88

99
## Installation
1010

11-
- `git clone [email protected]:Khan/slicker.git`
12-
- `git submodule update --init`
13-
- `pip install -r requirements.txt`
14-
- optionally, add `slicker.py` to your path
15-
16-
TODO(benkraft): Make slicker pip installable.
11+
`pip install slicker`
1712

1813
## Usage
1914

requirements.dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flake8
2+
twine

requirements.txt

-6
This file was deleted.

setup.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
from setuptools import setup
4+
5+
setup(
6+
name='slicker',
7+
version='0.9',
8+
description='A tool for moving python files.',
9+
author='Khan Academy',
10+
author_email='[email protected]',
11+
url='https://github.com/Khan/slicker',
12+
keywords=['codemod', 'refactor', 'refactoring'],
13+
packages=['slicker'],
14+
install_requires=['asttokens==1.1.8', 'tqdm==4.19.5', 'fix-includes==0.2'],
15+
entry_points={
16+
# setuptools magic to make a `slicker` binary
17+
'console_scripts': ['slicker = slicker.slicker:main'],
18+
},
19+
classifiers=[
20+
'License :: OSI Approved :: MIT License',
21+
'Programming Language :: Python :: 2',
22+
'Programming Language :: Python :: 2.7',
23+
],
24+
)

slicker/__init__.py

Whitespace-only changes.

inputs.py slicker/inputs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
import os
2727

28-
import khodemod
29-
import util
28+
from . import khodemod
29+
from . import util
3030

3131

3232
def _expand_and_normalize_one(project_root, old_fullname, new_fullname,

khodemod.py slicker/khodemod.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040

4141
import tqdm
4242

43-
import unicode_util
44-
4543

4644
DEFAULT_EXCLUDE_PATHS = ('genfiles', 'third_party')
4745
DEFAULT_EXTENSIONS = ('py',)
@@ -185,6 +183,7 @@ def read_file(root, filename):
185183
# TODO(benkraft): Cache contents.
186184
try:
187185
with open(os.path.join(root, filename)) as f:
186+
from . import unicode_util
188187
return unicode_util.decode(filename, f.read())
189188
except IOError as e:
190189
if e.errno == 2: # No such file
@@ -321,6 +320,7 @@ def write_file(self, root, filename, text, file_permissions=None):
321320
# We changed what files exist: clear the cache.
322321
_RESOLVE_PATHS_CACHE.clear()
323322
with open(abspath, 'w') as f:
323+
from . import unicode_util
324324
f.write(unicode_util.encode(filename, text))
325325
self._modified_files.add((root, filename))
326326
if file_permissions:

moves.py slicker/moves.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import os
66
import stat
77

8-
import khodemod
9-
import util
8+
from . import khodemod
9+
from . import util
1010

1111

1212
def _add_init_py(filename):

slicker.py slicker/slicker.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@
8383

8484
from fix_includes import fix_python_imports
8585

86-
import inputs
87-
import khodemod
88-
import moves
89-
import util
86+
from . import inputs
87+
from . import khodemod
88+
from . import moves
89+
from . import util
9090

9191

9292
_FILENAME_EXTENSIONS = ('.py', '.js', '.jsx', '.png', '.jpg', '.svg', '.html',
@@ -1716,4 +1716,6 @@ def main():
17161716

17171717

17181718
if __name__ == '__main__':
1719+
# Note that pip-installed slicker calls main() directly, rather than
1720+
# running this file as a script; this is just included for completeness.
17191721
main()

unicode_util.py slicker/unicode_util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import re
2121

22-
import khodemod
22+
from . import khodemod
2323

2424

2525
# From PEP 263: https://www.python.org/dev/peps/pep-0263/

util.py slicker/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import asttokens
88

9-
import khodemod
10-
import unicode_util
9+
from . import khodemod
10+
from . import unicode_util
1111

1212

1313
def filename_for_module_name(module_name):

test_inputs.py tests/test_inputs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
import inputs
5+
from slicker import inputs
66
import test_slicker
77

88

test_khodemod.py tests/test_khodemod.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import absolute_import
22

3-
import khodemod
3+
from slicker import khodemod
44
import test_slicker
55

66

test_moves.py tests/test_moves.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import stat
55

6-
import slicker
6+
from slicker import slicker
77
import test_slicker
88

99

test_slicker.py tests/test_slicker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import tempfile
77
import unittest
88

9-
import khodemod
10-
import slicker
11-
import util
9+
from slicker import khodemod
10+
from slicker import slicker
11+
from slicker import util
1212

1313

1414
class TestBase(unittest.TestCase):

0 commit comments

Comments
 (0)