Example #1
0
def install_dir(source_dir, target_dir):
    for root, dirs, files in walk(source_dir):
        for filename in files:
            s = np(join(source_dir, filename))
            t = np(join(target_dir, 'local-%s'%filename))

            if exists(t):                
                print "%8s %s" % ("[exists]", t)
                if not islink(t):
                    print "%s%s" % (" "*9, "Cannot override the file at this location.")
            else:
                # TODO: it would be much nicer we we could create
                # a proper relative link. It should be able to handle
                # two arbitrary paths though.
                ln(np(abspath(s)), t)
                print "%8s %s" % ("[mklink]", t)
Example #2
0
    def __init__(self,
                 content_path=alsangue_path,
                 build_path=join(alsangue_path, "build")):

        alsangue_path = dirname(realpath(__file__))

        self.build_path = realpath(build_path)
        self.content_path = realpath(content_path)

        self.config = dict_from_file(join(content_path, "config"))

        locales = [
            realpath(join(alsangue_path, "locales", l))
            for l in ls(join(alsangue_path, "locales"))
        ]
        self.locales = [dict_from_file(l) for l in locales]
        self.build_tree()

        self.sitemap = Sitemap(build_path)

        self.templates_path = join(content_path, "templates")

        self.articles = [
            realpath(join(content_path, "articles", a))
            for a in ls(join(content_path, "articles"))
        ]
        self.authors = [
            realpath(join(content_path, "authors", a))
            for a in ls(join(content_path, "authors"))
        ]

        for a in self.articles:
            self.build_article(a)
        for a in self.authors:
            self.build_author_page(a)
            self.build_archive(a)

        for l in self.locales:
            try:
                ln(
                    join(self.build_path, l['code'],
                         self.config['homepage'] + ".html"),
                    join(self.build_path, l['code'], "index.html"))
            except FileExistsError as e:
                rm(join(self.build_path, l['code'], "index.html"))
                ln(
                    join(self.build_path, l['code'],
                         self.config['homepage'] + ".html"),
                    join(self.build_path, l['code'], "index.html"))
            chosen = dict_from_file(
                join(alsangue_path, "locales", self.config['locale']))
            if l['code'] == chosen['code']:
                try:
                    ln(
                        join(self.build_path, chosen['code'],
                             self.config['homepage'] + ".html"),
                        join(self.build_path, "index.html"))
                except FileExistsError as e:
                    rm(join(self.build_path, "index.html"))
                    ln(
                        join(self.build_path, chosen['code'],
                             self.config['homepage'] + ".html"),
                        join(self.build_path, "index.html"))

        self.sitemap.save()
Example #3
0
def copyfile(src, dest=None, nothrow=None, symlink=False, aslink=False, nocopyempty=False):
  """ Copy ``src`` file onto ``dest`` directory or file.

      :param src:
          Source file.
      :param dest: 
          Destination file or directory.
      :param nothrow:
          Throwing is disable selectively depending on the content of nothrow:

            - exists: will not throw is src does not exist.
            - isfile: will not throw is src is not a file.
            - same: will not throw if src and dest are the same.
            - none: ``src`` can be None.
            - null: ``src`` can be '/dev/null'.
            - never: will never throw.

      :param symlink:
          Creates link rather than actual hard-copy. Symlink are
          created with relative paths given starting from the directory of
          ``dest``.  Defaults to False.
      :param aslink: 
          Creates link rather than actual hard-copy *if* ``src`` is
          itself a link. Links to the file which ``src`` points to, not to
          ``src`` itself. Defaults to False.
      :parma nocopyempty:
          Does not perform copy if file is empty. Defaults to False.

      This function fails selectively, depending on what is in ``nothrow`` list.
  """
  try:
    from os import getcwd, symlink as ln, remove
    from os.path import isdir, isfile, samefile, exists, basename, dirname,\
                        join, islink, realpath, relpath, getsize, abspath
    # sets up nothrow options.
    if nothrow is None: nothrow = []
    if isinstance(nothrow, str): nothrow = nothrow.split()
    if nothrow == 'all': nothrow = 'exists', 'same', 'isfile', 'none', 'null'
    nothrow = [u.lower() for u in nothrow]
    # checks and normalizes input.
    if src is None: 
      if 'none' in nothrow: return False
      raise IOError("Source is None.")
    if dest is None: dest = getcwd()
    if dest == '/dev/null': return True
    if src  == '/dev/null':
      if 'null' in nothrow: return False
      raise IOError("Source is '/dev/null' but Destination is {0}.".format(dest))

    # checks that input source file exists.
    if not exists(src): 
      if 'exists' in nothrow: return False
      raise IOError("{0} does not exist.".format(src))
    src = abspath(realpath(src))
    if not isfile(src):
      if 'isfile' in nothrow: return False
      raise IOError("{0} is not a file.".format(src))
    # makes destination a file.
    if exists(dest) and isdir(dest): dest = join(dest, basename(src))
    # checks if destination file and source file are the same.
    if exists(dest) and samefile(src, dest): 
      if 'same' in nothrow: return False
      raise IOError("{0} and {1} are the same file.".format(src, dest))
    if nocopyempty and isfile(src):
      if getsize(src) == 0: return
    if aslink and islink(src): symlink, src = True, realpath(src)
    if symlink:
      if exists(dest): remove(dest)
      src = realpath(abspath(src))
      dest = realpath(abspath(dest))
      if relpath(src, dirname(dest)).count("../") == relpath(src, '/').count("../"):
        ln(src, realpath(dest))
      else:
        with Changedir(dirname(dest)) as cwd:
           ln(relpath(src, dirname(dest)), basename(dest))
    else: _copyfile_impl(src, dest)
  except:
    if 'never' in nothrow: return False
    raise
  else: return True
Example #4
0
def copyfile(src,
             dest=None,
             nothrow=None,
             symlink=False,
             aslink=False,
             nocopyempty=False):
    """ Copy ``src`` file onto ``dest`` directory or file.

        :param src:
            Source file.
        :param dest: 
            Destination file or directory.
        :param nothrow:
            Throwing is disable selectively depending on the content of nothrow:

              - exists: will not throw is src does not exist.
              - isfile: will not throw is src is not a file.
              - same: will not throw if src and dest are the same.
              - none: ``src`` can be None.
              - null: ``src`` can be '/dev/null'.
              - never: will never throw.

        :param symlink:
            Creates link rather than actual hard-copy. Symlink are
            created with relative paths given starting from the directory of
            ``dest``.  Defaults to False.
        :param aslink: 
            Creates link rather than actual hard-copy *if* ``src`` is
            itself a link. Links to the file which ``src`` points to, not to
            ``src`` itself. Defaults to False.
        :parma nocopyempty:
            Does not perform copy if file is empty. Defaults to False.

        This function fails selectively, depending on what is in ``nothrow`` list.
    """
    try:
        from os import getcwd, symlink as ln, remove
        from os.path import isdir, isfile, samefile, exists, basename, dirname,\
            join, islink, realpath, relpath, getsize, abspath
        # sets up nothrow options.
        if nothrow is None:
            nothrow = []
        if isinstance(nothrow, str):
            nothrow = nothrow.split()
        if nothrow == 'all':
            nothrow = 'exists', 'same', 'isfile', 'none', 'null'
        nothrow = [u.lower() for u in nothrow]
        # checks and normalizes input.
        if src is None:
            if 'none' in nothrow:
                return False
            raise IOError("Source is None.")
        if dest is None:
            dest = getcwd()
        if dest == '/dev/null':
            return True
        if src == '/dev/null':
            if 'null' in nothrow:
                return False
            raise IOError(
                "Source is '/dev/null' but Destination is {0}.".format(dest))

        # checks that input source file exists.
        if not exists(src):
            if 'exists' in nothrow:
                return False
            raise IOError("{0} does not exist.".format(src))
        src = abspath(realpath(src))
        if not isfile(src):
            if 'isfile' in nothrow:
                return False
            raise IOError("{0} is not a file.".format(src))
        # makes destination a file.
        if exists(dest) and isdir(dest):
            dest = join(dest, basename(src))
        # checks if destination file and source file are the same.
        if exists(dest) and samefile(src, dest):
            if 'same' in nothrow:
                return False
            raise IOError("{0} and {1} are the same file.".format(src, dest))
        if nocopyempty and isfile(src):
            if getsize(src) == 0:
                return
        if aslink and islink(src):
            symlink, src = True, realpath(src)
        if symlink:
            if exists(dest):
                remove(dest)
            src = realpath(abspath(src))
            dest = realpath(abspath(dest))
            if relpath(src, dirname(dest)).count("../") == relpath(
                    src, '/').count("../"):
                ln(src, realpath(dest))
            else:
                with chdir(local_path(dest).dirname):
                    ln(relpath(src, dirname(dest)), basename(dest))
        else:
            _copyfile_impl(src, dest)
    except:
        if 'never' in nothrow:
            return False
        raise
    else:
        return True