[jhbuild] Fix handling of trailing slashes on command-line modules names



commit 2d12b3abdbc8655554bbfad162abd17621206073
Author: Emanuele Aina <emanuele aina collabora com>
Date:   Fri Feb 22 19:00:47 2013 +0100

    Fix handling of trailing slashes on command-line modules names
    
    It is currently possible to delete the whole checkoutroot by adding
    a trailing slash to a module name passed on the command line:
    
    • Run `jhbuild buildone gnome-desktop/`
    • Watch jhbuild error out as it is unable to find the repo
    • Select 'Wipe directory and start over' (just kidding, *don't*
      select it!)
    • Watch jhbuild delete your whole checkout root.
    
    This happens because the module name is passed to os.path.basename()
    which will return an empty string on trailing slashes.
    
    JHBuild will then try to operate on the base checkout root, failing to
    find any proper repo and promptly deleting all its content if told to
    do so.
    
    This patch does two things to prevent this error to occur:
    
    • Strip all the trailing slashes from module names in
      Branch.get_module_basename()
    • Refuse to accept an empty string for the module basename in
      Branch.get_checkoutdir()
    
    It also tries to strip trailing slashes from module names in a couple of
    other places, trying to keep stuff working even when being passed one.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=694466

 jhbuild/commands/base.py           |    1 +
 jhbuild/moduleset.py               |    1 +
 jhbuild/versioncontrol/__init__.py |    7 +++++--
 jhbuild/versioncontrol/git.py      |    4 +++-
 4 files changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/jhbuild/commands/base.py b/jhbuild/commands/base.py
index 2acccfc..b7dfdbf 100644
--- a/jhbuild/commands/base.py
+++ b/jhbuild/commands/base.py
@@ -322,6 +322,7 @@ class cmd_buildone(BuildCommand):
         module_set = jhbuild.moduleset.load(config)
         module_list = []
         for modname in args:
+            modname = modname.rstrip(os.sep)
             try:
                 module = module_set.get_module(modname, ignore_case=True)
             except KeyError, e:
diff --git a/jhbuild/moduleset.py b/jhbuild/moduleset.py
index f1cb677..3dc3aca 100644
--- a/jhbuild/moduleset.py
+++ b/jhbuild/moduleset.py
@@ -70,6 +70,7 @@ class ModuleSet:
         self.modules[module.name] = module
 
     def get_module(self, module_name, ignore_case = False):
+        module_name = module_name.rstrip(os.sep)
         if self.modules.has_key(module_name) or not ignore_case:
             return self.modules[module_name]
         module_name_lower = module_name.lower()
diff --git a/jhbuild/versioncontrol/__init__.py b/jhbuild/versioncontrol/__init__.py
index 16d5ff1..96b9d28 100644
--- a/jhbuild/versioncontrol/__init__.py
+++ b/jhbuild/versioncontrol/__init__.py
@@ -97,7 +97,9 @@ class Branch:
         raise NotImplementedError
 
     def get_module_basename(self):
-        module = os.path.basename(self.module)
+        # prevent basename() from returning empty strings on trailing '/'
+        module = self.module.rstrip(os.sep)
+        module = os.path.basename(module)
         # prune common filename extensions
         index = module.lower().rfind('.tar')
         if index > 0:
@@ -111,8 +113,9 @@ class Branch:
             return os.path.join(self.config.copy_dir, self.get_module_basename())
         if self.checkoutdir:
             return os.path.join(self.checkoutroot, self.checkoutdir)
-        else:
+        if self.get_module_basename():
             return os.path.join(self.checkoutroot, self.get_module_basename())
+        raise Exception('unable to get a valid checkout directory')
 
     def may_checkout(self, buildscript):
         if buildscript.config.nonetwork:
diff --git a/jhbuild/versioncontrol/git.py b/jhbuild/versioncontrol/git.py
index a93a9bd..3862b5c 100644
--- a/jhbuild/versioncontrol/git.py
+++ b/jhbuild/versioncontrol/git.py
@@ -143,7 +143,9 @@ class GitBranch(Branch):
         self.unmirrored_module = unmirrored_module
 
     def get_module_basename(self):
-        name = os.path.basename(self.module)
+        # prevent basename() from returning empty strings on trailing '/'
+        name = self.module.rstrip(os.sep)
+        name = os.path.basename(name)
         if name.endswith('.git'):
             name = name[:-4]
         return name


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]