f4e1ecc5f1
- artifact_fetch.py: 新增 dingtalk dws 取件器(adoc→doc export、axls→sheet 转换、其余→drive 直下),
ARTIFACT_DINGTALK_FETCHER=dws|auto|legacy 三级开关,auto 仅对可重试错误回落老路径;
平台侧判定(pick_suffix/SLIDES/magic/大小上限/sha256/spool/空壳检查)全部保留;
结果 JSON 字段不变(Go runner 用 DisallowUnknownFields 解析 stdout),取件器来源只写 stderr。
- axls_to_xlsx.py: 纯标准库 axls→xlsx(文本写入 sharedStrings 以通过空壳判定)。
- dws_error_map.py: dws 错误→平台 {ok,code,retryable,missing};未分类错误默认不重试。
- Dockerfile: alpine 增加 gcompat/libc6-compat;dws v1.0.63 固定版本+sha256 校验;构建期 dws version 自检。
- compose.yaml: worker 增加 dws 环境与凭据挂载(DWS_HOME 方案,XDG_DATA_HOME 不被 dws 识别)。
- 测试: artifact_fetch_test 新增 11 项(路由/回落语义/契约字段锁定);dws_error_map 17 断言。
- 实测: 真容器(--read-only --tmpfs --user 1000:1000) 端到端 adoc/axls/pdf 3/3 成功,docx sha 与平台基准一致。
189 lines
8.5 KiB
Python
189 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
||
"""dws axls → xlsx 转换器(纯标准库,无 openpyxl)。
|
||
|
||
用途:dws 没有 axls 直出接口,只能 sheet range read 拿单元格;本脚本把单元格写成
|
||
合法的 .xlsx,供平台 artifact_fetch 的 dingtalk dws 取件器落盘入库。
|
||
|
||
兼容平台既有检查:
|
||
- `exported_doc_is_blank()` 对 spreadsheet 只读 `xl/sharedStrings.xml` → 本脚本**把文本写进 sharedStrings**,
|
||
否则会被判定为"空壳文档"而拒收。
|
||
- 必须 PK magic、能被子解析器识别为 xlsx。
|
||
|
||
归一化约定(与平台既有 xlsx 产物对齐):
|
||
- 单元格换行 CRLF/CR → LF
|
||
- 日期保持 dws 返回的显示文本(平台旧产物是 Excel 序列号,转换后语义一致)
|
||
|
||
dws 可执行文件解析顺序:$DWS_BIN → PATH 里的 dws。
|
||
"""
|
||
import argparse, json, os, re, shutil, subprocess, sys, zipfile
|
||
from xml.sax.saxutils import escape
|
||
|
||
NUM_RE = re.compile(r"^-?\d+(\.\d+)?$")
|
||
|
||
|
||
def dws_binary():
|
||
return os.environ.get("DWS_BIN") or shutil.which("dws") or "/usr/local/bin/dws"
|
||
|
||
|
||
def run_json(args, timeout=180):
|
||
# dws 的文件型 keychain 在 $HOME/.local/share/dws-cli,容器里用 DWS_HOME 单独指定
|
||
env = dict(os.environ)
|
||
dws_home = os.environ.get("DWS_HOME")
|
||
if dws_home:
|
||
env["HOME"] = dws_home
|
||
p = subprocess.run([dws_binary(), *args, "-f", "json"], capture_output=True, text=True,
|
||
timeout=timeout, env=env)
|
||
try:
|
||
return json.loads(p.stdout)
|
||
except Exception:
|
||
raise SystemExit(f"dws 调用失败: {' '.join(args)} :: {(p.stdout or p.stderr)[:300]}")
|
||
|
||
|
||
def fetch_sheets(node):
|
||
"""返回 [{'name':str,'cells':[[...]]}]"""
|
||
listing = run_json(["sheet", "list", "--node", node])
|
||
sheets = listing.get("sheets") or []
|
||
if not sheets:
|
||
raise SystemExit(f"sheet list 无工作表: {json.dumps(listing, ensure_ascii=False)[:200]}")
|
||
out = []
|
||
for s in sheets:
|
||
sid = s.get("sheetId") or s.get("name")
|
||
r = run_json(["sheet", "range", "read", "--node", node, "--sheet-id", sid])
|
||
out.append({"name": s.get("name") or sid, "cells": r.get("cells") or []})
|
||
return out
|
||
|
||
|
||
def norm(v):
|
||
if v is None:
|
||
return None
|
||
s = str(v)
|
||
if "\r" in s:
|
||
s = s.replace("\r\n", "\n").replace("\r", "\n")
|
||
return s
|
||
|
||
|
||
def col_letter(idx):
|
||
"""0-based 列号 → A/B/.../AA"""
|
||
s = ""
|
||
idx += 1
|
||
while idx:
|
||
idx, rem = divmod(idx - 1, 26)
|
||
s = chr(65 + rem) + s
|
||
return s
|
||
|
||
|
||
def sheet_name(name, used):
|
||
n = re.sub(r"[\[\]:*?/\\]", "_", str(name or "Sheet")).strip()[:31] or "Sheet"
|
||
base, i = n, 1
|
||
while n.lower() in used:
|
||
suffix = f"_{i}"
|
||
n = base[: 31 - len(suffix)] + suffix
|
||
i += 1
|
||
used.add(n.lower())
|
||
return n
|
||
|
||
|
||
def build_xlsx(sheets, path):
|
||
# 1) 收集共享字符串 + 每表 XML
|
||
shared, shared_idx = [], {}
|
||
def sidx(text):
|
||
if text not in shared_idx:
|
||
shared_idx[text] = len(shared)
|
||
shared.append(text)
|
||
return shared_idx[text]
|
||
|
||
used, sheet_xml, names = set(), [], []
|
||
for s in sheets:
|
||
sname = sheet_name(s.get("name"), used)
|
||
names.append(sname)
|
||
rows = s.get("cells") or []
|
||
chunks = []
|
||
for ri, row in enumerate(rows, start=1):
|
||
cells = []
|
||
for ci, cell in enumerate(row):
|
||
v = cell.get("value") if isinstance(cell, dict) else cell
|
||
v = norm(v)
|
||
if v is None or v == "":
|
||
continue
|
||
ref = f"{col_letter(ci)}{ri}"
|
||
if NUM_RE.match(v):
|
||
cells.append(f'<c r="{ref}"><v>{v}</v></c>')
|
||
else:
|
||
cells.append(f'<c r="{ref}" t="s"><v>{sidx(v)}</v></c>')
|
||
if cells:
|
||
chunks.append(f'<row r="{ri}">{"".join(cells)}</row>')
|
||
sheet_xml.append(
|
||
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
|
||
f'<sheetData>{"".join(chunks)}</sheetData></worksheet>')
|
||
|
||
n = len(names)
|
||
ct = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
|
||
'<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
|
||
'<Default Extension="xml" ContentType="application/xml"/>'
|
||
'<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>'
|
||
'<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>'
|
||
'<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>'
|
||
+ "".join(f'<Override PartName="/xl/worksheets/sheet{i+1}.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>' for i in range(n))
|
||
+ '</Types>')
|
||
rels = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
|
||
'<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'
|
||
'</Relationships>')
|
||
wb = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" '
|
||
'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets>'
|
||
+ "".join(f'<sheet name="{escape(names[i])}" sheetId="{i+1}" r:id="rId{i+1}"/>' for i in range(n))
|
||
+ '</sheets></workbook>')
|
||
wb_rels = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
|
||
+ "".join(f'<Relationship Id="rId{i+1}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet{i+1}.xml"/>' for i in range(n))
|
||
+ f'<Relationship Id="rId{n+1}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'
|
||
+ f'<Relationship Id="rId{n+2}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>'
|
||
+ '</Relationships>')
|
||
styles = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
'<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
|
||
'<fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts>'
|
||
'<fills count="1"><fill><patternFill patternType="none"/></fill></fills>'
|
||
'<borders count="1"><border/></borders>'
|
||
'<cellStyleXfs count="1"><xf/></cellStyleXfs>'
|
||
'<cellXfs count="1"><xf/></cellXfs></styleSheet>')
|
||
sst = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||
f'<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="{len(shared)}" uniqueCount="{len(shared)}">'
|
||
+ "".join(f"<si><t xml:space=\"preserve\">{escape(t)}</t></si>" for t in shared)
|
||
+ '</sst>')
|
||
|
||
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z:
|
||
z.writestr("[Content_Types].xml", ct)
|
||
z.writestr("_rels/.rels", rels)
|
||
z.writestr("xl/workbook.xml", wb)
|
||
z.writestr("xl/_rels/workbook.xml.rels", wb_rels)
|
||
z.writestr("xl/styles.xml", styles)
|
||
z.writestr("xl/sharedStrings.xml", sst)
|
||
for i, xml in enumerate(sheet_xml):
|
||
z.writestr(f"xl/worksheets/sheet{i+1}.xml", xml)
|
||
return {"sheets": names, "shared_strings": len(shared)}
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--node")
|
||
ap.add_argument("--cells-json")
|
||
ap.add_argument("--out", required=True)
|
||
a = ap.parse_args()
|
||
if a.cells_json:
|
||
sheets = json.load(open(a.cells_json))["sheets"]
|
||
elif a.node:
|
||
sheets = fetch_sheets(a.node)
|
||
else:
|
||
raise SystemExit("需要 --node 或 --cells-json")
|
||
info = build_xlsx(sheets, a.out)
|
||
rows = sum(len(s.get("cells") or []) for s in sheets)
|
||
print(json.dumps({"success": True, "out": a.out, "sheets": info["sheets"],
|
||
"sheet_count": len(sheets), "rows": rows,
|
||
"shared_strings": info["shared_strings"]}, ensure_ascii=False))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |