Skip to content

Commit

Permalink
Move pip._internal:main to its own module
Browse files Browse the repository at this point in the history
Moving content out of `__init__` is preferred in general because it
avoids conflicts with module names and unnecessary imports.
  • Loading branch information
chrahunt committed Sep 21, 2019
1 parent 43864b8 commit 09fd200
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def find_version(*file_paths):
},
entry_points={
"console_scripts": [
"pip=pip._internal:main",
"pip%s=pip._internal:main" % sys.version_info[:1],
"pip%s.%s=pip._internal:main" % sys.version_info[:2],
"pip=pip._internal.main:main",
"pip%s=pip._internal.main:main" % sys.version_info[:1],
"pip%s.%s=pip._internal.main:main" % sys.version_info[:2],
],
},

Expand Down
2 changes: 1 addition & 1 deletion src/pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
path = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, path)

from pip._internal import main as _main # isort:skip # noqa
from pip._internal.main import main as _main # isort:skip # noqa

if __name__ == '__main__':
sys.exit(_main())
38 changes: 0 additions & 38 deletions src/pip/_internal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python
from __future__ import absolute_import

import locale
import logging
import os
import sys
import warnings

# We ignore certain warnings from urllib3, since they are not relevant to pip's
Expand All @@ -16,44 +12,10 @@

import pip._internal.utils.inject_securetransport # noqa
from pip._internal.cli.autocompletion import autocomplete
from pip._internal.cli.main_parser import parse_command
from pip._internal.commands import create_command
from pip._internal.exceptions import PipError
from pip._internal.utils import deprecation

# Raised when using --trusted-host.
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
# Raised since socks support depends on PySocks, which may not be installed.
# Barry Warsaw noted (on 2016-06-17) that this should be done before
# importing pip.vcs, which has since moved to pip._internal.vcs.
warnings.filterwarnings("ignore", category=DependencyWarning)

logger = logging.getLogger(__name__)


def main(args=None):
if args is None:
args = sys.argv[1:]

# Configure our deprecation warnings to be sent through loggers
deprecation.install_warning_logger()

autocomplete()

try:
cmd_name, cmd_args = parse_command(args)
except PipError as exc:
sys.stderr.write("ERROR: %s" % exc)
sys.stderr.write(os.linesep)
sys.exit(1)

# Needed for locale.getpreferredencoding(False) to work
# in pip._internal.utils.encoding.auto_decode
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error as e:
# setlocale can apparently crash if locale are uninitialized
logger.debug("Ignoring error %s when setting locale", e)
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))

return command.main(cmd_args)
44 changes: 44 additions & 0 deletions src/pip/_internal/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Primary application entrypoint.
"""
from __future__ import absolute_import

import locale
import logging
import os
import sys

from pip._internal.cli.autocompletion import autocomplete
from pip._internal.cli.main_parser import parse_command
from pip._internal.commands import create_command
from pip._internal.exceptions import PipError
from pip._internal.utils import deprecation

logger = logging.getLogger(__name__)


def main(args=None):
if args is None:
args = sys.argv[1:]

# Configure our deprecation warnings to be sent through loggers
deprecation.install_warning_logger()

autocomplete()

try:
cmd_name, cmd_args = parse_command(args)
except PipError as exc:
sys.stderr.write("ERROR: %s" % exc)
sys.stderr.write(os.linesep)
sys.exit(1)

# Needed for locale.getpreferredencoding(False) to work
# in pip._internal.utils.encoding.auto_decode
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error as e:
# setlocale can apparently crash if locale are uninitialized
logger.debug("Ignoring error %s when setting locale", e)
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))

return command.main(cmd_args)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import six
from setuptools.wheel import Wheel

import pip._internal
import pip._internal.main
from tests.lib import DATA_DIR, SRC_DIR, TestData
from tests.lib.path import Path
from tests.lib.scripttest import PipTestEnvironment
Expand Down Expand Up @@ -342,7 +342,7 @@ def pip(self, *args):
stdout = io.BytesIO()
sys.stdout = stdout
try:
returncode = pip._internal.main(list(args))
returncode = pip._internal.main.main(list(args))
except SystemExit as e:
returncode = e.code or 0
finally:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pytest

import pip._internal.configuration
from pip._internal import main
from pip._internal.commands import create_command
from pip._internal.exceptions import PipError
from pip._internal.main import main
from tests.lib.options_helpers import AddFakeCommandMixin


Expand Down

0 comments on commit 09fd200

Please sign in to comment.