예제 #1
0
 def on_any_event(self, event):
     exclude_path = os.path.join(os.getcwd(), exclude)
     if not utils.matches_pattern(exclude_path, event.src_path):
         build_files(root=root,
                     dest=dest,
                     pattern=pattern,
                     exclude=exclude)
예제 #2
0
def build_files(root=u'.', dest=u'_site', pattern=u'**/*.html', 
                exclude=u'_*/**', watch=False, force=False):
    try:
        os.stat(os.path.join(root, 'index.html'))
    except OSError:
        if not force:
            msg = "Oops, we can't find an index.html in the source folder.\n"+\
                  "If you want to build this folder anyway, use the --force\n"+\
                  "option."
            print(msg)
            sys.exit(1)

    print("Building site from '{0}' into '{1}'".format(root, dest))

    exclude = exclude or os.path.join(dest, u'**')
    for filename in utils.walk_folder(root or '.'):
        included = utils.matches_pattern(pattern, filename)
        excluded = utils.matches_pattern(exclude, filename)
        destfile = os.path.join(dest, filename)
        if included and not excluded: 
            build_file(filename, destfile, root=root)
        elif not excluded:
            filepath = os.path.join(root, filename)
            destpath = os.path.join(dest, filename)
            utils.copy_file(filepath, destpath)

    if watch:
        observer = _watch(root=root,
                          dest=dest,
                          pattern=pattern,
                          exclude=exclude)
        if not observer:
            return
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()