Skip to content

Switch to setuptools #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 16 additions & 43 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import shlex
import subprocess
import sys
from pkg_resources import resource_listdir, resource_filename
from os.path import dirname, basename
from functools import partial

from typing import Undefined, Dict, List, Tuple, cast, Set

Expand Down Expand Up @@ -70,6 +72,9 @@
final_state = TYPE_CHECKED_STATE


mypy_filename = partial(resource_filename, 'mypy')


def earlier_state(s: int, t: int) -> bool:
return s < t

Expand Down Expand Up @@ -133,10 +138,8 @@ def build(program_path: str,
flags = flags or []
module = module or '__main__'

data_dir = default_data_dir(bin_dir)

# Determine the default module search path.
lib_path = default_lib_path(data_dir, target, pyversion)
lib_path = default_lib_path(target, pyversion)

if TEST_BUILTINS in flags:
# Use stub builtins (to speed up test cases and to make them easier to
Expand All @@ -159,7 +162,7 @@ def build(program_path: str,
# build in the correct order.
#
# Ignore current directory prefix in error messages.
manager = BuildManager(data_dir, lib_path, target, output_dir,
manager = BuildManager(lib_path, target, output_dir,
pyversion=pyversion, flags=flags,
ignore_prefix=os.getcwd(),
custom_typing_module=custom_typing_module,
Expand All @@ -181,31 +184,7 @@ def build(program_path: str,
return result


def default_data_dir(bin_dir: str) -> str:
if not bin_dir:
# Default to current directory.
return ''
base = os.path.basename(bin_dir)
dir = os.path.dirname(bin_dir)
if (sys.platform == 'win32' and base.lower() == 'scripts'
and not os.path.isdir(os.path.join(dir, 'stubs'))):
# Installed, on Windows.
return os.path.join(dir, 'Lib', 'mypy')
elif base == 'scripts':
# Assume that we have a repo check out or unpacked source tarball.
return os.path.dirname(bin_dir)
elif base == 'bin':
# Installed to somewhere (can be under /usr/local or anywhere).
return os.path.join(dir, 'lib', 'mypy')
elif base == 'python3':
# Assume we installed python3 with brew on os x
return os.path.join(os.path.dirname(dir), 'lib', 'mypy')
else:
# Don't know where to find the data files!
raise RuntimeError("Broken installation: can't determine base dir")


def default_lib_path(data_dir: str, target: int, pyversion: int) -> List[str]:
def default_lib_path(target: int, pyversion: int) -> List[str]:
"""Return default standard library search paths."""
# IDEA: Make this more portable.
path = List[str]()
Expand All @@ -214,23 +193,21 @@ def default_lib_path(data_dir: str, target: int, pyversion: int) -> List[str]:
path_env = os.getenv('MYPYPATH')
if path_env is not None:
path[:0] = path_env.split(os.pathsep)

if target in [ICODE, C]:
# Add C back end library directory.
path.append(os.path.join(data_dir, 'lib'))
path.append(mypy_filename('lib'))
else:
# Add library stubs directory. By convention, they are stored in the
# stubs/x.y directory of the mypy installation.
version_dir = '3.2'
if pyversion < 3:
version_dir = '2.7'
path.append(os.path.join(data_dir, 'stubs', version_dir))
path.append(os.path.join(data_dir, 'stubs-auto', version_dir))
path.append(mypy_filename(os.path.join('stubs', version_dir)))
#Add py3.3 and 3.4 stubs
if sys.version_info.major == 3:
versions = ['3.' + str(x) for x in range(3, sys.version_info.minor + 1)]
for v in versions:
path.append(os.path.join(data_dir, 'stubs', v))
path.extend([mypy_filename(os.path.join('stubs', fname))
for fname in resource_listdir('mypy', 'stubs')
if fname.startswith(str(sys.version_info.major))])

# Add fallback path that can be used if we have a broken installation.
if sys.platform != 'win32':
Expand Down Expand Up @@ -267,7 +244,6 @@ class BuildManager:
build steps.

Attributes:
data_dir: Mypy data directory (contains stubs)
target: Build target; selects which passes to perform
lib_path: Library path for looking up modules
semantic_analyzer:
Expand Down Expand Up @@ -297,16 +273,14 @@ class BuildManager:
external objects. This module should not directly depend on them.
"""

def __init__(self, data_dir: str,
lib_path: List[str],
def __init__(self, lib_path: List[str],
target: int,
output_dir: str,
pyversion: int,
flags: List[str],
ignore_prefix: str,
custom_typing_module: str,
html_report_dir: str) -> None:
self.data_dir = data_dir
self.errors = Errors()
self.errors.set_ignore_prefix(ignore_prefix)
self.lib_path = lib_path
Expand Down Expand Up @@ -559,8 +533,7 @@ def generate_c_and_compile(self, files: List[MypyFile]) -> None:

if COMPILE_ONLY not in self.flags:
# Generate binary file.
data_dir = self.data_dir
vm_dir = os.path.join(data_dir, 'vm')
vm_dir = mypy_filename('vm')
cc = os.getenv('CC', 'gcc')
cflags = shlex.split(os.getenv('CFLAGS', '-O2'))
cmdline = [cc] + cflags +['-I%s' % vm_dir,
Expand Down
File renamed without changes.
41 changes: 5 additions & 36 deletions scripts/mypy → mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ def __init__(self) -> None:


def main() -> None:
bin_dir = find_bin_directory()
path, module, args, options = process_options(sys.argv[1:])
try:
if options.target == build.TYPE_CHECK:
type_check_only(path, module, bin_dir, args, options)
type_check_only(path, module, args, options)
elif options.target == build.C:
compile_to_c(path, module, bin_dir, args, options)
compile_to_c(path, module, args, options)
else:
raise RuntimeError('unsupported target %d' % options.target)
except CompileError as e:
Expand All @@ -49,37 +48,11 @@ def main() -> None:
sys.exit(1)


def find_bin_directory() -> str:
"""Find the directory that contains this script.

This is used by build to find stubs and other data files.
"""
script = __file__
# Follow up to 5 symbolic links (cap to avoid cycles).
for i in range(5):
if os.path.islink(script):
script = readlinkabs(script)
else:
break
return os.path.dirname(script)


def readlinkabs(link: str) -> str:
"""Return an absolute path to symbolic link destination."""
# Adapted from code by Greg Smith.
assert os.path.islink(link)
path = os.readlink(link)
if os.path.isabs(path):
return path
return os.path.join(os.path.dirname(link), path)


def type_check_only(path: str, module: str, bin_dir: str, args: List[str],
def type_check_only(path: str, module: str, args: List[str],
options: Options) -> None:
# Type check the program and dependencies and translate to Python.
build.build(path,
module=module,
bin_dir=bin_dir,
target=build.TYPE_CHECK,
pyversion=options.pyversion,
custom_typing_module=options.custom_typing_module,
Expand All @@ -103,14 +76,14 @@ def type_check_only(path: str, module: str, bin_dir: str, args: List[str],
sys.exit(status)


def compile_to_c(path: str, module: str, bin_dir: str, args: List[str],
def compile_to_c(path: str, module: str, args: List[str],
options: Options) -> None:
assert not module # Not supported yet
assert not args # Not supported yet
assert options.pyversion == 3

# Compile the program to C (also generate binary by default).
result = build.build(path, target=build.C, bin_dir=bin_dir,
result = build.build(path, target=build.C,
flags=options.build_flags)

if build.COMPILE_ONLY not in options.build_flags:
Expand Down Expand Up @@ -194,7 +167,3 @@ def usage(msg: str = None) -> None:
def fail(msg: str) -> None:
sys.stderr.write('%s\n' % msg)
sys.exit(1)


if __name__ == '__main__':
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 9 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import os.path
import sys

from distutils.core import setup
from mypy.codec import register

from setuptools import setup, find_packages

if sys.version_info < (3, 2, 0):
sys.stderr.write("ERROR: You need Python 3.2 or later to use mypy.\n")
Expand All @@ -22,20 +24,6 @@
interpreter.
'''.lstrip()

stubs = []

for version in ['3.4', '3.3', '3.2', '2.7']:
base = os.path.join('stubs', version)
if not os.path.exists(base):
os.mkdir(base)

stub_dirs = [''] + [name for name in os.listdir(base)
if os.path.isdir(os.path.join(base, name))]
for stub_dir in stub_dirs:
target = os.path.join('lib', 'mypy', 'stubs', version, stub_dir)
files = glob.glob(os.path.join(base, stub_dir, '*.py'))
stubs.append((target, files))

classifiers = [
'Development Status :: 2 - Pre-Alpha',
'Environment :: Console',
Expand All @@ -56,10 +44,12 @@
url='http://www.mypy-lang.org/',
license='MIT License',
platforms=['POSIX'],
package_dir={'': 'lib-typing/3.2', 'mypy': 'mypy'},
package_dir={'typing': 'lib-typing/3.2/typing', 'mypy': 'mypy'},
package_data={'mypy': ['stubs/*/*.py', 'lib/*py', 'vm/*']},
py_modules=['typing'],
packages=['mypy'],
scripts=['scripts/mypy'],
data_files=stubs,
packages=find_packages(),
entry_points={
'console_scripts': ['mypy = mypy.main:main']
},
classifiers=classifiers,
)