mirror of https://github.com/KLayout/klayout.git
WIP: grains collections for salt package manager.
This commit is contained in:
parent
b98afd5a8d
commit
59dd36f692
|
|
@ -105,23 +105,33 @@ SaltGrains::is_empty () const
|
||||||
}
|
}
|
||||||
|
|
||||||
SaltGrains
|
SaltGrains
|
||||||
SaltGrains::from_path (const std::string &path)
|
SaltGrains::from_path (const std::string &path, const std::string &prefix)
|
||||||
{
|
{
|
||||||
SaltGrains grains;
|
SaltGrains grains;
|
||||||
|
grains.set_path (path);
|
||||||
|
|
||||||
QDir dir (tl::to_qstring (path));
|
QDir dir (tl::to_qstring (path));
|
||||||
QStringList entries = dir.entryList (QDir::NoFilter, QDir::Name);
|
QStringList entries = dir.entryList (QDir::NoDotAndDotDot | QDir::Dirs, QDir::Name);
|
||||||
for (QStringList::const_iterator e = entries.begin (); e != entries.end (); ++e) {
|
for (QStringList::const_iterator e = entries.begin (); e != entries.end (); ++e) {
|
||||||
|
|
||||||
|
std::string new_prefix = prefix;
|
||||||
|
if (! new_prefix.empty ()) {
|
||||||
|
new_prefix += "/";
|
||||||
|
}
|
||||||
|
new_prefix += tl::to_string (*e);
|
||||||
|
|
||||||
std::string epath = tl::to_string (dir.absoluteFilePath (*e));
|
std::string epath = tl::to_string (dir.absoluteFilePath (*e));
|
||||||
if (SaltGrain::is_grain (epath)) {
|
if (SaltGrain::is_grain (epath)) {
|
||||||
try {
|
try {
|
||||||
grains.add_grain (SaltGrain::from_path (epath));
|
SaltGrain g (SaltGrain::from_path (epath));
|
||||||
|
g.set_name (new_prefix);
|
||||||
|
grains.add_grain (g);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
// ignore errors (TODO: what to do here?)
|
// ignore errors (TODO: what to do here?)
|
||||||
}
|
}
|
||||||
} else if (QFileInfo (tl::to_qstring (epath)).isDir ()) {
|
} else if (QFileInfo (tl::to_qstring (epath)).isDir ()) {
|
||||||
SaltGrains c = SaltGrains::from_path (epath);
|
SaltGrains c = SaltGrains::from_path (epath, new_prefix);
|
||||||
|
c.set_name (new_prefix);
|
||||||
if (! c.is_empty ()) {
|
if (! c.is_empty ()) {
|
||||||
grains.add_collection (c);
|
grains.add_collection (c);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ public:
|
||||||
*/
|
*/
|
||||||
collection_iterator end_collections () const
|
collection_iterator end_collections () const
|
||||||
{
|
{
|
||||||
return m_collections.begin ();
|
return m_collections.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -146,7 +146,7 @@ public:
|
||||||
*/
|
*/
|
||||||
grain_iterator end_grains () const
|
grain_iterator end_grains () const
|
||||||
{
|
{
|
||||||
return m_grains.begin ();
|
return m_grains.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -170,7 +170,7 @@ public:
|
||||||
* the grains from this path.
|
* the grains from this path.
|
||||||
* Sub-collections are created from folders which contain grains or sub-collections.
|
* Sub-collections are created from folders which contain grains or sub-collections.
|
||||||
*/
|
*/
|
||||||
static SaltGrains from_path (const std::string &path);
|
static SaltGrains from_path (const std::string &path, const std::string &pfx = std::string ());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include "laySaltGrain.h"
|
#include "laySaltGrain.h"
|
||||||
|
#include "laySaltGrains.h"
|
||||||
#include "utHead.h"
|
#include "utHead.h"
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
|
@ -107,3 +108,101 @@ TEST (2)
|
||||||
EXPECT_EQ (lay::SaltGrain::compare_versions ("990", "990"), 0);
|
EXPECT_EQ (lay::SaltGrain::compare_versions ("990", "990"), 0);
|
||||||
EXPECT_EQ (lay::SaltGrain::compare_versions ("991", "990"), 1);
|
EXPECT_EQ (lay::SaltGrain::compare_versions ("991", "990"), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static std::string grains_to_string (const lay::SaltGrains &gg)
|
||||||
|
{
|
||||||
|
std::string res;
|
||||||
|
res += "[";
|
||||||
|
bool first = true;
|
||||||
|
for (lay::SaltGrains::grain_iterator g = gg.begin_grains (); g != gg.end_grains (); ++g) {
|
||||||
|
if (! first) {
|
||||||
|
res += ",";
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
res += g->name ();
|
||||||
|
}
|
||||||
|
for (lay::SaltGrains::collection_iterator gc = gg.begin_collections (); gc != gg.end_collections (); ++gc) {
|
||||||
|
if (! first) {
|
||||||
|
res += ",";
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
res += gc->name ();
|
||||||
|
res += grains_to_string (*gc);
|
||||||
|
}
|
||||||
|
res += "]";
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool empty_dir (const QString &path)
|
||||||
|
{
|
||||||
|
QDir dir (path);
|
||||||
|
QStringList entries = dir.entryList (QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
|
||||||
|
for (QStringList::const_iterator e = entries.begin (); e != entries.end (); ++e) {
|
||||||
|
QFileInfo fi (dir.absoluteFilePath (*e));
|
||||||
|
if (fi.isDir ()) {
|
||||||
|
if (! empty_dir (fi.filePath ())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (! dir.rmdir (*e)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (fi.isFile ()) {
|
||||||
|
if (! dir.remove (*e)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (3)
|
||||||
|
{
|
||||||
|
const QString grain_spec_file = QString::fromUtf8 ("grain.xml");
|
||||||
|
|
||||||
|
lay::SaltGrain g;
|
||||||
|
g.set_name ("x");
|
||||||
|
|
||||||
|
QDir tmp_dir (QFileInfo (tl::to_qstring (tmp_file ())).absolutePath ());
|
||||||
|
QDir dir_a (tmp_dir.filePath (QString::fromUtf8 ("a")));
|
||||||
|
QDir dir_b (tmp_dir.filePath (QString::fromUtf8 ("b")));
|
||||||
|
QDir dir_c (tmp_dir.filePath (QString::fromUtf8 ("c")));
|
||||||
|
QDir dir_cu (dir_c.filePath (QString::fromUtf8 ("u")));
|
||||||
|
QDir dir_cc (dir_c.filePath (QString::fromUtf8 ("c")));
|
||||||
|
QDir dir_ccv (dir_cc.filePath (QString::fromUtf8 ("v")));
|
||||||
|
|
||||||
|
tl_assert (empty_dir (tmp_dir.path ()));
|
||||||
|
|
||||||
|
lay::SaltGrains gg;
|
||||||
|
gg = lay::SaltGrains::from_path (tl::to_string (tmp_dir.path ()));
|
||||||
|
EXPECT_EQ (gg.is_empty (), true);
|
||||||
|
EXPECT_EQ (grains_to_string (gg), "[]");
|
||||||
|
|
||||||
|
tmp_dir.mkdir (dir_a.dirName ());
|
||||||
|
tmp_dir.mkdir (dir_b.dirName ());
|
||||||
|
tmp_dir.mkdir (dir_c.dirName ());
|
||||||
|
dir_c.mkdir (dir_cu.dirName ());
|
||||||
|
dir_c.mkdir (dir_cc.dirName ());
|
||||||
|
dir_cc.mkdir (dir_ccv.dirName ());
|
||||||
|
|
||||||
|
gg = lay::SaltGrains::from_path (tl::to_string (tmp_dir.path ()));
|
||||||
|
EXPECT_EQ (gg.is_empty (), true);
|
||||||
|
EXPECT_EQ (grains_to_string (gg), "[]");
|
||||||
|
EXPECT_EQ (gg.path (), tl::to_string (tmp_dir.path ()));
|
||||||
|
|
||||||
|
g.save (tl::to_string (dir_a.absoluteFilePath (grain_spec_file)));
|
||||||
|
|
||||||
|
gg = lay::SaltGrains::from_path (tl::to_string (tmp_dir.path ()));
|
||||||
|
EXPECT_EQ (gg.is_empty (), false);
|
||||||
|
EXPECT_EQ (grains_to_string (gg), "[a]");
|
||||||
|
EXPECT_EQ (gg.begin_grains ()->path (), tl::to_string (dir_a.absolutePath ()));
|
||||||
|
|
||||||
|
g.save (tl::to_string (dir_b.absoluteFilePath (grain_spec_file)));
|
||||||
|
g.save (tl::to_string (dir_cu.absoluteFilePath (grain_spec_file)));
|
||||||
|
g.save (tl::to_string (dir_ccv.absoluteFilePath (grain_spec_file)));
|
||||||
|
|
||||||
|
gg = lay::SaltGrains::from_path (tl::to_string (tmp_dir.path ()));
|
||||||
|
EXPECT_EQ (gg.is_empty (), false);
|
||||||
|
EXPECT_EQ (grains_to_string (gg), "[a,b,c[c/u,c/c[c/c/v]]]");
|
||||||
|
EXPECT_EQ (gg.begin_collections ()->path (), tl::to_string (dir_c.absolutePath ()));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue