diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index f07e1db1d7..cdc79f73e5 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -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'; @@ -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}`); } diff --git a/packages/jsii-pacmak/package.json b/packages/jsii-pacmak/package.json index 65743d777c..5f0610cbf8 100644 --- a/packages/jsii-pacmak/package.json +++ b/packages/jsii-pacmak/package.json @@ -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" @@ -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", diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/python/setup.py b/packages/jsii-pacmak/test/expected.jsii-calc-base/python/setup.py index 6447be1672..815faa8474 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/python/setup.py +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/python/setup.py @@ -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", diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-lib/python/setup.py b/packages/jsii-pacmak/test/expected.jsii-calc-lib/python/setup.py index b09f751913..cea84cea21 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-lib/python/setup.py +++ b/packages/jsii-pacmak/test/expected.jsii-calc-lib/python/setup.py @@ -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", diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py b/packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py index 4a002726f2..74e31a0518 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py +++ b/packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py @@ -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",