"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "client/commands/initialize.py" between
pyre-check-0.0.54.tar.gz and pyre-check-0.0.55.tar.gz

About: Pyre is a performant type checker for Python (ships with Pysa, a security focused static analysis tool).

initialize.py  (pyre-check-0.0.54):initialize.py  (pyre-check-0.0.55)
skipping to change at line 15 skipping to change at line 15
import argparse import argparse
import json import json
import logging import logging
import os import os
import shutil import shutil
import subprocess import subprocess
import sys import sys
from logging import Logger from logging import Logger
from pathlib import Path from pathlib import Path
from typing import Any, Dict from typing import Any, Dict, Optional
from .. import log from .. import log
from ..exceptions import EnvironmentException
from ..find_directories import ( from ..find_directories import (
BINARY_NAME, BINARY_NAME,
CONFIGURATION_FILE, CONFIGURATION_FILE,
find_global_root, find_global_root,
find_taint_models_directory, find_taint_models_directory,
find_typeshed, find_typeshed,
) )
from .command import CommandParser from .command import CommandParser
LOG: Logger = logging.getLogger(__name__) LOG: Logger = logging.getLogger(__name__)
class InitializationException(Exception):
pass
class Initialize(CommandParser): class Initialize(CommandParser):
NAME = "initialize" NAME = "initialize"
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
@staticmethod @staticmethod
def from_arguments() -> "Initialize": def from_arguments() -> "Initialize":
return Initialize() return Initialize()
skipping to change at line 55 skipping to change at line 57
"--local", "--local",
action="store_true", action="store_true",
help="[DEPRECATED] Initializes a local configuration \ help="[DEPRECATED] Initializes a local configuration \
in a project subdirectory.", in a project subdirectory.",
) )
def _get_configuration(self) -> Dict[str, Any]: def _get_configuration(self) -> Dict[str, Any]:
configuration: Dict[str, Any] = {} configuration: Dict[str, Any] = {}
watchman_configuration_path = os.path.abspath(".watchmanconfig") watchman_configuration_path = os.path.abspath(".watchmanconfig")
if shutil.which("watchman") is not None and log.get_yes_no_input( watchman_path = shutil.which("watchman")
"Also initialize a watchman configuration?" if watchman_path is not None and log.get_yes_no_input(
"Also initialize watchman in the current directory?"
): ):
try: try:
with open(watchman_configuration_path, "w+") as configuration_fi if not os.path.isfile(watchman_configuration_path):
le: with open(watchman_configuration_path, "w+") as configuratio
configuration_file.write("{}\n") n_file:
subprocess.check_call(["watchman", "watch-project", "."]) configuration_file.write("{}\n")
except (IsADirectoryError, subprocess.CalledProcessError): LOG.warning(
LOG.warning("Unable to initialize watchman for the current direc f"Created basic `.watchmanconfig` at {watchman_configura
tory.") tion_path}"
)
subprocess.run(
[watchman_path, "watch-project", "."],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
LOG.warning("Current directory is being watched by `watchman`.")
except IsADirectoryError:
LOG.warning(f"Unable to write to {watchman_configuration_path}."
)
except subprocess.CalledProcessError:
LOG.warning("Failed to run `watchman watch-project .`.")
binary_path = shutil.which(BINARY_NAME) binary_path = shutil.which(BINARY_NAME)
if binary_path is None: if binary_path is None:
binary_path = shutil.which( binary_path = shutil.which(
os.path.join(os.path.dirname(sys.argv[0]), BINARY_NAME) os.path.join(os.path.dirname(sys.argv[0]), BINARY_NAME)
) )
if binary_path is None: if binary_path is None:
binary_path = os.path.abspath( binary_path = os.path.abspath(
log.get_input( log.get_input(
"No `{}` found, enter the path manually: ".format(BINARY_NAM E) "No `{}` found, enter the path manually: ".format(BINARY_NAM E)
) )
) )
if not os.path.isfile(binary_path): if not os.path.isfile(binary_path):
raise EnvironmentException( raise InitializationException(
"Unable to locate binary at `{}`.".format(binary_path) "Unable to locate binary at `{}`.".format(binary_path)
) )
else: else:
LOG.info("Binary found at `{}`".format(binary_path)) LOG.info("Binary found at `{}`".format(binary_path))
configuration["binary"] = binary_path configuration["binary"] = binary_path
typeshed = find_typeshed() typeshed: Optional[Path] = find_typeshed()
if typeshed is None: if typeshed is None:
typeshed = os.path.abspath( typeshed = Path(
log.get_input("Unable to locate typeshed, please enter its root: ") log.get_input("Unable to locate typeshed, please enter its root: ")
) ).resolve()
if not os.path.isdir(typeshed): if not typeshed.is_dir():
raise EnvironmentException( raise InitializationException(
"No typeshed directory found at `{}`.".format(typeshed) "No typeshed directory found at `{}`.".format(typeshed)
) )
configuration["typeshed"] = typeshed configuration["typeshed"] = str(typeshed)
taint_models_path = find_taint_models_directory() taint_models_path = find_taint_models_directory()
if taint_models_path is not None: if taint_models_path is not None:
configuration["taint_models_path"] = str(taint_models_path) configuration["taint_models_path"] = str(taint_models_path)
analysis_directory = log.get_optional_input( analysis_directory = log.get_optional_input(
"Which directory should pyre be initialized in?", "." "Which directory should pyre be initialized in?", "."
) )
configuration["source_directories"] = [analysis_directory] configuration["source_directories"] = [analysis_directory]
skipping to change at line 128 skipping to change at line 144
) )
configuration["source_directories"] = [ configuration["source_directories"] = [
directory.strip() for directory in source_directories.split(",") directory.strip() for directory in source_directories.split(",")
] ]
return configuration return configuration
def _is_local(self) -> bool: def _is_local(self) -> bool:
return find_global_root(Path(".")) is not None return find_global_root(Path(".")) is not None
def _run(self) -> None: def _run(self) -> None:
is_local = self._is_local() try:
current_directory = os.getcwd() is_local = self._is_local()
configuration_path = os.path.join(current_directory, CONFIGURATION_FILE) current_directory = os.getcwd()
if os.path.isfile(configuration_path): configuration_path = os.path.join(current_directory, CONFIGURATION_F
if is_local: ILE)
error = ( if os.path.isfile(configuration_path):
"Local configurations must be created in subdirectories of ` if is_local:
{}`" error = (
"as it already contains a `.pyre_configuration`.".format( "Local configurations must be created in subdirectories
current_directory of `{}`"
"as it already contains a `.pyre_configuration`.".format
(
current_directory
)
)
else:
error = "A pyre configuration already exists at `{}`.".forma
t(
configuration_path
)
raise InitializationException(error)
if os.path.isfile(configuration_path + ".local"):
raise InitializationException(
"A local pyre configuration already exists at `{}`.".format(
configuration_path + ".local"
) )
) )
if is_local:
configuration_path = configuration_path + ".local"
configuration = self._get_local_configuration()
else: else:
error = "A pyre configuration already exists at `{}`.".format( configuration = self._get_configuration()
configuration_path
)
raise EnvironmentException(error)
if os.path.isfile(configuration_path + ".local"):
raise EnvironmentException(
"A local pyre configuration already exists at `{}`.".format(
configuration_path + ".local"
)
)
if is_local:
configuration_path = configuration_path + ".local"
configuration = self._get_local_configuration()
else:
configuration = self._get_configuration()
with open(configuration_path, "w+") as configuration_file: with open(configuration_path, "w+") as configuration_file:
json.dump(configuration, configuration_file, sort_keys=True, indent= json.dump(configuration, configuration_file, sort_keys=True, ind
2) ent=2)
configuration_file.write("\n") configuration_file.write("\n")
LOG.info( LOG.log(
"Successfully initialized pyre! " log.SUCCESS,
+ "You can view the configuration at `{}`.".format(configuration_pat "Successfully initialized pyre! "
h) + "You can view the configuration at `{}`.".format(configuration
) _path),
)
except InitializationException as error:
LOG.error(f"{error}")
 End of changes. 14 change blocks. 
43 lines changed or deleted 64 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)