Пример #1
0
    def test_06_create_existing_dir_with_warn_True_raises_OSError(self):
        """
        Make sure an OSError is raised if the directory already exists and warn is set to True.
        """
        os.makedirs(self.test_dirname)
        exception = None

        try:
            create_dir(self.test_dirname, DEFAULT_PERMS, DEFAULT_USER, DEFAULT_GROUP, True)
        except Exception as inst:
            exception = inst

        self.assertTrue(type(exception) == OSError)
        self.assertEquals(errno.EISDIR, exception.errno)
Пример #2
0
    def test_01_create_dir_with_all_path_components_in_path_not_existing(self):
        """
        Test creating a directory where all components in path don't exist.
        """
        n_path_components = 5
    
        basedir = self.test_dirname

        for i in range(0,n_path_components):
            basedir = os.path.join(basedir, "dir%d" % i)

        create_dir(basedir, DEFAULT_PERMS, DEFAULT_USER, DEFAULT_GROUP)

        self.assertEquals(True, os.path.exists(basedir)) 
Пример #3
0
    def test_02_create_existing_dir(self):
        """
        Make sure a directory is created even though some components in the path do exist
        """
        n_path_components = 5

        basedir = self.test_dirname

        for i in range(0,n_path_components):
            basedir = os.path.join(basedir, "dir%d" % i)

        os.makedirs(basedir)

        create_dir(basedir, DEFAULT_PERMS, DEFAULT_USER, DEFAULT_GROUP)

        self.assertEquals(True, os.path.exists(basedir)) 
Пример #4
0
    def test_07_create_dir_with_existing_filename_and_warn_True_raises_OSError(self):
        """
        Make sure an OSError is raised if there is already a file with the same and warn
        is set to True.
        """
        tmp_file = NamedTemporaryFile(delete = False)
        exception = None

        try:
            create_dir(tmp_file.name, DEFAULT_PERMS, DEFAULT_USER, DEFAULT_GROUP, True)
        except Exception as inst:
            exception = inst
        finally:
            if tmp_file:
                os.unlink(tmp_file.name)

        self.assertTrue(type(exception) == OSError)
        self.assertEquals(errno.EEXIST, exception.errno)
Пример #5
0
  def startElement(self, name, attrs):
    """
    When an XML element is first read, this function is run
    to process it's attributes and content before moving on
    to it's contents and then endElement
    """
    
    # set the current directory
    self.current_dir = os.path.abspath(".")
    
    # get the basename attribute or None
    basename = attrs.get("basename", None)
    
    # get the permissions and ownership
    perms, uid, gid = self._return_perms_uid_gid(attrs)
    
    # get the directory name attribute or None
    self.dirname = attrs.get("dirname", None)
    
    # if xml elementname is dirtt let's get started
    if name == 'dirtt':
      self.logger.debug("Starting Directory Tree Template Build...")
      self.logger.debug("Changing current directory to: %s" % self.dirname)
      # change to our starting directory
      if not basename:
        self.dirname, basename = os.path.split(self.dirname)
      os.chdir(self.dirname)
      self.current_dir = os.path.abspath(".")
      self.idrefs[attrs.get("id", "root-dir")] = self.current_dir

    if basename:
      if self.skip_entity: self.skip_entity += 1
      
      # if the entity is our main dirtt entity or a directory proceed here
      if name in ('dirtt','dir'):
        dirname = attrs.get("dirname", None)
        if self.interactive:
          if not self.skip_entity:
            if not raw_input("Create Directory %s (yes/no)?" % os.path.join(self.current_dir,basename)) in ("yes","Yes","YES","Y","y"):
              self.skip_entity += 1
              self.logger.debug("Skipping dir: %s" % os.path.join(self.current_dir,basename))
            else:
              self.logger.debug("Creating dir: %s/%s (perms:%s uid:%i gid:%i)" % (self.current_dir, basename, oct(perms), uid, gid))
              if dirname:
                if name == 'dirtt':
                    # When dealding with a 'dirtt' tag use self.dirname as the current dirname
                    # as at this point self.dirname has been properly set (i.e if no basename was
                    # provided then the value for the basename it's inferred from dirname)
                    dirname = self.dirname
                newdir = os.path.join(dirname,basename)
              else:
                newdir = basename

            try:
                create_dir(newdir, perms, uid, gid, self.warn)
            except OSError as oserror:
                if oserror.errno == errno.EISDIR:
                    print >> sys.stderr, "A directory exists with that name ('%s'). \
                     \nAborting directory creation." % basename
                    sys.exit(-1)
                elif oserror.errno == errno.EISDIR:
                    print >> sys.stderr, "A file exists with the name of the desired dir ('%s'). \
                     \nAborting directory creation." % basename
                    sys.exit(-2)

            self._push_dir()
            os.chdir(basename)
            self.current_dir = os.path.abspath(".")

        else:
          self.logger.debug("Creating dir: %s/%s (perms:%s uid:%i gid:%i)" % (self.current_dir, basename, oct(perms), uid, gid))
          if dirname:
            if name == 'dirtt':
                # When dealding with a 'dirtt' tag use self.dirname as the current dirname
                # as at this point self.dirname has been properly set (i.e if no basename was
                # provided then the value for the basename it's inferred from dirname)
                dirname = self.dirname
            newdir = os.path.join(dirname,basename)
          else:
            newdir = basename
          
          try:
            create_dir(newdir, perms, uid, gid, self.warn)
          except OSError as oserror:
            if oserror.errno == errno.EISDIR:
                print >> sys.stderr, "A directory exists with that name ('%s'). \
                    \nAborting directory creation." % basename
                sys.exit(-1)
            elif oserror.errno == errno.EISDIR:
                print >> sys.stderr, "A file exists with the name of the desired dir ('%s'). \
                    \nAborting directory creation." % basename
                sys.exit(-2)
                
          self._push_dir()
          os.chdir(newdir)
          self.current_dir = os.path.abspath(".")

        if attrs.get("id"):
          self.idrefs[attrs.get("id")] = self.current_dir

      if name == 'file':
        self.logger.debug("Creating file: %s/%s (perms:%s uid:%i gid:%i)" % (self.current_dir, basename, oct(perms), uid, gid))
        href = attrs.get("href",None)
        content = ""
        if not href is None:
          template_file = os.path.join(TEMPLATES_DIR,href)
          template_str = self._read_template(template_file)
          content = self._parse_template(template_str, template_file)
        create_file(basename, content, perms, uid, gid)

    else:
      if name in ('dirtt', 'dir'):
        if not self.skip_entity:
          self.skip_entity += 1

    if name == 'link':
      try:
        if (not attrs.get("idref", attrs.get("ref", None))) or (not attrs.get("basename", None)):
           return
        ref = attrs.get("idref", attrs.get("ref"))
        link_name = attrs.get("basename")
        if ref == attrs.get("idref", None):
          ref = self.idrefs[ref]
        target_dir = attrs.get("dirname",self.current_dir)
        self.links.append({'basename': link_name, 'parent_dir': target_dir, 'ref': ref})
      except:
        pass

    if name == 'xi:include':
      href = attrs.get("href")
      # Check for an HTTP url or an absolute file location
      if href[0:7] in ('http://'):
        template_loc = href
      elif href[0:8] in ('file:///'):
        template_loc = href
      else:
        template_loc = os.path.join(self.tree_template_loc,href)
      c = DirectoryTreeHandler(self.verbose, template_loc, self.kwargs, self.interactive, self.warn, self.processed_templates)
      c.run()
    return