-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_validator.py
More file actions
344 lines (287 loc) · 12.6 KB
/
package_validator.py
File metadata and controls
344 lines (287 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
"""
Package Existence Checker
Validates if packages mentioned in code are real packages that exist in package repositories
"""
import requests
import re
import json
import time
from typing import List, Dict, Tuple, Set
import argparse
from urllib.parse import quote
import sys
class PackageValidator:
def __init__(self):
self.pypi_base_url = "https://pypi.org/pypi"
self.npm_base_url = "https://registry.npmjs.org"
self.headers = {
'User-Agent': 'PackageValidator/1.0'
}
# Cache to avoid redundant API calls
self.cache = {}
# Known fake/made-up packages in tutorials
self.known_fake_packages = {
'example-package', 'test-package', 'dummy-package', 'my-package',
'awesome-library', 'cool-tool', 'sample-module', 'placeholder',
'fakepackage', 'mocklib', 'dummylib', 'examplelib'
}
# Common patterns for fake packages
self.fake_patterns = [
r'^[Mm]y[-_].*',
r'^[Ee]xample[-_].*',
r'^[Tt]est[-_].*',
r'^[Dd]emo[-_].*',
r'^[Ff]ake[-_].*',
r'^[Mm]ock[-_].*',
r'^[Dd]ummy[-_].*',
r'.*[-_]example$',
r'.*[-_]demo$',
r'.*[-_]test$',
]
def check_pypi(self, package_name: str) -> Tuple[bool, Dict]:
"""Check if package exists on PyPI"""
if package_name in self.cache:
return self.cache[package_name]
url = f"{self.pypi_base_url}/{package_name}/json"
try:
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
data = response.json()
result = (True, {
'exists': True,
'name': data.get('info', {}).get('name'),
'version': data.get('info', {}).get('version'),
'summary': data.get('info', {}).get('summary', ''),
'home_page': data.get('info', {}).get('home_page', ''),
'downloads': data.get('info', {}).get('downloads', {}),
'last_serial': data.get('last_serial', 0)
})
elif response.status_code == 404:
result = (False, {'exists': False, 'error': 'Package not found on PyPI'})
else:
result = (False, {'exists': False, 'error': f'HTTP {response.status_code}'})
except requests.RequestException as e:
result = (False, {'exists': False, 'error': str(e)})
self.cache[package_name] = result
return result
def check_npm(self, package_name: str) -> Tuple[bool, Dict]:
"""Check if package exists on npm"""
url = f"{self.npm_base_url}/{package_name}"
try:
response = requests.get(url, headers=self.headers, timeout=10)
if response.status_code == 200:
data = response.json()
result = (True, {
'exists': True,
'name': data.get('name'),
'version': data.get('dist-tags', {}).get('latest'),
'description': data.get('description', ''),
'homepage': data.get('homepage', ''),
'repository': data.get('repository', {})
})
elif response.status_code == 404:
result = (False, {'exists': False, 'error': 'Package not found on npm'})
else:
result = (False, {'exists': False, 'error': f'HTTP {response.status_code}'})
except requests.RequestException as e:
result = (False, {'exists': False, 'error': str(e)})
return result
def search_package(self, package_name: str, repository: str = 'pypi') -> Dict:
"""
Search for package across repositories
Returns comprehensive information about package existence
"""
result = {
'package': package_name,
'repository': repository,
'exists': False,
'is_fake_likely': False,
'fake_reason': '',
'details': {}
}
# Check if package name looks fake
fake_assessment = self.assess_if_fake(package_name)
if fake_assessment['is_fake']:
result['is_fake_likely'] = True
result['fake_reason'] = fake_assessment['reason']
return result
# Check actual repository
if repository.lower() == 'pypi':
exists, details = self.check_pypi(package_name)
result['exists'] = exists
result['details'] = details
elif repository.lower() == 'npm':
exists, details = self.check_npm(package_name)
result['exists'] = exists
result['details'] = details
else:
result['details'] = {'error': f'Unsupported repository: {repository}'}
return result
def assess_if_fake(self, package_name: str) -> Dict:
"""Assess if a package name looks fake/made-up"""
lower_name = package_name.lower()
# Check against known fake packages
if lower_name in self.known_fake_packages:
return {'is_fake': True, 'reason': 'Known fake package name'}
# Check patterns
for pattern in self.fake_patterns:
if re.match(pattern, package_name):
return {'is_fake': True, 'reason': f'Matches fake pattern: {pattern}'}
# Check for suspicious names
suspicious_indicators = [
('placeholder', 'Contains "placeholder"'),
('example', 'Contains "example"'),
('demo', 'Contains "demo"'),
('test', 'Contains "test"'),
('fake', 'Contains "fake"'),
('dummy', 'Contains "dummy"'),
('mock', 'Contains "mock"'),
('sample', 'Contains "sample"'),
]
for indicator, reason in suspicious_indicators:
if indicator in lower_name:
return {'is_fake': True, 'reason': reason}
# Check for overly generic names
generic_names = {'library', 'module', 'package', 'tool', 'utility', 'helper'}
if lower_name in generic_names:
return {'is_fake': True, 'reason': 'Overly generic name'}
return {'is_fake': False, 'reason': ''}
def extract_packages_from_code(self, code: str) -> List[str]:
"""Extract package names from code text"""
packages = set()
# Python import patterns
python_patterns = [
r'^\s*(?:import|from)\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)',
r'pip\s+install\s+([a-zA-Z_][a-zA-Z0-9_\-]*)',
r'pip3\s+install\s+([a-zA-Z_][a-zA-Z0-9_\-]*)',
r'poetry\s+add\s+([a-zA-Z_][a-zA-Z0-9_\-]*)',
r'conda\s+install\s+([a-zA-Z_][a-zA-Z0-9_\-]*)'
]
for pattern in python_patterns:
matches = re.findall(pattern, code, re.MULTILINE)
for match in matches:
# Take only the first part (module.submodule -> module)
package = match.split('.')[0]
packages.add(package)
# npm/Node.js patterns
npm_patterns = [
r'npm\s+install\s+([@a-zA-Z_][a-zA-Z0-9_\-]*)',
r'yarn\s+add\s+([@a-zA-Z_][a-zA-Z0-9_\-]*)',
r'require\([\'"]([@a-zA-Z_][a-zA-Z0-9_\-]*)[\'"]\)',
r'import\s+.*from\s+[\'"]([@a-zA-Z_][a-zA-Z0-9_\-]*)[\'"]'
]
for pattern in npm_patterns:
matches = re.findall(pattern, code, re.MULTILINE)
packages.update(matches)
return sorted(list(packages))
def validate_code_snippet(self, code: str, repository: str = 'pypi') -> List[Dict]:
"""Validate all packages in a code snippet"""
packages = self.extract_packages_from_code(code)
results = []
print(f"📦 Found {len(packages)} package(s) in code:")
for pkg in packages:
print(f" - {pkg}")
print(f"\n🔍 Validating packages on {repository.upper()}...")
print("=" * 60)
for package in packages:
result = self.search_package(package, repository)
results.append(result)
# Display result
if result['is_fake_likely']:
print(f"❓ {package}: LIKELY FAKE - {result['fake_reason']}")
elif result['exists']:
details = result['details']
version = details.get('version', 'N/A')
print(f"✅ {package}: REAL (v{version})")
if details.get('summary'):
print(f" 📝 {details['summary'][:80]}...")
else:
print(f"❌ {package}: NOT FOUND")
if result['details'].get('error'):
print(f" ⚠️ {result['details']['error']}")
print()
return results
def generate_report(self, results: List[Dict]) -> Dict:
"""Generate summary report"""
total = len(results)
real = sum(1 for r in results if r['exists'])
fake = sum(1 for r in results if r['is_fake_likely'])
not_found = total - real - fake
report = {
'total_packages': total,
'real_packages': real,
'likely_fake': fake,
'not_found': not_found,
'real_packages_list': [r['package'] for r in results if r['exists']],
'fake_packages_list': [r['package'] for r in results if r['is_fake_likely']],
'not_found_list': [r['package'] for r in results if not r['exists'] and not r['is_fake_likely']]
}
return report
def main():
parser = argparse.ArgumentParser(description='Check if packages in code are real or made-up')
parser.add_argument('--code', type=str, help='Code snippet to analyze')
parser.add_argument('--file', type=str, help='File containing code to analyze')
parser.add_argument('--repo', choices=['pypi', 'npm'], default='pypi',
help='Package repository to check (default: pypi)')
parser.add_argument('--list', type=str, help='Comma-separated list of packages to check')
args = parser.parse_args()
validator = PackageValidator()
if args.list:
packages = [pkg.strip() for pkg in args.list.split(',')]
results = []
for package in packages:
result = validator.search_package(package, args.repo)
results.append(result)
if result['is_fake_likely']:
print(f"❓ {package}: LIKELY FAKE - {result['fake_reason']}")
elif result['exists']:
print(f"✅ {package}: REAL")
else:
print(f"❌ {package}: NOT FOUND")
elif args.file:
try:
with open(args.file, 'r', encoding='utf-8') as f:
code = f.read()
except FileNotFoundError:
print(f"Error: File '{args.file}' not found")
sys.exit(1)
results = validator.validate_code_snippet(code, args.repo)
elif args.code:
results = validator.validate_code_snippet(args.code, args.repo)
else:
# Example usage
example_code = """
import numpy as np
import pandas as pd
import fake_package
from example_module import something
import my_custom_lib
# Installation commands
pip install requests
pip install made-up-library
npm install non-existent-package
"""
print("=" * 60)
print("EXAMPLE: Validating packages in sample code")
print("=" * 60)
results = validator.validate_code_snippet(example_code, 'pypi')
# Generate and display report
report = validator.generate_report(results)
print("=" * 60)
print("📊 VALIDATION REPORT")
print("=" * 60)
print(f"Total packages checked: {report['total_packages']}")
print(f"✅ Real packages: {report['real_packages']}")
print(f"❓ Likely fake: {report['likely_fake']}")
print(f"❌ Not found: {report['not_found']}")
if report['fake_packages_list']:
print(f"\n⚠️ Likely fake packages:")
for pkg in report['fake_packages_list']:
print(f" - {pkg}")
if report['not_found_list']:
print(f"\n🔍 Packages not found (might be real but private/custom):")
for pkg in report['not_found_list']:
print(f" - {pkg}")
if __name__ == "__main__":
main()