|
await exec('bash', [ |
|
'-c', |
|
'test $(which python) != "/usr/bin/python" -a $(python -m pip freeze | wc -l) -gt 0 && python -m pip freeze | xargs python -m pip uninstall -y || true' |
|
]); |
|
// Step 1: Check if python is not /usr/bin/python |
|
let pythonPath = ''; |
|
await exec('which python', [], { |
|
listeners: { |
|
stdout: (data: Buffer) => { |
|
pythonPath += data.toString(); |
|
} |
|
} |
|
}); |
|
pythonPath = pythonPath.trim(); |
|
|
|
if (pythonPath !== '/usr/bin/python') { |
|
// Step 2: Check if there are any installed pip packages |
|
let pipFreezeOutput = ''; |
|
await exec('python', ['-m', 'pip', 'freeze'], { |
|
listeners: { |
|
stdout: (data: Buffer) => { |
|
pipFreezeOutput += data.toString(); |
|
} |
|
} |
|
}); |
|
const packages = pipFreezeOutput |
|
.split('\n') |
|
.map(line => line.trim()) |
|
.filter(line => line.length > 0); |
|
|
|
if (packages.length > 0) { |
|
// Step 3: Uninstall all pip packages |
|
await exec('python', ['-m', 'pip', 'uninstall', '-y', ...packages]); |
|
} |
|
} |