Skip to content
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
35 changes: 32 additions & 3 deletions cli/commands/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@
from spice.analyze import analyze_file


def analyze_to_dict(file, selected_stats=None):
"""
Analyze the given file and return results as a dictionary.
This function is used by tests and the JSON output functionality.

Args:
file (str): Path to the file to analyze
selected_stats (list): List of stats to include in analysis

Returns:
dict: Analysis results in dictionary format
"""
# If no stats specified, use all available stats
if selected_stats is None:
selected_stats = [
"line_count",
"function_count",
"comment_line_count",
"inline_comment_count",
"external_dependencies_count",
"indentation_level"
]

# Get analysis results from analyze_file
return analyze_file(file, selected_stats=selected_stats)


def analyze_command(file, all, json_output, LANG_FILE):
"""
Analyze the given file.
Expand All @@ -18,6 +45,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
"function_count",
"comment_line_count",
"inline_comment_count",
"external_dependencies_count",
"indentation_level"
]

Expand All @@ -27,6 +55,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
"function_count": messages.get("function_count_option", "Function Count"),
"comment_line_count": messages.get("comment_line_count_option", "Comment Line Count"),
"inline_comment_count": messages.get("inline_comment_count_option", "Inline Comment Count"),
"external_dependencies_count": messages.get("external_dependencies_count_option", "External Dependencies Count"),
"indentation_level": messages.get("indentation_level_option", "Indentation Analysis")
}

Expand Down Expand Up @@ -68,8 +97,8 @@ def analyze_command(file, all, json_output, LANG_FILE):
if not json_output:
print(f"{messages.get('analyzing_file', 'Analyzing file')}: {file}")

# get analysis results from analyze_file
results = analyze_file(file, selected_stats=selected_stat_keys)
# get analysis results using analyze_to_dict function
results = analyze_to_dict(file, selected_stats=selected_stat_keys)

# output in JSON format if flag
if json_output:
Expand All @@ -91,4 +120,4 @@ def analyze_command(file, all, json_output, LANG_FILE):
error_msg = str(e).replace('\n', ' ')
print(json.dumps({"error": error_msg}))
else:
print(f"{messages.get('error', 'Error')}: {e}")
print(f"{messages.get('error', 'Error')}: {e}")
2 changes: 2 additions & 0 deletions cli/translations/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"function_count": "The file has {count} functions",
"comment_line_count": "The file has {count} comment lines",
"inline_comment_count": "The file has {count} inline comments",
"external_dependencies_count": "The file has {count} external dependencies",
# keys for analyze command checkbox menu
"select_stats": "Select stats to display:",
"line_count_option": "Line Count",
"function_count_option": "Function Count",
"comment_line_count_option": "Comment Line Count",
"inline_comment_count_option": "Inline Comment Count",
"external_dependencies_count_option": "External Dependencies Count",
"no_stats_selected": "No stats selected. Analysis cancelled.",
"confirm_and_analyze": "Confirm and analyze",
"checkbox_hint": "(Use space to select, enter to confirm)",
Expand Down
2 changes: 2 additions & 0 deletions cli/translations/fremen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"function_count": "The file holds {count} sacred routines",
"comment_line_count": "The file whispers {count} lines of hidden lore",
"inline_comment_count": "The file contains {count} passages of dual meaning",
"external_dependencies_count": "The file summons {count} foreign powers from beyond the sands",
# keys for analyze command checkbox menu
"select_stats": "Choose the omens to unveil:",
"line_count_option": "Dune Count",
"function_count_option": "Sacred Routines",
"comment_line_count_option": "Whispered Lore",
"inline_comment_count_option": "Passages of Dual Meaning",
"external_dependencies_count_option": "Foreign Powers Beyond the Sands",
"no_stats_selected": "No omens were heeded. The analysis fades into the sands.",
"confirm_and_analyze": "Seal your fate and analyze",
"checkbox_hint": "(Use space to mark, enter to proceed)"
Expand Down
2 changes: 2 additions & 0 deletions cli/translations/pt-br.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"function_count": "O arquivo tem {count} funções",
"comment_line_count": "O arquivo tem {count} linhas de comentário",
"inline_comment_count": "O arquivo tem {count} comentários inline",
"external_dependencies_count": "O arquivo tem {count} dependências externas",
# chaves para o menu de seleção do comando analyze
"select_stats": "Selecione as estatísticas para exibir:",
"line_count_option": "Contagem de Linhas",
"function_count_option": "Contagem de Funções",
"comment_line_count_option": "Contagem de Linhas de Comentário",
"inline_comment_count_option": "Contagem de Comentários Inline",
"external_dependencies_count_option": "Contagem de Dependências Externas",
"no_stats_selected": "Nenhuma estatística selecionada. Análise cancelada.",
"confirm_and_analyze": "Confirmar e analisar",
"checkbox_hint": "(Use espaço para selecionar, enter para confirmar)"
Expand Down
16 changes: 14 additions & 2 deletions spice/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
file_path (str): Path to the file to analyze
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
Valid stats are: "line_count", "function_count", "comment_line_count",
"inline_comment_count", "indentation_level"
"inline_comment_count", "external_dependencies_count", "indentation_level"

Returns:
dict: Dictionary containing the requested stats and file information
Expand All @@ -35,7 +35,14 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
raise ValueError("File has no extension")

# Define valid stats
valid_stats = ["line_count", "function_count", "comment_line_count", "inline_comment_count", "indentation_level"]
valid_stats = [
"line_count",
"function_count",
"comment_line_count",
"inline_comment_count",
"external_dependencies_count",
"indentation_level"
]

# default to all stats if none specified
if selected_stats is None:
Expand Down Expand Up @@ -79,6 +86,11 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
LexerClass = get_lexer_for_file(file_path)
lexer = LexerClass(source_code=code) # Pass source_code explicitly
results["inline_comment_count"] = count_inline_comments(file_path)

# external dependencies count if requested
if "external_dependencies_count" in selected_stats:
from spice.analyzers.count_external_dependencies import count_external_dependencies
results["external_dependencies_count"] = count_external_dependencies(file_path)

# indentation analysis if requested
if "indentation_level" in selected_stats:
Expand Down
196 changes: 196 additions & 0 deletions spice/analyzers/count_external_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# this will count external dependencies (imports/requires from external libraries)
# EXTERNAL DEPENDENCY is an import/require statement that references an external library
from utils.get_lexer import get_lexer_for_file
from lexers.token import TokenType
import os
import re
import sys

# Python standard library modules to exclude from dependency count
PYTHON_STD_LIBS = set([
'abc', 'argparse', 'ast', 'asyncio', 'base64', 'collections', 'concurrent', 'contextlib',
'copy', 'csv', 'ctypes', 'datetime', 'decimal', 'enum', 'functools', 'glob', 'gzip',
'hashlib', 'http', 'importlib', 'inspect', 'io', 'itertools', 'json', 'logging', 'math',
'multiprocessing', 'operator', 'os', 'pathlib', 'pickle', 'queue', 're', 'random',
'shutil', 'signal', 'socket', 'sqlite3', 'statistics', 'string', 'subprocess', 'sys',
'tempfile', 'threading', 'time', 'typing', 'unittest', 'urllib', 'uuid', 'warnings',
'weakref', 'xml', 'zipfile', 'zlib'
])

# Node.js standard modules to exclude
NODE_STD_LIBS = set([
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'console', 'constants',
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'module', 'net',
'os', 'path', 'perf_hooks', 'process', 'punycode', 'querystring', 'readline', 'repl',
'stream', 'string_decoder', 'timers', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'
])

# Go standard packages to exclude
GO_STD_LIBS = set([
'archive', 'bufio', 'builtin', 'bytes', 'compress', 'container', 'context', 'crypto',
'database', 'debug', 'encoding', 'errors', 'expvar', 'flag', 'fmt', 'go', 'hash',
'html', 'image', 'index', 'io', 'log', 'math', 'mime', 'net', 'os', 'path', 'plugin',
'reflect', 'regexp', 'runtime', 'sort', 'strconv', 'strings', 'sync', 'syscall',
'testing', 'text', 'time', 'unicode', 'unsafe'
])

# Ruby standard libraries to exclude
RUBY_STD_LIBS = set([
'abbrev', 'base64', 'benchmark', 'bigdecimal', 'cgi', 'csv', 'date', 'delegate',
'digest', 'drb', 'e2mmap', 'erb', 'etc', 'fcntl', 'fiddle', 'fileutils', 'find',
'forwardable', 'io', 'ipaddr', 'irb', 'json', 'logger', 'matrix', 'monitor', 'mutex_m',
'net', 'observer', 'open-uri', 'open3', 'optparse', 'ostruct', 'pathname', 'pp',
'prettyprint', 'prime', 'profile', 'profiler', 'pstore', 'pty', 'racc', 'rake',
'rdoc', 'readline', 'resolv', 'rexml', 'rinda', 'ripper', 'rss', 'rubygems', 'scanf',
'sdbm', 'set', 'shellwords', 'singleton', 'socket', 'stringio', 'strscan', 'sync',
'syslog', 'tempfile', 'thread', 'thwait', 'time', 'timeout', 'tmpdir', 'tracer',
'tsort', 'uri', 'weakref', 'webrick', 'yaml', 'zlib'
])

def count_external_dependencies(file_path):
"""Count external dependencies in a file.

Args:
file_path (str): Path to the file to analyze

Returns:
int: Number of external dependencies found
"""
# Get file extension to determine language
_, ext = os.path.splitext(file_path)

# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
code = f.read()

# Count dependencies based on the language
if ext == '.py':
return count_python_dependencies(code)
elif ext == '.js':
return count_javascript_dependencies(code)
elif ext == '.rb':
return count_ruby_dependencies(code)
elif ext == '.go':
return count_go_dependencies(code)
else:
# Default to 0 for unsupported languages
return 0

def count_python_dependencies(code):
"""Count external dependencies in Python code."""
# Regular expressions for import patterns
import_pattern = r'^\s*import\s+([a-zA-Z0-9_.,\s]+)'
from_pattern = r'^\s*from\s+([a-zA-Z0-9_.]+)\s+import'

dependencies = set()

# Find all import statements
for line in code.split('\n'):
# Check for 'import X' pattern
import_match = re.match(import_pattern, line)
if import_match:
modules = [m.strip() for m in import_match.group(1).split(',')]
for module in modules:
# Get the top-level package
top_level = module.split('.')[0]
dependencies.add(top_level)

# Check for 'from X import Y' pattern
from_match = re.match(from_pattern, line)
if from_match:
# Get the top-level package
module = from_match.group(1)
top_level = module.split('.')[0]
dependencies.add(top_level)

# Filter out standard library modules
external_deps = {dep for dep in dependencies if dep not in PYTHON_STD_LIBS}

return len(external_deps)

def count_javascript_dependencies(code):
"""Count external dependencies in JavaScript code."""
# Regular expressions for require and import patterns
require_pattern = r'(?:const|let|var)\s+.+\s*=\s*require\([\'"]([^\'".]+)[\'"]'
import_pattern = r'import\s+(?:.+\s+from\s+)?[\'"]([^\'".]+)[\'"]'

dependencies = set()

# Find all require statements
for match in re.finditer(require_pattern, code):
module_name = match.group(1)
dependencies.add(module_name)

# Find all import statements
for match in re.finditer(import_pattern, code):
module_name = match.group(1)
dependencies.add(module_name)

# Filter out Node.js standard libraries and relative imports
external_deps = {
dep for dep in dependencies
if dep not in NODE_STD_LIBS and not dep.startswith('./') and not dep.startswith('../')
}

return len(external_deps)

def count_ruby_dependencies(code):
"""Count external dependencies in Ruby code."""
# Regular expressions for require and gem patterns
require_pattern = r'require\s+[\'"]([^\'"]+)[\'"]'
gem_pattern = r'gem\s+[\'"]([^\'"]+)[\'"]'

dependencies = set()

# Find all require statements
for match in re.finditer(require_pattern, code):
module_name = match.group(1)
dependencies.add(module_name)

# Find all gem statements
for match in re.finditer(gem_pattern, code):
module_name = match.group(1)
dependencies.add(module_name)

# Filter out Ruby standard libraries
external_deps = {
dep for dep in dependencies
if dep not in RUBY_STD_LIBS and not dep.startswith('./') and not dep.startswith('../')
}

return len(external_deps)

def count_go_dependencies(code):
"""Count external dependencies in Go code."""
# Regular expression for import statements
single_import_pattern = r'import\s+[\'"]([^\'"]+)[\'"]'
multi_import_pattern = r'import\s+\(\s*((?:[\'"][^\'"]+[\'"][\s\n]*)+)\)'

dependencies = set()

# Find all single import statements
for match in re.finditer(single_import_pattern, code):
module_path = match.group(1)
if module_path:
# Get the top-level package
dependencies.add(module_path)

# Find all multi-line import statements
for match in re.finditer(multi_import_pattern, code, re.DOTALL):
imports_block = match.group(1)
for line in imports_block.strip().split('\n'):
line = line.strip()
if line and (line.startswith('"') or line.startswith("'")):
# Extract package path from quoted string
module_path = re.findall(r'[\'"]([^\'"]+)[\'"]', line)
if module_path:
dependencies.add(module_path[0])

# Filter out standard library imports (packages without a domain or github.com/)
external_deps = set()
for dep in dependencies:
# Skip standard library packages (no dots in path)
if '.' in dep and not any(dep.startswith(std + '/') for std in GO_STD_LIBS):
external_deps.add(dep)

return len(external_deps)
Loading
Loading