Skip to content

Commit

Permalink
Handle Python Dependency Versions Correctly
Browse files Browse the repository at this point in the history
Change logic of python dependency version ranges. Parse semver number
either as specific version or range and output to setup.py.

Fix #676
  • Loading branch information
MrArnoldPalmer committed Nov 28, 2019
1 parent 9eacbe3 commit 8fc81ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
26 changes: 15 additions & 11 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path = require('path');

import * as path from 'path';
import * as semver from 'semver';
import { CodeMaker, toSnakeCase } from 'codemaker';
import * as escapeStringRegexp from 'escape-string-regexp';
import * as reflect from 'jsii-reflect';
Expand Down Expand Up @@ -1161,17 +1161,21 @@ class Package {
const expectedDeps = this.metadata.dependencies ?? {};
for (const depName of Object.keys(expectedDeps)) {
const depInfo = expectedDeps[depName];
// We need to figure out what our version range is.
// Basically, if it starts with Zero we want to restrict things to
// ~=X.Y.Z. If it does not start with zero, then we want to do ~=X.Y,>=X.Y.Z.
const versionParts = depInfo.version.split('.');
let versionSpecifier: string;
if (versionParts[0] === '0') {
versionSpecifier = `~=${versionParts.slice(0, 3).join('.')}`;
const { version } = depInfo;

// Parse either a single version or version range.
const parsedVersion = semver.valid(version);
const parsedVersionRange = semver.validRange(version);
let versionSpecifier;

// Convert parsed version info to expected format
if (parsedVersion) {
versionSpecifier = `==${version}`;
} else if (parsedVersionRange) {
versionSpecifier = parsedVersionRange.replace(' ', ', ');
} else {
versionSpecifier = `~=${versionParts.slice(0, 2).join('.')},>=${versionParts.slice(0, 3).join('.')}`;
throw new Error(`Unexpected version format '${version}' for dependency: 'depName'`);
}

dependencies.push(`${depInfo.targets!.python!.distName}${versionSpecifier}`);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-pacmak/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jsii-reflect": "^0.20.8",
"jsii-rosetta": "^0.20.8",
"jsii-spec": "^0.20.8",
"semver": "^6.3.0",
"spdx-license-list": "^6.1.0",
"xmlbuilder": "^13.0.2",
"yargs": "^15.0.2"
Expand All @@ -54,6 +55,7 @@
"@types/jest": "^24.0.23",
"@types/mock-fs": "^4.10.0",
"@types/node": "^10.17.6",
"@types/semver": "^6.2.0",
"@types/yargs": "^13.0.3",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/parser": "^2.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base-of-base~=0.20.8"
"scope.jsii-calc-base-of-base==0.20.8"
],
"classifiers": [
"Intended Audience :: Developers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base~=0.20.8"
"scope.jsii-calc-base==0.20.8"
],
"classifiers": [
"Intended Audience :: Developers",
Expand Down
6 changes: 3 additions & 3 deletions packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base~=0.20.8",
"scope.jsii-calc-base-of-base~=0.20.8",
"scope.jsii-calc-lib~=0.20.8"
"scope.jsii-calc-base==0.20.8",
"scope.jsii-calc-base-of-base==0.20.8",
"scope.jsii-calc-lib==0.20.8"
],
"classifiers": [
"Intended Audience :: Developers",
Expand Down

0 comments on commit 8fc81ca

Please sign in to comment.