-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
167 additions
and
0 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
14-Provide-the-project/40-Steps-B/run_30-Convert-markdown-files.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#!/usr/bin/env python | ||
|
||
# ================================================== | ||
# open | ||
# -------------------------------------------------- | ||
|
||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
import os | ||
import tct | ||
import sys | ||
# | ||
import shutil | ||
|
||
ospe = os.path.exists | ||
ospj = os.path.join | ||
|
||
params = tct.readjson(sys.argv[1]) | ||
binabspath = sys.argv[2] | ||
facts = tct.readjson(params['factsfile']) | ||
milestones = tct.readjson(params['milestonesfile']) | ||
resultfile = params['resultfile'] | ||
result = tct.readjson(resultfile) | ||
loglist = result['loglist'] = result.get('loglist', []) | ||
toolname = params['toolname'] | ||
toolname_pure = params['toolname_pure'] | ||
workdir = params['workdir'] | ||
exitcode = CONTINUE = 0 | ||
|
||
|
||
# ================================================== | ||
# Make a copy of milestones for later inspection? | ||
# -------------------------------------------------- | ||
|
||
if 0 or milestones.get('debug_always_make_milestones_snapshot'): | ||
tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1]) | ||
|
||
|
||
# ================================================== | ||
# Helper functions | ||
# -------------------------------------------------- | ||
|
||
def lookup(D, *keys, **kwdargs): | ||
result = tct.deepget(D, *keys, **kwdargs) | ||
loglist.append((keys, result)) | ||
return result | ||
|
||
|
||
# ================================================== | ||
# define | ||
# -------------------------------------------------- | ||
|
||
xeq_name_cnt = 0 | ||
|
||
|
||
# ================================================== | ||
# Check params | ||
# -------------------------------------------------- | ||
|
||
if exitcode == CONTINUE: | ||
loglist.append('CHECK PARAMS') | ||
|
||
documentation_folder = lookup(milestones, 'documentation_folder') | ||
pandoc = lookup(milestones, 'known_systemtools', 'pandoc') | ||
if not (documentation_folder and pandoc): | ||
exitcode = 22 | ||
|
||
if exitcode == CONTINUE: | ||
loglist.append('PARAMS are ok') | ||
else: | ||
loglist.append('Bad PARAMS or nothing to do') | ||
|
||
|
||
# ========================================================= | ||
# how to start a subprocess with perfect logging | ||
# --------------------------------------------------------- | ||
|
||
if exitcode == CONTINUE: | ||
|
||
import codecs | ||
import os | ||
import subprocess | ||
|
||
def cmdline(cmd, cwd=None): | ||
if cwd is None: | ||
cwd = os.getcwd() | ||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd) | ||
out, err = process.communicate() | ||
exitcode = process.returncode | ||
return exitcode, cmd, out, err | ||
|
||
def execute_cmdlist(cmdlist): | ||
global xeq_name_cnt | ||
cmd = ' '.join(cmdlist) | ||
cmd_multiline = ' \\\n '.join(cmdlist) + '\n' | ||
|
||
xeq_name_cnt += 1 | ||
filename_cmd = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'cmd') | ||
filename_err = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'err') | ||
filename_out = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'out') | ||
|
||
with codecs.open(os.path.join(workdir, filename_cmd), 'w', 'utf-8') as f2: | ||
f2.write(cmd_multiline.decode('utf-8', 'replace')) | ||
|
||
exitcode, cmd, out, err = cmdline(cmd, cwd=workdir) | ||
loglist.append({'exitcode': exitcode, 'cmd': cmd, 'out': out, 'err': err}) | ||
|
||
with codecs.open(os.path.join(workdir, filename_out), 'w', 'utf-8') as f2: | ||
f2.write(out.decode('utf-8', 'replace')) | ||
|
||
with codecs.open(os.path.join(workdir, filename_err), 'w', 'utf-8') as f2: | ||
f2.write(err.decode('utf-8', 'replace')) | ||
|
||
return exitcode, cmd, out, err | ||
|
||
|
||
# ================================================== | ||
# work | ||
# -------------------------------------------------- | ||
|
||
markdown_files = [] | ||
result_list = [] | ||
|
||
if exitcode == CONTINUE: | ||
for relpath, dirs, files in os.walk(documentation_folder): | ||
for fname in files: | ||
if fname.endswith('.md'): | ||
markdown_files.append(ospj(relpath, fname)) | ||
|
||
if markdown_files: | ||
for fpath in markdown_files: | ||
mdfile = ospj(documentation_folder, fpath) | ||
rstfile = mdfile[:-3] + '.rst' | ||
if ospe(rstfile): | ||
reason = 'existed' | ||
converted = 0 | ||
else: | ||
thisexitcode, cmd, out, err = execute_cmdlist([pandoc, '--from markdown --to rst', mdfile, '-o', rstfile]) | ||
if thisexitcode == 0: | ||
reason = '' | ||
converted = 1 | ||
else: | ||
reason = 'exitcode ' + str(thisexitcode) | ||
converted = 0 | ||
result_list.append((converted, fpath, reason)) | ||
loglist.append((len(result_list), 'found')) | ||
|
||
|
||
# ================================================== | ||
# Set MILESTONE | ||
# -------------------------------------------------- | ||
if result_list: | ||
result['MILESTONES'].append({'markdown_files': result_list}) | ||
|
||
|
||
# ================================================== | ||
# save result | ||
# -------------------------------------------------- | ||
|
||
tct.writejson(result, resultfile) | ||
|
||
|
||
# ================================================== | ||
# Return with proper exitcode | ||
# -------------------------------------------------- | ||
|
||
sys.exit(exitcode) |