finishing touches on basic functionality.

Misc cleanup, more comments, and finishing the basic functionality.
Just need to implement issue #4.
Possible early release candidate?
This commit is contained in:
gabe 2021-09-05 00:07:48 -05:00
parent 1e05e48c7b
commit 74796c804e

View file

@ -4,8 +4,8 @@
import subprocess, sys, argparse, logging, tempfile, shutil, os, re import subprocess, sys, argparse, logging, tempfile, shutil, os, re
from pathlib import Path from pathlib import Path
#logging.basicConfig(format='%(asctime)s:%(message)s', level=logging.INFO) logging.basicConfig(format='%(asctime)s:%(message)s', level=logging.INFO)
logging.basicConfig(format='%(asctime)s:%(message)s', level=logging.DEBUG) #logging.basicConfig(format='%(asctime)s:%(message)s', level=logging.DEBUG)
def parse_arguments(): def parse_arguments():
parser=argparse.ArgumentParser(description='create a website directory structure by converting .adoc files in a directory strucutre to .html files.') parser=argparse.ArgumentParser(description='create a website directory structure by converting .adoc files in a directory strucutre to .html files.')
@ -26,14 +26,14 @@ def parse_arguments():
#If outfile was not set, set it. #If outfile was not set, set it.
if args.output == None: if args.output == None:
outFile = args.inputDir.with_name(args.inputDir.name+'_compiled') outFile=args.inputDir.with_name(args.inputDir.name+'_compiled').resolve()
else: else:
outFile=args.output outFile=args.output.resovle()
#add .tar.gz if compress is set and the outfile does not already have it. #add .tar.gz if compress is set and the outfile does not already have it.
if compress and outFile.suffixes != ['.tar', '.gz']: if compress and outFile.suffixes != ['.tar', '.gz']:
logging.info(f'outFile was {outFile}, corrected because compress flag is set.') logging.info(f'outFile was {outFile}, corrected because compress flag is set.')
outFile = outFile.with_suffix('.tar.gz') outFile=outFile.with_suffix('.tar.gz').resolve()
if args.inputDir.resolve() == outFile.resolve(): if args.inputDir.resolve() == outFile.resolve():
raise FileExistsError('output file cannot have the same path as the input file!') raise FileExistsError('output file cannot have the same path as the input file!')
@ -42,28 +42,28 @@ def parse_arguments():
logging.info(f'outputting to {outFile.resolve()}') logging.info(f'outputting to {outFile.resolve()}')
logging.debug(f'compress is {compress}') logging.debug(f'compress is {compress}')
return args.inputDir, outFile, compress return args.inputDir.resolve(), outFile, compress
#Doing it in a tmpDir first, as some distrubutions put temp files on a ramdisk. this should speed up the operation sigificantly. #Doing it in a tmpDir first, as some distrubutions put temp files on a ramdisk. this should speed up the operation sigificantly.
class TmpDir: class TmpDir:
def __init__(self, srcDir): def __init__(self, srcDir):
self.tmpDir=tempfile.TemporaryDirectory() self.tmpDir=tempfile.TemporaryDirectory()
logging.debug(f'making tmp file from {srcDir} at {self.tmpDir.name}') logging.debug(f'making tmp file from {srcDir} at {self.tmpDir.name}')
self.path = self.tmpDir.name+'/data/' self.path=self.tmpDir.name+'/'+Path(srcDir).resolve().name
self.ignorePattern=shutil.ignore_patterns('*.adoc', '.git', '.gitignore') self.ignorePattern=shutil.ignore_patterns('*.adoc', '.git', '.gitignore')
shutil.copytree(srcDir, self.path, ignore=self.ignorePattern, symlinks=False) shutil.copytree(srcDir, self.path, ignore=self.ignorePattern, symlinks=False)
#copy out from tmpDir (which may be in RAM, depending on distrubution) to disk #copy out from tmpDir (which may be in RAM, depending on distrubution) to disk
def copy_self_to(self, destDir): def copy_self_to(self, destPath):
logging.debug(f'outputting to {destPath}') logging.debug(f'outputting to {Path(destPath).resolve()}')
shutil.copytree(self.path, destDir, symlinks=False) shutil.copytree(self.path, destPath, symlinks=False)
#copy out from tmpDir (which may be in RAM, depending on distrubution) to a compressed file on disk #copy out from tmpDir (which may be in RAM, depending on distrubution) to a compressed file on disk
def compress_and_copy_self_to(self, destPath): def compress_and_copy_self_to(self, destPath):
#shutil.make_archive wants destPath to be without file extentions for some godforsaken reason. #shutil.make_archive wants destPath to be without file extentions for some godforsaken reason.
destPath=Path(destPath.with_name(destPath.name.split('.')[0])).resolve() destPath=Path(destPath.with_name(destPath.name.split('.')[0])).resolve()
logging.debug(f'compressing to {destPath}') logging.debug(f'compressing to {Path(destPath).resolve()} from {Path(self.path).parent}')
tarFile = shutil.make_archive(destPath, 'gztar', self.path) tarFile=shutil.make_archive(destPath, 'gztar', Path(self.path).parent)
def cleanup(self): def cleanup(self):
self.tmpDir.cleanup() self.tmpDir.cleanup()
@ -87,7 +87,13 @@ def convert_file(inDir, outDir, inFile):
logging.debug(f'converting {inFile} from directory {inDir} to directory {outDir}') logging.debug(f'converting {inFile} from directory {inDir} to directory {outDir}')
try: try:
#the destdir can be used instead of destfile in order to preserve the directory structure relative to the base dir. really useful. #the destdir can be used instead of destfile in order to preserve the directory structure relative to the base dir. really useful.
subprocess.run(['asciidoctor', f'--base-dir={inDir}', f'--source-dir={inDir}', f'--destination-dir={outDir}', inFile], check=True) subprocess.run(['asciidoctor',
#specifies the source directory root.
f'--source-dir={inDir}',
#Destination dir. It takes the file from the subtree --source-dir and puts it in the equivilant location in the subtree --destination-dir. (talking about filesystem subtrees).
f'--destination-dir={outDir}',
inFile],
check=True)
except Exception as e: except Exception as e:
logging.error(f'could not convert {inFile}!') logging.error(f'could not convert {inFile}!')
logging.error(f'stdErr was {e.stderr}') logging.error(f'stdErr was {e.stderr}')
@ -97,7 +103,13 @@ inFile, outFile, compress = parse_arguments()
os.chdir(inFile) os.chdir(inFile)
tmpDir=TmpDir('./') tmpDir=TmpDir('./')
pathsToConvert=find_paths_to_convert('./', []) pathsToConvert=find_paths_to_convert('./', [])
for i in pathsToConvert: for i in pathsToConvert:
convert_file('./', tmpDir.path, i) convert_file('./', tmpDir.path, i)
breakpoint()
if compress:
tmpDir.compress_and_copy_self_to(outFile)
else:
tmpDir.copy_self_to(outFile)
tmpDir.cleanup() tmpDir.cleanup()