Changeset View
Changeset View
Standalone View
Standalone View
bin/InstallDB.py
Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Line(s) | |||||
112 | class InstallDB(object): | 112 | class InstallDB(object): | ||
113 | """ a database object which provides the methods for adding and removing a package and | 113 | """ a database object which provides the methods for adding and removing a package and | ||
114 | checking its installation status. | 114 | checking its installation status. | ||
115 | In case the database doesn't exist if the constructor is called, a new database is constructed | 115 | In case the database doesn't exist if the constructor is called, a new database is constructed | ||
116 | """ | 116 | """ | ||
117 | 117 | | |||
118 | def __init__( self, filename = None ): | 118 | def __init__( self, filename = None ): | ||
119 | if filename == None: | 119 | if filename == None: | ||
120 | EmergeStandardDirs.allowShortpaths(False) | 120 | old = EmergeStandardDirs.allowShortpaths(False) | ||
121 | filename = os.path.join( EmergeStandardDirs.etcPortageDir(), 'install.db' ) | 121 | filename = os.path.join( EmergeStandardDirs.etcPortageDir(), 'install.db' ) | ||
122 | EmergeStandardDirs.allowShortpaths(True) | 122 | EmergeStandardDirs.allowShortpaths(old) | ||
123 | 123 | | |||
124 | self.dbfilename = filename | 124 | self.dbfilename = filename | ||
125 | self._prepareDatabase() | 125 | self._prepareDatabase() | ||
126 | 126 | | |||
127 | def getLastId( self ): | 127 | def getLastId( self ): | ||
128 | """ returns the last id from a table, which is essentially the """ | 128 | """ returns the last id from a table, which is essentially the """ | ||
129 | cmd = '''SELECT max(packageId) FROM packageList;''' | 129 | cmd = '''SELECT max(packageId) FROM packageList;''' | ||
130 | 130 | | |||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Line(s) | 142 | # | |||
247 | 247 | | |||
248 | def getInstalledPackages( self, category, package, prefix = None ): | 248 | def getInstalledPackages( self, category, package, prefix = None ): | ||
249 | """ return an installed package """ | 249 | """ return an installed package """ | ||
250 | cursor = self.connection.cursor() | 250 | cursor = self.connection.cursor() | ||
251 | return [ InstallPackage( cursor, pId ) for pId in self.getPackageIds( category, package, prefix ) ] | 251 | return [ InstallPackage( cursor, pId ) for pId in self.getPackageIds( category, package, prefix ) ] | ||
252 | 252 | | |||
253 | def _prepareDatabase( self ): | 253 | def _prepareDatabase( self ): | ||
254 | """ prepare a new database and add the required table layout """ | 254 | """ prepare a new database and add the required table layout """ | ||
255 | oldShortPathvalue = EmergeStandardDirs.allowShortpaths(False) | ||||
255 | if not os.path.exists( self.dbfilename ): | 256 | if not os.path.exists( self.dbfilename ): | ||
256 | EmergeStandardDirs.allowShortpaths(False) | | |||
257 | if not os.path.exists( EmergeStandardDirs.etcPortageDir( ) ): | 257 | if not os.path.exists( EmergeStandardDirs.etcPortageDir( ) ): | ||
258 | os.makedirs( EmergeStandardDirs.etcPortageDir( ) ) | 258 | os.makedirs( EmergeStandardDirs.etcPortageDir( ) ) | ||
259 | print( "database does not exist yet: creating database", file = sys.stderr ) | 259 | print( "database does not exist yet: creating database", file = sys.stderr ) | ||
260 | self.connection = sqlite3.connect( self.dbfilename ) | 260 | self.connection = sqlite3.connect( self.dbfilename ) | ||
261 | cursor = self.connection.cursor() | 261 | cursor = self.connection.cursor() | ||
262 | 262 | | |||
263 | # first, create the required tables | 263 | # first, create the required tables | ||
264 | cursor.execute( '''CREATE TABLE packageList (packageId INTEGER PRIMARY KEY AUTOINCREMENT, | 264 | cursor.execute( '''CREATE TABLE packageList (packageId INTEGER PRIMARY KEY AUTOINCREMENT, | ||
265 | prefix TEXT, category TEXT, packageName TEXT, version TEXT, revision TEXT)''' ) | 265 | prefix TEXT, category TEXT, packageName TEXT, version TEXT, revision TEXT)''' ) | ||
266 | cursor.execute( '''CREATE TABLE fileList (fileId INTEGER PRIMARY KEY AUTOINCREMENT, | 266 | cursor.execute( '''CREATE TABLE fileList (fileId INTEGER PRIMARY KEY AUTOINCREMENT, | ||
267 | packageId INTEGER, filename TEXT, fileHash TEXT)''' ) | 267 | packageId INTEGER, filename TEXT, fileHash TEXT)''' ) | ||
268 | self.connection.commit() | 268 | self.connection.commit() | ||
269 | else: | 269 | else: | ||
270 | self.connection = sqlite3.connect( self.dbfilename ) | 270 | self.connection = sqlite3.connect( self.dbfilename ) | ||
271 | cursor = self.connection.cursor() | 271 | cursor = self.connection.cursor() | ||
272 | cursor.execute( '''PRAGMA table_info('packageList')''') | 272 | cursor.execute( '''PRAGMA table_info('packageList')''') | ||
273 | if not len(cursor.fetchall()) == 6: | 273 | if not len(cursor.fetchall()) == 6: | ||
274 | cursor.execute('''ALTER TABLE packageList ADD COLUMN revision TEXT''') | 274 | cursor.execute('''ALTER TABLE packageList ADD COLUMN revision TEXT''') | ||
275 | self.connection.commit() | 275 | self.connection.commit() | ||
276 | EmergeStandardDirs.allowShortpaths(True) | 276 | EmergeStandardDirs.allowShortpaths(oldShortPathvalue) | ||
277 | 277 | | |||
278 | 278 | | |||
279 | # get a global object | 279 | # get a global object | ||
280 | installdb = InstallDB() | 280 | installdb = InstallDB() | ||
281 | 281 | | |||
282 | # an additional function from portage.py | 282 | # an additional function from portage.py | ||
283 | def printInstalled(): | 283 | def printInstalled(): | ||
284 | """get all the packages that are already installed""" | 284 | """get all the packages that are already installed""" | ||
▲ Show 20 Lines • Show All 101 Lines • Show Last 20 Lines |