diff --git a/.github/workflows/automatic_generation.yml b/.github/workflows/automatic_generation.yml index 21aa6ae..e52af37 100644 --- a/.github/workflows/automatic_generation.yml +++ b/.github/workflows/automatic_generation.yml @@ -9,10 +9,16 @@ on: jobs: generate_package: runs-on: "ubuntu-latest" + outputs: + current_ha_version: ${{ steps.current-ha-version.outputs.current-ha-version }} + new_ha_version: ${{ steps.new-ha-version.outputs.new-ha-version }} + need_to_release: ${{ steps.need-to-release.outputs.need-to-release }} steps: - - uses: "actions/checkout@v2" - name: checkout repo content uses: actions/checkout@v2 + - name: store current ha version + id: current-ha-version + run: echo "::set-output name=current-ha-version::$(cat ha_version)" - name: setup python uses: actions/setup-python@v2 with: @@ -23,9 +29,109 @@ jobs: run: | export PYTHONPATH=$PYTHONPATH:$(pwd) python generate_phacc/generate_phacc.py + - name: store new ha version + id: new-ha-version + run: echo "::set-output name=new-ha-version::$(cat ha_version)" + - name: check need to release + id: need-to-release + run: | + if [[ "${{ steps.current-ha-version.outputs.current-ha-version}}" == "${{ steps.new-ha-version.outputs.new-ha-version }}" ]]; then + echo "::set-output name=need-to-release::false" + else + echo "::set-output name=need-to-release::true" + fi + - name: list files + run: ls -a - name: publish artifact uses: actions/upload-artifact@v2 with: name: generated-package - path: ~/pytest_homeassistant_custom_component/ + path: | + ./ + !**/*.pyc + !tmp_dir/ + !.git/ if-no-files-found: error + test: + needs: generate_package + runs-on: "ubuntu-latest" + if: ${{ needs.generate_package.outputs.need_to_release }} + strategy: + matrix: + python-version: [3.8, 3.9] + steps: + - name: checkout repo content + uses: actions/checkout@v2 + - name: download artifact + uses: actions/download-artifact@v2 + with: + name: generated-package + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e . + - name: Test with pytest + run: | + pytest + make_release: + needs: [generate_package, test] + runs-on: "ubuntu-latest" + if: ${{ needs.generate_package.outputs.need_to_release }} + steps: + - uses: actions/checkout@v2 + - name: download artifact + uses: actions/download-artifact@v2 + with: + name: generated-package + - name: need_to_release_print + run: "echo ${{ needs.generate_package.outputs.need_to_release }}" + - id: next_version + uses: zwaldowski/semver-release-action@v2 + with: + dry_run: true + bump: patch + github_token: ${{ secrets.REPO_SCOPED_TOKEN }} + - run: echo "${{ steps.next_version.outputs.version }}" > version + - run: echo "${{ steps.next_version.outputs.version }}" + - run: | + git config user.name 'Matthew Flamm' + git config user.email 'MatthewFlamm@users.noreply.github.com' + git add . + git commit -m "Bump version" + git push + echo "sha=$(git rev-parse HEAD)" >> $GITHUB_ENV + - uses: zwaldowski/semver-release-action@v2 + with: + github_token: ${{ secrets.REPO_SCOPED_TOKEN }} + sha: ${{ steps.git_commit.outputs.sha }} + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.REPO_SCOPED_TOKEN }} + with: + tag_name: ${{ steps.next_version.outputs.version }} + release_name: Release ${{ steps.next_version.outputs.version }} + body: | + Automatic release + draft: false + prerelease: false + - name: Set up Python + uses: actions/setup-python@v1 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* diff --git a/generate_phacc/const.py b/generate_phacc/const.py index a70132c..7cf280d 100644 --- a/generate_phacc/const.py +++ b/generate_phacc/const.py @@ -28,3 +28,5 @@ LICENSE_FILE_HA = "LICENSE.md" LICENSE_FILE_NEW = "LICENSE_HA_CORE.md" + +HA_VERSION_FILE = "ha_version" diff --git a/generate_phacc/generate_phacc.py b/generate_phacc/generate_phacc.py index 4f9e6a1..bb93c34 100755 --- a/generate_phacc/generate_phacc.py +++ b/generate_phacc/generate_phacc.py @@ -8,8 +8,10 @@ import git from ha import prepare_homeassistant -from const import TMP_DIR, PACKAGE_DIR, REQUIREMENTS_FILE, CONST_FILE, REQUIREMENTS_FILE_DEV, LICENSE_FILE_HA, LICENSE_FILE_NEW, path, files, requirements_remove - +from const import ( + TMP_DIR, PACKAGE_DIR, REQUIREMENTS_FILE, CONST_FILE, REQUIREMENTS_FILE_DEV, LICENSE_FILE_HA, + LICENSE_FILE_NEW, path, files, requirements_remove, HA_VERSION_FILE, +) if os.path.isdir(PACKAGE_DIR): shutil.rmtree(PACKAGE_DIR) @@ -18,141 +20,153 @@ ha_version = prepare_homeassistant() -os.mkdir(PACKAGE_DIR) -os.mkdir(os.path.join(PACKAGE_DIR, "test_util")) -shutil.copy2(os.path.join(TMP_DIR, REQUIREMENTS_FILE), REQUIREMENTS_FILE) -shutil.copy2( - os.path.join(TMP_DIR, "homeassistant", CONST_FILE), - os.path.join(PACKAGE_DIR, CONST_FILE), -) -shutil.copy2( - os.path.join(TMP_DIR, "tests", "test_util", "aiohttp.py"), - os.path.join(PACKAGE_DIR, "test_util", "aiohttp.py"), -) -shutil.copy2( - os.path.join(TMP_DIR, "tests", "test_util", "__init__.py"), - os.path.join(PACKAGE_DIR, "test_util", "__init__.py"), -) -shutil.copy2( - os.path.join(TMP_DIR, LICENSE_FILE_HA), - LICENSE_FILE_NEW, -) - -for f in files: - shutil.copy2(os.path.join(TMP_DIR, "tests", f), os.path.join(PACKAGE_DIR, f)) - - filename = os.path.join(PACKAGE_DIR, f) - - with open(filename, "r") as file: - filedata = file.read() - - filedata = filedata.replace("tests.", ".") - - with open(filename, "w") as file: - file.write(filedata) +with open(HA_VERSION_FILE, 'r') as f: + current_version = f.read() +print(f"Current Version: {current_version}") + +def process_files(): + os.mkdir(PACKAGE_DIR) + os.mkdir(os.path.join(PACKAGE_DIR, "test_util")) + shutil.copy2(os.path.join(TMP_DIR, REQUIREMENTS_FILE), REQUIREMENTS_FILE) + shutil.copy2( + os.path.join(TMP_DIR, "homeassistant", CONST_FILE), + os.path.join(PACKAGE_DIR, CONST_FILE), + ) + shutil.copy2( + os.path.join(TMP_DIR, "tests", "test_util", "aiohttp.py"), + os.path.join(PACKAGE_DIR, "test_util", "aiohttp.py"), + ) + shutil.copy2( + os.path.join(TMP_DIR, "tests", "test_util", "__init__.py"), + os.path.join(PACKAGE_DIR, "test_util", "__init__.py"), + ) + shutil.copy2( + os.path.join(TMP_DIR, LICENSE_FILE_HA), + LICENSE_FILE_NEW, + ) + + for f in files: + shutil.copy2(os.path.join(TMP_DIR, "tests", f), os.path.join(PACKAGE_DIR, f)) + + filename = os.path.join(PACKAGE_DIR, f) + + with open(filename, "r") as file: + filedata = file.read() + + filedata = filedata.replace("tests.", ".") + + with open(filename, "w") as file: + file.write(filedata) + + os.rename( + os.path.join(PACKAGE_DIR, "conftest.py"), os.path.join(PACKAGE_DIR, "plugins.py") + ) + + with open(os.path.join(PACKAGE_DIR, CONST_FILE), "r") as original_file: + data = original_file.readlines() + with open(os.path.join(PACKAGE_DIR, CONST_FILE), "w") as new_file: + new_file.write("".join(data[0:10])) -os.rename( - os.path.join(PACKAGE_DIR, "conftest.py"), os.path.join(PACKAGE_DIR, "plugins.py") -) + added_text = "This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.\n" + triple_quote = '"""\n' -with open(os.path.join(PACKAGE_DIR, CONST_FILE), "r") as original_file: - data = original_file.readlines() -with open(os.path.join(PACKAGE_DIR, CONST_FILE), "w") as new_file: - new_file.write("".join(data[0:10])) + for f in pathlib.Path(PACKAGE_DIR).rglob("*.py"): + with open(f, "r") as original_file: + data = original_file.readlines() + old_docstring = data[0][3:][:-4] + new_docstring = f"{triple_quote}{old_docstring}\n\n{added_text}{triple_quote}" + body = "".join(data[1:]) + with open(f, "w") as new_file: + new_file.write("".join([new_docstring, body])) -added_text = "This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.\n" -triple_quote = '"""\n' + added_text = "# This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.\n" -for f in pathlib.Path(PACKAGE_DIR).rglob("*.py"): - with open(f, "r") as original_file: + with open(REQUIREMENTS_FILE, "r") as original_file: data = original_file.readlines() - old_docstring = data[0][3:][:-4] - new_docstring = f"{triple_quote}{old_docstring}\n\n{added_text}{triple_quote}" - body = "".join(data[1:]) - with open(f, "w") as new_file: - new_file.write("".join([new_docstring, body])) -added_text = "# This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.\n" -with open(REQUIREMENTS_FILE, "r") as original_file: - data = original_file.readlines() + def is_test_requirement(requirement): + # if == not in d this is either a comment or unkown package, include + if "==" not in requirement: + return True + regex = re.compile('types-.+') + if re.match(regex, requirement): + return False -def is_test_requirement(requirement): - # if == not in d this is either a comment or unkown package, include - if "==" not in requirement: - return True - - regex = re.compile('types-.+') - if re.match(regex, requirement): - return False + if d.split("==")[0] in requirements_remove: + return False - if d.split("==")[0] in requirements_remove: - return False + return True - return True + new_data = [] + removed_data = [] + for d in data: + if is_test_requirement(d): + new_data.append(d) + else: + removed_data.append(d) + new_data.append(f"homeassistant=={ha_version}\n") + new_data.insert(0, added_text) -new_data = [] -removed_data = [] -for d in data: - if is_test_requirement(d): - new_data.append(d) - else: - removed_data.append(d) -new_data.append(f"homeassistant=={ha_version}\n") -new_data.insert(0, added_text) + def find_sqlalchemy(data): + for d in data: + if "sqlalchemy" in d: + return d + raise ValueError("could not find sqlalchemy") -def find_sqlalchemy(data): - for d in data: - if "sqlalchemy" in d: - return d - raise ValueError("could not find sqlalchemy") + with open(os.path.join(TMP_DIR, "requirements_all.txt"), "r") as f: + data = f.readlines() + sqlalchemy = find_sqlalchemy(data) + if not "\n" == sqlalchemy[-2:]: + sqlalchemy = f"{sqlalchemy}\n" + new_data.append(sqlalchemy) -with open(os.path.join(TMP_DIR, "requirements_all.txt"), "r") as f: - data = f.readlines() -sqlalchemy = find_sqlalchemy(data) -if not "\n" == sqlalchemy[-2:]: - sqlalchemy = f"{sqlalchemy}\n" -new_data.append(sqlalchemy) + removed_data.insert(0, added_text) -removed_data.insert(0, added_text) + with open(REQUIREMENTS_FILE, "w") as new_file: + new_file.writelines(new_data) -with open(REQUIREMENTS_FILE, "w") as new_file: - new_file.writelines(new_data) + with open(REQUIREMENTS_FILE_DEV, "w") as new_file: + new_file.writelines(removed_data) -with open(REQUIREMENTS_FILE_DEV, "w") as new_file: - new_file.writelines(removed_data) + from pytest_homeassistant_custom_component.const import __version__ -from pytest_homeassistant_custom_component.const import __version__ + with open("README.md", "r") as original_file: + data = original_file.readlines() -with open("README.md", "r") as original_file: - data = original_file.readlines() + data[ + 2 + ] = f"![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message={__version__}&labelColor=blue)\n" -data[ - 2 -] = f"![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message={__version__}&labelColor=blue)\n" + with open("README.md", "w") as new_file: + new_file.write("".join(data)) -with open("README.md", "w") as new_file: - new_file.write("".join(data)) + print(f"New Version: {__version__}") -print(f"Version: {__version__}") + # modify load_fixture + with open(os.path.join(PACKAGE_DIR, "common.py"), "r") as original_file: + data = original_file.readlines() -# modify load_fixture -with open(os.path.join(PACKAGE_DIR, "common.py"), "r") as original_file: - data = original_file.readlines() + import_time_lineno = [i for i, line in enumerate(data) if "from time" in line] + assert len(import_time_lineno) == 1 + data.insert(import_time_lineno[0] + 1, "import traceback\n") -import_time_lineno = [i for i, line in enumerate(data) if "from time" in line] -assert len(import_time_lineno) == 1 -data.insert(import_time_lineno[0] + 1, "import traceback\n") + load_fixture_lineno = [i for i, line in enumerate(data) if "load_fixture" in line] + assert len(load_fixture_lineno) == 1 + data.insert(load_fixture_lineno[0] + 2, " start_path = traceback.extract_stack()[-2].filename\n") + data[load_fixture_lineno[0] + 3] = data[load_fixture_lineno[0] + 3].replace("__file__", "start_path") -load_fixture_lineno = [i for i, line in enumerate(data) if "load_fixture" in line] -assert len(load_fixture_lineno) == 1 -data.insert(load_fixture_lineno[0] + 2, " start_path = traceback.extract_stack()[-2].filename\n") -data[load_fixture_lineno[0] + 3] = data[load_fixture_lineno[0] + 3].replace("__file__", "start_path") + with open(os.path.join(PACKAGE_DIR, "common.py"), "w") as new_file: + new_file.writelines(data) -with open(os.path.join(PACKAGE_DIR, "common.py"), "w") as new_file: - new_file.writelines(data) +if ha_version != current_version: + process_files() + with open(HA_VERSION_FILE, 'w') as f: + f.write(ha_version) +else: + print("Already up to date") diff --git a/ha_version b/ha_version new file mode 100644 index 0000000..4bd5f73 --- /dev/null +++ b/ha_version @@ -0,0 +1 @@ +2021.11.0b0 \ No newline at end of file