From 48b729b3ba6fdcc263b6f4e9007f5c5ff200cc73 Mon Sep 17 00:00:00 2001 From: zhbaor Date: Sun, 5 Jan 2025 09:41:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=A7=BB=E9=99=A4git=5Frev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mower/__init__.py | 75 ++++++++++- mower/utils/device/emulator/__init__.py | 4 +- mower/utils/device/method/adb/__init__.py | 8 +- mower/utils/device/method/utils.py | 4 +- mower/utils/git_rev.py | 146 ---------------------- 5 files changed, 76 insertions(+), 161 deletions(-) delete mode 100644 mower/utils/git_rev.py diff --git a/mower/__init__.py b/mower/__init__.py index 52a2d1e7..15f588c2 100644 --- a/mower/__init__.py +++ b/mower/__init__.py @@ -1,12 +1,75 @@ +""" +Copyright (c) 2023 zhbaor + +This file is part of mower-ng (https://git.zhaozuohong.vip/mower-ng/mower-ng). + +Mower-ng is free software: you may copy, redistribute and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation, version 3 or later. + +This file is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +This file incorporates work covered by the following copyright and +permission notice: + + Copyright (c) 2021 Nano + Copyright (c) 2022 YuiTH <396698132@qq.com> + + 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 platform -from pathlib import Path +import subprocess -__rootdir__ = Path(__file__).parent.resolve() +from mower.utils.path import get_path -from mower.utils.git_rev import revision_info +__rootdir__ = get_path("@install") / "mower" -branch, commit = revision_info() - -__version__ = f"{branch}+{commit[:10]}" __system__ = platform.system().lower() + +__creation_flags__ = 0 +if __system__ == "windows": + __creation_flags__ = subprocess.CREATE_NO_WINDOW + +git_path = get_path("@install").parent / "git" / "bin" / "git.exe" +if not git_path.exists(): + git_path = "git" + +branch = subprocess.run( + [str(git_path), "rev-parse", "--abbrev-ref", "HEAD"], + capture_output=True, + text=True, + creationflags=__creation_flags__, +).stdout.strip() + +commit = subprocess.run( + [str(git_path), "rev-parse", "HEAD"], + capture_output=True, + text=True, + creationflags=__creation_flags__, +).stdout.strip() + +__version__ = f"{branch}+{commit[:10]}" diff --git a/mower/utils/device/emulator/__init__.py b/mower/utils/device/emulator/__init__.py index e2123cc7..56e081cc 100644 --- a/mower/utils/device/emulator/__init__.py +++ b/mower/utils/device/emulator/__init__.py @@ -2,7 +2,7 @@ import subprocess from datetime import datetime from enum import Enum -from mower import __system__ +from mower import __creation_flags__, __system__ from mower.utils import config from mower.utils.csleep import MowerExit, csleep from mower.utils.log import logger @@ -108,7 +108,7 @@ def exec_cmd(cmd, folder_path, wait_time): cmd, shell=True, cwd=folder_path, - creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0, + creationflags=__creation_flags__, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, diff --git a/mower/utils/device/method/adb/__init__.py b/mower/utils/device/method/adb/__init__.py index c89e046e..61455f53 100644 --- a/mower/utils/device/method/adb/__init__.py +++ b/mower/utils/device/method/adb/__init__.py @@ -8,7 +8,7 @@ import numpy as np from adbutils import AdbClient, AdbDevice from adbutils.errors import AdbConnectionError, AdbError -from mower import __system__ +from mower import __creation_flags__, __system__ from mower.utils import config from mower.utils.csleep import MowerExit, csleep from mower.utils.device.emulator import restart_emulator @@ -189,7 +189,7 @@ class ADB: cmd, stdout=subprocess.DEVNULL, stderr=stderr, - creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0, + creationflags=__creation_flags__, ) def capture_display(self) -> np.ndarray: @@ -331,9 +331,7 @@ class DIY(ADB): data = subprocess.check_output( command, shell=True, - creationflags=subprocess.CREATE_NO_WINDOW - if __system__ == "windows" - else 0, + creationflags=__creation_flags__, ) return bytes2img(data) except Exception as e: diff --git a/mower/utils/device/method/utils.py b/mower/utils/device/method/utils.py index 000ef1bf..3a05effc 100644 --- a/mower/utils/device/method/utils.py +++ b/mower/utils/device/method/utils.py @@ -1,6 +1,6 @@ import subprocess -from mower import __system__ +from mower import __creation_flags__ from mower.utils.log import logger @@ -18,7 +18,7 @@ def subprocess_run(cmd, timeout=10): cmd, stdout=subprocess.PIPE, shell=False, - creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0, + creationflags=__creation_flags__, ) try: stdout, stderr = process.communicate(timeout=timeout) diff --git a/mower/utils/git_rev.py b/mower/utils/git_rev.py deleted file mode 100644 index a7fcb1c7..00000000 --- a/mower/utils/git_rev.py +++ /dev/null @@ -1,146 +0,0 @@ -""" -https://gist.github.com/pkienzle/5e13ec07077d32985fa48ebe43486832 - -Get commit id from the git repo. - -Drop the file rev.py into directory PACKAGE_PATH of your application. From -within that package you can then do:: - - from . import rev - - rev.print_revision() # print the repo version - commit = rev.revision_info() # return commit id - -On "pip install" the repo root directory will not be available. In this -case the code looks for PACKAGE_NAME/git_revision, which you need to -install into site-pacakges along with your other sources. - -The simplest way to create PACKAGE_NAME/git_revision is to run rev -from setup.py:: - - import sys - import os - - # Create the resource file git_revision. - if os.system(f'"{sys.executable}" PACKAGE_NAME/rev.py') != 0: - print("setup.py failed to build PACKAGE_NAME/git_revision", file=sys.stderr) - sys.exit(1) - - ... - - # Include git revision in the package data, eitherj by adding - # "include PACKAGE_NAME/git_revision" to MANIFEST.in, or by - # adding the following to setup.py: - #package_data = {"PACKAGE_NAME": ["git_revision"]} - setup( - ... - #package_data=package_data, - include_package_data=True, - ... - ) - -Add the following to .gitignore, substituting your package name:: - - /PACKAGE_NAME/git_revision - -""" - -from pathlib import Path -from warnings import warn - - -def repo_path(): - """Return the root of the project git repo or None if not in a repo.""" - base = Path(__file__).absolute() - for path in base.parents: - if (path / ".git").exists(): - return path - return None - - -def print_revision(): - """Print the git revision""" - revision = revision_info() - print("git revision", revision) - - -def store_revision(): - """ - Call from setup.py to save the git revision to the distribution. - - See :mod:`rev` for details. - """ - commit = git_rev(repo_path()) - path = Path(__file__).absolute().parent / RESOURCE_NAME - with path.open("w") as fd: - fd.write(commit + "\n") - - -RESOURCE_NAME = "git_revision" -_REVISION_INFO = None # cached value of git revision - - -def revision_info(): - """ - Get the git hash and mtime of the repository, or the installed files. - """ - # TODO: test with "pip install -e ." for developer mode - global _REVISION_INFO - - if _REVISION_INFO is None: - _REVISION_INFO = git_rev(repo_path()) - - return _REVISION_INFO - - -def git_rev(repo): - """ - Get the git revision for the repo in the path *repo*. - - Returns the commit id of the current head. - - Note: this function parses the files in the git repository directory - without using the git application. It may break if the structure of - the git repository changes. It only reads files, so it should not do - any damage to the repository in the process. - """ - # Based on stackoverflow am9417 - # https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script/59950703#59950703 - if repo is None: - return None - - git_root = Path(repo) / ".git" - git_head = git_root / "HEAD" - - # Read .git/HEAD file - with git_head.open("r") as fd: - head_ref = fd.read() - - # Find head file .git/HEAD (e.g. ref: ref/heads/master => .git/ref/heads/master) - if not head_ref.startswith("ref: "): - return "detached", head_ref - head_ref = head_ref[5:].strip() - - # Read commit id from head file - head_path = git_root.joinpath(*head_ref.split("/")) - if not head_path.exists(): - warn(f"path {head_path} referenced from {git_head} does not exist") - return None - - with head_path.open("r") as fd: - commit = fd.read().strip() - - branch_name = head_ref.split("/")[-1] - return branch_name, commit - - -def main(): - """ - When run as a python script create git_revision in the current directory. - """ - print_revision() - store_revision() - - -if __name__ == "__main__": - main() From 47a12b983942818b54b8cb576d3fd345d658b139 Mon Sep 17 00:00:00 2001 From: zhbaor Date: Sun, 5 Jan 2025 09:46:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=84=E7=90=86=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=A4=B1=E8=B4=A5=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mower/__init__.py | 28 +++++++++++++---------- mower/utils/device/method/adb/__init__.py | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mower/__init__.py b/mower/__init__.py index 15f588c2..dda7843b 100644 --- a/mower/__init__.py +++ b/mower/__init__.py @@ -58,18 +58,22 @@ git_path = get_path("@install").parent / "git" / "bin" / "git.exe" if not git_path.exists(): git_path = "git" -branch = subprocess.run( - [str(git_path), "rev-parse", "--abbrev-ref", "HEAD"], - capture_output=True, - text=True, - creationflags=__creation_flags__, -).stdout.strip() +try: + branch = subprocess.run( + [str(git_path), "rev-parse", "--abbrev-ref", "HEAD"], + capture_output=True, + text=True, + creationflags=__creation_flags__, + ).stdout.strip() -commit = subprocess.run( - [str(git_path), "rev-parse", "HEAD"], - capture_output=True, - text=True, - creationflags=__creation_flags__, -).stdout.strip() + commit = subprocess.run( + [str(git_path), "rev-parse", "HEAD"], + capture_output=True, + text=True, + creationflags=__creation_flags__, + ).stdout.strip() +except Exception: + branch = "unknown" + commit = "unknown" __version__ = f"{branch}+{commit[:10]}" diff --git a/mower/utils/device/method/adb/__init__.py b/mower/utils/device/method/adb/__init__.py index 61455f53..65f5c3eb 100644 --- a/mower/utils/device/method/adb/__init__.py +++ b/mower/utils/device/method/adb/__init__.py @@ -8,7 +8,7 @@ import numpy as np from adbutils import AdbClient, AdbDevice from adbutils.errors import AdbConnectionError, AdbError -from mower import __creation_flags__, __system__ +from mower import __creation_flags__ from mower.utils import config from mower.utils.csleep import MowerExit, csleep from mower.utils.device.emulator import restart_emulator