Skip to content

✨ sort the python imports and some quality improvements #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 51 additions & 40 deletions .github/scripts/publish_packages.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
from collections import namedtuple
import glob
import hashlib
import json
import os
import yaml
import re
import shutil
import tempfile
import hashlib
import subprocess
import tempfile
import time
import json
import re
from collections import namedtuple
from typing import List
from typing import Tuple

import yaml

Package = namedtuple("Package", "name version location title description author tags")


UploadedPackage = namedtuple("UploadedPackage", "name version")

should_publish = os.getenv("PUBLISH", "false") == "true"

RELEASES_VERSION = "v1.0.0"


# Get the packages that have been published on the Github Releases section
def get_released_packages() -> List[UploadedPackage]:
"""Get the packages that have been published on the Github Releases section"""
result = subprocess.run(
["gh", "release", "view", "--json", "assets"], stdout=subprocess.PIPE
)
Expand All @@ -32,7 +34,7 @@ def get_released_packages() -> List[UploadedPackage]:
packages = []
for package in assets:
file_name = package["name"]
match = re.search("(?P<name>.*?)-(?P<version>\d+.\d+.\d+).zip", file_name)
match = re.search(r"(?P<name>.*?)-(?P<version>\d+.\d+.\d+).zip", file_name)
if match is not None:
name = match.group("name")
version = match.group("version")
Expand All @@ -42,12 +44,13 @@ def get_released_packages() -> List[UploadedPackage]:
return packages


# Get the packages defined in the hub repository
def get_repository_packages() -> List[Package]:
"""Get the packages defined in the hub repository"""
packages = []
for path in glob.glob("packages/*/*/_manifest.yml"):
print(f"reading: {path}")
package_dir = os.path.dirname(path)
with open(path, "r") as stream:
with open(path, "r", encoding="utf-8") as stream:
try:
manifest = yaml.safe_load(stream)
name = manifest["name"]
Expand All @@ -65,11 +68,12 @@ def get_repository_packages() -> List[Package]:
return packages


# Calculate which packages have NOT been published yet on Releases,
# the ones that will need to be published during this run
def calculate_missing_packages(
released: List[Package], repository: List[Package]
released: List[UploadedPackage], repository: List[Package]
) -> List[Package]:
"""Calculate which packages have NOT been published yet on Releases,
the ones that will need to be published during this run
"""
published_packages: set[str] = set()
for package in released:
published_packages.add(f"{package.name}@{package.version}")
Expand All @@ -83,17 +87,17 @@ def calculate_missing_packages(
return missing_packages


# Calculate SHA256 of the given file
def calculate_sha256(filename) -> str:
def calculate_sha256(filename: str) -> str:
"""Calculate SHA256 of the given file"""
sha256_hash = hashlib.sha256()
with open(filename, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()


# Archive the given package, calculating its hash
def create_archive(package: Package) -> Tuple[str, str, str]:
"""Archive the given package, calculating its hash"""
temp_dir = tempfile.gettempdir()
target_name = os.path.join(temp_dir, f"{package.name}-{package.version}")
shutil.make_archive(target_name, "zip", package.location)
Expand All @@ -110,8 +114,8 @@ def create_archive(package: Package) -> Tuple[str, str, str]:
return (target_file, target_sha_file, hash)


# Upload the package files (archive + SHA256 sum) on GH Releases
def upload_to_releases(archive_path, archive_hash_path):
"""Upload the package files (archive + SHA256 sum) on GH Releases"""
if should_publish:
subprocess.run(
[
Expand All @@ -126,8 +130,8 @@ def upload_to_releases(archive_path, archive_hash_path):
)


# Generate the updated index and upload it to GH Releases
def update_index(repository: List[Package]):
"""Generate the updated index and upload it to GH Releases"""
packages = []

for package in repository:
Expand Down Expand Up @@ -163,35 +167,42 @@ def update_index(repository: List[Package]):
)


print("Reading packages from repository...")
repository_packages = get_repository_packages()
def main():
print("Reading packages from repository...")
repository_packages = get_repository_packages()

print("Obtaining released packages from GitHub Releases...")
released_packages = get_released_packages()

print("")
print("Calculating delta...")
missing_packages = calculate_missing_packages(
released_packages, repository_packages
)

print("Obtaining released packages from GitHub Releases...")
released_packages = get_released_packages()
if len(missing_packages) == 0:
print("Packages are already up-to-date")
quit(0)

print("")
print("Calculating delta...")
missing_packages = calculate_missing_packages(released_packages, repository_packages)
print("")
print("Packages to publish:")
for package in missing_packages:
print(f"--> {package.name}@{package.version}")

if len(missing_packages) == 0:
print("Packages are already up-to-date")
quit(0)
archive_path, archive_hash_path, archive_hash = create_archive(package)

print("")
print("Packages to publish:")
for package in missing_packages:
print(f"--> {package.name}@{package.version}")
print(f"Created archive {archive_path}, hash: {archive_hash}")

archive_path, archive_hash_path, archive_hash = create_archive(package)
print("Uploading to Github Releases...")
upload_to_releases(archive_path, archive_hash_path)
print("Done!")

print(f"Created archive {archive_path}, hash: {archive_hash}")
print("")
print("Updating index...")
update_index(repository_packages)

print("Uploading to Github Releases...")
upload_to_releases(archive_path, archive_hash_path)
print("Done!")

print("")
print("Updating index...")
update_index(repository_packages)

print("Done!")
if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions .github/scripts/validate/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob
import os
import sys

from validate import validate_package

report_errors = []
Expand Down
3 changes: 2 additions & 1 deletion .github/scripts/validate/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import unittest

from validate import validate_package
import os

TEST_PACKAGES_DIR = os.path.join(os.path.dirname(__file__), "test_packages")

Expand Down
11 changes: 6 additions & 5 deletions .github/scripts/validate/validate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from collections import namedtuple
import os
from collections import namedtuple
from typing import List
from .rules.missing_mandatory_files import MissingMandatoryFiles
from .rules.invalid_version_path import InvalidVersionPath
from .rules.invalid_package_name import InvalidPackageName

from .rules.incoherent_path import IncoherentPath
from .rules.missing_manifest_fields import MissingManifestFields
from .rules.invalid_package_name import InvalidPackageName
from .rules.invalid_version_path import InvalidVersionPath
from .rules.invalid_yaml import InvalidYAML
from .rules.missing_mandatory_files import MissingMandatoryFiles
from .rules.missing_manifest_fields import MissingManifestFields
from .rules.no_yaml_extension import NoYAMLExtension

RULES = [
Expand Down
4 changes: 3 additions & 1 deletion .github/scripts/validate/validate/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from abc import ABC, abstractmethod
import glob
import os
from abc import ABC
from abc import abstractmethod
from typing import List

import yaml


Expand Down
1 change: 1 addition & 0 deletions .github/scripts/validate/validate/rules/incoherent_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from . import ValidationRule


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re

from . import ValidationRule

VALIDATE_REGEX = re.compile(r"^[a-z0-9\-]+$")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glob
import os

from . import ValidationRule


Expand Down
2 changes: 2 additions & 0 deletions .github/scripts/validate/validate/rules/invalid_yaml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import glob
import os

import yaml

from . import ValidationRule


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from . import ValidationRule

REQUIRED_FILES = ["package.yml", "README.md", "_manifest.yml"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glob
import os

from . import ValidationRule


Expand Down