"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/packages/snyk-fix/src/plugins/python/index.ts" (20 Feb 2024, 3030 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as debugLib from 'debug';
    2 import * as pMap from 'p-map';
    3 import * as ora from 'ora';
    4 import * as chalk from 'chalk';
    5 
    6 import { EntityToFix, FixOptions } from '../../types';
    7 import { FailedToFix, FixHandlerResultByPlugin } from '../types';
    8 import { loadHandler } from './load-handler';
    9 import { SUPPORTED_HANDLER_TYPES } from './supported-handler-types';
   10 import { mapEntitiesPerHandlerType } from './map-entities-per-handler-type';
   11 import { partitionByFixable } from './handlers/is-supported';
   12 import { CustomError } from '../../lib/errors/custom-error';
   13 
   14 const debug = debugLib('snyk-fix:python');
   15 
   16 export async function pythonFix(
   17   entities: EntityToFix[],
   18   options: FixOptions,
   19 ): Promise<FixHandlerResultByPlugin> {
   20   const spinner = ora({ isSilent: options.quiet, stream: process.stdout });
   21   const spinnerMessage = 'Looking for supported Python items';
   22   spinner.text = spinnerMessage;
   23   spinner.start();
   24 
   25   const handlerResult: FixHandlerResultByPlugin = {
   26     python: {
   27       succeeded: [],
   28       failed: [],
   29       skipped: [],
   30     },
   31   };
   32   const results = handlerResult.python;
   33   const { entitiesPerType, skipped: notSupported } = mapEntitiesPerHandlerType(
   34     entities,
   35   );
   36   results.skipped.push(...notSupported);
   37 
   38   spinner.stopAndPersist({
   39     text: spinnerMessage,
   40     symbol: chalk.green('\n✔'),
   41   });
   42 
   43   await pMap(
   44     Object.keys(entitiesPerType),
   45     async (projectType) => {
   46       const projectsToFix: EntityToFix[] = entitiesPerType[projectType];
   47       if (!projectsToFix.length) {
   48         return;
   49       }
   50       const processingMessage = `Processing ${projectsToFix.length} ${projectType} items`;
   51       const processedMessage = `Processed ${projectsToFix.length} ${projectType} items`;
   52 
   53       spinner.text = processingMessage;
   54       spinner.render();
   55 
   56       try {
   57         const handler = loadHandler(projectType as SUPPORTED_HANDLER_TYPES);
   58         // drop unsupported Python entities early so only potentially fixable items get
   59         // attempted to be fixed
   60         const { fixable, skipped: notFixable } = await partitionByFixable(
   61           projectsToFix,
   62         );
   63         results.skipped.push(...notFixable);
   64 
   65         const { failed, skipped, succeeded } = await handler(fixable, options);
   66         results.failed.push(...failed);
   67         results.skipped.push(...skipped);
   68         results.succeeded.push(...succeeded);
   69       } catch (e) {
   70         debug(
   71           `Failed to fix ${projectsToFix.length} ${projectType} projects.\nError: ${e.message}`,
   72         );
   73         results.failed.push(...generateFailed(projectsToFix, e as CustomError));
   74       }
   75       spinner.stopAndPersist({
   76         text: processedMessage,
   77         symbol: chalk.green('✔'),
   78       });
   79     },
   80     {
   81       concurrency: 5,
   82     },
   83   );
   84   return handlerResult;
   85 }
   86 
   87 function generateFailed(
   88   projectsToFix: EntityToFix[],
   89   error: CustomError,
   90 ): FailedToFix[] {
   91   const failed: FailedToFix[] = [];
   92   for (const project of projectsToFix) {
   93     failed.push({ original: project, error: error });
   94   }
   95   return failed;
   96 }