def main(): usage = 'usage: %prog [options] [pagename ...]' version = '%%prog %s (%s)' % (__version__, __date__) optparser = OptionParser(usage=usage, version=version) optparser.add_option('-m', '--make', action='store_true', help='build modified pages') optparser.add_option('-b', '--build', action='store_true', help='build all pages') optparser.add_option('-c', '--clean', action='store_true', help='remove html files') optparser.add_option( '-s', '--synchronize', action='store_true', help='upload modified files to the FTP ' 'server; wiki files are not uploded; subdirectories *ARE* uploaded; requires ftputil library; FTP ' 'has to be configured using the config file; this switch can be combined with any of the above ' 'three') optparser.add_option( '-f', '--force', action='store_true', help='when used together with --synchronize, ' 'causes the files and directories that does not exist locally to be deleted from the FTP server' ) optparser.add_option('-d', '--directory', dest='dir', help='wiki directory, defaults to current directory', default='.') optparser.add_option( '-g', '--config', help='name of the config file relative to the wiki directory, ' 'defaults to _statwiki.config', default='_statwiki.config') options, args = optparser.parse_args() a = [ name for name, value in options.__dict__.items() if name in ('make', 'build', 'clean') and value ] if len(a) > 1: sys.exit( 'error: only one of --make, --build and --clean switches can be used at once' ) try: mode = a[0] except IndexError: if options.synchronize: # if only --synchronize was specified, do nothing besides syncing mode = 'idle' else: sys.exit( 'error: one of the --make, --build, --clean or --synchronize switches must ' 'be specified; use --help for more information') os.chdir(options.dir) config.parse(wikiutil.fixFileNameCase(options.config)) if args: # Add .wiki to the names if needed. wikipages = [] for name in args: if not name.endswith('.wiki'): name = wikiutil.pageName2inputFile(name) wikipages.append(wikiutil.fixFileNameCase(name)) else: wikipages = [ x for x in os.listdir('.') if x.endswith('.wiki') and not x.startswith('_') ] if mode == 'clean': print 'Cleaning...' for filename in wikipages: pagename = wikiutil.inputFile2pageName(filename) try: os.unlink(wikiutil.pageName2outputFile(pagename)) print wikiutil.pageName2outputFile(pagename) except OSError: pass elif mode == 'make': print 'Making...' todo = [] for filename in wikipages: ofilename = wikiutil.inputFile2outputFile(filename) if not os.path.exists(ofilename) or \ os.path.getmtime(ofilename) < os.path.getmtime(filename): todo.append(filename) process(todo) elif mode == 'build': print 'Building...' process(wikipages) if options.synchronize: print 'Synchronizing with %s...' % getattr(config.ftp, 'host', '???') try: host = config.ftp.host except AttributeError: sys.exit( 'cannot synchronize, configure the FTP server access first') # Import ftpsync only if --synchronize was specified so that ftputil doesn't have to be # installed if this option is not used. from ftpsync import synchronize synchronize(options.force) elif options.force: sys.exit('error: --force can only be used together with --synchronize')
def main(): usage = 'usage: %prog [options] [pagename ...]' version = '%%prog %s (%s)' % (__version__, __date__) optparser = OptionParser(usage=usage, version=version) optparser.add_option('-m', '--make', action='store_true', help='build modified pages') optparser.add_option('-b', '--build', action='store_true', help='build all pages') optparser.add_option('-c', '--clean', action='store_true', help='remove html files') optparser.add_option('-s', '--synchronize', action='store_true', help='upload modified files to the FTP ' 'server; wiki files are not uploded; subdirectories *ARE* uploaded; requires ftputil library; FTP ' 'has to be configured using the config file; this switch can be combined with any of the above ' 'three') optparser.add_option('-f', '--force', action='store_true', help='when used together with --synchronize, ' 'causes the files and directories that does not exist locally to be deleted from the FTP server') optparser.add_option('-d', '--directory', dest='dir', help='wiki directory, defaults to current directory', default='.') optparser.add_option('-g', '--config', help='name of the config file relative to the wiki directory, ' 'defaults to _statwiki.config', default='_statwiki.config') options, args = optparser.parse_args() a = [name for name, value in options.__dict__.items() if name in ('make', 'build', 'clean') and value] if len(a) > 1: sys.exit('error: only one of --make, --build and --clean switches can be used at once') try: mode = a[0] except IndexError: if options.synchronize: # if only --synchronize was specified, do nothing besides syncing mode = 'idle' else: sys.exit('error: one of the --make, --build, --clean or --synchronize switches must ' 'be specified; use --help for more information') os.chdir(options.dir) config.parse(wikiutil.fixFileNameCase(options.config)) if args: # Add .wiki to the names if needed. wikipages = [] for name in args: if not name.endswith('.wiki'): name = wikiutil.pageName2inputFile(name) wikipages.append(wikiutil.fixFileNameCase(name)) else: wikipages = [x for x in os.listdir('.') if x.endswith('.wiki') and not x.startswith('_')] if mode == 'clean': print 'Cleaning...' for filename in wikipages: pagename = wikiutil.inputFile2pageName(filename) try: os.unlink(wikiutil.pageName2outputFile(pagename)) print wikiutil.pageName2outputFile(pagename) except OSError: pass elif mode == 'make': print 'Making...' todo = [] for filename in wikipages: ofilename = wikiutil.inputFile2outputFile(filename) if not os.path.exists(ofilename) or \ os.path.getmtime(ofilename) < os.path.getmtime(filename): todo.append(filename) process(todo) elif mode == 'build': print 'Building...' process(wikipages) if options.synchronize: print 'Synchronizing with %s...' % getattr(config.ftp, 'host', '???') try: host = config.ftp.host except AttributeError: sys.exit('cannot synchronize, configure the FTP server access first') # Import ftpsync only if --synchronize was specified so that ftputil doesn't have to be # installed if this option is not used. from ftpsync import synchronize synchronize(options.force) elif options.force: sys.exit('error: --force can only be used together with --synchronize')
def process(wikipages): donepages = [] # Load all pragmas. global all_pragmas # used by Content objects too all_pragmas = Pragmas(force_update=wikipages) # Check for deleted pages. for filename, pragmas in all_pragmas.previous_items(): if not os.path.exists(filename): # Process categories so the page is removed from them. for category in pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) if fname not in wikipages: wikipages.append(fname) all_pragmas.remove(filename) # Process the pages. while wikipages: filename = wikipages[0] del wikipages[0] assert os.path.exists(filename), '%s does not exist' % filename pagename = wikiutil.inputFile2pageName(filename) pragmas = all_pragmas[filename] prev_pragmas = all_pragmas.previous(filename) # Prepare the namespace for the generator script. globs = dict(pagename=pagename, summary=pragmas.single('#summary', ''), genconfig=config.generator) print filename # Process categories this page was added to. for category in pragmas.multiple('#category'): if category not in prev_pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) wikiutil.assertFileNameCase(fname) if fname not in donepages and fname not in wikipages: wikipages.append(fname) # Process categories this page was removed from. for category in prev_pragmas.multiple('#category'): if category not in pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) if os.path.exists(fname): wikiutil.assertFileNameCase(fname) if fname not in donepages and fname not in wikipages and \ os.path.exists(fname): wikipages.append(fname) # Load included pages. for inc in pragmas.multiple('#include'): args = inc.split() assert len( args) == 3 and args[1] == 'as', '#include pragma syntax error' globs[args[2]] = Content(args[0]) # Create a HTML using the generator script. globs.update( dict(content=Content(pagename), modify_time=time.strftime( config.general.timeformat, time.gmtime(os.path.getmtime(filename))), generate_time=time.strftime(config.general.timeformat, time.gmtime()), out=codecs.open(wikiutil.pageName2outputFile(pagename), 'wt', encoding=config.charset))) try: for args in pragmas.multiple('#execute'): exec args in globs execfile(config.general.generator, globs) except: globs['out'].close() os.remove(wikiutil.pageName2outputFile(pagename)) raise globs['out'].close() if filename not in donepages: donepages.append(filename) # Add pages including the current page to processing. for filename, pragmas in all_pragmas.items(): try: inc = pragmas.single('#include').split(None, 1)[0] except ValueError: continue if inc != pagename: continue if filename not in donepages and filename not in wikipages: assert os.path.exists(filename), '%s does not exist' % filename wikipages.append(filename)
def process(wikipages): donepages = [] # Load all pragmas. global all_pragmas # used by Content objects too all_pragmas = Pragmas(force_update=wikipages) # Check for deleted pages. for filename, pragmas in all_pragmas.previous_items(): if not os.path.exists(filename): # Process categories so the page is removed from them. for category in pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) if fname not in wikipages: wikipages.append(fname) all_pragmas.remove(filename) # Process the pages. while wikipages: filename = wikipages[0] del wikipages[0] assert os.path.exists(filename), '%s does not exist' % filename pagename = wikiutil.inputFile2pageName(filename) pragmas = all_pragmas[filename] prev_pragmas = all_pragmas.previous(filename) # Prepare the namespace for the generator script. globs = dict(pagename=pagename, summary=pragmas.single('#summary', ''), genconfig=config.generator) print filename # Process categories this page was added to. for category in pragmas.multiple('#category'): if category not in prev_pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) wikiutil.assertFileNameCase(fname) if fname not in donepages and fname not in wikipages: wikipages.append(fname) # Process categories this page was removed from. for category in prev_pragmas.multiple('#category'): if category not in pragmas.multiple('#category'): fname = wikiutil.pageName2inputFile(category) if os.path.exists(fname): wikiutil.assertFileNameCase(fname) if fname not in donepages and fname not in wikipages and \ os.path.exists(fname): wikipages.append(fname) # Load included pages. for inc in pragmas.multiple('#include'): args = inc.split() assert len(args) == 3 and args[1] == 'as', '#include pragma syntax error' globs[args[2]] = Content(args[0]) # Create a HTML using the generator script. globs.update(dict(content=Content(pagename), modify_time=time.strftime(config.general.timeformat, time.gmtime(os.path.getmtime(filename))), generate_time=time.strftime(config.general.timeformat, time.gmtime()), out=codecs.open(wikiutil.pageName2outputFile(pagename), 'wt', encoding=config.charset))) try: for args in pragmas.multiple('#execute'): exec args in globs execfile(config.general.generator, globs) except: globs['out'].close() os.remove(wikiutil.pageName2outputFile(pagename)) raise globs['out'].close() if filename not in donepages: donepages.append(filename) # Add pages including the current page to processing. for filename, pragmas in all_pragmas.items(): try: inc = pragmas.single('#include').split(None, 1)[0] except ValueError: continue if inc != pagename: continue if filename not in donepages and filename not in wikipages: assert os.path.exists(filename), '%s does not exist' % filename wikipages.append(filename)