<commit_before>from config import config, load_config
import process_source as ps
import os,sys
import shutil
import re,time
import server
import argparse,init


def build_site(ap=None):
    """
    Walk the content tree and build the entire site.
    """

    # Either empty the existing entire site folder
    if not config.has_key('site'):
        print ap.print_help()
        sys.exit()

    if os.path.exists(config['site']):
        # We don't want to just remove the entire folder  - if we do and we're running a
        # server, it will lose it's directory and will have to be re-run
        # by just emptying the folder, the server will automatically find the new site
        for f in os.listdir(config['site']):
            filename = config['site']+"/"+f
            if os.path.isfile(filename):
                os.remove(filename)
            else:
                # We can safely remove entire subtrees of the site folder
                shutil.rmtree(filename)
    else: # or make a new site folder if one doesn't exist
        os.mkdir(config['site'])

    # Copy any static stuff over
    if os.path.exists(config['static']):
        shutil.copytree(config['static'],config['site']+"/"+config['static_dest'])

    # Generate the content
    valid_extensions = config['extensions'].keys()

    # Walk through the content folder (making subdirectories as needed
    # and process each file
    # We'll have to revisit this when we do the posts stuff
    # but for now it works
    for (dir,subs,files) in os.walk(config['content']):
        dest = re.sub(config['content'],config['site'],dir)
        for sub in subs:
            if not os.path.exists(dest+"/"+sub):
                os.mkdir(dest+"/"+sub)
        for f in files:
            (name,ext)=os.path.splitext(f)
            if name[0] != '.' and ext[1:] in valid_extensions:
                result = ps.process_file(dir+"/"+f)
                outfilename=dest+"/"+name+".html"
                outfile = open(outfilename,"w")
                outfile.write(result)
                outfile.close()


def serve():
    """
    Start the server (see server.py) and then check every second to see
    if a file changes, if so,  rebuild the site.

    It's not efficient, it just sees if ANY file that isn't hidden changes.
    """
    build_site()
    olds=" ".join([" ".join(["%f"%os.stat(dir+"/"+f).st_mtime for f in files if f[0]!='.']) for (dir,subs,files) in os.walk(config['content'])])
    pid = server.start_server()
    while  True:
        time.sleep(1)
        news=" ".join([" ".join(["%f"%os.stat(dir+"/"+f).st_mtime for f in files if f[0] !='.']) for (dir,subs,files) in os.walk(config['content'])])
        if news!=olds:
            print "REBUILDING"
            build_site()
            olds = news



def parse_args():

    parser = argparse.ArgumentParser(description="LesStatic Argument")
    parser.add_argument('-i','--init',action='store_true',help='initialize new project')
    parser.add_argument('folder',metavar='FOLDER',nargs="?",help='Folder for project (undefined = current folder)')
    parser.add_argument('-p','--port',nargs='?')<commit_msg>modified help<commit_after>from config import config, load_config
import process_source as ps
import os,sys
import shutil
import re,time
import server
import argparse,init


def build_site(ap=None):
    """
    Walk the content tree and build the entire site.
    """

    # Either empty the existing entire site folder
    if not config.has_key('site'):
        print ap.print_help()
        sys.exit()

    if os.path.exists(config['site']):
        # We don't want to just remove the entire folder  - if we do and we're running a
        # server, it will lose it's directory and will have to be re-run
        # by just emptying the folder, the server will automatically find the new site
        for f in os.listdir(config['site']):
            filename = config['site']+"/"+f
            if os.path.isfile(filename):
                os.remove(filename)
            else:
                # We can safely remove entire subtrees of the site folder
                shutil.rmtree(filename)
    else: # or make a new site folder if one doesn't exist
        os.mkdir(config['site'])

    # Copy any static stuff over
    if os.path.exists(config['static']):
        shutil.copytree(config['static'],config['site']+"/"+config['static_dest'])

    # Generate the content
    valid_extensions = config['extensions'].keys()

    # Walk through the content folder (making subdirectories as needed
    # and process each file
    # We'll have to revisit this when we do the posts stuff
    # but for now it works
    for (dir,subs,files) in os.walk(config['content']):
        dest = re.sub(config['content'],config['site'],dir)
        for sub in subs:
            if not os.path.exists(dest+"/"+sub):
                os.mkdir(dest+"/"+sub)
        for f in files:
            (name,ext)=os.path.splitext(f)
            if name[0] != '.' and ext[1:] in valid_extensions:
                result = ps.process_file(dir+"/"+f)
                outfilename=dest+"/"+name+".html"
                outfile = open(outfilename,"w")
                outfile.write(result)
                outfile.close()


def serve():
    """
    Start the server (see server.py) and then check every second to see
    if a file changes, if so,  rebuild the site.

    It's not efficient, it just sees if ANY file that isn't hidden changes.
    """
    build_site()
    olds=" ".join([" ".join(["%f"%os.stat(dir+"/"+f).st_mtime for f in files if f[0]!='.']) for (dir,subs,files) in os.walk(config['content'])])
    pid = server.start_server()
    while  True:
        time.sleep(1)
        news=" ".join([" ".join(["%f"%os.stat(dir+"/"+f).st_mtime for f in files if f[0] !='.']) for (dir,subs,files) in os.walk(config['content'])])
        if news!=olds:
            print "REBUILDING"
            build_site()
            olds = news



def parse_args():

    parser = argparse.ArgumentParser(description="LesStatic needs to be run in a valid LesStatic project folder (with a config.yaml) or with valid arguments")
    parser.add_argument('-i','--init',action='store_true',help='initialize new project')
    parser.add_argument('folder',metavar='FOLDER',nargs="?",help='Folder for project (undefined = current folder)')
    parser.add_argument('-p','--port',nargs='?')
{'fraction_non_alphanumeric': 0.13459730492008776, 'fraction_numerical': 0.0015669069257286117, 'mean_word_length': 2.6164305949008497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2934875', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1862, 'n_words': 582}