Explorar el Código

- factor out code to get a single package FromName()
- check in Grouped* first without modifier interpretation

David Kalnischkies hace 16 años
padre
commit
bd63159562
Se han modificado 3 ficheros con 116 adiciones y 54 borrados
  1. 99 52
      cmdline/cacheset.cc
  2. 14 1
      cmdline/cacheset.h
  3. 3 1
      debian/changelog

+ 99 - 52
cmdline/cacheset.cc

@@ -25,11 +25,7 @@
 namespace APT {
 namespace APT {
 // FromTask - Return all packages in the cache from a specific task	/*{{{*/
 // FromTask - Return all packages in the cache from a specific task	/*{{{*/
 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
-	PackageSet pkgset;
-	if (Cache.BuildCaches() == false || Cache.BuildDepCache() == false)
-		return pkgset;
-
-	size_t archfound = pattern.find_last_of(':');
+	size_t const archfound = pattern.find_last_of(':');
 	std::string arch = "native";
 	std::string arch = "native";
 	if (archfound != std::string::npos) {
 	if (archfound != std::string::npos) {
 		arch = pattern.substr(archfound+1);
 		arch = pattern.substr(archfound+1);
@@ -37,9 +33,13 @@ PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheS
 	}
 	}
 
 
 	if (pattern[pattern.length() -1] != '^')
 	if (pattern[pattern.length() -1] != '^')
-		return pkgset;
+		return APT::PackageSet();
 	pattern.erase(pattern.length()-1);
 	pattern.erase(pattern.length()-1);
 
 
+	if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
+		return APT::PackageSet();
+
+	PackageSet pkgset;
 	// get the records
 	// get the records
 	pkgRecords Recs(Cache);
 	pkgRecords Recs(Cache);
 
 
@@ -83,11 +83,9 @@ PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheS
 									/*}}}*/
 									/*}}}*/
 // FromRegEx - Return all packages in the cache matching a pattern	/*{{{*/
 // FromRegEx - Return all packages in the cache matching a pattern	/*{{{*/
 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
-	PackageSet pkgset;
 	static const char * const isregex = ".?+*|[^$";
 	static const char * const isregex = ".?+*|[^$";
-
 	if (pattern.find_first_of(isregex) == std::string::npos)
 	if (pattern.find_first_of(isregex) == std::string::npos)
-		return pkgset;
+		return PackageSet();
 
 
 	size_t archfound = pattern.find_last_of(':');
 	size_t archfound = pattern.find_last_of(':');
 	std::string arch = "native";
 	std::string arch = "native";
@@ -105,9 +103,13 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, Cache
 		char Error[300];
 		char Error[300];
 		regerror(Res, &Pattern, Error, sizeof(Error));
 		regerror(Res, &Pattern, Error, sizeof(Error));
 		_error->Error(_("Regex compilation error - %s"), Error);
 		_error->Error(_("Regex compilation error - %s"), Error);
-		return pkgset;
+		return PackageSet();
 	}
 	}
 
 
+	if (unlikely(Cache.GetPkgCache() == 0))
+		return PackageSet();
+
+	PackageSet pkgset;
 	for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
 	for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
 	{
 	{
 		if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
 		if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
@@ -135,6 +137,33 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, Cache
 	return pkgset;
 	return pkgset;
 }
 }
 									/*}}}*/
 									/*}}}*/
+// FromName - Returns the package defined  by this string		/*{{{*/
+pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
+			std::string const &str, CacheSetHelper &helper) {
+	std::string pkg = str;
+	size_t archfound = pkg.find_last_of(':');
+	std::string arch;
+	if (archfound != std::string::npos) {
+		arch = pkg.substr(archfound+1);
+		pkg.erase(archfound);
+	}
+
+	if (Cache.GetPkgCache() == 0)
+		return pkgCache::PkgIterator(Cache, 0);
+
+	pkgCache::PkgIterator Pkg(Cache, 0);
+	if (arch.empty() == true) {
+		pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
+		if (Grp.end() == false)
+			Pkg = Grp.FindPreferredPkg();
+	} else
+		Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
+
+	if (Pkg.end() == true)
+		return helper.canNotFindPkgName(Cache, str);
+	return Pkg;
+}
+									/*}}}*/
 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
 std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
 std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
 		pkgCacheFile &Cache, const char **cmdline,
 		pkgCacheFile &Cache, const char **cmdline,
@@ -144,6 +173,7 @@ std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
 	for (const char **I = cmdline; *I != 0; ++I) {
 	for (const char **I = cmdline; *I != 0; ++I) {
 		unsigned short modID = fallback;
 		unsigned short modID = fallback;
 		std::string str = *I;
 		std::string str = *I;
+		bool modifierPresent = false;
 		for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
 		for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
 		     mod != mods.end(); ++mod) {
 		     mod != mods.end(); ++mod) {
 			size_t const alength = strlen(mod->Alias);
 			size_t const alength = strlen(mod->Alias);
@@ -160,8 +190,18 @@ std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
 			case PackageSet::Modifier::NONE:
 			case PackageSet::Modifier::NONE:
 				continue;
 				continue;
 			}
 			}
+			modifierPresent = true;
 			break;
 			break;
 		}
 		}
+		if (modifierPresent == true) {
+			bool const errors = helper.showErrors(false);
+			pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
+			helper.showErrors(errors);
+			if (Pkg.end() == false) {
+				pkgsets[fallback].insert(Pkg);
+				continue;
+			}
+		}
 		pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
 		pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
 	}
 	}
 	return pkgsets;
 	return pkgsets;
@@ -179,42 +219,26 @@ PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline
 									/*}}}*/
 									/*}}}*/
 // FromString - Return all packages matching a specific string		/*{{{*/
 // FromString - Return all packages matching a specific string		/*{{{*/
 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
-	std::string pkg = str;
-	size_t archfound = pkg.find_last_of(':');
-	std::string arch;
-	if (archfound != std::string::npos) {
-		arch = pkg.substr(archfound+1);
-		pkg.erase(archfound);
-	}
-
-	pkgCache::PkgIterator Pkg;
-	if (arch.empty() == true) {
-		pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
-		if (Grp.end() == false)
-			Pkg = Grp.FindPreferredPkg();
-	} else
-		Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
-
-	if (Pkg.end() == false) {
-		PackageSet pkgset;
-		pkgset.insert(Pkg);
-		return pkgset;
-	}
-
 	_error->PushToStack();
 	_error->PushToStack();
 
 
-	PackageSet pset = FromTask(Cache, str, helper);
-	if (pset.empty() == true) {
-		pset = FromRegEx(Cache, str, helper);
-		if (pset.empty() == true)
-			pset = helper.canNotFindPackage(Cache, str);
+	PackageSet pkgset;
+	pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
+	if (Pkg.end() == false)
+		pkgset.insert(Pkg);
+	else {
+		pkgset = FromTask(Cache, str, helper);
+		if (pkgset.empty() == true) {
+			pkgset = FromRegEx(Cache, str, helper);
+			if (pkgset.empty() == true)
+				pkgset = helper.canNotFindPackage(Cache, str);
+		}
 	}
 	}
 
 
-	if (pset.empty() == false)
+	if (pkgset.empty() == false)
 		_error->RevertToStack();
 		_error->RevertToStack();
 	else
 	else
 		_error->MergeWithStack();
 		_error->MergeWithStack();
-	return pset;
+	return pkgset;
 }
 }
 									/*}}}*/
 									/*}}}*/
 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
@@ -227,6 +251,7 @@ std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
 		unsigned short modID = fallback;
 		unsigned short modID = fallback;
 		VersionSet::Version select = VersionSet::NEWEST;
 		VersionSet::Version select = VersionSet::NEWEST;
 		std::string str = *I;
 		std::string str = *I;
+		bool modifierPresent = false;
 		for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
 		for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
 		     mod != mods.end(); ++mod) {
 		     mod != mods.end(); ++mod) {
 			if (modID == fallback && mod->ID == fallback)
 			if (modID == fallback && mod->ID == fallback)
@@ -246,8 +271,19 @@ std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
 			case VersionSet::Modifier::NONE:
 			case VersionSet::Modifier::NONE:
 				continue;
 				continue;
 			}
 			}
+			modifierPresent = true;
 			break;
 			break;
 		}
 		}
+
+		if (modifierPresent == true) {
+			bool const errors = helper.showErrors(false);
+			VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
+			helper.showErrors(errors);
+			if (vset.empty() == false) {
+				versets[fallback].insert(vset);
+				continue;
+			}
+		}
 		versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
 		versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
 	}
 	}
 	return versets;
 	return versets;
@@ -257,16 +293,15 @@ std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
 		APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
 		APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
 	VersionSet verset;
 	VersionSet verset;
-	for (const char **I = cmdline; *I != 0; ++I) {
-		VersionSet vset = VersionSet::FromString(Cache, *I, fallback, helper);
-		verset.insert(vset.begin(), vset.end());
-	}
+	for (const char **I = cmdline; *I != 0; ++I)
+		verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
 	return verset;
 	return verset;
 }
 }
 									/*}}}*/
 									/*}}}*/
 // FromString - Returns all versions spedcified by a string		/*{{{*/
 // FromString - Returns all versions spedcified by a string		/*{{{*/
 APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
 APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
-		APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
+		APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
+		bool const &onlyFromName) {
 	std::string ver;
 	std::string ver;
 	bool verIsRel = false;
 	bool verIsRel = false;
 	size_t const vertag = pkg.find_last_of("/=");
 	size_t const vertag = pkg.find_last_of("/=");
@@ -275,7 +310,13 @@ APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
 		verIsRel = (pkg[vertag] == '/');
 		verIsRel = (pkg[vertag] == '/');
 		pkg.erase(vertag);
 		pkg.erase(vertag);
 	}
 	}
-	PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), helper);
+	PackageSet pkgset;
+	if (onlyFromName == false)
+		pkgset = PackageSet::FromString(Cache, pkg, helper);
+	else {
+		pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
+	}
+
 	VersionSet verset;
 	VersionSet verset;
 	for (PackageSet::const_iterator P = pkgset.begin();
 	for (PackageSet::const_iterator P = pkgset.begin();
 	     P != pkgset.end(); ++P) {
 	     P != pkgset.end(); ++P) {
@@ -372,8 +413,8 @@ pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
 	if (Cache.IsDepCacheBuilt() == true)
 	if (Cache.IsDepCacheBuilt() == true)
 		Cand = Cache[Pkg].CandidateVerIter(Cache);
 		Cand = Cache[Pkg].CandidateVerIter(Cache);
 	else {
 	else {
-		if (unlikely(Cache.BuildPolicy() == false))
-			return pkgCache::VerIterator(*Cache);
+		if (unlikely(Cache.GetPolicy() == 0))
+			return pkgCache::VerIterator(Cache);
 		Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
 		Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
 	}
 	}
 	if (Cand.end() == true)
 	if (Cand.end() == true)
@@ -389,6 +430,14 @@ pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
 	return Pkg.CurrentVer();
 	return Pkg.CurrentVer();
 }
 }
 									/*}}}*/
 									/*}}}*/
+// canNotFindPkgName - handle the case no package has this name		/*{{{*/
+pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
+			std::string const &str) {
+	if (ShowError == true)
+		_error->Error(_("Unable to locate package %s"), str.c_str());
+	return pkgCache::PkgIterator(Cache, 0);
+}
+									/*}}}*/
 // canNotFindTask - handle the case no package is found for a task	/*{{{*/
 // canNotFindTask - handle the case no package is found for a task	/*{{{*/
 PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
 PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
 	if (ShowError == true)
 	if (ShowError == true)
@@ -405,8 +454,6 @@ PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string patt
 									/*}}}*/
 									/*}}}*/
 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
 PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
 PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
-	if (ShowError == true)
-		_error->Error(_("Unable to locate package %s"), str.c_str());
 	return PackageSet();
 	return PackageSet();
 }
 }
 									/*}}}*/
 									/*}}}*/
@@ -431,7 +478,7 @@ pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
 		pkgCache::PkgIterator const &Pkg) {
 		pkgCache::PkgIterator const &Pkg) {
 	if (ShowError == true)
 	if (ShowError == true)
 		_error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
 		_error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
-	return pkgCache::VerIterator(*Cache);
+	return pkgCache::VerIterator(Cache);
 }
 }
 									/*}}}*/
 									/*}}}*/
 // canNotFindCandidateVer						/*{{{*/
 // canNotFindCandidateVer						/*{{{*/
@@ -439,7 +486,7 @@ pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache
 		pkgCache::PkgIterator const &Pkg) {
 		pkgCache::PkgIterator const &Pkg) {
 	if (ShowError == true)
 	if (ShowError == true)
 		_error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
 		_error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
-	return pkgCache::VerIterator(*Cache);
+	return pkgCache::VerIterator(Cache);
 }
 }
 									/*}}}*/
 									/*}}}*/
 // canNotFindInstalledVer						/*{{{*/
 // canNotFindInstalledVer						/*{{{*/
@@ -447,7 +494,7 @@ pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache
 		pkgCache::PkgIterator const &Pkg) {
 		pkgCache::PkgIterator const &Pkg) {
 	if (ShowError == true)
 	if (ShowError == true)
 		_error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
 		_error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
-	return pkgCache::VerIterator(*Cache);
+	return pkgCache::VerIterator(Cache);
 }
 }
 									/*}}}*/
 									/*}}}*/
 }
 }

+ 14 - 1
cmdline/cacheset.h

@@ -40,6 +40,7 @@ public:									/*{{{*/
 	virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
 	virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
 				 string const &ver, bool const &verIsRel) {};
 				 string const &ver, bool const &verIsRel) {};
 
 
+	virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
 	virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
 	virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
 	virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
 	virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
 	virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
 	virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
@@ -146,6 +147,17 @@ public:									/*{{{*/
 		return APT::PackageSet::FromString(Cache, string, helper);
 		return APT::PackageSet::FromString(Cache, string, helper);
 	}
 	}
 
 
+	/** \brief returns a package specified by a string
+
+	    \param Cache the package is in
+	    \param string String the package name should be extracted from
+	    \param out stream to print various notices to */
+	static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
+	static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) {
+		CacheSetHelper helper;
+		return APT::PackageSet::FromName(Cache, string, helper);
+	}
+
 	/** \brief returns all packages specified on the commandline
 	/** \brief returns all packages specified on the commandline
 
 
 	    Get all package names from the commandline and executes regex's if needed.
 	    Get all package names from the commandline and executes regex's if needed.
@@ -268,7 +280,8 @@ public:									/*{{{*/
 	}
 	}
 
 
 	static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
 	static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
-			APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
+			APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
+			bool const &onlyFromName = false);
 	static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
 	static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
 			APT::VersionSet::Version const &fallback) {
 			APT::VersionSet::Version const &fallback) {
 		CacheSetHelper helper;
 		CacheSetHelper helper;

+ 3 - 1
debian/changelog

@@ -6,6 +6,8 @@ apt (0.7.26~exp8) UNRELEASED; urgency=low
       or use the policy which is a bit faster than depcache generation
       or use the policy which is a bit faster than depcache generation
     - get packages by task^ with FromTask()
     - get packages by task^ with FromTask()
     - only print errors if all tries to get a package by string failed
     - only print errors if all tries to get a package by string failed
+    - factor out code to get a single package FromName()
+    - check in Grouped* first without modifier interpretation
   * cmdline/apt-get.cc:
   * cmdline/apt-get.cc:
     - use the cachsets in the install commands
     - use the cachsets in the install commands
   * apt-pkg/orderlist.cc:
   * apt-pkg/orderlist.cc:
@@ -32,7 +34,7 @@ apt (0.7.26~exp8) UNRELEASED; urgency=low
     - add a Dir::Ignore-Files-Silently list option to control the notice
     - add a Dir::Ignore-Files-Silently list option to control the notice
   * 
   * 
 
 
- -- David Kalnischkies <kalnischkies@gmail.com>  Sat, 26 Jun 2010 21:17:08 +0200
+ -- David Kalnischkies <kalnischkies@gmail.com>  Mon, 28 Jun 2010 22:12:24 +0200
 
 
 apt (0.7.26~exp7) experimental; urgency=low
 apt (0.7.26~exp7) experimental; urgency=low