From a0ee8e28b4374e28f2b66f15b11af25ba863bb3c Mon Sep 17 00:00:00 2001
From: LUIS CONTRERAS <luis.contreras@ess.eu>
Date: Thu, 9 Mar 2023 13:42:01 +0100
Subject: [PATCH] First commit

---
 .gitlab-ci.yml      | 21 ++++++++++++++
 process_archives.py | 71 +++++++++++++++++++++++++++++++++++++++++++++
 send_files.py       | 54 ++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 process_archives.py
 create mode 100644 send_files.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..4061ef6
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,21 @@
+---
+stages:
+  - deploy
+
+deploy:
+  tags:
+    - archiver
+  stage: deploy
+  image: python:3.6
+  before_script:
+    - >
+      pip install py-epicsarchiver
+      -i https://artifactory.esss.lu.se/artifactory/api/pypi/pypi-virtual/simple
+  script:
+    - echo "test"
+    - python process_archives.py -v --before-sha $CI_COMMIT_BEFORE_SHA --sha $CI_COMMIT_SHA --host $ARCHIVER_HOST
+  only:
+    refs:
+      - master@archiver-appliance/archiver-appliance-config-aa-linac-prod
+    changes:
+      - "*/*.archive"
diff --git a/process_archives.py b/process_archives.py
new file mode 100644
index 0000000..e6f684c
--- /dev/null
+++ b/process_archives.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+# Copyright (c) 2018 European Spallation Source ERIC
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+import click
+import shlex
+import subprocess
+from pathlib import Path
+from pprint import pprint
+from epicsarchiver import ArchiverAppliance
+
+
+def run(cmd, cwd=None):
+    """Run the shell command and return the result"""
+    args = shlex.split(cmd)
+    result = subprocess.run(args, check=True, stdout=subprocess.PIPE, cwd=cwd)
+    return result.stdout.decode("utf-8").strip()
+
+
+def get_files_modified(commit_before_sha, commit_sha):
+    """Return the files modified between the 2 commits"""
+    result = run(
+        f"git diff --name-only --diff-filter=d {commit_before_sha}..{commit_sha}"
+    )
+    return [Path(filename) for filename in set(result.split("\n")) if filename]
+
+
+@click.command()
+@click.option("--before-sha", help="Previous commit sha", required=True)
+@click.option("--sha", help="Current commit sha [default: HEAD]", default="HEAD")
+@click.option("--host", help="Archiver appliance host", required=True)
+@click.option("--verbose", "-v", is_flag=True, help="Increase verbosity")
+def cli(before_sha, sha, host, verbose):
+    """Send PVs to the archiver for files changed between 2 commits"""
+    archive_files = [
+        filepath
+        for filepath in get_files_modified(before_sha, sha)
+        if filepath.suffix == ".archive"
+    ]
+    print(archive_files)
+    if archive_files:
+        click.echo(f"Sending PVs from {','.join([str(f) for f in archive_files])} to {host}")
+        archiver = ArchiverAppliance(host)
+        result = archiver.archive_pvs_from_files(
+            archive_files
+            #, appliance=archiver.identity
+        )
+        if verbose:
+            pprint(result)
+    else:
+        click.echo(f"No new PV for {host}")
+
+
+if __name__ == "__main__":
+    cli()
diff --git a/send_files.py b/send_files.py
new file mode 100644
index 0000000..611df96
--- /dev/null
+++ b/send_files.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# Copyright (c) 2018 European Spallation Source ERIC
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+import click
+import shlex
+import subprocess
+from pathlib import Path
+from pprint import pprint
+from epicsarchiver import ArchiverAppliance
+from process_archives import get_files_per_archiver
+
+
+@click.command()
+@click.option("--verbose", "-v", is_flag=True, help="Increase verbosity")
+@click.argument("filenames", nargs=-1, type=click.Path(exists=True))
+def cli(filenames, verbose):
+    """Send PVs to the archiver(s) from the given files"""
+    if not filenames:
+        raise click.UsageError("You must pass the files to send in arguments")
+    archivers = get_files_per_archiver([Path(name) for name in filenames])
+    if verbose:
+        pprint(archivers)
+    for hostname, files in archivers.items():
+        if files:
+            click.echo(
+                f"Sending PVs from {','.join([str(f) for f in files])} to {hostname}"
+            )
+            archiver = ArchiverAppliance(hostname)
+            result = archiver.archive_pvs_from_files(files, appliance=archiver.identity)
+            if verbose:
+                pprint(result)
+        else:
+            click.echo(f"No new PV for {hostname}")
+
+
+if __name__ == "__main__":
+    cli()
-- 
GitLab