This commit is contained in:
parent
a5eb9e6bbe
commit
48b729b3ba
5 changed files with 76 additions and 161 deletions
|
@ -1,12 +1,75 @@
|
||||||
|
"""
|
||||||
|
Copyright (c) 2023 zhbaor <zhbaor@zhaozuohong.vip>
|
||||||
|
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
This file incorporates work covered by the following copyright and
|
||||||
|
permission notice:
|
||||||
|
|
||||||
|
Copyright (c) 2021 Nano <nanoapezlk@gmail.com>
|
||||||
|
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
|
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()
|
__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]}"
|
||||||
|
|
|
@ -2,7 +2,7 @@ import subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from mower import __system__
|
from mower import __creation_flags__, __system__
|
||||||
from mower.utils import config
|
from mower.utils import config
|
||||||
from mower.utils.csleep import MowerExit, csleep
|
from mower.utils.csleep import MowerExit, csleep
|
||||||
from mower.utils.log import logger
|
from mower.utils.log import logger
|
||||||
|
@ -108,7 +108,7 @@ def exec_cmd(cmd, folder_path, wait_time):
|
||||||
cmd,
|
cmd,
|
||||||
shell=True,
|
shell=True,
|
||||||
cwd=folder_path,
|
cwd=folder_path,
|
||||||
creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0,
|
creationflags=__creation_flags__,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
universal_newlines=True,
|
universal_newlines=True,
|
||||||
|
|
|
@ -8,7 +8,7 @@ import numpy as np
|
||||||
from adbutils import AdbClient, AdbDevice
|
from adbutils import AdbClient, AdbDevice
|
||||||
from adbutils.errors import AdbConnectionError, AdbError
|
from adbutils.errors import AdbConnectionError, AdbError
|
||||||
|
|
||||||
from mower import __system__
|
from mower import __creation_flags__, __system__
|
||||||
from mower.utils import config
|
from mower.utils import config
|
||||||
from mower.utils.csleep import MowerExit, csleep
|
from mower.utils.csleep import MowerExit, csleep
|
||||||
from mower.utils.device.emulator import restart_emulator
|
from mower.utils.device.emulator import restart_emulator
|
||||||
|
@ -189,7 +189,7 @@ class ADB:
|
||||||
cmd,
|
cmd,
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=stderr,
|
stderr=stderr,
|
||||||
creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0,
|
creationflags=__creation_flags__,
|
||||||
)
|
)
|
||||||
|
|
||||||
def capture_display(self) -> np.ndarray:
|
def capture_display(self) -> np.ndarray:
|
||||||
|
@ -331,9 +331,7 @@ class DIY(ADB):
|
||||||
data = subprocess.check_output(
|
data = subprocess.check_output(
|
||||||
command,
|
command,
|
||||||
shell=True,
|
shell=True,
|
||||||
creationflags=subprocess.CREATE_NO_WINDOW
|
creationflags=__creation_flags__,
|
||||||
if __system__ == "windows"
|
|
||||||
else 0,
|
|
||||||
)
|
)
|
||||||
return bytes2img(data)
|
return bytes2img(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from mower import __system__
|
from mower import __creation_flags__
|
||||||
from mower.utils.log import logger
|
from mower.utils.log import logger
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ def subprocess_run(cmd, timeout=10):
|
||||||
cmd,
|
cmd,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
shell=False,
|
shell=False,
|
||||||
creationflags=subprocess.CREATE_NO_WINDOW if __system__ == "windows" else 0,
|
creationflags=__creation_flags__,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
stdout, stderr = process.communicate(timeout=timeout)
|
stdout, stderr = process.communicate(timeout=timeout)
|
||||||
|
|
|
@ -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()
|
|
Loading…
Add table
Add a link
Reference in a new issue