summaryrefslogtreecommitdiff
path: root/cpp/src/slice2py/Main.cpp
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2004-09-24 22:59:18 +0000
committerMark Spruiell <mes@zeroc.com>2004-09-24 22:59:18 +0000
commite1478290bf8253da21eea57645ee685db013e200 (patch)
tree2c8643f163eb9801dc6d4ebc8c650f28a18ba085 /cpp/src/slice2py/Main.cpp
parentadding generateUUID (diff)
downloadice-e1478290bf8253da21eea57645ee685db013e200.tar.bz2
ice-e1478290bf8253da21eea57645ee685db013e200.tar.xz
ice-e1478290bf8253da21eea57645ee685db013e200.zip
adding package metadata; misc fixes
Diffstat (limited to 'cpp/src/slice2py/Main.cpp')
-rw-r--r--cpp/src/slice2py/Main.cpp155
1 files changed, 123 insertions, 32 deletions
diff --git a/cpp/src/slice2py/Main.cpp b/cpp/src/slice2py/Main.cpp
index 3706e00bdbe..40e9d4b652e 100644
--- a/cpp/src/slice2py/Main.cpp
+++ b/cpp/src/slice2py/Main.cpp
@@ -66,6 +66,11 @@ private:
static const char* _moduleTag;
static const char* _submoduleTag;
+ bool createDirectory(const string&);
+
+ bool addModule(const string&, const string&);
+ bool addSubmodule(const string&, const string&);
+
bool readInit(const string&, StringList&, StringList&);
bool writeInit(const string&, const StringList&, const StringList&);
@@ -95,71 +100,157 @@ PackageVisitor::visitModuleStart(const ModulePtr& p)
{
assert(!_pathStack.empty());
string name = fixIdent(p->name());
- string path = _pathStack.front() + "/" + name;
+
+ string path;
+ if(_pathStack.size() == 1)
+ {
+ path = _pathStack.front();
+
+ //
+ // Check top-level modules for package metadata and create the package
+ // directories.
+ //
+ string package = getPackageMetadata(p);
+ if(!package.empty())
+ {
+ vector<string> v;
+ if(!splitString(package, v, "."))
+ {
+ return false;
+ }
+ for(vector<string>::iterator q = v.begin(); q != v.end(); ++q)
+ {
+ if(q != v.begin() && !addSubmodule(path, fixIdent(*q)))
+ {
+ return false;
+ }
+
+ path += "/" + *q;
+ if(!createDirectory(path))
+ {
+ return false;
+ }
+
+ if(!addModule(path, _module))
+ {
+ return false;
+ }
+ }
+
+ if(!addSubmodule(path, name))
+ {
+ return false;
+ }
+ }
+
+ path += "/" + name;
+ }
+ else
+ {
+ path = _pathStack.front() + "/" + name;
+ }
+
string parentPath = _pathStack.front();
_pathStack.push_front(path);
+ if(!createDirectory(path))
+ {
+ return false;
+ }
+
+ //
+ // If necessary, add this module to the set of imported modules in __init__.py.
+ //
+ if(!addModule(path, _module))
+ {
+ return false;
+ }
+
+ //
+ // If this is a submodule, then modify the parent's __init__.py to import us.
+ //
+ ModulePtr mod = ModulePtr::dynamicCast(p->container());
+ if(mod && !addSubmodule(parentPath, name))
+ {
+ return false;
+ }
+
+ return true;
+}
+
+void
+PackageVisitor::visitModuleEnd(const ModulePtr& p)
+{
+ assert(!_pathStack.empty());
+ _pathStack.pop_front();
+}
+
+bool
+PackageVisitor::createDirectory(const string& dir)
+{
struct stat st;
int result;
- result = stat(path.c_str(), &st);
+ result = stat(dir.c_str(), &st);
if(result != 0)
{
#ifdef _WIN32
- result = _mkdir(path.c_str());
+ result = _mkdir(dir.c_str());
#else
- result = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
+ result = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
if(result != 0)
{
- cerr << _name << ": unable to create `" << path << "': " << strerror(errno) << endl;
+ cerr << _name << ": unable to create `" << dir << "': " << strerror(errno) << endl;
return false;
}
}
+ return true;
+}
+
+bool
+PackageVisitor::addModule(const string& dir, const string& name)
+{
//
- // If necessary, add this file to the set of imported modules in __init__.py.
+ // Add a module to the set of imported modules in __init__.py.
//
StringList modules, submodules;
- if(readInit(path, modules, submodules))
+ if(readInit(dir, modules, submodules))
{
- StringList::iterator p;
- p = find(modules.begin(), modules.end(), _module);
+ StringList::iterator p = find(modules.begin(), modules.end(), name);
if(p == modules.end())
{
- modules.push_back(_module);
- writeInit(path, modules, submodules);
+ modules.push_back(name);
+ return writeInit(dir, modules, submodules);
}
+
+ return true;
}
+ return false;
+}
+
+bool
+PackageVisitor::addSubmodule(const string& dir, const string& name)
+{
//
- // If this is a submodule, then modify the parent's __init__.py to import us.
+ // Add a submodule to the set of imported modules in __init__.py.
//
- ModulePtr mod = ModulePtr::dynamicCast(p->container());
- if(mod)
+ StringList modules, submodules;
+ if(readInit(dir, modules, submodules))
{
- modules.clear();
- submodules.clear();
- if(readInit(parentPath, modules, submodules))
+ StringList::iterator p = find(submodules.begin(), submodules.end(), name);
+ if(p == submodules.end())
{
- StringList::iterator p;
- p = find(submodules.begin(), submodules.end(), name);
- if(p == submodules.end())
- {
- submodules.push_back(name);
- writeInit(parentPath, modules, submodules);
- }
+ submodules.push_back(name);
+ return writeInit(dir, modules, submodules);
}
- }
- return true;
-}
+ return true;
+ }
-void
-PackageVisitor::visitModuleEnd(const ModulePtr& p)
-{
- assert(!_pathStack.empty());
- _pathStack.pop_front();
+ return false;
}
bool