-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up pre-commit hooks and workflows to check code style (#313)
- Loading branch information
Showing
44 changed files
with
948 additions
and
840 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Migrate code style to Black | ||
a74466c154d01201388f5232b167538275aa74c4 |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
This pull request was created by GitHub Actions/AWS CodeBuild! Before merging, please do the following: | ||
- [ ] Review changelog/staleness report. | ||
- [ ] Review build/test results by clicking *Build Logs* in CI Report (be patient, tests take ~4hr). | ||
- [ ] Review ECR Scan results. | ||
- [ ] Review ECR Scan results. |
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,50 @@ | ||
name: check_code_quality | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
paths: | ||
- "src/**.py" | ||
- "test/**.py" | ||
- "template/**.py" | ||
|
||
pull_request: | ||
branches: [ main ] | ||
paths: | ||
- "src/**.py" | ||
- "test/**.py" | ||
- "template/**.py" | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
defaults: | ||
run: | ||
shell: bash -l {0} | ||
|
||
jobs: | ||
build: | ||
name: Check Code Quality | ||
runs-on: ubuntu-latest | ||
if: github.repository == 'aws/sagemaker-distribution' | ||
permissions: | ||
pull-requests: write | ||
contents: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: mamba-org/setup-micromamba@v1 | ||
with: | ||
environment-file: ./environment.yml | ||
environment-name: sagemaker-distribution | ||
init-shell: bash | ||
- name: Free up disk space | ||
run: rm -rf /opt/hostedtoolcache | ||
- name: Activate sagemaker-distribution | ||
run: micromamba activate sagemaker-distribution | ||
- name: Check style with black | ||
run: black --line-length=120 --check src test template | ||
- name: Check style with autoflake | ||
run: autoflake --in-place --expand-star-imports --ignore-init-module-imports --remove-all-unused-imports -rc src test template | ||
- name: Check style with isort | ||
run: isort --profile black -c src test template |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__pycache__ | ||
.idea | ||
.DS_Store | ||
.DS_Store |
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,26 @@ | ||
default_language_version: | ||
# force all unspecified python hooks to run python3 | ||
python: python3 | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- id: detect-aws-credentials | ||
- repo: https://github.com/humitos/mirrors-autoflake.git | ||
rev: v1.3 | ||
hooks: | ||
- id: autoflake | ||
args: ['--in-place', '--expand-star-imports', '--ignore-init-module-imports', '--remove-all-unused-imports'] | ||
- repo: https://github.com/psf/black | ||
rev: 23.3.0 | ||
hooks: | ||
- id: black | ||
args: [--line-length=120] | ||
- repo: https://github.com/pycqa/isort | ||
rev: 5.12.0 | ||
hooks: # imports sorting | ||
- id: isort | ||
name: isort (python) | ||
args: ["--profile", "black"] |
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
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
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 |
---|---|---|
|
@@ -10,3 +10,7 @@ dependencies: | |
- pytest-mock | ||
- pytest-xdist | ||
- semver | ||
- autoflake | ||
- black | ||
- isort | ||
- pre-commit |
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 |
---|---|---|
@@ -1,67 +1,65 @@ | ||
import os | ||
from utils import ( | ||
get_dir_for_version, | ||
get_semver, | ||
get_match_specs, | ||
) | ||
|
||
from semver import Version | ||
|
||
from utils import get_dir_for_version, get_match_specs, get_semver | ||
|
||
|
||
def _derive_changeset(target_version_dir, source_version_dir, image_config) -> (dict[str, | ||
list[str]], dict[str, str]): | ||
env_in_file_name = image_config['build_args']['ENV_IN_FILENAME'] | ||
env_out_file_name = image_config['env_out_filename'] | ||
required_packages_from_target = get_match_specs( | ||
target_version_dir + "/" + env_in_file_name | ||
).keys() | ||
def _derive_changeset(target_version_dir, source_version_dir, image_config) -> (dict[str, list[str]], dict[str, str]): | ||
env_in_file_name = image_config["build_args"]["ENV_IN_FILENAME"] | ||
env_out_file_name = image_config["env_out_filename"] | ||
required_packages_from_target = get_match_specs(target_version_dir + "/" + env_in_file_name).keys() | ||
target_match_spec_out = get_match_specs(target_version_dir + "/" + env_out_file_name) | ||
source_match_spec_out = get_match_specs(source_version_dir + "/" + env_out_file_name) | ||
|
||
# Note: required_packages_from_source is not currently used. | ||
# In the future, If we remove any packages from env.in, at that time required_packages_from_source will be needed. | ||
# We only care about the packages which are present in the target version env.in file | ||
installed_packages_from_target = {k: str(v.get('version')).removeprefix('==') | ||
for k, v in target_match_spec_out.items() | ||
if k in required_packages_from_target} | ||
installed_packages_from_target = { | ||
k: str(v.get("version")).removeprefix("==") | ||
for k, v in target_match_spec_out.items() | ||
if k in required_packages_from_target | ||
} | ||
# Note: A required package in the target version might not be a required package in the source version | ||
# But source version could still have this package pulled as a dependency of a dependency. | ||
installed_packages_from_source = {k: str(v.get('version')).removeprefix('==') for | ||
k, v in source_match_spec_out.items() | ||
if k in required_packages_from_target} | ||
upgrades = {k: [installed_packages_from_source[k], v] for k, v in installed_packages_from_target.items() | ||
if k in installed_packages_from_source and installed_packages_from_source[k] != v} | ||
new_packages = {k: v for k, v in installed_packages_from_target.items() | ||
if k not in installed_packages_from_source} | ||
installed_packages_from_source = { | ||
k: str(v.get("version")).removeprefix("==") | ||
for k, v in source_match_spec_out.items() | ||
if k in required_packages_from_target | ||
} | ||
upgrades = { | ||
k: [installed_packages_from_source[k], v] | ||
for k, v in installed_packages_from_target.items() | ||
if k in installed_packages_from_source and installed_packages_from_source[k] != v | ||
} | ||
new_packages = {k: v for k, v in installed_packages_from_target.items() if k not in installed_packages_from_source} | ||
# TODO: Add support for removed packages. | ||
return upgrades, new_packages | ||
|
||
|
||
def generate_change_log(target_version: Version, image_config): | ||
target_version_dir = get_dir_for_version(target_version) | ||
source_version_txt_file_path = f'{target_version_dir}/source-version.txt' | ||
source_version_txt_file_path = f"{target_version_dir}/source-version.txt" | ||
if not os.path.exists(source_version_txt_file_path): | ||
print('[WARN]: Generating CHANGELOG is skipped because \'source-version.txt\' isn\'t ' | ||
'found.') | ||
print("[WARN]: Generating CHANGELOG is skipped because 'source-version.txt' isn't " "found.") | ||
return | ||
with open(source_version_txt_file_path, 'r') as f: | ||
with open(source_version_txt_file_path, "r") as f: | ||
source_patch_version = f.readline() | ||
source_version = get_semver(source_patch_version) | ||
source_version_dir = get_dir_for_version(source_version) | ||
image_type = image_config['image_type'] | ||
upgrades, new_packages = _derive_changeset(target_version_dir, source_version_dir, | ||
image_config) | ||
with open(f'{target_version_dir}/CHANGELOG-{image_type}.md', 'w') as f: | ||
f.write('# Change log: ' + str(target_version) + '(' + image_type + ')\n\n') | ||
image_type = image_config["image_type"] | ||
upgrades, new_packages = _derive_changeset(target_version_dir, source_version_dir, image_config) | ||
with open(f"{target_version_dir}/CHANGELOG-{image_type}.md", "w") as f: | ||
f.write("# Change log: " + str(target_version) + "(" + image_type + ")\n\n") | ||
if len(upgrades) != 0: | ||
f.write('## Upgrades: \n\n') | ||
f.write('Package | Previous Version | Current Version\n') | ||
f.write('---|---|---\n') | ||
f.write("## Upgrades: \n\n") | ||
f.write("Package | Previous Version | Current Version\n") | ||
f.write("---|---|---\n") | ||
for package in upgrades: | ||
f.write(package + '|' + upgrades[package][0] + '|' | ||
+ upgrades[package][1] + '\n') | ||
f.write(package + "|" + upgrades[package][0] + "|" + upgrades[package][1] + "\n") | ||
if len(new_packages) != 0: | ||
f.write('\n## What\'s new: \n\n') | ||
f.write('Package | Version \n') | ||
f.write('---|---\n') | ||
f.write("\n## What's new: \n\n") | ||
f.write("Package | Version \n") | ||
f.write("---|---\n") | ||
for package in new_packages: | ||
f.write(package + '|' + new_packages[package] + '\n') | ||
f.write(package + "|" + new_packages[package] + "\n") |
Oops, something went wrong.