From 2ffcb45c15f93cc09d871ab48148b1f48a85d284 Mon Sep 17 00:00:00 2001
From: Recolic <git@me.recolic.net>
Date: Sun, 28 May 2023 08:24:11 -0700
Subject: [PATCH] add remove orphan prog

---
 remove-orphan-swddrw/README.md |  6 +++++
 remove-orphan-swddrw/main.py   | 46 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 remove-orphan-swddrw/README.md
 create mode 100644 remove-orphan-swddrw/main.py

diff --git a/remove-orphan-swddrw/README.md b/remove-orphan-swddrw/README.md
new file mode 100644
index 0000000..947b114
--- /dev/null
+++ b/remove-orphan-swddrw/README.md
@@ -0,0 +1,6 @@
+# usage
+
+run this program in a directory. This directory contains some SLDPRT, SLDASM, and SLDDRW files. 
+
+for every SLDDRW file, if there is no corresponding SLDPRT/SLDASM file (with the same filename), the SLDDRW file would be deleted.
+
diff --git a/remove-orphan-swddrw/main.py b/remove-orphan-swddrw/main.py
new file mode 100644
index 0000000..528ed60
--- /dev/null
+++ b/remove-orphan-swddrw/main.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+# A script to deal with product sheet. (see test-*.csv)
+import traceback
+def show_exception_and_exit(exc_type, exc_value, tb):
+    traceback.print_exception(exc_type, exc_value, tb)
+    input("按回车键退出")
+    sys.exit(-1)
+
+import sys
+import os
+if os.name == 'nt':
+    sys.excepthook = show_exception_and_exit
+
+
+#########################################################################
+
+import os, shutil
+
+def main():
+    print("DEBUG: workdir=", os.getcwd())
+    is_drw = lambda fname: fname.lower().endswith('.slddrw')
+    is_prt_asm = lambda fname: (fname.lower().endswith('.sldprt') or fname.lower().endswith('.sldasm'))
+    files = [f for f in os.listdir('.') if os.path.isfile(f)]
+    filesl = [f.lower() for f in os.listdir('.') if os.path.isfile(f)]
+    for f in files:
+        if is_drw(f):
+            print("CHECKING ", f)
+
+            ext_len = len('.slddrw')
+            expecting1 = f[:-ext_len] + '.sldprt'
+            expecting2 = f[:-ext_len] + '.sldasm'
+            if expecting1.lower() in filesl or expecting2.lower() in filesl:
+                # good
+                pass
+            else:
+                # delete this file
+                print("DELETE ", f)
+                os.remove(f)
+
+
+main()
+input("按回车键退出")
+
+
+
+
-- 
GitLab