-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rules): add Python partial support
- Loading branch information
1 parent
73139ab
commit 7385001
Showing
16 changed files
with
254 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`python (lockfile) > should match everything 1`] = ` | ||
[ | ||
"algolia", | ||
"datadog", | ||
"python", | ||
] | ||
`; | ||
|
||
exports[`python (lockfile) > should match everything 2`] = ` | ||
[ | ||
[ | ||
"python", | ||
"algoliasearch", | ||
"1.0.0", | ||
], | ||
[ | ||
"python", | ||
"algoliasearch-django", | ||
"1.0.0", | ||
], | ||
[ | ||
"python", | ||
"datadog", | ||
"1.0.0", | ||
], | ||
[ | ||
"python", | ||
"tensorflow", | ||
"1.0.0", | ||
], | ||
] | ||
`; | ||
|
||
exports[`python (lockfile) > should match weird syntax 1`] = ` | ||
[ | ||
"algolia", | ||
"datadog", | ||
"python", | ||
] | ||
`; | ||
|
||
exports[`python (lockfile) > should match weird syntax 2`] = ` | ||
[ | ||
[ | ||
"python", | ||
"algoliasearch", | ||
"1.9.1", | ||
], | ||
[ | ||
"python", | ||
"datadog", | ||
"1.4.3", | ||
], | ||
[ | ||
"python", | ||
"tensorflow", | ||
"latest", | ||
], | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { register } from '../../../register.js'; | ||
|
||
import { detectPythonLockfile } from './lockfile.js'; | ||
|
||
register({ | ||
tech: 'python', | ||
name: 'Python', | ||
type: 'language', | ||
files: ['requirements.txt'], | ||
extensions: [ | ||
'.py', | ||
'.gyp', | ||
'.py3', | ||
'.pyde', | ||
'.pyi', | ||
'.pyp', | ||
'.pyt', | ||
'.pyw', | ||
'.rpy', | ||
], | ||
dependencies: [ | ||
{ type: 'githubAction', name: 'actions/setup-python' }, | ||
{ type: 'docker', name: 'python' }, | ||
{ type: 'docker', name: 'circleci/python' }, | ||
{ type: 'docker', name: 'cimg/python' }, | ||
{ type: 'docker', name: 'bitnami/python' }, | ||
], | ||
detect: [detectPythonLockfile], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { analyser } from '../../../analyser/index.js'; | ||
import { rawList } from '../../../loader.js'; | ||
import { flatten } from '../../../payload/helpers.js'; | ||
import { FakeProvider } from '../../../provider/fake.js'; | ||
|
||
import '../../../autoload.js'; | ||
|
||
describe('python (lockfile)', () => { | ||
it('should match everything', async () => { | ||
const deps = []; | ||
for (const item of rawList) { | ||
if (item.type !== 'dependency' || item.ref.type !== 'python') { | ||
continue; | ||
} | ||
|
||
const example = 'example' in item.ref ? item.ref.example : item.ref.name; | ||
deps.push(`${example}==1.0.0`); | ||
} | ||
|
||
const lockfile: string = deps.join('\n'); | ||
|
||
const res = await analyser({ | ||
provider: new FakeProvider({ | ||
paths: { | ||
'/': ['requirements.txt'], | ||
}, | ||
files: { | ||
'/requirements.txt': lockfile, | ||
}, | ||
}), | ||
}); | ||
|
||
const merged = flatten(res, { merge: true }); | ||
expect(Array.from(merged.techs).sort()).toMatchSnapshot(); | ||
expect(Array.from(merged.dependencies).sort()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match weird syntax', async () => { | ||
const res = await analyser({ | ||
provider: new FakeProvider({ | ||
paths: { | ||
'/': ['requirements.txt'], | ||
}, | ||
files: { | ||
'/requirements.txt': [ | ||
// No version | ||
'tensorflow', | ||
// gte | ||
'algoliasearch>=1.9.1', | ||
// gte-lt | ||
'datadog>=1.4.3,<7.0.0', | ||
].join('\n'), | ||
}, | ||
}), | ||
}); | ||
|
||
const merged = flatten(res, { merge: true }); | ||
expect(Array.from(merged.techs).sort()).toMatchSnapshot(); | ||
expect(Array.from(merged.dependencies).sort()).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import path from 'node:path'; | ||
|
||
import { matchDependencies } from '../../../matchDependencies.js'; | ||
import { Payload } from '../../../payload/index.js'; | ||
import type { Dependency } from '../../../types/index.js'; | ||
import type { ComponentMatcher } from '../../../types/rule.js'; | ||
|
||
const LOCKFILE = 'requirements.txt'; | ||
const lineReg = /(^([a-zA-Z0-9._-]+)$|^([a-zA-Z0-9._-]+)(([>=]+)([0-9.]+)))/; | ||
|
||
export const detectPythonLockfile: ComponentMatcher = async ( | ||
files, | ||
provider | ||
) => { | ||
for (const file of files) { | ||
if (file.name !== LOCKFILE) { | ||
continue; | ||
} | ||
|
||
const content = await provider.open(file.fp); | ||
if (!content) { | ||
continue; | ||
} | ||
|
||
const pl = new Payload({ | ||
name: 'virtual', | ||
folderPath: path.dirname(file.fp), | ||
}); | ||
|
||
const dependencies: Dependency[] = []; | ||
// We only register docker service with image and that we know | ||
for (const line of content.split(/\r?\n/)) { | ||
if (line.startsWith('#')) { | ||
continue; | ||
} | ||
|
||
const match = line.match(lineReg); | ||
if (!match) { | ||
continue; | ||
} | ||
|
||
const name = match[2] || match[3]; | ||
const version = match[6] || 'latest'; | ||
|
||
if (!name) { | ||
continue; | ||
} | ||
|
||
dependencies.push(['python', name, version || 'latest']); | ||
const matched = matchDependencies([name], 'python'); | ||
if (matched.size <= 0) { | ||
continue; | ||
} | ||
|
||
pl.addTechs(matched); | ||
} | ||
|
||
pl.dependencies = [...dependencies]; | ||
|
||
return pl; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters