From 0e4b47de383f902898c6fdc0adfb168145b0747c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trevor=20Ba=C4=8Da?= Date: Sat, 25 Jan 2025 11:25:51 -0500 Subject: [PATCH] Clean up scr/ directory (#1634) * remove scr/abjad-log * remove scr/fix-tests * remove scr/make-packaging-transcript * remove scr/trash OLD: scr/abjad NEW: scr/abjad.py OLD: scr/prime-parser-tables NEW: scr/prime_parser_tables.py OLD: scr/timer NEW: scr/timer.py OLD: scr/write-parser-syntax-skeleton NEW: scr/write_parser_syntax_skeleton.py --- .github/workflows/main.yml | 2 +- abjad/contextmanagers.py | 2 +- scr/abjad | 32 ---- scr/abjad-log | 4 - scr/fix-tests | 166 ------------------ scr/make-packaging-transcript | 130 -------------- ...e-parser-tables => prime_parser_tables.py} | 0 scr/start_abjad.py | 19 ++ scr/{timer => timer.py} | 5 + scr/trash | 35 ---- ...eleton => write_parser_syntax_skeleton.py} | 6 +- 11 files changed, 30 insertions(+), 371 deletions(-) delete mode 100755 scr/abjad delete mode 100755 scr/abjad-log delete mode 100755 scr/fix-tests delete mode 100755 scr/make-packaging-transcript rename scr/{prime-parser-tables => prime_parser_tables.py} (100%) create mode 100755 scr/start_abjad.py rename scr/{timer => timer.py} (81%) delete mode 100755 scr/trash rename scr/{write-parser-syntax-skeleton => write_parser_syntax_skeleton.py} (86%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 67393dd6bd3..58fe4e2ebf1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -87,7 +87,7 @@ jobs: run: | python -m pip install .[dev] python -c "import abjad; print(abjad.Configuration().configuration_file_path)" - python scr/prime-parser-tables + python scr/prime_parser_tables.py - name: Run checks run: | diff --git a/abjad/contextmanagers.py b/abjad/contextmanagers.py index 6618d62755a..e473b745790 100644 --- a/abjad/contextmanagers.py +++ b/abjad/contextmanagers.py @@ -703,7 +703,7 @@ def __enter__(self) -> "Timer": self._stop_time = None self._start_time = time.time() if self.print_continuously_from_background: - path = configuration.abjad_directory.parent / "scr" / "timer" + path = configuration.abjad_directory.parent / "scr" / "timer.py" interval = str(int(self.print_continuously_from_background)) process = subprocess.Popen([path, interval], shell=False) self._process = process diff --git a/scr/abjad b/scr/abjad deleted file mode 100755 index b6872ba7470..00000000000 --- a/scr/abjad +++ /dev/null @@ -1,32 +0,0 @@ -#! /usr/bin/env python -import pathlib -import sys - -import abjad - -if __name__ == "__main__": - - try: - file_name = sys.argv[1] - except IndexError: - file_name = "" - - namespace = {} - configuration = abjad.Configuration() - path = configuration.abjad_directory / "abjad" / "_version.py" - with open(path) as pointer: - lines = pointer.readlines() - string = "".join(lines) - exec(string, namespace) - version_string = namespace["__version__"] - startup_string = f"Abjad {version_string} (development)" - - commands = " ".join( - [ - "import abjad;", - f"print({startup_string!r});", - ] - ) - - string = rf'''python -i {file_name} -c "{commands}"''' - abjad.io.spawn_subprocess(string) diff --git a/scr/abjad-log b/scr/abjad-log deleted file mode 100755 index 438ad383c01..00000000000 --- a/scr/abjad-log +++ /dev/null @@ -1,4 +0,0 @@ -#! /usr/bin/env python -import abjad - -abjad.io.open_last_log() diff --git a/scr/fix-tests b/scr/fix-tests deleted file mode 100755 index 2c09f4c5d20..00000000000 --- a/scr/fix-tests +++ /dev/null @@ -1,166 +0,0 @@ -#! /usr/bin/env python -import os - -import abjad - - -def fix_test_case_names(): - total_test_modules = 0 - total_test_cases = 0 - total_nonmatching_names = 0 - for directory, subdirectory_names, file_names in os.walk("."): - test_modules = [] - for file_name in file_names: - if file_name.startswith("test_") and file_name.endswith(".py"): - test_modules.append(file_name) - total_test_modules += len(test_modules) - for test_module in test_modules: - result = process_test_module_name_fixes(test_module, directory) - total_test_cases_in_module, total_nonmatching_names_in_module = result - total_test_cases += total_test_cases_in_module - total_nonmatching_names += total_nonmatching_names_in_module - print(f"Total test modules: {total_test_modules}") - print(f"Total test cases: {total_test_cases}") - if total_nonmatching_names == 1: - suffix = "" - else: - suffix = "s" - print(f"Total misnamed test case name{suffix}: {total_nonmatching_names}") - - -def process_test_module_name_fixes(test_module, directory): - total_nonmatching_names_in_current_module = 0 - test_cases_in_current_module = 0 - full_module_name = os.path.join(directory, test_module) - short_module_name = test_module[:-3] - desired_test_case_prefix = short_module_name - new_lines = [] - with open(full_module_name, "r") as file_pointer: - lines = file_pointer.readlines() - for line in lines: - if ( - line.startswith("def test") or line.startswith("#def test") - ) and "init" not in line: - test_cases_in_current_module += 1 - - # TODO: replace with regex - test_case_name = line.strip() - if test_case_name.startswith("#"): - test_case_name = test_case_name[1:] - test_case_name = test_case_name.split("def ")[1] - test_case_name = test_case_name.split("(")[0] - - # TODO: replace with regex - actual_test_case_prefix = line.strip() - if actual_test_case_prefix.startswith("#"): - actual_test_case_prefix = actual_test_case_prefix[1:] - actual_test_case_prefix = actual_test_case_prefix.split("def ")[1] - actual_test_case_prefix = actual_test_case_prefix.split("(")[0] - actual_test_case_prefix = actual_test_case_prefix[:-3] - - if not actual_test_case_prefix == desired_test_case_prefix: - total_nonmatching_names_in_current_module += 1 - print(f"NONMATCHING in {test_module}") - new_line = line.replace( - actual_test_case_prefix, desired_test_case_prefix - ) - stripped_line = line.strip("\n") - stripped_new_line = new_line.strip("\n") - print(f"OLD LINE: {stripped_line}") - print(f"NEW LINE: {stripped_new_line}") - choice = "y" - if choice.lower() == "y": - new_lines.append(new_line) - else: - new_lines.append(line) - print() - else: - new_lines.append(line) - else: - new_lines.append(line) - with open(full_module_name, "w") as file_pointer: - file_pointer.writelines(new_lines) - return ( - test_cases_in_current_module, - total_nonmatching_names_in_current_module, - ) - - -def fix_test_case_numbers(): - total_test_modules = 0 - total_test_cases = 0 - total_misnumbered_cases = 0 - for directory, subdirectory_names, file_names in os.walk("."): - test_modules = [] - for file_name in file_names: - if file_name.startswith("test_") and file_name.endswith(".py"): - test_modules.append(file_name) - total_test_modules += len(test_modules) - for test_module in test_modules: - result = process_test_module_number_fixes(test_module, directory) - test_cases_in_current_module = result[0] - misnumbered_cases_in_current_module = result[1] - total_test_cases += test_cases_in_current_module - total_misnumbered_cases += misnumbered_cases_in_current_module - print(f"Total test modules: {total_test_modules}") - print(f"Total test cases: {total_test_cases}") - if total_misnumbered_cases == 1: - suffix = "" - else: - suffix = "s" - print(f"Total misnumbered test case name{suffix}: {total_misnumbered_cases}") - - -def process_test_module_number_fixes(test_module, directory): - test_cases_in_current_module = 0 - misnumbered_cases_in_current_module = 0 - full_module_name = os.path.join(directory, test_module) - short_module_name = test_module[:-3] - desired_test_case_prefix = short_module_name - new_lines = [] - with open(full_module_name, "r") as file_pointer: - lines = file_pointer.readlines() - current_case_number = 1 - for line in lines: - if ( - line.startswith("def test") or line.startswith("#def test") - ) and "init" not in line: - test_cases_in_current_module += 1 - desired_test_case_number = str(current_case_number).zfill(2) - - # TODO: replace with regex - actual_test_case_number = line.split("(")[0] - actual_test_case_number = actual_test_case_number[-2:] - - if not actual_test_case_number == desired_test_case_number: - misnumbered_cases_in_current_module += 1 - print(f"NONMATCHING in {test_module}") - new_line = line.replace( - actual_test_case_number, desired_test_case_number - ) - stripped_line = line.strip("\n") - stripped_new_line = new_line.strip("\n") - print(f"OLD LINE: {stripped_line}") - print(f"NEW LINE: {stripped_new_line}") - choice = "y" - if choice.lower() == "y": - new_lines.append(new_line) - else: - new_lines.append(line) - print() - else: - new_lines.append(line) - current_case_number += 1 - else: - new_lines.append(line) - with open(full_module_name, "w") as file_pointer: - file_pointer.writelines(new_lines) - return test_cases_in_current_module, misnumbered_cases_in_current_module - - -if __name__ == "__main__": - abjad.io.spawn_subprocess("clear") - fix_test_case_names() - print() - fix_test_case_numbers() - print() diff --git a/scr/make-packaging-transcript b/scr/make-packaging-transcript deleted file mode 100755 index 095cebf26e3..00000000000 --- a/scr/make-packaging-transcript +++ /dev/null @@ -1,130 +0,0 @@ -#! /usr/bin/env python -import datetime -import subprocess - -import abjad - -template = """\ -Abjad {abjad_version} ({build_start_date}) packaging environment - - 1. MD5 hash of Abjad Github repository: {md5_hash} - 2. uname -v: {kernel_version} - 3. git --version: {git_version} - 4. python --version: {python_version} - 5. pip --version: {pip_version} - 6. py.test --version: {pytest_version} - 7. sphinx-build --version: {sphinx_build_version} - 8. lilypond --version: {lilypond_version} - 9. convert --version: {imagemagick_version} - 10. dot -V: {graphviz_version} -""" - - -def get_abjad_version(): - return abjad.__version__ - - -def get_build_start_date(): - return datetime.date.today().isoformat() - - -def get_md5_hash(): - command = 'git log -n 1 --pretty=format:"%H"' - result = subprocess.check_output(command, shell=True).decode("utf-8") - return result.splitlines()[0] - - -def get_kernel_version(): - command = "uname -v" - result = subprocess.check_output(command, shell=True).decode("utf-8") - result = result.splitlines()[0] - return result.partition(":")[0] - - -def get_git_version(): - command = "git --version" - result = subprocess.check_output(command, shell=True).decode("utf-8") - return result.splitlines()[0] - - -def get_python_version(): - command = "python --version" - pipe = subprocess.Popen( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - stdout, stderr = pipe.communicate() - return stdout.decode("utf-8").splitlines()[0] - - -def get_pip_version(): - command = "pip -V" - result = subprocess.check_output(command, shell=True).decode("utf-8") - return result.partition(" from ")[0] - - -def get_pytest_version(): - command = "py.test --version" - pipe = subprocess.Popen( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - _, stderr = pipe.communicate() - return stderr.decode("utf-8").splitlines()[0].partition(",")[0] - - -def get_sphinx_build_version(): - command = "sphinx-build --version" - result = subprocess.check_output(command, shell=True).decode("utf-8") - return result.splitlines()[0] - - -def get_lilypond_version(): - command = "lilypond --version" - result = subprocess.check_output(command, shell=True).decode("utf-8") - return result.splitlines()[0] - - -def get_imagemagick_version(): - command = "convert --version" - result = subprocess.check_output(command, shell=True).decode("utf-8") - result = result.splitlines()[0] - return result.partition("Version: ")[-1] - - -def get_graphviz_version(): - command = "dot -V" - pipe = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE) - _, stderr = pipe.communicate() - result = stderr.decode("utf-8").splitlines()[0] - return result.partition("dot - ")[-1] - - -abjad_version = get_abjad_version() -build_start_date = get_build_start_date() -md5_hash = get_md5_hash() -kernel_version = get_kernel_version() -git_version = get_git_version() -python_version = get_python_version() -pip_version = get_pip_version() -pytest_version = get_pytest_version() -sphinx_build_version = get_sphinx_build_version() -lilypond_version = get_lilypond_version() -imagemagick_version = get_imagemagick_version() -graphviz_version = get_graphviz_version() - - -string = template.format( - abjad_version=abjad_version, - build_start_date=build_start_date, - md5_hash=md5_hash, - kernel_version=kernel_version, - git_version=git_version, - python_version=python_version, - pip_version=pip_version, - pytest_version=pytest_version, - sphinx_build_version=sphinx_build_version, - lilypond_version=lilypond_version, - imagemagick_version=imagemagick_version, - graphviz_version=graphviz_version, -) - -print(string) diff --git a/scr/prime-parser-tables b/scr/prime_parser_tables.py similarity index 100% rename from scr/prime-parser-tables rename to scr/prime_parser_tables.py diff --git a/scr/start_abjad.py b/scr/start_abjad.py new file mode 100755 index 00000000000..9ee4367d078 --- /dev/null +++ b/scr/start_abjad.py @@ -0,0 +1,19 @@ +#! /usr/bin/env python +import sys + +import abjad + + +def main(): + try: + file_name = sys.argv[1] + except IndexError: + file_name = "" + startup_string = f"Abjad {abjad.__version__}" + commands = f"import abjad; print({startup_string!r});" + string = rf'''python -i {file_name} -c "{commands}"''' + abjad.io.spawn_subprocess(string) + + +if __name__ == "__main__": + main() diff --git a/scr/timer b/scr/timer.py similarity index 81% rename from scr/timer rename to scr/timer.py index 678f4980f12..9901b324983 100755 --- a/scr/timer +++ b/scr/timer.py @@ -1,4 +1,9 @@ #! /usr/bin/env python + +""" +Called by abjad.contextmanager.Timer on __enter__(). +""" + import sys import time diff --git a/scr/trash b/scr/trash deleted file mode 100755 index e7d9d12280a..00000000000 --- a/scr/trash +++ /dev/null @@ -1,35 +0,0 @@ -# Name: trash -# Source: internet -# Description: move $1 to OS X trash -# Usage: safe alternative to Unix rm -# Availability: Unix, OS X - - -while [ -n "$1" ]; do - if [ ! -e "$1" ]; then - echo "'$1' not found; exiting" - return - fi - - file=`basename -- "$1"` - - # Chop trailing '/' if there - file=${file%/} - - destination='' - - if [ -e "$HOME/.Trash/$file" ]; then - # Extract file and extension - ext=`expr "$file" : ".*\(\.[^\.]*\)$"` - base=${file%$ext} - - # Add a space between base and timestamp - test -n "$base" && base="$base " - - destination="/$base`date +%H-%M-%S`_$RANDOM$ext" - fi - - echo "Trashing $1 ..." - \mv -i -- "$1" "$HOME/.Trash$destination" - shift -done diff --git a/scr/write-parser-syntax-skeleton b/scr/write_parser_syntax_skeleton.py similarity index 86% rename from scr/write-parser-syntax-skeleton rename to scr/write_parser_syntax_skeleton.py index 9cfb0650165..23634758ace 100755 --- a/scr/write-parser-syntax-skeleton +++ b/scr/write_parser_syntax_skeleton.py @@ -11,7 +11,7 @@ def usage(): result.append("Usage:") result.append("") result.append( - "write-parser-syntax-skeleton PARSER_output_PATH PARSER_TAB_HH_PATH SKELETON_PATH" + "write_parser_syntax_skeleton.py PARSER_output_PATH PARSER_TAB_HH_PATH SKELETON_PATH" ) result.append("") result.append( @@ -29,7 +29,9 @@ def write(parser_output_path, parser_tab_hh_path, skeleton_path): assert os.path.exists(parser_output_path) assert os.path.exists(parser_tab_hh_path) LilyPondGrammarGenerator()._write_parser_syntax_skeleton( - skeleton_path, parser_output_path, parser_tab_hh_path, + skeleton_path, + parser_output_path, + parser_tab_hh_path, )