MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ import pyalpm from pycman import config import question as q from random import choice, randint from sys import modules config.init_with_config("/etc/pacman.conf") localdb = pyalpm.get_localdb() #questionTypes= (q.definition,q.depends, # q.fileOwner,q.installedSize, # q.packager) types = [getattr(q, t) for t in dir(q) if str(type(getattr(q, t))) == "<class 'type'>"] questionTypes = [qtype for qtype in types if (issubclass(qtype, q.Question) and qtype is not q.Question)] del(types) def getRandomQuestion(package=None, numWrongAnswers=3): """Returns a tuple with size of 3, first question text, second correct answer, third list of wrong answers @param package: A pyalpm.package type @param numWrongAnswers: integer @return: tuple """
def main(tmp_db_path): # Use a temporary database path to avoid issues caused by synchronizing the # sync database without a full system upgrade. # See the discussion here: # https://bbs.archlinux.org/viewtopic.php?pid=951285#p951285 # Basically, if you sync the database and then install packages without first # upgrading the system (-y), you can do some damage. tmp_db_path = os.path.abspath(tmp_db_path) conf = config.PacmanConfig(conf = '/etc/pacman.conf') db_path = conf.options['DBPath'] if tmp_db_path == db_path: print("temporary path cannot be %s" % db_path) sys.exit(1) local_db_path = os.path.join(db_path, 'local') tmp_local_db_path = os.path.join(tmp_db_path, 'local') # Set up the temporary database path if not os.path.exists(tmp_db_path): os.makedirs(tmp_db_path) os.symlink(local_db_path, tmp_local_db_path) elif not os.path.islink(tmp_local_db_path): # Move instead of unlinking just in case. os.rename(tmp_local_db_path, tmp_local_db_path + '.old') os.symlink(local_db_path, tmp_local_db_path) # Redirect the stdout messages download messages to stderr. with open(os.devnull, 'w') as f: sys.stdout = f action_sync.main(('-b', tmp_db_path, '-y')) sys.stdout = sys.__stdout__ installed = set(p for p in pyalpm.get_localdb().pkgcache) upgradable = OrderedDict() syncdbs = pyalpm.get_syncdbs() for db in syncdbs: # Without "list" the set cannot be altered with "remove" below. for pkg in list(installed): pkgname = pkg.name syncpkg = db.get_pkg(pkgname) if syncpkg: if pyalpm.vercmp(syncpkg.version, pkg.version) > 0: try: upgradable[db.name].add((pkg, syncpkg)) except KeyError: upgradable[db.name] = set(((pkg, syncpkg),)) installed.remove(pkg) foreign = dict([(p.name,p) for p in installed]) try: aur = AUR.AUR(threads=10) aur_pkgs = aur.info(foreign.keys()) except AUR.AURError as e: sys.stderr.write(str(e)) sys.exit(1) upgradable_aur = list() for aur_pkg in aur_pkgs: installed_pkg = foreign[aur_pkg['Name']] if pyalpm.vercmp(aur_pkg['Version'], installed_pkg.version) > 0: upgradable_aur.append((installed_pkg, aur_pkg)) installed.remove(installed_pkg) display(upgradable, upgradable_aur)