-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15: Adding Poetry Python package migration in foundation-upgrade
- Loading branch information
1 parent
ab71ced
commit 0625dc2
Showing
11 changed files
with
435 additions
and
2 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
95 changes: 95 additions & 0 deletions
95
...rade/src/main/java/com/boozallen/aissemble/upgrade/migration/AbstractPoetryMigration.java
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,95 @@ | ||
package com.boozallen.aissemble.upgrade.migration; | ||
|
||
/*- | ||
* #%L | ||
* aiSSEMBLE::Foundation::Upgrade | ||
* %% | ||
* Copyright (C) 2021 Booz Allen | ||
* %% | ||
* This software package is licensed under the Booz Allen Public License. All Rights Reserved. | ||
* #L% | ||
*/ | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.boozallen.aissemble.upgrade.util.FileUtils; | ||
import com.boozallen.aissemble.upgrade.util.PythonPackageMigration; | ||
import com.electronwill.nightconfig.core.Config; | ||
import com.electronwill.nightconfig.core.file.FileConfig; | ||
|
||
/** | ||
* | ||
* Baton migration used to migrate aiSSEMBLE python packages to the new naming convention | ||
*/ | ||
public abstract class AbstractPoetryMigration extends AbstractAissembleMigration { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AbstractPoetryMigration.class); | ||
public static final String DEPENDENCIES_KEY = "tool.poetry.dependencies"; | ||
|
||
@Override | ||
protected boolean shouldExecuteOnFile(File file) { | ||
boolean shouldExecute = false; | ||
FileConfig poetryConfig = FileConfig.of(file); | ||
poetryConfig.load(); | ||
Optional<Config> dependenciesOpt = poetryConfig.getOptional(DEPENDENCIES_KEY); | ||
if(dependenciesOpt.isPresent()) { | ||
Config dependencies = dependenciesOpt.get(); | ||
shouldExecute = hasOldPackages(dependencies); | ||
} | ||
else { | ||
logger.warn("Could not get dependencies for file {}", file.getAbsolutePath()); | ||
} | ||
return shouldExecute; | ||
} | ||
|
||
private boolean hasOldPackages(Config currentPackages) { | ||
boolean hasOldPackages = false; | ||
for(PythonPackageMigration packageMigration : getPythonPackageMigrations()) { | ||
if(currentPackages.contains(packageMigration.getOldName())) { | ||
hasOldPackages = true; | ||
break; | ||
} | ||
} | ||
return hasOldPackages; | ||
} | ||
|
||
@Override | ||
protected boolean performMigration(File file) { | ||
boolean performedSuccessfully = false; | ||
FileConfig pyproject = FileConfig.of(file); | ||
pyproject.load(); | ||
Config dependencies = pyproject.get(DEPENDENCIES_KEY); | ||
try { | ||
performedSuccessfully = migrateOldPythonPackages(dependencies, file); | ||
} catch (IOException e) { | ||
logger.error(e.getMessage()); | ||
} | ||
return performedSuccessfully; | ||
} | ||
|
||
private boolean migrateOldPythonPackages(Config config, File pyproject) throws IOException { | ||
boolean success = false; | ||
for(PythonPackageMigration packageMigration : getPythonPackageMigrations()) { | ||
String oldPackageName = packageMigration.getOldName(); | ||
if(config.getOptional(oldPackageName).isPresent()) { | ||
// Old package is preset in pyproject.toml | ||
String regex = String.format("%s = \".+\"", packageMigration.getOldName()); | ||
String replacement = String.format("%s = \"%s\"", packageMigration.getNewName(), packageMigration.getNewVersion()); | ||
FileUtils.replaceInFile(pyproject, regex, replacement); | ||
logger.info("Replacing Python package {} -> {} = \"{}\"", | ||
packageMigration.getOldName(), packageMigration.getNewName(), packageMigration.getNewVersion()); | ||
success = true; | ||
} | ||
} | ||
return success; | ||
} | ||
|
||
public abstract Set<PythonPackageMigration> getPythonPackageMigrations(); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ozallen/aissemble/upgrade/migration/v1_7_0/FoundationExtensionPythonPackageMigration.java
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,44 @@ | ||
package com.boozallen.aissemble.upgrade.migration.v1_7_0; | ||
|
||
import java.util.HashSet; | ||
|
||
/*- | ||
* #%L | ||
* aiSSEMBLE::Foundation::Upgrade | ||
* %% | ||
* Copyright (C) 2021 Booz Allen | ||
* %% | ||
* This software package is licensed under the Booz Allen Public License. All Rights Reserved. | ||
* #L% | ||
*/ | ||
|
||
import java.util.Set; | ||
|
||
import com.boozallen.aissemble.upgrade.migration.AbstractPoetryMigration; | ||
import com.boozallen.aissemble.upgrade.util.PythonPackageMigration; | ||
|
||
public class FoundationExtensionPythonPackageMigration extends AbstractPoetryMigration { | ||
|
||
private static final Set<String> OLD_PYTHON_PACKAGES = Set.of( | ||
"foundation-core-python", | ||
"foundation-pdp-client-python", | ||
"foundation-model-lineage", | ||
"foundation-encryption-policy-python", | ||
"extensions-encryption-vault-python", | ||
"extensions-data-delivery-spark-py", | ||
"foundation-data-lineage-python" | ||
); | ||
private static final String PACKAGE_PREFIX = "aissemble-"; | ||
|
||
@Override | ||
public Set<PythonPackageMigration> getPythonPackageMigrations() { | ||
Set<PythonPackageMigration> packageMigrations = new HashSet<>(); | ||
String aissembleVersion = getAissembleVersion().replaceAll("-SNAPSHOT", ".*"); | ||
for(String oldPackageName : OLD_PYTHON_PACKAGES) { | ||
String newPackageName = String.format("%s%s", PACKAGE_PREFIX, oldPackageName); | ||
packageMigrations.add(new PythonPackageMigration(oldPackageName, newPackageName, aissembleVersion)); | ||
} | ||
return packageMigrations; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...on-upgrade/src/main/java/com/boozallen/aissemble/upgrade/util/PythonPackageMigration.java
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,38 @@ | ||
package com.boozallen.aissemble.upgrade.util; | ||
|
||
/*- | ||
* #%L | ||
* aiSSEMBLE::Foundation::Upgrade | ||
* %% | ||
* Copyright (C) 2021 Booz Allen | ||
* %% | ||
* This software package is licensed under the Booz Allen Public License. All Rights Reserved. | ||
* #L% | ||
*/ | ||
|
||
|
||
public class PythonPackageMigration { | ||
|
||
private final String oldName; | ||
private final String newName; | ||
private final String newVersion; | ||
|
||
public PythonPackageMigration(String oldName, String newName, String newVersion) { | ||
this.oldName = oldName; | ||
this.newName = newName; | ||
this.newVersion = newVersion; | ||
} | ||
|
||
public String getOldName() { | ||
return this.oldName; | ||
} | ||
|
||
public String getNewName() { | ||
return this.newName; | ||
} | ||
|
||
public String getNewVersion() { | ||
return this.newVersion; | ||
} | ||
|
||
} |
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
96 changes: 96 additions & 0 deletions
96
...upgrade/src/test/java/com/boozallen/aissemble/upgrade/migration/PoetryMigrationSteps.java
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,96 @@ | ||
package com.boozallen.aissemble.upgrade.migration; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
|
||
import com.boozallen.aissemble.upgrade.migration.extensions.PoetryMigrationTest; | ||
import com.boozallen.aissemble.upgrade.util.PythonPackageMigration; | ||
import com.electronwill.nightconfig.core.Config; | ||
import com.electronwill.nightconfig.core.file.FileConfig; | ||
|
||
import io.cucumber.java.Before; | ||
|
||
/*- | ||
* #%L | ||
* aiSSEMBLE::Foundation::Upgrade | ||
* %% | ||
* Copyright (C) 2021 Booz Allen | ||
* %% | ||
* This software package is licensed under the Booz Allen Public License. All Rights Reserved. | ||
* #L% | ||
*/ | ||
|
||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import static com.boozallen.aissemble.upgrade.migration.utils.PoetryMigrationUtils.getOldPackages; | ||
import static com.boozallen.aissemble.upgrade.migration.utils.PoetryMigrationUtils.createPyproject; | ||
import static com.boozallen.aissemble.upgrade.migration.utils.PoetryMigrationUtils.getPartialOldPythonPackages; | ||
import static com.boozallen.aissemble.upgrade.migration.utils.PoetryMigrationUtils.getNewPackages; | ||
import static com.boozallen.aissemble.upgrade.migration.utils.PoetryMigrationUtils.verifyPoetryMigration; | ||
|
||
|
||
public class PoetryMigrationSteps extends AbstractMigrationTest { | ||
|
||
private static final String PYPROJECT_FILE = Paths.get("pyproject", "pyproject.toml").toString(); | ||
private static AbstractPoetryMigration migration; | ||
|
||
@BeforeClass | ||
public void createTestPyproject() throws IOException { | ||
addTestFile(PYPROJECT_FILE); | ||
} | ||
|
||
@AfterClass | ||
public void deletePyproject() { | ||
getTestFile(PYPROJECT_FILE).delete(); | ||
} | ||
|
||
@Before | ||
public void setTestFile() { | ||
testFile = getTestFile(PYPROJECT_FILE); | ||
migration = new PoetryMigrationTest(); | ||
} | ||
|
||
@Given("a pyproject.toml file with old aiSSEMBLE Python dependency naming conventions and versions") | ||
public void a_pyproject_toml_file_with_all_old_ai_ssemble_python_dependencies() throws IOException { | ||
createPyproject(getOldPackages(migration.getPythonPackageMigrations()), testFile); | ||
} | ||
|
||
@Given("a pyproject.toml file with old and new aiSSEMBLE Python dependency naming conventions and versions") | ||
public void a_pyproject_toml_file_with_old_ai_ssemble_python_dependencies() throws IOException { | ||
createPyproject(getPartialOldPythonPackages(migration.getPythonPackageMigrations()), testFile); | ||
} | ||
|
||
@Given("a pyproject.toml file with new aiSSEMBLE Python dependencies") | ||
public void a_pyproject_toml_file_with_new_ai_ssemble_python_dependencies() { | ||
createPyproject(getNewPackages(migration.getPythonPackageMigrations()), testFile); | ||
} | ||
|
||
@When("the pyproject.toml file migration is executed") | ||
public void the_pyproject_toml_file_migration_is_executed() { | ||
performMigration(migration); | ||
} | ||
|
||
@Then("the migration is skipped") | ||
public void the_migration_is_skipped() { | ||
assertFalse(shouldExecute); | ||
} | ||
|
||
@Then("the dependencies are updated to the newest naming convention and version") | ||
public void the_dependencies_are_updated_to_the_newest_naming_convention() { | ||
verifyPoetryMigration(migration.getPythonPackageMigrations(), testFile); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...c/test/java/com/boozallen/aissemble/upgrade/migration/extensions/PoetryMigrationTest.java
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,31 @@ | ||
package com.boozallen.aissemble.upgrade.migration.extensions; | ||
|
||
/*- | ||
* #%L | ||
* aiSSEMBLE::Foundation::Upgrade | ||
* %% | ||
* Copyright (C) 2021 Booz Allen | ||
* %% | ||
* This software package is licensed under the Booz Allen Public License. All Rights Reserved. | ||
* #L% | ||
*/ | ||
|
||
import java.util.Set; | ||
|
||
import com.boozallen.aissemble.upgrade.migration.AbstractPoetryMigration; | ||
import com.boozallen.aissemble.upgrade.util.PythonPackageMigration; | ||
|
||
public class PoetryMigrationTest extends AbstractPoetryMigration { | ||
|
||
private final static Set<PythonPackageMigration> MIGRATION_PYTHON_PACKAGES = Set.of( | ||
new PythonPackageMigration("package_1", "new_package_1", "2.0.0"), | ||
new PythonPackageMigration("package_2", "new_package_2", "2.0.0"), | ||
new PythonPackageMigration("package_3", "new_package_3", "1.0.0") | ||
); | ||
|
||
@Override | ||
public Set<PythonPackageMigration> getPythonPackageMigrations() { | ||
return MIGRATION_PYTHON_PACKAGES; | ||
} | ||
|
||
} |
Oops, something went wrong.