Skip to content

Commit

Permalink
Merge pull request #5 from CMSgov/feature/gradle-support
Browse files Browse the repository at this point in the history
Feature/gradle support
  • Loading branch information
aleemmalik-gov authored Jan 22, 2025
2 parents 49d6eaf + 98b792f commit defbb0b
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 59 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ license_files =

[pylint]
ignore = version.py
disable = R0801,R0903
disable = R0801,R0903,W0246,R1735,W0718,R1734,R1735,W0718,C0209,too-many-positional-arguments
max-line-length = 140
output-format = colorized

[tool:pytest]
Expand Down Expand Up @@ -61,3 +62,4 @@ tests =
mock
codecov
tox

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from ploigos_step_runner.results import StepResult
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.step_implementers.shared import GradleGeneric
from ploigos_step_runner.utils.gradle import run_gradle, GradleGroovyParser

from ploigos_step_runner.utils.gradle import GradleGroovyParser


DEFAULT_CONFIG = {
'build-file': 'app/build.gradle',
Expand All @@ -41,7 +43,7 @@
class Gradle(GradleGeneric):
"""`StepImplementer` for the `generate-metadata` step using Gradle.
"""

@staticmethod
def step_implementer_config_defaults():
"""Getter for the StepImplementer's configuration defaults.
Expand Down Expand Up @@ -88,7 +90,7 @@ def _validate_required_config_or_previous_step_result_artifact_keys(self):
If step configuration or previous step result artifacts have invalid required values
"""
super()._validate_required_config_or_previous_step_result_artifact_keys()

def _run_step(self):
"""Runs the step implemented by this StepImplementer.
Expand Down Expand Up @@ -116,4 +118,5 @@ def _run_step(self):
step_result.success = False
step_result.message = str(error)


return step_result
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

"""PSR Step for pushing artifact with Gradle to artifactory"""
import os
import xml.etree.ElementTree as ET
import subprocess
import yaml
import time
from pathlib import Path
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.results.step_result import StepResult
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric
Expand Down Expand Up @@ -74,9 +71,10 @@ def read_and_replace_password(self):
current_path = os.path.join(os.getcwd(), "app/build")
print("current_path")
print(current_path)
files_via_Path = os.listdir(current_path)
for file in files_via_Path:
print("\n files_via_Path ::" + file)
files_via_path = os.listdir(current_path)
for file in files_via_path:
print("\n files_via_path ::" + file)


current_working_directory = os.getcwd()
print("current_working_directory")
Expand All @@ -89,7 +87,8 @@ def read_and_replace_password(self):
artifactory_password = self.get_value("gradle-token-alpha")

# # Read the properties file
with open(properties_file, "r") as file:

with open(properties_file, "r", encoding="utf8") as file:
for line in file:
# Skip comments and empty lines
line = line.strip()
Expand All @@ -101,13 +100,14 @@ def read_and_replace_password(self):
if "artifactory_password" in properties:
properties["artifactory_password"] = artifactory_password

# Write the modified properties back to the file
with open(properties_file, "w") as file:

with open(properties_file, "w", encoding="utf8") as file:
for key, value in properties.items():
file.write(f"{key}={value}\n")

# print out the properties file
with open(properties_file, "r") as file:

with open(properties_file, "r", encoding="utf8") as file:
content = file.read()
print("\n build.properties file: ")
print(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Configuration Key | Required? | Default | Description
-----------------------------|-----------|---------|-----------
`build-file` | Yes | `'build.gradle'` | buildfile used when executing gradle.
`build-file` | Yes | `'build.gradle'` | builfile used when executing gradle.
`tasks` | Yes | | List of gradle tasks to execute.
`gradle-console-plain` | No | `True` | `True` use old append style log output. \
`False` use new fancy screen redraw log output.
Expand Down Expand Up @@ -109,7 +111,7 @@ def _validate_required_config_or_previous_step_result_artifact_keys(self):
if build_file is not None:
assert os.path.exists(build_file), \
f'Given gradle build file does not exist: {build_file}'

@property
def gradle_tasks(self):
"""Property for getting the gradle tasks to execute which can either come
Expand All @@ -129,7 +131,7 @@ def gradle_tasks(self):
gradle_tasks = self.get_value('gradle-tasks')

return gradle_tasks

def _run_gradle_step(
self,
gradle_output_file_path,
Expand Down
15 changes: 10 additions & 5 deletions src/ploigos_step_runner/step_implementers/unit_test/gradle_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

"""PSR step for running Unit Tests with Gradle"""
import os
import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -113,11 +115,12 @@ def _run_step(self):
for filename in os.listdir(test_report_dir):
if filename.endswith('.xml'):
fullname = os.path.join(test_report_dir, filename)
test_results = self._get_test_results_from_file(fullname, self.TEST_RESULTS_ATTRIBUTES)
test_results = \
self._get_test_results_from_file(fullname, self.TEST_RESULTS_ATTRIBUTES)

# check for valid file
if not test_results:
StepResult.message += (f'\nWARNING: Did not find any test results for file {fullname}')
step_result.message += (f'\nWARNING: Did not find any test results for file {fullname}')

# check for required attributes
missing_attributes = self._get_missing_required_test_attributes(test_results, self.TEST_RESULTS_ATTRIBUTES_REQUIRED)
Expand Down Expand Up @@ -175,13 +178,15 @@ def _combine_test_results(self, total, current):
for k in total.keys():
if k in current:
string = current[k]
if '.' in string:

if '.' in string:

num = float(string)
total[k] = float(total[k]) + num
else:
num = int(string)
total[k] = int(total[k]) + num
except Exception as e:
print(f"WARNING: Error converting string to number in file \n {e}")
return total

print(f"WARNING: Error converting string to number in file \n {e}")
return total
74 changes: 40 additions & 34 deletions src/ploigos_step_runner/utils/gradle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.utils.io import \
create_sh_redirect_to_multiple_streams_fn_callback



class GradleGroovyParserException(Exception):
"""An exception dedicated to gradle's groovy DSL parsing.
"""An exception dedicated to gradle's groovy DSL parsing.
Parameters
----------
file_name : str
Path to the file that is being parsed.
message : str,
The message detailing the exception.
Parameters
----------
file_name : str
Path to the file that is being parsed.
message : str,
The message detailing the exception.
Returns
-------
Str
String with the file name and message detailing the exception.
"""
def __init__(self, file_name, message):

self.file_name = file_name
self.message = message
Returns
-------
Str
String with the file name and message detailing the exception.
"""
def __init__(self, file_name, message):
self.file_name = file_name
self.message = message

def __str__(self):
return "%s file: %s" % (self.file_name, self.message)

def __str__(self):
return "%s file: %s" % (self.file_name, self.message)

class GradleGroovyParser:
"""A gradle groovy build file parser.
Expand All @@ -53,15 +54,17 @@ class GradleGroovyParser:
Bool
True if step completed successfully
False if step returned an error message
"""
"""
file_name = ""
raw_file = None

def __init__(self, file_name):
self.file_name = file_name
with open(file_name) as f:
with open(file_name, encoding='utf-8') as f:
self.raw_file = f.read()



def get_version(self):
"""Gets the project version from a gradle groovy build file.
Expand All @@ -80,15 +83,18 @@ def get_version(self):
if len(tokens) == 1:
version = tokens[0].strip()
elif len(tokens) > 1:
raise GradleGroovyParserException(self.file_name, "More than one version found. " + str(tokens) )
return version

raise GradleGroovyParserException(self.file_name, "More than one version found. " + str(tokens) )


def run_gradle( #pylint: disable=too-many-arguments, too-many-locals
gradle_output_file_path,
build_file,
tasks,
additional_arguments=None,
console_plain=True

console_plain=True

):
"""Runs gradle using the given configuration.
Expand Down Expand Up @@ -117,18 +123,22 @@ def run_gradle( #pylint: disable=too-many-arguments, too-many-locals
StepRunnerException
If gradle returns a none 0 exit code.
"""



if not isinstance(tasks, list):
tasks = [tasks]



# create console plain argument
console_plain_argument = None
if console_plain:
console_plain_argument = '--console=plain'



if not additional_arguments:
additional_arguments = []



# run gradle
gradle_output_buff = StringIO()
try:
Expand Down Expand Up @@ -161,7 +171,3 @@ def run_gradle( #pylint: disable=too-many-arguments, too-many-locals
gradle_output_stripped_ansi = re.compile(r'\x1b[^m]*m').sub('', gradle_output)

return gradle_output_stripped_ansi




Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def create_step_implementer(
def test_step_implementer_config_defaults(self):
defaults = Gradle.step_implementer_config_defaults()
expected_defaults = {
'build-file': 'build.gradle',

'build-file': 'app/build.gradle',

'gradle-additional-arguments': [],
'gradle-console-plain': True
}
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def test_local_file_download_file_prefix(self):

def test_local_file_download_forward_slash_prefix(self):
sample_file_path = os.path.join(

os.path.dirname(__file__),
'files',
'simple.xml'

)

with TempDirectory() as test_dir:
Expand Down

0 comments on commit defbb0b

Please sign in to comment.