-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasset_project.py
More file actions
166 lines (133 loc) · 4.76 KB
/
Copy pathasset_project.py
File metadata and controls
166 lines (133 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import csv
import json
import os
import subprocess
import sys
import tempfile
from datetime import datetime
def save_text_file(project_dir, filename, content):
if not project_dir:
return
path = os.path.join(project_dir, filename)
with open(path, "w", encoding="utf-8") as handle:
handle.write(content)
def overwrite_csv_rows(csv_path, fieldnames, rows):
with open(csv_path, "w", newline="", encoding="utf-8-sig") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
def append_csv_rows(csv_path, fieldnames, rows):
file_exists = os.path.exists(csv_path)
with open(csv_path, "a", newline="", encoding="utf-8-sig") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames, extrasaction="ignore")
if not file_exists:
writer.writeheader()
writer.writerows(rows)
def append_lines(path, lines):
if not lines:
return
with open(path, "a", encoding="utf-8") as handle:
handle.write("\n".join(lines) + "\n")
def default_project_root(project_dir, workspace_dir):
if project_dir and os.path.isdir(project_dir):
return os.path.dirname(project_dir)
return os.path.abspath(workspace_dir)
def fail_sample_path(project_dir, fail_sample_file):
if not project_dir:
return ""
return os.path.join(project_dir, fail_sample_file)
def is_subpath(candidate, parent):
try:
candidate_abs = os.path.abspath(candidate)
parent_abs = os.path.abspath(parent)
return os.path.commonpath([candidate_abs, parent_abs]) == parent_abs
except Exception:
return False
def open_local_path(path):
if sys.platform == "win32":
os.startfile(path)
elif sys.platform == "darwin":
subprocess.Popen(["open", path])
else:
subprocess.Popen(["xdg-open", path])
def progress_state_path(project_dir, progress_file):
if not project_dir:
return ""
return os.path.join(project_dir, progress_file)
def read_progress_state(project_dir, progress_file):
path = progress_state_path(project_dir, progress_file)
if not path or not os.path.exists(path):
return {}
try:
if os.path.getsize(path) <= 0:
return {}
except OSError:
return {}
try:
with open(path, "r", encoding="utf-8") as handle:
data = json.load(handle)
return data if isinstance(data, dict) else {}
except (json.JSONDecodeError, OSError, ValueError):
return {}
def atomic_json_dump(path, payload):
directory = os.path.dirname(path) or "."
fd, tmp_path = tempfile.mkstemp(prefix=".tmp-progress-", suffix=".json", dir=directory)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
json.dump(payload, handle, ensure_ascii=False, indent=2)
handle.flush()
os.fsync(handle.fileno())
os.replace(tmp_path, path)
finally:
if os.path.exists(tmp_path):
try:
os.remove(tmp_path)
except OSError:
pass
def write_progress_state(
project_dir,
progress_file,
scanned_current,
current=None,
total=None,
bar_total=0,
active=None,
stats=None,
scan_signature=None,
):
if not project_dir:
return {}
state = read_progress_state(project_dir, progress_file)
if current is None:
current = max(scanned_current, int(state.get("current", 0) or 0))
else:
current = max(scanned_current, int(current))
existing_total = int(state.get("total", 0) or 0)
if total is None:
total = max(existing_total, int(bar_total or 0), current, 1)
else:
total = max(int(total), current, 1)
state["current"] = current
state["total"] = total
if active is not None:
state["active"] = bool(active)
if scan_signature is not None:
state["scan_signature"] = str(scan_signature or "")
if isinstance(stats, dict):
state["stats"] = {
"total": int(stats.get("total", 0) or 0),
"success": int(stats.get("success", 0) or 0),
"fail": int(stats.get("fail", 0) or 0),
}
if isinstance(stats.get("top_fail_reasons"), dict):
state["stats"]["top_fail_reasons"] = dict(stats.get("top_fail_reasons"))
state["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
path = progress_state_path(project_dir, progress_file)
atomic_json_dump(path, state)
return state
def save_settings(project_dir, settings):
if not project_dir:
return
path = os.path.join(project_dir, "settings.json")
with open(path, "w", encoding="utf-8") as handle:
json.dump(settings, handle, ensure_ascii=False, indent=4)