From 56872fd5756d6de006ab738b3884a8324d64e91c Mon Sep 17 00:00:00 2001 From: infeeeee Date: Thu, 23 Mar 2023 16:12:16 +0100 Subject: [PATCH 1/3] Windows test fixes --- dyn2py/files.py | 21 ++++++++++----------- tests/test_PythonFile.py | 8 ++++---- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/dyn2py/files.py b/dyn2py/files.py index 8e107a0..6c7cfd5 100644 --- a/dyn2py/files.py +++ b/dyn2py/files.py @@ -2,7 +2,6 @@ from __future__ import annotations import simplejson as json import hashlib import pathlib -import textwrap import logging import os from datetime import datetime @@ -399,10 +398,10 @@ class PythonFile(File): # Do not read from disk: super().__init__(filepath, read_from_disk=False) - header_notice = """\ - This file was generated with dyn2py from a Dynamo graph. - Do not edit this section, if you want to update the Dynamo graph!\ - """ + header_notice = os.linesep.join([ + "This file was generated with dyn2py from a Dynamo graph.", + "Do not edit this section, if you want to update the Dynamo graph!" + ]) # Double escape path: dyn_path_string = str(dynamo_file.realpath) @@ -420,14 +419,14 @@ class PythonFile(File): "py_engine": python_node.engine } - header_string = "\r\n".join( + header_string = os.linesep.join( [f"{k}:{self.header_data[k]}" for k in self.header_data]) header_wrapper = '"""' - self.text = "\r\n".join([ + self.text = os.linesep.join([ header_wrapper, HEADER_SEPARATOR, - textwrap.dedent(header_notice), + header_notice, HEADER_SEPARATOR, header_string, HEADER_SEPARATOR, @@ -462,7 +461,8 @@ class PythonFile(File): logging.info(f"Reading file: {self.filepath}") with open(self.filepath, mode="r", newline="", encoding="utf-8") as input_py: - python_lines = input_py.readlines() + python_lines = [line.strip("\r\n") + for line in input_py.readlines()] self.text = os.linesep.join(python_lines) self.header_data = {} @@ -470,7 +470,6 @@ class PythonFile(File): code_start_line = 0 for i, line in enumerate(python_lines): - line = line.strip() logging.debug(f"Reading line: {line}") # Skip the first lines: @@ -490,7 +489,7 @@ class PythonFile(File): raise PythonFileException("Error reading header!") self.header_data[line[0:sl]] = line[sl+1:] - self.code = "".join(python_lines[code_start_line:]) + self.code = os.linesep.join(python_lines[code_start_line:]) self.open_files.add(self) logging.debug(f"Header data from python file: {self.header_data}") diff --git a/tests/test_PythonFile.py b/tests/test_PythonFile.py index e1ff28e..4cd131b 100644 --- a/tests/test_PythonFile.py +++ b/tests/test_PythonFile.py @@ -2,6 +2,7 @@ import unittest import dyn2py import shutil from time import sleep +import os from tests.support import * @@ -11,8 +12,7 @@ class TestPythonFile(unittest.TestCase): def test_init(self): extract_single_node_dyn() - py1 = dyn2py.PythonFile( - f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py") + py1 = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py") dyn2py.DynamoFile.open_files.clear() dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn") @@ -22,8 +22,8 @@ class TestPythonFile(unittest.TestCase): for py in [py1, py2]: - self.assertEqual(len(py.code.split("\n")), 17) - self.assertEqual(len(py.text.split("\r\n")), 31) + self.assertEqual(len(py.code.split(os.linesep)), 17) + self.assertEqual(len(py.text.split(os.linesep)), 32, msg=py.filepath) self.assertIs(type(py.header_data), dict) self.assertTrue(py in dyn2py.PythonFile.open_files) From d2d27832f91a5fa0c899653d8017c41e4572b471 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Fri, 24 Mar 2023 16:55:00 +0100 Subject: [PATCH 2/3] Platform independent tests --- dyn2py/files.py | 21 ++++++++++++--------- tests/test_DynamoFile.py | 2 +- tests/test_PythonFile.py | 2 +- tests/test_PythonNode.py | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/dyn2py/files.py b/dyn2py/files.py index 6c7cfd5..29ef868 100644 --- a/dyn2py/files.py +++ b/dyn2py/files.py @@ -316,8 +316,8 @@ class DynamoFile(File): self.python_nodes.remove(python_node_in_file) self.python_nodes.add(python_node) - # Update the dict: - node_dict["Code"] = python_node.code + # Update the dict, Dyn files are always CRLF: + node_dict["Code"] = "\r\n".join(python_node.code) self.modified = True @@ -370,8 +370,8 @@ class DynamoFile(File): class PythonFile(File): """A Python file, subclass of File()""" - code: str - """The python code as a string.""" + code: list[str] + """The python code.""" header_data: dict """Parsed dict from the header of a python file.""" text: str @@ -431,7 +431,7 @@ class PythonFile(File): header_string, HEADER_SEPARATOR, header_wrapper, - python_node.code + os.linesep.join(python_node.code) ]) self.code = python_node.code @@ -489,7 +489,7 @@ class PythonFile(File): raise PythonFileException("Error reading header!") self.header_data[line[0:sl]] = line[sl+1:] - self.code = os.linesep.join(python_lines[code_start_line:]) + self.code = python_lines[code_start_line:] self.open_files.add(self) logging.debug(f"Header data from python file: {self.header_data}") @@ -563,7 +563,7 @@ class PythonNode(): """The id of the node""" engine: str """The engine of the node, IronPython2 or CPython3""" - code: str + code: list[str] """The full code""" checksum: str """The checksum of the code, for checking changes""" @@ -599,7 +599,8 @@ class PythonNode(): else: self.engine = "IronPython2" - self.code = node_dict_from_dyn["Code"] + # It's read from a dynamo file, so separator is always CRLF + self.code = node_dict_from_dyn["Code"].split("\r\n") # Get the name of the node: self.name = next( @@ -618,6 +619,7 @@ class PythonNode(): "_".join(filename_parts) + ".py") self.filepath = dynamo_file.dirpath.joinpath(self.filename) + # Initialize from a python file: elif python_file and not node_dict_from_dyn and not dynamo_file: self.id = python_file.header_data["py_id"] self.engine = python_file.header_data["py_engine"] @@ -629,4 +631,5 @@ class PythonNode(): raise PythonNodeException # Calculate checksum: - self.checksum = hashlib.md5(self.code.encode()).hexdigest() + checksums = [hashlib.md5(l.encode()).hexdigest() for l in self.code] + self.checksum = hashlib.md5("".join(checksums).encode()).hexdigest() diff --git a/tests/test_DynamoFile.py b/tests/test_DynamoFile.py index 495948d..0088beb 100644 --- a/tests/test_DynamoFile.py +++ b/tests/test_DynamoFile.py @@ -37,7 +37,7 @@ class TestDynamoFile(unittest.TestCase): self.assertEqual(len(dyn.python_nodes), 6) self.assertTrue(py_node) self.assertIn(py_node, dyn.python_nodes) - self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26") + self.assertEqual(py_node.checksum, "e830a6ae6b395bcfd4e5a40da48f3bfc") with self.assertRaises(dyn2py.PythonNodeNotFoundException): dyn.get_python_node_by_id("wrongid") diff --git a/tests/test_PythonFile.py b/tests/test_PythonFile.py index 4cd131b..89ece01 100644 --- a/tests/test_PythonFile.py +++ b/tests/test_PythonFile.py @@ -22,7 +22,7 @@ class TestPythonFile(unittest.TestCase): for py in [py1, py2]: - self.assertEqual(len(py.code.split(os.linesep)), 17) + self.assertEqual(len(py.code), 17) self.assertEqual(len(py.text.split(os.linesep)), 32, msg=py.filepath) self.assertIs(type(py.header_data), dict) self.assertTrue(py in dyn2py.PythonFile.open_files) diff --git a/tests/test_PythonNode.py b/tests/test_PythonNode.py index d6a40ba..7909d99 100644 --- a/tests/test_PythonNode.py +++ b/tests/test_PythonNode.py @@ -21,7 +21,7 @@ class TestPythonNode(unittest.TestCase): self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.engine, "CPython3") - self.assertEqual(node.checksum, "ec2c85a11ddbf8375da03f11272d427a") + self.assertEqual(node.checksum, "92d46019bf538072db6bd1287267c147") self.assertEqual(node.name, "Python Script") self.assertEqual( node.filename, "single_node_1c5d99792882409e97e132b3e9f814b0.py") @@ -36,7 +36,7 @@ class TestPythonNode(unittest.TestCase): self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.engine, "CPython3") - self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e") + self.assertEqual(node.checksum, "64a73c52bd213b6f1b593697cdde789b") def test_init_exception(self): From 92f7823576729cb751658b29a2f8476de8b287f8 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Fri, 24 Mar 2023 17:17:12 +0100 Subject: [PATCH 3/3] Update workflows --- .github/workflows/unittests.yml | 3 ++- .github/workflows/website.yml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index b12afb3..c518fd1 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -11,10 +11,11 @@ permissions: jobs: tests: - runs-on: ubuntu-latest strategy: matrix: + os: [ubuntu-latest, windows-latest] python-version: [3.8, 3.9, "3.10", 3.11] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index afd0d95..71d06d0 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -1,4 +1,4 @@ -name: website +name: Deploy website # build the documentation whenever there are new commits on main on: