Skip to content

Commit

Permalink
Fixes #19. Issue when generating Maven package from Python as a script
Browse files Browse the repository at this point in the history
  • Loading branch information
jamolina committed Apr 28, 2023
1 parent 7e96c5b commit 18b7867
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 98 deletions.
36 changes: 4 additions & 32 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,23 @@
Converts the porting-advisor python module into a Windows executable.
#>

try {
$p = python --version
} catch {
throw '**ERROR**: python is missing, please install it before running this build script'
}

try {
$p = pip --version
} catch {
throw '**ERROR**: pip is missing, please install it before running this build script'
if (!(Test-Path -Path .venv)) {
.\Setup-Environment.ps1
}

$Filename = .\Get-Binary-Name.ps1
Write-Host "*** Will use $Filename as name ***"

if (!(Test-Path -Path .venv)) {
Write-Host "Creating Python virtual environment"
python -m venv .venv
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: could not create Python Virtual Environment."
}
}

Write-Host "Making sure Python Virtual Environment is active"
.\.venv\Scripts\Activate.ps1
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: could not activate Python Virtual Environment."
}

Write-Host "Installing requirements"
pip install -r requirements-build.txt
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: error installing required packages"
}

try {
mvn package --file .\src\advisor\tools\graviton-ready-java\pom.xml
} catch {
Write-Host "Could not find Maven. Skipping jar generation for Graviton Ready Java tool."
}

Write-Host "Generating executable"
Write-Host "🏗️ Generating executable"
pyinstaller --clean porting-advisor-win-x64.spec --noconfirm
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: pyinstaller failed, binary was not created"
}

Write-Output "*** Success: Executable saved at dist\$Filename ***"
Write-Output "🎉 *** Success: Executable saved at dist\$Filename ***"
36 changes: 36 additions & 0 deletions Setup-Environment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<#
.Synopsis
Sets up Python Virtual Environment.
#>

try {
$p = python --version
} catch {
throw '**ERROR**: python is missing, please install it before running this build script'
}

try {
$p = pip --version
} catch {
throw '**ERROR**: pip is missing, please install it before running this build script'
}

if (!(Test-Path -Path .venv)) {
Write-Host "💻 Creating Python virtual environment"
python -m venv .venv
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: could not create Python Virtual Environment."
}
}

Write-Host "💡 Making sure Python Virtual Environment is active"
.\.venv\Scripts\Activate.ps1
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: could not activate Python Virtual Environment."
}

Write-Host "☁️ Installing requirements"
pip install -r requirements-build.txt
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: error installing required packages"
}
16 changes: 11 additions & 5 deletions Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
Builds project, runs unit tests, runs integration tests
#>

Write-Host "Building project"
.\Build.ps1
Write-Host "🐍 Setup virtual environment"
.\Setup-Environment.ps1
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: failed to build project"
throw "*ERROR**: failed to initialize Python Virtual Environment"
}

Write-Host "Running unit tests"
Write-Host "🔬 Running unit tests"
.\Unit-Test.ps1
if($LASTEXITCODE -ne 0) {
throw "*ERROR**: unit tests failed"
}

Write-Host "Running integration tests"
Write-Host "⚒️ Building project"
.\Build.ps1
if($LASTEXITCODE -ne 0) {
throw "**ERROR**: failed to build project"
}

Write-Host "🧪 Running integration tests"
.\Integration-Test.ps1
if($LASTEXITCODE -ne 0) {
throw "*ERROR**: integration tests failed"
Expand Down
2 changes: 1 addition & 1 deletion Unit-Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.Synopsis
Run unit tests for project.
#>
Write-Host "*** running unit tests ***"
Write-Host "🔬 *** running unit tests ***"
$Err = coverage run --source=./src -m unittest discover -s unittest -p "test_*.py" -v
if ($null -ne $Err) {
Write-Error "Unit tests failed"
Expand Down
65 changes: 17 additions & 48 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,26 @@
# As a prerequisite, pip3 must already be installed
# The resulting native binary does not need sudo to execute.

if hash python3
then
echo "python3 is installed"

if hash pip3
then
echo "pip is installed"
else
echo "**ERROR**: python pip3 not found, please install pip3" && exit 1
fi
if [ ! -f ".venv/bin/activate" ]; then
./setup-environment.sh
fi

FILE_NAME=`./getBinaryName.sh`
echo "*** Will use $FILE_NAME as name ***"
FILE_NAME=`./getBinaryName.sh`
echo "*** Will use $FILE_NAME as name ***"

if [ ! -f ".venv/bin/activate" ]; then
echo "Creating Python virtual environment"
python3 -m venv .venv
if [ $? -ne 0 ]; then
echo "**ERROR**: could not create Python Virtual Environment." && exit 1
fi
fi

echo "Making sure Python Virtual Environment is active"
. .venv/bin/activate
if [ $? -ne 0 ]; then
echo "**ERROR**: could not activate Python Virtual Environment." && exit 1
fi

echo "Installing requirements"
python3 -m pip install -r requirements-build.txt
if hash mvn
then
mvn package --file ./src/advisor/tools/graviton-ready-java/pom.xml
if [ $? -ne 0 ]; then
echo "**ERROR**: error installing required packages" && exit 1
fi

if hash mvn
then
mvn package --file ./src/advisor/tools/graviton-ready-java/pom.xml
if [ $? -ne 0 ]; then
echo "**ERROR**: error generating jar for Graviton Ready Java tool" && exit 1
fi
echo "**ERROR**: error generating jar for Graviton Ready Java tool" && exit 1
fi
fi

echo "Generating executable"
CERT_PATH=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
pyinstaller --onefile --clean --noconfirm --distpath dist --add-data 'src/advisor/rules/*.json:advisor/rules' --add-data 'src/advisor/tools/graviton-ready-java/target/*:advisor/tools/graviton-ready-java/target' --add-data 'src/advisor/templates/template.html:advisor/templates' --add-data "$CERT_PATH/certifi/cacert.pem:certifi" --name "$FILE_NAME" "src/porting-advisor.py" --runtime-hook 'src/updater.py' --exclude-module readline
if [ $? -ne 0 ]; then
echo "**ERROR**: pyinstaller failed, binary was not created" && exit 1
fi
echo "*** Success: Executable saved at dist/$FILE_NAME ***"
exit 0
else
echo "**ERROR**: python3 is missing, please install it before running this build script"
exit 1
echo "🏗️ Generating executable"
CERT_PATH=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
pyinstaller --onefile --clean --noconfirm --distpath dist --add-data 'src/advisor/rules/*.json:advisor/rules' --add-data 'src/advisor/tools/graviton-ready-java/target/*:advisor/tools/graviton-ready-java/target' --add-data 'src/advisor/templates/template.html:advisor/templates' --add-data "$CERT_PATH/certifi/cacert.pem:certifi" --name "$FILE_NAME" "src/porting-advisor.py" --runtime-hook 'src/updater.py' --exclude-module readline
if [ $? -ne 0 ]; then
echo "**ERROR**: pyinstaller failed, binary was not created" && exit 1
fi
echo 🎉 "*** Success: Executable saved at dist/$FILE_NAME ***"
exit 0
39 changes: 39 additions & 0 deletions setup-environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

# This script sets up the Python Virtual Environment to install dependencies

if hash python3
then
echo "🐍 python3 is installed"

if hash pip3
then
echo "📦 pip is installed"
else
echo "**ERROR**: python pip3 not found, please install pip3" && exit 1
fi

if [ ! -f ".venv/bin/activate" ]; then
echo "💻 Creating Python virtual environment"
python3 -m venv .venv
if [ $? -ne 0 ]; then
echo "**ERROR**: could not create Python Virtual Environment." && exit 1
fi
fi

echo "💡 Making sure Python Virtual Environment is active"
. .venv/bin/activate
if [ $? -ne 0 ]; then
echo "**ERROR**: could not activate Python Virtual Environment." && exit 1
fi

echo "☁️ Installing requirements"
python3 -m pip install -r requirements-build.txt
if [ $? -ne 0 ]; then
echo "**ERROR**: error installing required packages" && exit 1
fi
exit 0
else
echo "**ERROR**: python3 is missing, please install it before running this build script"
exit 1
fi
2 changes: 1 addition & 1 deletion src/advisor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""

__project__ = 'porting-advisor'
__version__ = '1.0.0'
__version__ = '1.0.2'
__summary__ = 'Produces an aarch64 porting readiness report.'
__webpage__ = 'http://www.gitlab.com/arm-hpc/porting-advisor'

Expand Down
3 changes: 2 additions & 1 deletion src/advisor/helpers/java/java_tool_invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def ensure_jar(self):
return False

pom_path = path.abspath(path.join(path.dirname(__file__), '..', '..', 'tools', 'graviton-ready-java', 'pom.xml'))
mvn_process = subprocess.run(['mvn', 'package', '--file', pom_path], capture_output=True)
logging.debug(f'POM file: {pom_path}')
mvn_process = subprocess.run(f'mvn package --file {pom_path}', capture_output=True, shell=True)
if (mvn_process.returncode == 0):
return True

Expand Down
4 changes: 2 additions & 2 deletions src/advisor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def main(argv=sys.argv[1:]):
args = parser.parse_args(argv)

if (args.log_to_console):
logging.basicConfig(level=get_logging_level(args.logging_level), format='%(asctime)s-%(levelname)s-%(message)s')
logging.basicConfig(level=get_logging_level(args.logging_level), format='%(asctime)s - %(levelname)s - %(message)s')
else:
logging.basicConfig(level=get_logging_level(args.logging_level), format='%(asctime)s-%(levelname)s-%(message)s', filename=args.log_file)
logging.basicConfig(level=get_logging_level(args.logging_level), format='%(asctime)s - %(levelname)s - %(message)s', filename=args.log_file)

if not os.path.exists(args.root):
print(_('%s: directory not found.') % args.root, file=sys.stderr)
Expand Down
19 changes: 13 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

# Runs unit tests, generates the binary and then runs it against sample projects

# build project
echo "Building project"
./build.sh
# run unit tests
echo "🐍 Setup virtual environment"
./setup-environment.sh
if [ $? -ne 0 ]; then
echo "**ERROR**: failed to build project" && exit 1
echo "**ERROR**: failed to initialize Python Virtual Environment" && exit 1
fi

# run unit tests
echo "Running unit tests"
echo "🔬 Running unit tests"
./unit-test.sh
if [ $? -ne 0 ]; then
echo "**ERROR**: unit tests failed" && exit 1
fi

echo "Running integration tests"
# build project
echo "⚒️ Building project"
./build.sh
if [ $? -ne 0 ]; then
echo "**ERROR**: failed to build project" && exit 1
fi

echo "🧪 Running integration tests"
./integration-test.sh
if [ $? -ne 0 ]; then
echo "**ERROR**: integration tests failed" && exit 1
Expand Down
4 changes: 2 additions & 2 deletions unit-test.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/sh

echo "Making sure Python Virtual Environment is active"
echo "🐍 Making sure Python Virtual Environment is active"
. .venv/bin/activate
if [ $? -ne 0 ]; then
echo "**ERROR**: could not activate Python Virtual Environment." && exit 1
fi

# run unit tests
echo "*** running unit tests ***"
echo "🔬 *** running unit tests ***"
python -m coverage run --source=./src -m unittest discover -s unittest -p "test_*.py" -v
if [ $? -ne 0 ]; then
echo "**ERROR**: unit tests failed" && exit 1
Expand Down

0 comments on commit 18b7867

Please sign in to comment.