"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/packages/snyk-fix/src/plugins/python/handlers/pip-requirements/extract-version-provenance.ts" (20 Feb 2024, 1735 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 path from 'path';
    2 import * as debugLib from 'debug';
    3 
    4 import {
    5   ParsedRequirements,
    6   parseRequirementsFile,
    7 } from './update-dependencies/requirements-file-parser';
    8 import { Workspace } from '../../../../types';
    9 import { containsRequireDirective } from './contains-require-directive';
   10 
   11 export interface PythonProvenance {
   12   [fileName: string]: ParsedRequirements;
   13 }
   14 
   15 const debug = debugLib('snyk-fix:python:extract-version-provenance');
   16 
   17 export async function extractProvenance(
   18   workspace: Workspace,
   19   rootDir: string,
   20   dir: string,
   21   fileName: string,
   22   provenance: PythonProvenance = {},
   23 ): Promise<PythonProvenance> {
   24   const requirementsFileName = path.join(dir, fileName);
   25   const requirementsTxt = await workspace.readFile(requirementsFileName);
   26   // keep all provenance paths with `/` as a separator
   27   const relativeTargetFileName = path
   28     .normalize(path.relative(rootDir, requirementsFileName))
   29     .replace(path.sep, '/');
   30   provenance = {
   31     ...provenance,
   32     [relativeTargetFileName]: parseRequirementsFile(requirementsTxt),
   33   };
   34   const { containsRequire, matches } = await containsRequireDirective(
   35     requirementsTxt,
   36   );
   37   if (containsRequire) {
   38     for (const match of matches) {
   39       const requiredFilePath = match[2];
   40       if (provenance[requiredFilePath]) {
   41         debug('Detected recursive require directive, skipping');
   42         continue;
   43       }
   44 
   45       const { dir: requireDir, base } = path.parse(
   46         path.join(dir, requiredFilePath),
   47       );
   48 
   49       provenance = {
   50         ...provenance,
   51         ...(await extractProvenance(
   52           workspace,
   53           rootDir,
   54           requireDir,
   55           base,
   56           provenance,
   57         )),
   58       };
   59     }
   60   }
   61   return provenance;
   62 }