Skip to content

Commit

Permalink
ref: upload to twine synchronously in a single call (#530)
Browse files Browse the repository at this point in the history
I suspect pypi can't handle parallel requests
  • Loading branch information
asottile-sentry authored Mar 26, 2024
1 parent 25e9aa8 commit a28e295
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
45 changes: 45 additions & 0 deletions src/targets/__tests__/pypi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PypiTarget } from '../pypi';
import { NoneArtifactProvider } from '../../artifact_providers/none';
import { RemoteArtifact } from '../../artifact_providers/base';

jest.mock('../../utils/system', () => ({
...jest.requireActual('../../utils/system'),
checkExecutableIsPresent: jest.fn(),
}));

describe('pypi', () => {
const oldEnv = { ...process.env };

beforeEach(() => {
process.env.TWINE_USERNAME = '__token__';
process.env.TWINE_PASSWORD = 'getsentry/craft:bogus';
});

afterAll(() => {
process.env = { ...oldEnv };
});

test('it uploads all artifacts in a single twine call', async () => {
const target = new PypiTarget({name: 'pypi'}, new NoneArtifactProvider());
target.getArtifactsForRevision = jest
.fn()
.mockResolvedValueOnce([
{ filename: 'pkg-1-py3-none-macos_11_0_arm64.whl' },
{ filename: 'pkg-1-py3-none-manylinux_2_17_x86_64.whl' },
{ filename: 'pkg-1.tar.gz' },
]);
target.artifactProvider.downloadArtifact = jest.fn(
async (artifact: RemoteArtifact, _downloadDirectory?: string | undefined) => `downloaded/path/${artifact.filename}`
);
const upload = jest.fn();
target.uploadAssets = upload;

await target.publish('version', 'deadbeef');

expect(upload.mock.lastCall[0]).toStrictEqual([
'downloaded/path/pkg-1-py3-none-macos_11_0_arm64.whl',
'downloaded/path/pkg-1-py3-none-manylinux_2_17_x86_64.whl',
'downloaded/path/pkg-1.tar.gz',
]);
});
});
16 changes: 5 additions & 11 deletions src/targets/pypi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,9 @@ export class PypiTarget extends BaseTarget {
};
}

/**
* Uploads an archive to PyPI using twine
*
* @param path Absolute path to the archive to upload
* @returns A promise that resolves when the upload has completed
*/
public async uploadAsset(path: string): Promise<any> {
async uploadAssets(paths: string[]): Promise<any> {
// TODO: Sign the package with "--sign"
return spawnProcess(TWINE_BIN, ['upload', path]);
return spawnProcess(TWINE_BIN, ['upload', ...paths]);
}

/**
Expand All @@ -95,13 +89,13 @@ export class PypiTarget extends BaseTarget {
return undefined;
}

await Promise.all(
const paths = await Promise.all(
packageFiles.map(async (file: RemoteArtifact) => {
const path = await this.artifactProvider.downloadArtifact(file);
this.logger.info(`Uploading file "${file.filename}" via twine`);
return this.uploadAsset(path);
return this.artifactProvider.downloadArtifact(file);
})
);
await this.uploadAssets(paths);

this.logger.info('PyPI release complete');
}
Expand Down

0 comments on commit a28e295

Please sign in to comment.