Repository: SoftwareUnderstanding/inspect4py (language: python)

Issue:
Convert code inspector into a package
It would be great to run code_inspector as a command (maybe ci? or si?) instead of having to call python name of script, etc.


Discussion / hints:

Fix patch:
diff --git a/.gitignore b/.gitignore
index 8532534..e752ca7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ OutputDir/
 output_dir/
 defoe/*
 env_*
+code_inspector.egg-info/*
diff --git a/README.md b/README.md
index aacd4fa..c89153a 100644
--- a/README.md
+++ b/README.md
@@ -50,11 +50,11 @@ First, make sure you have graphviz installed:
 sudo apt-get install graphviz
 ```
 
-Then, prepare a virtual Python3 enviroment and install the required packages.
+Then, prepare a virtual Python3 enviroment, `cd` into the `code_inspector` folder and install the package as follows:
 
-`pip install -r requirements.txt`
+`pip install -e .`
 
-- Dependencies: 
+### Dependencies: 
   - cdmcfparser==2.3.2
   - docstring_parser==0.7
   - astor
@@ -105,17 +105,18 @@ The tool by default stores the results in the "OutputDir" directory, but users c
 And the tools allows users to specify if control flow figures will be generated or not. By default they wont be generated. To indicate the generation of control flow figures, users should use -f or --fig.  
 
 ```
-python -m code_inspector.code_inspector_cli --input_path <FILE.py | DIRECTORY> [--fig , --output_dir "OutputDir", --ignore_dir_pattern "__", ignore_file_pattern "__" --requirements --html_output]
+code_inspector --input_path <FILE.py | DIRECTORY> [--fig , --output_dir "OutputDir", --ignore_dir_pattern "__", ignore_file_pattern "__" --requirements --html_output]
 ```
 
 
 For clarity, we have added the help option to explain each input parameters
 
-```python -m code_inspector.code_inspector_cli --help
+```code_inspector --help
 
-Usage: code_inspector.py [OPTIONS]
+Usage: code_inspector [OPTIONS]
 
 Options:
+  --version                       Show the version and exit.
   -i, --input_path TEXT           input path of the file or directory to
                                   inspect.  [required]
   -f, --fig                       activate the control_flow figure generator.
diff --git a/code_inspector/__init__.py b/code_inspector/__init__.py
index b3c06d4..b8023d8 100644
--- a/code_inspector/__init__.py
+++ b/code_inspector/__init__.py
@@ -1,1 +1,1 @@
-__version__ = "0.0.1"
\ No newline at end of file
+__version__ = '0.0.1'
diff --git a/code_inspector/code_inspector_cli.py b/code_inspector/code_inspector_cli.py
index de26aad..5376a73 100644
--- a/code_inspector/code_inspector_cli.py
+++ b/code_inspector/code_inspector_cli.py
@@ -18,6 +18,7 @@
 from cdmcfparser import getControlFlowFromFile
 from docstring_parser import parse as doc_parse
 
+from code_inspector import __version__
 from code_inspector.staticfg import builder
 from code_inspector.utils import *
 
@@ -726,6 +727,7 @@ def create_output_dirs(output_dir, control_flow):
 
 
 @click.command()
+@click.version_option(__version__)
 @click.option('-i', '--input_path', type=str, required=True, help="input path of the file or directory to inspect.")
 @click.option('-f', '--fig', type=bool, is_flag=True, help="activate the control_flow figure generator.")
 @click.option('-o', '--output_dir', type=str, default="output_dir",
diff --git a/code_inspector/main_command_evaluation/run_directory_type_evaluation.py b/code_inspector/main_command_evaluation/run_directory_type_evaluation.py
index f249726..207a3c5 100644
--- a/code_inspector/main_command_evaluation/run_directory_type_evaluation.py
+++ b/code_inspector/main_command_evaluation/run_directory_type_evaluation.py
@@ -62,8 +62,7 @@ def extract_types_from_response(response_data):
 
 for dir_name in os.listdir(repo_path):
     print("######## Processing: " + dir_name) # repo_path
-    repo_path_tmp = repo_path.replace("../../../", "../") # Temporary hack until we invoke package in non-module mode
-    cmd = 'cd ../.. && python -m code_inspector.code_inspector_cli -i ' + repo_path_tmp + dir_name + " -o output_dir/ -si"
+    cmd = 'code_inspector -i ' + repo_path + dir_name + " -o ../../output_dir/ -si"
     proc = subprocess.Popen(cmd.encode('utf-8'), shell=True, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     stdout, stderr = proc.communicate()
@@ -137,7 +136,3 @@ def extract_types_from_response(response_data):
 
 print("Accuracy (entities): " + str(num_correct_entity) + " out of " + str(num_analyses_entity) +
       ". Num errors = " + str(num_error_entity) + ". " + str(num_correct_entity / num_analyses_entity))
-
-# TO DO
-# print("Accuracy (entity): " + str(num_correct) + " out of " + str(num_analyses) + ". Num errors=" + str(num_error) + ". " + str(
-#    num_correct / num_analyses))
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..ea5f93d
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,43 @@
+import os
+from distutils.core import setup
+from setuptools import find_packages  # type: ignore
+
+from code_inspector import __version__
+
+
+def read(fname):
+    return open(os.path.join(os.path.dirname(__file__), fname)).read()
+
+
+with open('requirements.txt', 'r') as f:
+    install_requires = list()
+    dependency_links = list()
+    for line in f:
+        re = line.strip()
+        if re:
+            if re.startswith('git+') or re.startswith('svn+') or re.startswith('hg+'):
+                dependency_links.append(re)
+            else:
+                install_requires.append(re)
+
+packages = find_packages()
+
+setup(
+    name='code_inspector',
+    version=__version__,
+    packages=packages,
+    url='https://github.com/SoftwareUnderstanding/code_inspector',
+    license='Apache2.0',
+    author='Rosa Filgueira and Daniel Garijo',
+    description='Package for performing static code analysis on Python projects',
+    long_description=read("README.md"),
+    long_description_content_type="text/markdown",
+    include_package_data=True,
+    install_requires=install_requires,
+    dependency_links=dependency_links,
+    entry_points={
+        'console_scripts': [
+            'code_inspector = code_inspector.code_inspector_cli:main',
+        ],
+    }
+)


Test patch:
diff --git a/test/test_code_inspector.py b/test/test_code_inspector.py
index ec7fe6a..704cc8d 100644
--- a/test/test_code_inspector.py
+++ b/test/test_code_inspector.py
@@ -7,7 +7,7 @@ def test_extract_software_type(self):
         json = [{"type": "service",
                  "run": "python /GitHub/test_repos/Chowlk/app.py"},
                 {"type": "script with main",
-                 "run": "python //GitHub/test_repos/Chowlk/converter.py"}]
+                 "run": "python /GitHub/test_repos/Chowlk/converter.py"}]
         type_script = extract_software_type(json)
         assert (type_script == "service")