-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython-command-executor.js
More file actions
385 lines (326 loc) · 10.6 KB
/
python-command-executor.js
File metadata and controls
385 lines (326 loc) · 10.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env node
/**
* Python Command Executor Module for PythonCommandRunner
*
* Command execution methods: runTests, runLinter, runFormatter, runTypeChecker, manageDependencies, runSetup
*/
const path = require('path');
const fs = require('fs');
const { LoggingUtils } = require('../../lib');
class PythonCommandExecutor {
constructor(projectPath, pythonConfig, initializer, projectAnalyzer, coreExecutor) {
this.projectPath = projectPath;
this.pythonConfig = pythonConfig;
this.initializer = initializer;
this.projectAnalyzer = projectAnalyzer;
this.coreExecutor = coreExecutor;
}
/**
* Run tests with configured test runner
*/
async runTests(options = {}) {
await this.initializer.initialize();
const testRunner = this.pythonConfig.testRunner || 'pytest';
await this.initializer.checkTool(testRunner);
// Log test information
const projectInfo = this.projectAnalyzer.getPythonProjectInfo();
if (projectInfo) {
LoggingUtils.debug(`Python files: ${projectInfo.pythonFiles}`);
}
const args = [];
// Add coverage if requested
if (options.coverage) {
if (testRunner === 'pytest') {
args.push('--cov=.', '--cov-report=term', '--cov-report=html');
LoggingUtils.info('📊 Coverage reporting enabled');
}
}
// Add verbose flag
if (options.verbose) {
args.push('-v');
LoggingUtils.debug('Verbose mode enabled');
}
// Add specific test file
if (options.file) {
args.push(options.file);
LoggingUtils.debug(`Testing specific file: ${options.file}`);
}
// Add test name pattern
if (options.test) {
if (testRunner === 'pytest') {
args.push('-k', options.test);
LoggingUtils.debug(`Test pattern: ${options.test}`);
} else if (testRunner === 'unittest') {
args.push(options.test);
LoggingUtils.debug(`Test pattern: ${options.test}`);
}
}
// Log test configuration
LoggingUtils.info(`Running tests with ${testRunner}...`);
// Execute test runner
if (testRunner === 'pytest') {
return this.coreExecutor.executeCommand('pytest', args);
} else if (testRunner === 'unittest') {
return this.coreExecutor.executePythonModule('unittest', args);
} else {
throw new Error(`Unsupported test runner: ${testRunner}`);
}
}
/**
* Run linter with configured tool
*/
async runLinter(options = {}) {
await this.initializer.initialize();
const linter = this.pythonConfig.linter || 'ruff';
await this.initializer.checkTool(linter);
// Log linter information
LoggingUtils.info(`Running ${linter}...`);
if (options.fix) {
LoggingUtils.debug('Fix mode enabled');
}
const args = [];
// Check or fix mode
if (options.fix) {
if (linter === 'ruff') {
args.push('check', '--fix');
} else if (linter === 'flake8') {
// flake8 doesn't have fix mode
args.push('.');
LoggingUtils.warn("flake8 doesn't support auto-fix mode");
} else if (linter === 'pylint') {
args.push('.');
LoggingUtils.warn("pylint doesn't support auto-fix mode");
}
} else {
if (linter === 'ruff') {
args.push('check');
} else {
args.push('.');
}
}
// Add specific file
if (options.file) {
args.push(options.file);
LoggingUtils.debug(`Linting specific file: ${options.file}`);
}
// Add exclude patterns
if (options.exclude) {
if (linter === 'ruff') {
args.push('--exclude', options.exclude);
} else if (linter === 'flake8') {
args.push('--exclude', options.exclude);
}
}
// Execute linter
return this.coreExecutor.executeCommand(linter, args);
}
/**
* Run formatter with configured tool
*/
async runFormatter(options = {}) {
await this.initializer.initialize();
const formatter = this.pythonConfig.formatter || 'black';
await this.initializer.checkTool(formatter);
// Log formatter information
LoggingUtils.info(`Running ${formatter}...`);
if (options.check) {
LoggingUtils.debug('Check mode (no changes)');
}
const args = [];
// Check or format mode
if (options.check) {
if (formatter === 'black') {
args.push('--check');
} else if (formatter === 'isort') {
args.push('--check-only');
} else if (formatter === 'yapf') {
args.push('--diff');
}
}
// Add specific file or directory
if (options.file) {
args.push(options.file);
LoggingUtils.debug(`Formatting specific file: ${options.file}`);
} else {
args.push('.');
}
// Add line length if specified
if (options.lineLength) {
if (formatter === 'black') {
args.push('--line-length', options.lineLength.toString());
} else if (formatter === 'yapf') {
args.push('--style', `{based_on_style: pep8, column_limit: ${options.lineLength}}`);
}
}
// Execute formatter
return this.coreExecutor.executeCommand(formatter, args);
}
/**
* Run type checker with configured tool
*/
async runTypeChecker(options = {}) {
await this.initializer.initialize();
const typeChecker = this.pythonConfig.typeChecker || 'mypy';
await this.initializer.checkTool(typeChecker);
// Log type checker information
LoggingUtils.info(`Running ${typeChecker}...`);
const args = [];
// Add specific file or directory
if (options.file) {
args.push(options.file);
LoggingUtils.debug(`Type checking specific file: ${options.file}`);
} else {
args.push('.');
}
// Add strict mode
if (options.strict) {
if (typeChecker === 'mypy') {
args.push('--strict');
} else if (typeChecker === 'pyright') {
args.push('--lib');
}
}
// Add config file if specified
if (options.config) {
if (typeChecker === 'mypy') {
args.push('--config-file', options.config);
} else if (typeChecker === 'pyright') {
args.push('--config', options.config);
}
}
// Execute type checker
return this.coreExecutor.executeCommand(typeChecker, args);
}
/**
* Manage Python dependencies
*/
async manageDependencies(action, packages = [], options = {}) {
await this.initializer.initialize();
const packageManager = this.pythonConfig.packageManager || 'pip';
await this.initializer.checkTool(packageManager);
// Log dependency management information
LoggingUtils.info(`Managing dependencies with ${packageManager}...`);
const args = [];
// Handle different actions
switch (action) {
case 'install':
args.push('install');
if (packages.length === 0) {
// Install from requirements file
if (this.projectAnalyzer.hasFile('requirements.txt')) {
args.push('-r', 'requirements.txt');
LoggingUtils.debug('Installing from requirements.txt');
} else if (this.projectAnalyzer.hasFile('pyproject.toml')) {
if (packageManager === 'poetry') {
args.push('--no-root');
} else if (packageManager === 'pip') {
args.push('.');
}
}
} else {
// Install specific packages
args.push(...packages);
LoggingUtils.debug(`Installing packages: ${packages.join(', ')}`);
}
break;
case 'uninstall':
args.push('uninstall', ...packages);
LoggingUtils.debug(`Uninstalling packages: ${packages.join(', ')}`);
break;
case 'update':
args.push('update');
if (packages.length > 0) {
args.push(...packages);
LoggingUtils.debug(`Updating packages: ${packages.join(', ')}`);
} else {
LoggingUtils.debug('Updating all packages');
}
break;
case 'list':
args.push('list');
break;
case 'show':
if (packages.length > 0) {
args.push('show', ...packages);
LoggingUtils.debug(`Showing info for: ${packages.join(', ')}`);
} else {
throw new Error('Package name required for show action');
}
break;
default:
throw new Error(`Unknown dependency action: ${action}`);
}
// Add additional options
if (options.dev && packageManager === 'poetry') {
args.push('--dev');
}
if (options.noCache && packageManager === 'pip') {
args.push('--no-cache-dir');
}
// Execute package manager
if (packageManager === 'pip') {
return this.coreExecutor.executePythonModule('pip', args);
} else {
return this.coreExecutor.executeCommand(packageManager, args);
}
}
/**
* Run project setup
*/
async runSetup(options = {}) {
await this.initializer.initialize();
LoggingUtils.info('Setting up Python project...');
const setupTasks = [];
// Check Python installation
try {
const python = this.projectAnalyzer.getPythonExecutable();
setupTasks.push(`✓ Python executable: ${python}`);
} catch (error) {
setupTasks.push(`✗ Python not found: ${error.message}`);
throw error;
}
// Check virtual environment
const venvPath = path.join(this.projectPath, 'venv');
if (!fs.existsSync(venvPath) && options.createVenv) {
setupTasks.push('Creating virtual environment...');
await this.coreExecutor.executePythonModule('venv', ['venv']);
setupTasks.push('✓ Virtual environment created');
} else if (fs.existsSync(venvPath)) {
setupTasks.push('✓ Virtual environment found');
}
// Install dependencies
if (options.installDeps) {
setupTasks.push('Installing dependencies...');
await this.manageDependencies('install', [], {});
setupTasks.push('✓ Dependencies installed');
}
// Run initial tests
if (options.runTests) {
setupTasks.push('Running initial tests...');
try {
await this.runTests({ verbose: false });
setupTasks.push('✓ Tests passed');
} catch (error) {
setupTasks.push(`⚠ Tests failed: ${error.message}`);
if (!options.force) {
throw error;
}
}
}
// Log setup summary
LoggingUtils.info('Setup completed:');
setupTasks.forEach((task) => {
if (task.startsWith('✓')) {
LoggingUtils.success(task);
} else if (task.startsWith('✗')) {
LoggingUtils.error(task);
} else if (task.startsWith('⚠')) {
LoggingUtils.warn(task);
} else {
LoggingUtils.info(task);
}
});
return { success: true, tasks: setupTasks };
}
}
module.exports = PythonCommandExecutor;