# -*- coding: utf-8 -*- # lp4all: literate programming embedded in source code as wiki comments # Copyright (C) 2006 Jean-Marie Favreau, Frédéric Lehobey, David Mentré # and Thomas Petazzoni # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os import re import htmlbasicout
def getdirs(currentdir, files): first_tree = {} result_tree = {}
Using file in filelist (containts in currentdir), find directories
in currentdir and add it to first_tree. All files containts in
current dir are added to result_tree.
for file in files: file = os.path.normpath(file) filec = file.split('/', 1) if os.path.isdir(os.path.join(currentdir, filec[0])): if first_tree.has_key(filec[0]): first_tree[filec[0]].append(filec[1]) else: first_tree[filec[0]] = [filec[1]] if os.path.isfile(os.path.join(currentdir, filec[0])): result_tree[filec[0]] = []
For each directorie containts in current dir, use getdirs
recurcively.
for dir, subdirs in first_tree.iteritems(): result_tree[dir] = getdirs(os.path.join(currentdir, dir), subdirs)
if result_treeis empty, currentdir containts neither subdirectory or
file (not possible).
if len(result_tree) == 0: result_tree['.'] = True return result_tree
given input dir tree (build by [[#getdirs|getdirs]]), build .lp4all
file list.
def findLp4allFiles(currentdir, inputdirs): file_list = []
for each file in currentdir, add it to result file_list
if file name match with .*\.lp4all, removing .lp4all
itemlist = os.listdir(currentdir) for item in itemlist: if re.compile(".+\.lp4all$").match(item): file_list.append(os.path.normpath(os.path.join(currentdir, item[:-7]))) for inputdir, subdirs in inputdirs.iteritems(): realdir = os.path.join(currentdir, inputdir)
if inputdir is a directory (not a file), process
if len(subdirs) != 0:
recursive call on children
file_list += findLp4allFiles(realdir, subdirs) return file_list def mkdirs(targetdir, subdirs): if not os.path.exists(targetdir): os.makedirs(targetdir) for dir, subsubdirs in subdirs.iteritems(): if len(subsubdirs) != 0: mkdirs(os.path.join(targetdir, dir), subsubdirs)
Given the input file list, we build an input dir tree. To do so, we build a list of all filenames in which each filename is itself a list of each path element or file.