コード例 #1
0
    def run(self):
        sections = self.sections()
        # @@ how much of this is shared with merge.py?
        for section in sections:
            cfg = self.make_cfg(section)
            #@@ use logging
            print("Extracting docs for %s" % section)
            sourcedir = cfg['root']
            outdir = cfg['output']
            # gather data for each source file
            files = {}
            for root, dirs, entries in os.walk(sourcedir):
                for filename in entries:
                    if filename.endswith(
                            SUFFIX_JS) and not filename.startswith("."):
                        filepath = os.path.join(root,
                                                filename)[len(sourcedir) + 1:]
                        jsfile = SourceFile.from_filename(
                            os.path.join(sourcedir, filepath), cfg)
                        if jsfile.data:
                            files[filepath] = jsfile

            # create dict of dependencies
            dependencies = {}
            for filepath, jsfile in files.items():
                dependencies[filepath] = jsfile.extends

            # extend data with any data from parents
            for filepath in tsort.sort(dependencies):
                jsfile = files[filepath]
                if jsfile.extends:
                    jsfile.inherit([
                        files[parentpath]
                        for parentpath in sorted(jsfile.extends, reverse=True)
                        if parentpath in files
                    ])

                # parse template for each file
                template_filename = os.path.join(
                    sourcedir,
                    filepath.split(SUFFIX_JS)[0] + SUFFIX_JST)
                if not os.path.exists(template_filename):
                    # throw something if template not given in config here
                    template_filename = cfg["template"]
                template = Template(open(template_filename, "U").read())
                out = template.render(jsfile.data)
                output_filename = os.path.join(
                    outdir,
                    filepath.split(SUFFIX_JS)[0] + SUFFIX_RST)
                output_dirname = os.path.dirname(output_filename)
                if not os.path.exists(output_dirname):
                    os.makedirs(output_dirname)
                f = open(output_filename, "w")
                f.write(out)
                f.close()
コード例 #2
0
ファイル: jst.py プロジェクト: GunioRobot/jstools
 def run(self):
     sections = self.sections()
     # @@ how much of this is shared with merge.py?
     for section in sections:
         cfg = self.make_cfg(section)
         #@@ use logging
         print("Extracting docs for %s" % section)
         sourcedir = cfg['root']
         outdir = cfg['output']
         # gather data for each source file
         files = {}
         for root, dirs, entries in os.walk(sourcedir):
             for filename in entries:
                 if filename.endswith(SUFFIX_JS) and not filename.startswith("."):
                     filepath = os.path.join(root, filename)[len(sourcedir)+1:]
                     jsfile = SourceFile.from_filename(
                         os.path.join(sourcedir, filepath),
                         cfg)
                     if jsfile.data:
                         files[filepath] = jsfile
     
         # create dict of dependencies
         dependencies = {}
         for filepath, jsfile in files.items():
             dependencies[filepath] = jsfile.extends
         
         # extend data with any data from parents
         for filepath in tsort.sort(dependencies):
             jsfile = files[filepath]
             if jsfile.extends:
                 jsfile.inherit([files[parentpath] for parentpath in sorted(jsfile.extends, reverse=True) if parentpath in files])
                 
             # parse template for each file
             template_filename = os.path.join(sourcedir, filepath.split(SUFFIX_JS)[0] + SUFFIX_JST)
             if not os.path.exists(template_filename):
                 # throw something if template not given in config here
                 template_filename = cfg["template"]
             template = Template(open(template_filename, "U").read())
             out = template.render(jsfile.data)
             output_filename = os.path.join(outdir, filepath.split(SUFFIX_JS)[0] + SUFFIX_RST)
             output_dirname = os.path.dirname(output_filename)
             if not os.path.exists(output_dirname):
                 os.makedirs(output_dirname)
             f = open(output_filename, "w")
             f.write(out)
             f.close()
コード例 #3
0
ファイル: merge.py プロジェクト: bbinet/jstools
    def extract_deps(self, cfg, depmap=None):
        #@@ this function needs to be decomposed into smaller testable bits
        sourcedirs = cfg['root']

        # assemble all files in source directory according to config
        include = cfg.get('include', tuple())
        exclude = [Exclude(e) for e in cfg['exclude']]
        all_inc = cfg['first'] + cfg['include'] + cfg['last']
        files = {}
        implicit = False
        if not len(include):            
            # implicit file inclusion
            implicit = True

        for sourcedir in sourcedirs:
            newfiles = []
            for filepath in jsfiles_for_dir(os.path.join(self.root_dir, sourcedir)):
                fitem = filepath, srcfile = filepath, self.make_sourcefile(sourcedir, filepath, exclude),
                if implicit and not filepath in exclude:
                    all_inc.append(filepath)
                newfiles.append(fitem)

            newfiles = list((filepath, sf) for filepath, sf in newfiles if filepath in all_inc and filepath not in exclude)
            files.update(dict(newfiles))

        # ensure all @include and @requires references are in
        complete = False
        while not complete:
            complete = True
            for filepath, info in files.items():
                for path in info.include + info.requires:
                    if path not in exclude and not files.has_key(path):
                        complete = False
                        for sourcedir in sourcedirs:
                            if os.path.exists(os.path.join(self.root_dir, sourcedir, path)):
                                files[path] = self.make_sourcefile(sourcedir, path, exclude)
                                break
                        else:
                            raise MissingImport("File '%s' not found in root directories" % path)
        
        # create list of dependencies
        dependencies = {}
        for filepath, info in files.items():
            dependencies[filepath] = info.requires

        
        # get tuple of files ordered by dependency
        self.printer.debug("Sorting dependencies.")
        order = [x for x in tsort.sort(dependencies)]

        # move forced first and last files to the required position
        self.printer.debug("Re-ordering files.")
        order = cfg['first'] + [item
                     for item in order
                     if ((item not in cfg['first']) and
                         (item not in cfg['last']))] + cfg['last']

        parts = ('first', 'include', 'last')
        required_files = []
        
        ## Make sure all imports are in files dictionary
        for part in parts:
            for fp in cfg[part]:
                if not fp in exclude and not files.has_key(fp):
                    raise MissingImport("File from '%s' not found: %s" % (part, fp))

        ## Output the files in the determined order
        result = []
        for fp in order:
            result.append(files[fp])

        return result
コード例 #4
0
ファイル: merge.py プロジェクト: GunioRobot/jstools
    def merge(self, cfg, depmap=None):
        #@@ this function needs to be decomposed into smaller testable bits
        sourcedirs = cfg['root']

        # assemble all files in source directory according to config
        include = cfg.get('include', tuple())
        exclude = cfg['exclude']
        all_inc = cfg['first'] + cfg['include'] + cfg['last']
        files = {}
        implicit = False
        if not len(include):
            # implicit file inclusion
            implicit = True

        for sourcedir in sourcedirs:
            newfiles = []
            for filepath in jsfiles_for_dir(sourcedir):
                fitem = filepath, srcfile = filepath, self.make_sourcefile(
                    sourcedir, filepath, exclude),
                if implicit and not filepath in exclude:
                    all_inc.append(filepath)
                newfiles.append(fitem)

            newfiles = list((filepath, sf) for filepath, sf in newfiles
                            if filepath in all_inc and filepath not in exclude)
            files.update(dict(newfiles))

        # ensure all @include and @requires references are in
        complete = False
        while not complete:
            complete = True
            for filepath, info in files.items():
                for path in info.include + info.requires:
                    if path not in cfg['exclude'] and not files.has_key(path):
                        complete = False
                        for sourcedir in sourcedirs:
                            if os.path.exists(os.path.join(sourcedir, path)):
                                files[path] = self.make_sourcefile(
                                    sourcedir, path, exclude)
                                break
                        else:
                            raise MissingImport(
                                "File '%s' not found in root directories" %
                                path)

        # create list of dependencies
        dependencies = {}
        for filepath, info in files.items():
            dependencies[filepath] = info.requires

        # get tuple of files ordered by dependency
        self.printer("Sorting dependencies.")
        order = [x for x in tsort.sort(dependencies)]

        # move forced first and last files to the required position
        self.printer("Re-ordering files.")
        order = cfg['first'] + [
            item for item in order if (
                (item not in cfg['first']) and (item not in cfg['last']))
        ] + cfg['last']

        parts = ('first', 'include', 'last')
        required_files = []

        ## Make sure all imports are in files dictionary
        for part in parts:
            for fp in cfg[part]:
                if not fp in cfg['exclude'] and not files.has_key(fp):
                    raise MissingImport("File from '%s' not found: %s" %
                                        (part, fp))

        ## Header inserted at the start of each file in the output
        HEADER = "/* " + "=" * 70 + "\n    %s\n" + "   " + "=" * 70 + " */\n\n"

        ## Output the files in the determined order
        result = []
        for fp in order:
            f = files[fp]
            self.printer("Exporting: " + f.filepath)
            result.append(HEADER % f.filepath)
            source = f.source
            result.append(source)
            if not source.endswith("\n"):
                result.append("\n")

        self.printer("\nTotal files merged: %d " % len(files))
        merged = "".join(result)
        if cfg['closure']:
            merged = '(function(){%s})();' % merged
        return merged
コード例 #5
0
ファイル: merge.py プロジェクト: GunioRobot/jstools
    def merge(self, cfg, depmap=None):
        # @@ this function needs to be decomposed into smaller testable bits
        sourcedirs = cfg["root"]

        # assemble all files in source directory according to config
        include = cfg.get("include", tuple())
        exclude = cfg["exclude"]
        all_inc = cfg["first"] + cfg["include"] + cfg["last"]
        files = {}
        implicit = False
        if not len(include):
            # implicit file inclusion
            implicit = True

        for sourcedir in sourcedirs:
            newfiles = []
            for filepath in jsfiles_for_dir(sourcedir):
                fitem = filepath, srcfile = filepath, self.make_sourcefile(sourcedir, filepath, exclude)
                if implicit and not filepath in exclude:
                    all_inc.append(filepath)
                newfiles.append(fitem)

            newfiles = list(
                (filepath, sf) for filepath, sf in newfiles if filepath in all_inc and filepath not in exclude
            )
            files.update(dict(newfiles))

        # ensure all @include and @requires references are in
        complete = False
        while not complete:
            complete = True
            for filepath, info in files.items():
                for path in info.include + info.requires:
                    if path not in cfg["exclude"] and not files.has_key(path):
                        complete = False
                        for sourcedir in sourcedirs:
                            if os.path.exists(os.path.join(sourcedir, path)):
                                files[path] = self.make_sourcefile(sourcedir, path, exclude)
                                break
                        else:
                            raise MissingImport("File '%s' not found in root directories" % path)

        # create list of dependencies
        dependencies = {}
        for filepath, info in files.items():
            dependencies[filepath] = info.requires

        # get tuple of files ordered by dependency
        self.printer("Sorting dependencies.")
        order = [x for x in tsort.sort(dependencies)]

        # move forced first and last files to the required position
        self.printer("Re-ordering files.")
        order = (
            cfg["first"]
            + [item for item in order if ((item not in cfg["first"]) and (item not in cfg["last"]))]
            + cfg["last"]
        )

        parts = ("first", "include", "last")
        required_files = []

        ## Make sure all imports are in files dictionary
        for part in parts:
            for fp in cfg[part]:
                if not fp in cfg["exclude"] and not files.has_key(fp):
                    raise MissingImport("File from '%s' not found: %s" % (part, fp))

        ## Header inserted at the start of each file in the output
        HEADER = "/* " + "=" * 70 + "\n    %s\n" + "   " + "=" * 70 + " */\n\n"

        ## Output the files in the determined order
        result = []
        for fp in order:
            f = files[fp]
            self.printer("Exporting: " + f.filepath)
            result.append(HEADER % f.filepath)
            source = f.source
            result.append(source)
            if not source.endswith("\n"):
                result.append("\n")

        self.printer("\nTotal files merged: %d " % len(files))
        merged = "".join(result)
        if cfg["closure"]:
            merged = "(function(){%s})();" % merged
        return merged
コード例 #6
0
ファイル: merge.py プロジェクト: zopyx/jstools
    def extract_deps(self, cfg, depmap=None):
        #@@ this function needs to be decomposed into smaller testable bits
        sourcedirs = cfg['root']

        # assemble all files in source directory according to config
        include = cfg.get('include', tuple())
        exclude = [Exclude(e) for e in cfg['exclude']]
        all_inc = cfg['first'] + cfg['include'] + cfg['last']
        files = {}
        implicit = False
        if not len(include):
            # implicit file inclusion
            implicit = True

        for sourcedir in sourcedirs:
            newfiles = []
            for filepath in jsfiles_for_dir(os.path.join(self.root_dir, sourcedir)):
                fitem = filepath, srcfile = filepath, self.make_sourcefile(sourcedir, filepath, exclude),
                if implicit and not filepath in exclude:
                    all_inc.append(filepath)
                newfiles.append(fitem)

            newfiles = list((filepath, sf) for filepath, sf in newfiles if filepath in all_inc and filepath not in exclude)
            files.update(dict(newfiles))

        # ensure all @include and @requires references are in
        complete = False
        while not complete:
            complete = True
            for filepath, info in files.items():
                for path in info.include + info.requires:
                    if path not in exclude and not files.has_key(path):
                        complete = False
                        for sourcedir in sourcedirs:
                            if os.path.exists(os.path.join(self.root_dir, sourcedir, path)):
                                files[path] = self.make_sourcefile(sourcedir, path, exclude)
                                break
                        else:
                            raise MissingImport("File '%s' not found in root directories" % path)

        # create list of dependencies
        dependencies = {}
        for filepath, info in files.items():
            dependencies[filepath] = info.requires


        # get tuple of files ordered by dependency
        self.printer.debug("Sorting dependencies.")
        order = [x for x in tsort.sort(dependencies)]

        # move forced first and last files to the required position
        self.printer.debug("Re-ordering files.")
        order = cfg['first'] + [item
                     for item in order
                     if ((item not in cfg['first']) and
                         (item not in cfg['last']))] + cfg['last']

        parts = ('first', 'include', 'last')
        required_files = []

        ## Make sure all imports are in files dictionary
        for part in parts:
            for fp in cfg[part]:
                if not fp in exclude and not files.has_key(fp):
                    raise MissingImport("File from '%s' not found: %s" % (part, fp))

        ## Output the files in the determined order
        result = []
        for fp in order:
            result.append(files[fp])

        return result