From cba123e69a8f18be12a700004cf27cf0814f05f9 Mon Sep 17 00:00:00 2001 From: Kevin Peterson Date: Thu, 28 Apr 2022 15:21:20 -0500 Subject: [PATCH] Use the travis post-prepare.py and util.py scripts --- .ci-local/github-actions/post-prepare.py | 88 +++++++----------------- .ci-local/github-actions/util.py | 68 ++++++++++++++++++ 2 files changed, 93 insertions(+), 63 deletions(-) create mode 100644 .ci-local/github-actions/util.py diff --git a/.ci-local/github-actions/post-prepare.py b/.ci-local/github-actions/post-prepare.py index 7dccc497..01e985b6 100755 --- a/.ci-local/github-actions/post-prepare.py +++ b/.ci-local/github-actions/post-prepare.py @@ -1,70 +1,32 @@ #!/usr/bin/env python import os -import shutil -import re +import sys +import subprocess -# Setup ANSI Colors (copied from cue.py) -ANSI_RED = "\033[31;1m" -ANSI_GREEN = "\033[32;1m" -ANSI_YELLOW = "\033[33;1m" -ANSI_BLUE = "\033[34;1m" -ANSI_MAGENTA = "\033[35;1m" -ANSI_CYAN = "\033[36;1m" -ANSI_RESET = "\033[0m" -ANSI_CLEAR = "\033[0K" +from util import * -def cat(filename): - ''' - Print the contents of a file - ''' - with open(filename, 'r') as fh: - for line in fh: - print(line.strip()) +def get_submodule_releases(): + # + releases = [] + for root, dirs, files in os.walk('modules'): + for submodule in dirs: + if 'motor' in submodule: + release_path = "modules/{}/configure/RELEASE".format(submodule) + if os.path.exists(release_path): + releases.append(release_path) + return releases[:] -def sanity_check(filename): - ''' - Include the contents of a file in the github-actions log - ''' - print("{}Contents of {}{}".format(ANSI_BLUE, filename, ANSI_RESET)) - cat(filename) - print("{}End of {}{}".format(ANSI_BLUE, filename, ANSI_RESET)) +# Comment out SUPPORT from motor's RELEASE file +motor_release = "configure/RELEASE" +update_release_file(motor_release, "SUPPORT", '#') +print("{}Updated {}{}".format(ANSI_BLUE, motor_release, ANSI_RESET)) +grep_release_file(motor_release, "#SUPPORT") -if 'HOME' in os.environ: - # Linux & OS X - cache_dir = os.path.join(os.environ['HOME'], ".cache") -else: - # Windows - cache_dir = os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'], ".cache") - -module_dir = os.getenv('GITHUB_WORKSPACE') - -# Copy the github-actions RELEASE.local to the configure dir -filename = "configure/RELEASE.local" -shutil.copy("{}/RELEASE.local".format(cache_dir), filename) - -# Get the variable from the example release file -example = "configure/EXAMPLE_RELEASE.local" -fh = open(example, "r") -lines = fh.readlines() -fh.close() -pObj = re.compile('(MOTOR_[^=]+)') -module_var = None -for line in lines: - mObj = pObj.match(line) - if mObj != None: - module_var = mObj.group() - break - -# Add the path to the driver module to the RELEASE.local file, since it is needed by the example IOC -fh = open(filename, "a") -fh.write("{}={}\n".format(module_var, module_dir)) -fh.close() -sanity_check(filename) - -# Enable the building of example IOCs -filename = "configure/CONFIG_SITE.local" -fh = open(filename, 'w') -fh.write("BUILD_IOCS = YES") -fh.close() -sanity_check(filename) +# Comment out SUPPORT from the driver module RELEASE files +releases = get_submodule_releases() +for release in releases: + updated = update_release_file(release, "SUPPORT", '#') + if updated: + print("{}Updated {}{}".format(ANSI_BLUE, release, ANSI_RESET)) + grep_release_file(release, "#SUPPORT") diff --git a/.ci-local/github-actions/util.py b/.ci-local/github-actions/util.py new file mode 100644 index 00000000..8b2af1e2 --- /dev/null +++ b/.ci-local/github-actions/util.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +from __future__ import print_function + +import logging +import fileinput +import os + +# Setup ANSI Colors +ANSI_RED = "\033[31;1m" +ANSI_GREEN = "\033[32;1m" +ANSI_YELLOW = "\033[33;1m" +ANSI_BLUE = "\033[34;1m" +ANSI_MAGENTA = "\033[35;1m" +ANSI_CYAN = "\033[36;1m" +ANSI_RESET = "\033[0m" +ANSI_CLEAR = "\033[0K" + +logger = logging.getLogger(__name__) + +def create_file(filename): + dirname = os.path.dirname(filename) + print(dirname) + if not os.path.exists(dirname): + os.makedirs(dirname, 0755) + if not os.path.exists(filename): + fh = open(filename, 'w') + fh.close() + else: + logger.debug("{} already exists".format(filename)) + +# Update a definition in a release file that already exists +# Append a defintion to a release file that already exists +# Commented out a definition in a release file (location='#') +def update_release_file(filename, var, location): + updated_line = '{0}={1}'.format(var, location.replace('\\', '/')) + + found = False + logger.debug("Opening '%s' for adding '%s'", filename, updated_line) + for line in fileinput.input(filename, inplace=1): + output_line = line.strip() + if '{0}='.format(var) in line: + logger.debug("Found '%s=' line, replacing", var) + found = True + if location != '#': + output_line = updated_line + else: + if output_line[0] != '#': + output_line = "#{}".format(output_line) + logger.debug("Writing line to '%s': '%s'", filename, output_line) + print(output_line) + fileinput.close() + + if not found and location != '#': + fh = open(release_local, "a") + logger.debug("Adding new definition: '%s'", updated_line) + print(updated_line, file=fh) + fh.close() + + return found + +def grep_release_file(filename, var): + fh = open(filename, 'r') + for line in fh: + if '{0}='.format(var) in line: + print(line) + fh.close() +