-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
386 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,75 @@ | ||
import platform | ||
import subprocess | ||
|
||
import yapf_third_party | ||
from PyInstaller import __main__ as pyi | ||
|
||
|
||
def gen_client_py(): | ||
code = """ | ||
import random | ||
import socket | ||
import threading | ||
from dfs_generate.server import app | ||
def get_unused_port(): | ||
while True: | ||
port = random.randint(1024, 65535) # 端口范围一般为1024-65535 | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
try: | ||
sock.bind(("localhost", port)) | ||
sock.close() | ||
return port | ||
except OSError: | ||
pass | ||
import webview | ||
def desktop_client(): | ||
port = get_unused_port() | ||
t = threading.Thread(target=app.run, kwargs={"port": port}) | ||
t.daemon = True | ||
t.start() | ||
webview.create_window("DFS代码生成", f"http://127.0.0.1:{port}") | ||
webview.start() | ||
if __name__ == '__main__': | ||
desktop_client() | ||
""" | ||
with open("dfs_generate/client.py", "w", encoding="utf-8") as f: | ||
f.write(code) | ||
|
||
|
||
gen_client_py() | ||
|
||
|
||
params = [ | ||
"--windowed", | ||
"--onefile", | ||
"--add-data", | ||
"static:static", | ||
"web/dist:web/dist", | ||
"--add-data", | ||
f'{yapf_third_party.__file__.replace("__init__.py", "")}:yapf_third_party', | ||
"--clean", | ||
"--noconfirm", | ||
"--name=client", | ||
"server.py", | ||
"dfs_generate/client.py", | ||
] | ||
|
||
|
||
pyi.run(params) | ||
|
||
|
||
# 如果是macos,则压缩打包后的目录 | ||
if platform.system() == "Darwin": | ||
cmds = ["zip", "-r", "dist/client.zip", "dist/"] | ||
subprocess.call(cmds) | ||
rm_cmds = ["rm", "-rf", "dist/client.app"] | ||
subprocess.call(rm_cmds) | ||
# 删除空目录 | ||
rm_cmds = ["rm", "-rf", "dist/client"] | ||
subprocess.call(rm_cmds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,45 @@ | ||
# 构建桌面端 | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: | ||
- 25-todo | ||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ windows-latest, macos-latest ] | ||
steps: | ||
- name: Checkout代码 | ||
uses: actions/checkout@v3 | ||
- name: 设置Node.js环境 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18.15 | ||
- name: 进入web目录 | ||
run: cd web | ||
- name: 安装依赖 | ||
run: npm i | ||
- name: 打包Node.js应用 | ||
run: npm run build | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python 3.11 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.11" | ||
- name: Install dependencies | ||
run: | | ||
pip install -r requirements-build.txt | ||
- name: Build executable | ||
run: | | ||
python .github/workflows/build.py | ||
- name: Upload artifact | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: Setup | ||
retention-days: 1 | ||
path: ./dist/* |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
from dfs_generate.client import get_unused_port, desktop_client | ||
|
||
|
||
# 由于get_unused_port函数依赖于随机性和系统状态,我们倾向于对socket操作进行mock | ||
@pytest.mark.parametrize("mocked_port", [12345]) | ||
def test_get_unused_port(mocker, mocked_port): | ||
# Mocking the socket operations | ||
mock_socket = mocker.patch("dfs_generate.client.socket.socket") | ||
mock_socket.bind.return_value = None | ||
mock_socket.close.return_value = None | ||
|
||
# To ensure we control the behavior of randint for predictable testing | ||
mock_randint = mocker.patch("dfs_generate.client.random.randint") | ||
mock_randint.return_value = mocked_port | ||
|
||
# Test the function | ||
port = get_unused_port() | ||
assert port == mocked_port | ||
mock_socket.bind.assert_called_once_with(("localhost", mocked_port)) | ||
mock_socket.close.assert_called_once() | ||
|
||
|
||
@pytest.fixture | ||
def mock_app_run(mocker): | ||
"""Fixture to mock app.run method.""" | ||
mock_run = mocker.patch("dfs_generate.server.app.run") | ||
yield mock_run | ||
|
||
|
||
@pytest.fixture | ||
def mock_webview(mocker): | ||
"""Fixture to mock webview functions.""" | ||
mock_create_window = mocker.patch("webview.create_window") | ||
mock_start = mocker.patch("webview.start") | ||
yield mock_create_window, mock_start | ||
|
||
|
||
def test_desktop_client(mock_app_run, mock_webview): | ||
"""Test the desktop_client function.""" | ||
# Since get_unused_port is mocked in test_get_unused_port, we can assume it works. | ||
# Here we focus on verifying app.run and webview interactions. | ||
desktop_client() | ||
|
||
mock_app_run.assert_called_once_with( | ||
port=12345 | ||
) # Assuming 12345 is a typical port used in tests | ||
create_window, start = mock_webview | ||
create_window.assert_called_once_with("DFS代码生成", "http://127.0.0.1:12345") | ||
start.assert_called_once() |
Oops, something went wrong.