Luke 发布的文章

cd /tmp

wget https://github.com/loong64/docker-ce-packaging/raw/refs/heads/main/docker.service
wget https://github.com/loong64/docker-ce-packaging/releases/download/v27.4.1/docker-27.4.1.tgz
wget -O docker-buildx https://github.com/loong64/buildx/releases/download/v0.19.3/buildx-v0.19.3-linux-loongarch64
wget -O docker-compose https://github.com/loong64/compose/releases/download/v2.32.1/docker-compose-linux-loongarch64

mkdir -p /usr/local/libexec/docker/cli-plugins

tar -xf docker-27.4.1.tgz
sudo chown root:root docker/* docker-buildx docker-compose
sudo chmod 755 docker/* docker-buildx docker-compose
sudo mv docker.service /etc/systemd/system
sudo mv docker/* /usr/local/bin/
sudo mv docker-buildx /usr/local/libexec/docker/cli-plugins/
sudo mv docker-compose /usr/local/libexec/docker/cli-plugins/
sudo systemctl enable docker --now
sudo docker version
sudo docker buildx version
sudo docker compose version

删除docker

sudo systemctl stop docker docker.socket

sudo rm -rf /usr/local/libexec/docker/cli-plugins
sudo rm -f /usr/local/bin/docker*
sudo rm -f /usr/local/bin/container*
sudo rm -f /usr/local/bin/ctr
sudo rm -f /usr/local/bin/runc
sudo rm -f /etc/systemd/system/docker.service

背景

开发了一个项目,需要打成pip包。

重要概念

pyproject.toml 就是“告诉 Python:这个项目叫什么、依赖什么、怎么打包、打完后生成什么”的配置文件。

步骤

1、目录结构

myproj/
  mypkg/
    __init__.py

2、准备project.toml

[project]
name = "mypkg"
version = "0.1.0"
description = "My package"
requires-python = ">=3.11"
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

3、准备构建工具。

python3 -m venv .pkgbuild
./.pkgbuild/bin/python -m ensurepip --upgrade
./.pkgbuild/bin/python -m pip install -U pip build hatchling -i https://pypi.tuna.tsinghua.edu.cn/simple

4、在项目根目录构建包
这里的“项目根目录”就是有 pyproject.toml 的目录

./.pkgbuild/bin/python -m build --no-isolation

最简模板

  1. 纯库包模板
    适合:别人 import mypkg,但没有命令行入口。

假设目录是:

myproj/
  pyproject.toml
  README.md
  mypkg/
    __init__.py

模板:

[project]
name = "mypkg"
version = "0.1.0"
description = "My Python package"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]
  1. 带 CLI 的包模板
    适合:安装后能直接运行命令,比如mytool。

假设目录是:

myproj/
  pyproject.toml
  README.md
  mypkg/
    __init__.py
    cli.py

如果 cli.py 里有一个可调用入口,例如:

def main():
    print("hello")

那模板可以写成:

[project]
name = "mypkg"
version = "0.1.0"
description = "My command line tool"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[project.scripts]
mypkg = "mypkg.cli:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

这里最关键的是这句:

[project.scripts]
mypkg = "mypkg.cli:main"

意思是安装后可以直接运行 mypkg 命令,它会调用 mypkg/cli.py 里的 main()。

  1. 带资源文件的包模板
    适合:除了 Python 代码,还要把脚本、配置、jar、模板等文件一起打进包里。这个最接近你现在的。

假设目录是:

myproj/
  pyproject.toml
  README.md
  bridge/
    package.json
  softwares/
    tool.jar
  mypkg/
    __init__.py
    cli.py
    resources/
      config.json
    skills/
      demo/
        SKILL.md
        scripts/
          run.sh
          helper.py

模板:

[project]
name = "mypkg"
version = "0.1.0"
description = "My package with bundled resources"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[project.scripts]
mypkg = "mypkg.cli:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

[tool.hatch.build]
include = [
  "mypkg/**/*.py",
  "mypkg/**/*.md",
  "mypkg/**/*.sh",
  "mypkg/**/*.json",
]

[tool.hatch.build.targets.sdist]
include = [
  "mypkg/",
  "bridge/",
  "README.md",
]

artifacts = [
  "softwares/**",
]

[tool.hatch.build.targets.wheel.force-include]
"bridge" = "mypkg/bridge"
"softwares" = "mypkg/softwares"

这里最值得你记住的是:

include:选进正常资源
sdist.include:源码包里带什么
artifacts:专门处理像 softwares/ 这种可能被 .gitignore 忽略、但仍然要打包的内容
force-include:把包目录外的目录塞进最终 wheel

背景

在VSCode中使用Roo Code时,遇到终端集成问题,Roo Code无法在终端中执行各种操作,也无法获取输出,非常不方便。

解决步骤

1、检查powershell版本。
按下 Win + R,输入 pwsh,如果报找不到文件pswh,则版本不对,需要升级。
2、在powershell命令行中输入以下命令,安装新版本。

winget search Microsoft.PowerShell
winget install --id Microsoft.PowerShell --source winget

3、用pswh方式打开新的powershell,执行以下命令。

New-Item -Path $PROFILE -ItemType File -Force

在创建的文件中写入以下内容:

if ($env:TERM_PROGRAM -eq "vscode") {
  . "$(code --locate-shell-integration-path pwsh)"
}

4、以管理员方式启动powershell,执行以下命令。

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

5、在VSCode中按下Ctrl + Shift + P,输入Preferences: Open User Settings (JSON),填入以下内容。

    "terminal.integrated.defaultProfile.windows": "PowerShell 7",
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ]
        },
        "PowerShell 7": {
            "path": "你的pwsh.exe路径",
            "args": []
        }
    },

6、在VScode终端中输入以下命令:

$PSVersionTable.PSVersion

看是否有正确输出。

参考资料

https://zhuanlan.zhihu.com/p/25724740375

背景

每次AirPods连接MAC都会被音量吓一跳。

步骤

1、安装Hammerspoon。

brew install hammerspoon --cask

2、创建默认配置。

mkdir -p ~/.hammerspoon
touch ~/.hammerspoon/init.lua

3、在init.lua中写入配置

-- 存储上一次默认音频输出设备的名称
local lastOutputDeviceName = hs.audiodevice.defaultOutputDevice():name()

-- 处理设备变更的回调函数
local function handleDeviceChanges()
-- 获取当前默认音频输出设备的名称
local currentOutputDeviceName = hs.audiodevice.defaultOutputDevice():name()
-- 判断设备名称是否发生了变化
if lastOutputDeviceName ~= currentOutputDeviceName then
-- 判断当前设备是否为蓝牙耳机
if string.find(currentOutputDeviceName, "AirPods") then
-- 连接蓝牙耳机时将音量设置为 20
hs.audiodevice.defaultOutputDevice():setVolume(20)
hs.alert.show("音量已设为 20")
-- 判断上一个设备是否为蓝牙耳机
elseif string.find(lastOutputDeviceName, "AirPods") then
-- 断开蓝牙耳机时将音量设置为 0
hs.audiodevice.defaultOutputDevice():setVolume(0)
hs.alert.show("音量已设为 0")
end
end
-- 更新 lastOutputDeviceName 为当前设备名称
lastOutputDeviceName = currentOutputDeviceName
end

-- 为音频设备变动设置回调函数
hs.audiodevice.watcher.setCallback(handleDeviceChanges)
-- 启动音频设备监听器
hs.audiodevice.watcher.start()

4、重新加载配置。
在启动台搜索Hammerspoon,启动应用。然后在标题栏点击他的图标,然后选择Reload Config。

参考资料

https://www.v2ex.com/t/1011644