ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Gromulus/Gromulus_Lib.py
Revision: 806
Committed: Thu Dec 21 18:25:36 2023 UTC (2 years, 3 months ago) by nino.borges
Content type: text/x-python
File size: 7008 byte(s)
Log Message:
I'm tagging this as the final version of version 1. This version works, although it's limited, but it doesnt support zip files and archives in general.  I'm going to re-do the tables so that it better supports this.  This will then start the version 2.

File Contents

# User Rev Content
1 nino.borges 795 """
2     This creates the main library of methods for Gromulus.
3    
4     """
5    
6 nino.borges 805 import sqlite3, configparser, shutil
7 nino.borges 795 import xml.etree.ElementTree as ET
8     import Tool_Box.NinoGenTools, os
9    
10    
11 nino.borges 805 config = configparser.ConfigParser()
12     config.read("/home/nino/.gromulus/gromulus_cfg.ini")
13 nino.borges 795
14 nino.borges 805 conn = sqlite3.connect(config.get('RootConfig','databasePath')) #"GromulusDatabase.db")
15 nino.borges 795
16    
17     def ImportNewNoIntroDat(datPath):
18     """Deletes all of the data in the two No_Intro tables and load the info from the new dat."""
19     tree = ET.parse(datPath)
20     root = tree.getroot()
21    
22     ## First gather information from header
23     for elm in root.findall('header'):
24     headerID = elm.find('id').text
25     headerName = elm.find('name').text
26     headerDesc = elm.find('description').text
27     headerVer = elm.find('version').text
28    
29     #print(headerID)
30     #print (headerName)
31     #print(headerDesc)
32     #print(headerVer)
33     conn.execute("""DELETE FROM no_intro_system""" )
34     conn.execute(f'INSERT INTO no_intro_system VALUES ({headerID}, "{headerName}", "{headerDesc}", "{headerVer}")')
35    
36     conn.execute("""DELETE FROM no_intro_main""" )
37     for elm in root.findall('game'):
38     gameName = elm.get('name')
39     gameID = elm.get('id')
40     gameCloneOfID = elm.get('cloneofid')
41     gameDesc = elm.find('description').text
42    
43     romName = elm.find('rom').get('name')
44     romSize = elm.find('rom').get('size')
45     romCRC = elm.find('rom').get('crc')
46     romMD5 = elm.find('rom').get('md5')
47     romSHA1 = elm.find('rom').get('sha1')
48     romSHA256 = elm.find('rom').get('sha256')
49     romStatus = elm.find('rom').get('status')
50     conn.execute(f'INSERT INTO no_intro_main VALUES ("{gameName}", "{gameID}", "{gameCloneOfID}", "{gameDesc}", "{romName}", "{romCRC}", "{romMD5}", "{romSHA1}", "{romSHA256}", "{romStatus}", {headerID})')
51    
52     conn.commit()
53     conn.close()
54    
55    
56 nino.borges 806 def ImportNewTosecDat(datPath,systemID):
57     """Deletes all of the data in the single tosec table and load the info from the new dat."""
58     tree = ET.parse(datPath)
59     root = tree.getroot()
60    
61     conn.execute("""DELETE FROM tosec_main""")
62     for elm in root.findall('game'):
63     gameName = elm.get('name')
64     gameDesc = elm.find('description').text
65    
66     romName = elm.find('rom').get('name')
67     romCRC = elm.find('rom').get('crc')
68     romMD5 = elm.find('rom').get('md5')
69     romSHA1 = elm.find('rom').get('sha1')
70     conn.execute(f'INSERT INTO tosec_main VALUES ("{gameName}", "{gameDesc}", "{romName}", "{romCRC}", "{romMD5}", "{romSHA1}", {systemID})')
71    
72     conn.commit()
73     conn.close()
74    
75 nino.borges 805 def AddNewSystem(systemName, shortName, relativePath):
76     """Adding a single system, with path where the ROMs will live."""
77     conn.execute(f'INSERT INTO main_app_system("name", "short_name", "relative_file_path")VALUES ("{systemName}", "{shortName}", "{relativePath}")')
78     conn.commit()
79     conn.close()
80    
81 nino.borges 795 ## Next gather game information
82 nino.borges 805 def AddNewRoms(pathToRoms, systemID, testOnly = False):
83 nino.borges 795 """Adding additional roms."""
84 nino.borges 805 importedCount = 0
85     alreadyExistsCount = 0
86     errImportCount = 0
87    
88     sysResult = conn.execute(f'SELECT relative_file_path FROM main_app_system WHERE id={systemID}')
89     for s in sysResult:
90     systemRomPath = s[0]
91     fullRomPath = os.path.join(config.get('RootConfig','softwareBackupStartDir'),systemRomPath)
92    
93 nino.borges 795 res = conn.execute('SELECT md5 FROM main_app')
94     currentHashList = []
95     for h in res:
96 nino.borges 805 #print(h[0])
97 nino.borges 795 currentHashList.append(h[0])
98     #print(currentHashList)
99     print(len(currentHashList))
100 nino.borges 805
101 nino.borges 795 hshr = Tool_Box.NinoGenTools.HashFileContents('md5')
102     for testFile in os.listdir(pathToRoms):
103 nino.borges 805 print(f"\n\nNow analyzing {testFile}...")
104 nino.borges 795 hashVal = hshr.HashFile(os.path.join(pathToRoms,testFile))
105     if hashVal in currentHashList:
106     print("This file is already in your collection, skipping.")
107 nino.borges 805 alreadyExistsCount +=1
108 nino.borges 795 else:
109     print("This file is unique. Adding to collection.")
110 nino.borges 806 fileNameInc = 0
111     targetFileName = testFile
112     while os.path.isfile(os.path.join(fullRomPath,targetFileName)):
113     fileNameInc +=1
114     testFileNamePart, testFileExt = os.path.splitext(testFile)
115     targetFileName = testFileNamePart +"_"+ str(fileNameInc) + testFileExt
116     if testOnly:
117     pass
118 nino.borges 805 else:
119 nino.borges 806 shutil.copyfile(os.path.join(pathToRoms,testFile),os.path.join(fullRomPath,targetFileName))
120     conn.execute(f'INSERT INTO main_app("file_name", "system_console", "md5")VALUES("{testFile}","{systemID}","{hashVal}")')
121     conn.commit()
122     importedCount +=1
123 nino.borges 795 conn.close()
124 nino.borges 805 return importedCount, alreadyExistsCount, errImportCount
125 nino.borges 795
126 nino.borges 805 def GetSystemList():
127     res = conn.execute("SELECT id, name, short_name, relative_file_path FROM main_app_system ")
128     systemNamesMatrix = {}
129     for h in res:
130     systemNamesMatrix[h[1]] = [h[0],h[2],h[3]]
131     return systemNamesMatrix
132    
133 nino.borges 795 def GetGameListBySystem(systemName):
134 nino.borges 806 res = conn.execute("SELECT main_app.id, main_app.game_name, no_intro_main.game_name, tosec_main.game_name,main_app.file_name FROM main_app LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5 LEFT JOIN tosec_main ON main_app.md5 = tosec_main.md5")
135     #res = conn.execute("SELECT main_app.id, main_app.game_name, no_intro_main.game_name, main_app.file_name FROM main_app LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5")
136 nino.borges 795 gameNamesMatrix = {}
137     for h in res:
138     if h[1]:
139     gameNamesMatrix[h[1]] = h[0]
140     elif h[2]:
141     gameNamesMatrix[h[2]] = h[0]
142 nino.borges 806 elif h[3]:
143     gameNamesMatrix[h[3]] = h[0]
144 nino.borges 795 else:
145 nino.borges 806 gameNamesMatrix[h[4]] = h[0]
146 nino.borges 795
147     #for g in gameNamesList:
148     # print(g)
149     return gameNamesMatrix
150    
151     def GetSingleGameById(id):
152     res = conn.execute(f"""SELECT main_app.game_name, main_app.md5, main_app.file_name, main_app.relative_file_path, no_intro_main.game_name, no_intro_system.name FROM main_app
153     LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5
154     LEFT JOIN no_intro_system ON no_intro_main.no_intro_system_id = no_intro_system.id
155     WHERE main_app.id = {id}""")
156     for h in res:
157     gameTuple = h
158     print(h)
159     return gameTuple
160    
161    
162    
163     if __name__ == '__main__':
164 nino.borges 806 pass
165 nino.borges 805 #test = GetSystemList()
166     #print(test.keys())
167     #AddNewSystem("Super Nintendo", "SNES", r"Roms/Snes/Gromulus")
168 nino.borges 795 #ImportNewNoIntroDat("/home/nino/MyCode/Python/Active_prgs/Gromulus/_data/_dats/Nintendo - Super Nintendo Entertainment System (20230516-033614).dat")
169 nino.borges 806 #AddNewRoms('/mnt/smb/HomeData/ninoborges/Emulation/Roms/Snes/Random2',"1",testOnly=True)
170 nino.borges 795 #res = conn.execute("SELECT * FROM main_app LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5")
171     #for h in res:
172     # print(h)
173     # print("\n")
174     #GetGameListBySystem('snes')
175 nino.borges 805 #GetSingleGameById('224')
176 nino.borges 795