-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeff Gordon
committed
Oct 2, 2024
1 parent
57f7c6b
commit 1f81100
Showing
19 changed files
with
625 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ yarn.lock | |
|
||
# Lockfiles | ||
*.lock | ||
!uv.lock | ||
|
||
# Distribution / packaging | ||
.Python | ||
|
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,103 @@ | ||
const fse = require('fs-extra'); | ||
const path = require('path'); | ||
const spawn = require('child-process-ext/spawn'); | ||
const { EOL } = require('os'); | ||
const semver = require('semver'); | ||
|
||
async function getUvVersion() { | ||
try { | ||
const res = await spawn('uv', ['--version'], { | ||
cwd: this.servicePath, | ||
}); | ||
|
||
const stdoutBuffer = | ||
(res.stdoutBuffer && res.stdoutBuffer.toString().trim()) || ''; | ||
|
||
const version = stdoutBuffer.split(' ')[1]; | ||
|
||
if (semver.valid(version)) { | ||
return version; | ||
} else { | ||
throw new this.serverless.classes.Error( | ||
`Unable to parse uv version!`, | ||
'PYTHON_REQUIREMENTS_UV_VERSION_ERROR' | ||
); | ||
} | ||
} catch (e) { | ||
const stderrBufferContent = | ||
(e.stderrBuffer && e.stderrBuffer.toString()) || ''; | ||
|
||
if (stderrBufferContent.includes('command not found')) { | ||
throw new this.serverless.classes.Error( | ||
`uv not found! Install it according to the uv docs.`, | ||
'PYTHON_REQUIREMENTS_UV_NOT_FOUND' | ||
); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* uv to requirements.txt | ||
*/ | ||
async function uvToRequirements() { | ||
if ( | ||
!this.options.useUv || | ||
!fse.existsSync(path.join(this.servicePath, 'uv.lock')) | ||
) { | ||
return; | ||
} | ||
|
||
let generateRequirementsProgress; | ||
if (this.progress && this.log) { | ||
generateRequirementsProgress = this.progress.get( | ||
'python-generate-requirements-uv' | ||
); | ||
generateRequirementsProgress.update( | ||
'Generating requirements.txt from uv.lock' | ||
); | ||
this.log.info('Generating requirements.txt from uv.lock'); | ||
} else { | ||
this.serverless.cli.log('Generating requirements.txt from uv.lock...'); | ||
} | ||
|
||
let res; | ||
|
||
try { | ||
await getUvVersion(); | ||
res = await spawn( | ||
'uv', | ||
['export', '--no-dev', '--frozen', '--no-hashes'], | ||
{ | ||
cwd: this.servicePath, | ||
} | ||
); | ||
|
||
fse.ensureDirSync(path.join(this.servicePath, '.serverless')); | ||
fse.writeFileSync( | ||
path.join(this.servicePath, '.serverless/requirements.txt'), | ||
removeEditableFlagFromRequirementsString(res.stdoutBuffer) | ||
); | ||
} finally { | ||
generateRequirementsProgress && generateRequirementsProgress.remove(); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param requirementBuffer | ||
* @returns Buffer with editable flags remove | ||
*/ | ||
function removeEditableFlagFromRequirementsString(requirementBuffer) { | ||
const flagStr = '-e '; | ||
const lines = requirementBuffer.toString('utf8').split(EOL); | ||
for (let i = 0; i < lines.length; i++) { | ||
if (lines[i].startsWith(flagStr)) { | ||
lines[i] = lines[i].substring(flagStr.length); | ||
} | ||
} | ||
return Buffer.from(lines.join(EOL)); | ||
} | ||
|
||
module.exports = { uvToRequirements }; |
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
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 @@ | ||
3.9 |
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,2 @@ | ||
slimPatterns: | ||
- '**/__main__.py' |
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,5 @@ | ||
import requests | ||
|
||
|
||
def hello(event, context): | ||
return requests.get('https://httpbin.org/get').json() |
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,14 @@ | ||
{ | ||
"name": "example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"serverless-python-requirements": "file:serverless-python-requirements-6.1.1.tgz" | ||
} | ||
} |
Oops, something went wrong.