@@ -38,57 +38,116 @@ def get_project_by_name(client: SysMLV2Client, name: str):
3838 print (f"Error getting projects: { e } " )
3939 return None , None
4040
41- def delete_project_data (client : SysMLV2Client , proj_id :str , branch_id :str = None ):
41+ def delete_project_data (client : SysMLV2Client , proj_id : str , branch_id : str = None ):
4242 # Get the latest commit
4343 commits = client .list_commits (proj_id )
4444 if not commits :
45- return
45+ return None , None
46+
4647 latest = commits [0 ] if isinstance (commits , list ) else commits
4748 commit_id = latest .get ("@id" ) or latest .get ("id" )
4849 if not commit_id :
49- return
50+ return None , None
5051
5152 # Fetch elements for that commit
5253 elements = client .list_elements (proj_id , commit_id )
53- print (f"_delete_project_data: Found { len (elements )} elements to delete " )
54+ print (f"_delete_project_data: Found { len (elements )} elements total " )
5455
5556 if not elements :
56- return
57-
58- # include elements in the change attribute which have an identity but no payload
59- # {
60- # "@type": "Commit",
61- # "description": "Commit 1: Create initial elements",
62- # "change": [
63- # {
64- # "identity": {
65- # "@id": "4b0d767c-318f-4160-8a5f-2a76a3c5a65a"
66- # }
67- # }
57+ return None , None
58+
59+ # Filter out root namespace (no owningNamespace)
60+ elements_to_delete = [
61+ elem for elem in elements
62+ if "@id" in elem and elem .get ("owningNamespace" ) is not None
63+ ]
64+
65+ print (f"_delete_project_data: Deleting { len (elements_to_delete )} elements (excluding root)" )
66+
6867 change_payload = [
6968 {
7069 "identity" : {
7170 "@id" : elem ["@id" ]
7271 }
7372 }
74- for elem in elements
75- if "@id" in elem
73+ for elem in elements_to_delete
7674 ]
77- commit = {"@type" : "Commit" , "description" : f"delete all elements" , "change" : change_payload }
78- #print (commit)
75+
76+ # print (change_payload)
77+
78+ if not change_payload :
79+ print ("_delete_project_data: Nothing to delete." )
80+ return None , None
81+
82+ commit = {
83+ "@type" : "Commit" ,
84+ "description" : "delete all elements except root namespace" ,
85+ "change" : change_payload
86+ }
7987
8088 try :
8189 resp = client .create_commit (proj_id , commit , branch_id = branch_id )
82- commit1_id = resp .get (' @id' )
90+ commit1_id = resp .get (" @id" )
8391 if not commit1_id :
84- print ("\n *** WARNING: Could not extract commit ID ('@id') from response! ***" )
92+ print ("*** WARNING: Could not extract commit ID ('@id') from response! ***" )
8593 return None , None
86- else :
87- return resp , commit1_id
94+ return resp , commit1_id
95+
8896 except SysMLV2Error as e :
89- print (f"Error creating commit 1 : { e } " )
97+ print (f"Error creating delete commit : { e } " )
9098 return None , None
9199
100+ # def delete_project_data (client: SysMLV2Client, proj_id:str, branch_id:str = None):
101+ # # Get the latest commit
102+ # commits = client.list_commits(proj_id)
103+ # if not commits:
104+ # return
105+ # latest = commits[0] if isinstance(commits, list) else commits
106+ # commit_id = latest.get("@id") or latest.get("id")
107+ # if not commit_id:
108+ # return
109+
110+ # # Fetch elements for that commit
111+ # elements = client.list_elements(proj_id, commit_id)
112+ # print(f"_delete_project_data: Found {len(elements)} elements to delete")
113+
114+ # if not elements:
115+ # return
116+
117+ # # include elements in the change attribute which have an identity but no payload
118+ # # {
119+ # # "@type": "Commit",
120+ # # "description": "Commit 1: Create initial elements",
121+ # # "change": [
122+ # # {
123+ # # "identity": {
124+ # # "@id": "4b0d767c-318f-4160-8a5f-2a76a3c5a65a"
125+ # # }
126+ # # }
127+ # change_payload = [
128+ # {
129+ # "identity": {
130+ # "@id": elem["@id"]
131+ # }
132+ # }
133+ # for elem in elements
134+ # if "@id" in elem
135+ # ]
136+ # commit = {"@type": "Commit", "description": f"delete all elements", "change": change_payload}
137+ # #print (commit)
138+
139+ # try:
140+ # resp = client.create_commit(proj_id, commit, branch_id=branch_id)
141+ # commit1_id = resp.get('@id')
142+ # if not commit1_id:
143+ # print("\n*** WARNING: Could not extract commit ID ('@id') from response! ***")
144+ # return None, None
145+ # else:
146+ # return resp, commit1_id
147+ # except SysMLV2Error as e:
148+ # print(f"Error creating commit 1: {e}")
149+ # return None, None
150+
92151def commit_to_project (client : SysMLV2Client , proj_id :str , payload :str , commit_msg :str = "default commit message" , user_commit_data = None , delete_project_data :bool = False , branch_id = None ):
93152 if user_commit_data is None :
94153 commit1_data = {
0 commit comments