diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a8ec525b31714b12acb055b8a3f4f2012d6b0de0..f673c099b4682350060377c6cd273dec5a1b736b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,3 +1,6 @@ +variables: + meeting_pattern: '/meeting.*/' + workflow: rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" @@ -38,7 +41,7 @@ generate_artefacts: stage: build interruptible: true rules: - - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME !~ $meeting_pattern script: - echo $CI_PROJECT_ID - echo $CI_PROJECT_NAME @@ -55,3 +58,26 @@ generate_artefacts: - "*.docx" name: $CI_MERGE_REQUEST_TITLE expire_in: 30 days + +generate_attachments: + image: "forge.etsi.org:5050/li/schemas-definitions/forgelib" + stage: build + interruptible: true + rules: + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ $meeting_pattern + script: + - echo $CI_PROJECT_ID + - echo $CI_PROJECT_NAME + - echo $CI_PROJECT_PATH + - echo $CI_PIPELINE_SOURCE + - echo $CI_OPEN_MERGE_REQUESTS + - echo $CI_MERGE_REQUEST_IID + - echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME + - echo $ARTEFACT_NAME + - python create_attachments.py + artifacts: + untracked: true + paths: + - "*.zip" + expire_in: 30 days diff --git a/create_attachments.py b/create_attachments.py new file mode 100644 index 0000000000000000000000000000000000000000..4beaaa0d01c973b926697d7044f702396ce0e2d1 --- /dev/null +++ b/create_attachments.py @@ -0,0 +1,53 @@ +from io import BytesIO +from pathlib import Path +import zipfile + +# For ETSI portal attachments, a single zip archive is created which +# contains: +# - All files in the top-level spec directory +# - A zipfile for each subdirectory, which contains all the files in that subdirectory +# This second step is recursive, so each zip file contains only files and other zipfiles, never subdirectories +# For an explanation as to *why* this is the case, speak to your ETSI technical officer. +# +# Example: Assuming the files for the portal attachment are of the following form: +# +# (contents of directory) +# - top_level_file.ext +# - subdirectory_1 +# - subdirectory_1_file.ext +# - subdirectory_2 +# - subdirectory_2_file.ext +# - TS 102 232-1 +# +# ...then the correct form for ETSI portal attachments is as follows +# +# ts_xxxxxx_vxxyyzzp0.zip +# which contains: +# - top_level_file.ext +# - subdirectory_1.zip +# (which contains) +# - subdirectory_1_file.ext +# - subdirectory_2.zip +# (which contains) +# - subdirectory_2_file.ext + + +def recursively_zip_directory(directory: Path, zipname: str, recursion=0): + buffer = BytesIO() + zip = zipfile.ZipFile(buffer, "a") + for f in directory.iterdir(): + if f.is_file(): + zip.write(f, f.name) + elif f.is_dir(): + zipname = f.with_suffix(".zip").name + recurse_buffer = recursively_zip_directory(f, zipname, recursion + 1) + zip.writestr(zipname, recurse_buffer.getvalue()) + return buffer + + +if __name__ == "__main__": + for directory in Path(".").glob("1*"): + zip_name = f"ts_{directory.name}vXXYYZZp0.zip" + zip_buffer = recursively_zip_directory(directory, zip_name) + with open(zip_name, "wb") as f: + f.write(zip_buffer.getvalue())