<commit_before>#!/usr/bin/env python
NXCONVERT = "nxconvert"
XSLT_PROC = "xsltproc"

def run(cmd, **kwargs):
  """Execute the supplied command and return the tuple (return value,
  stdout)"""
  shell = kwargs.get("shell", True)

  import subprocess as sub
  proc = sub.Popen(cmd, stdout=sub.PIPE, stderr=sub.STDOUT, shell=shell)
  (stdout, stderr) = proc.communicate()
  return_code = proc.wait()
  return (return_code, stdout)

def nxconvert(definition=None, input=None, output=None, verbose=0):
  command = "%s -d %s %s" % (NXCONVERT, input, output)
  if verbose > 1:
    print command
  (code, stdout) = run(command)
  if verbose > 1:
    print stdout
  if code != 0:
    raise RuntimeError("Failed to run " + NXCONVERT)
  

def process(xml=None, xslt=None, output=None):
  if xml is None:
    raise Exception("xml file is None")
  if xslt is None:
    raise Exception("xslt file is None")
  command = "%s -o %s %s %s" % (XSLT_PROC, output, xslt, xml)
  print command
  (code, stdout) = run(command)
  print "RETURN:", code
  print "STDOUT:", stdout

if __name__ == "__main__":
  import os
  doc_root = "~/code/nexus-dfn/"

  xslt = os.path.join(doc_root, "xslt/nxdl2xsd.xsl")
  xml = os.path.join(doc_root, "base_classes/NXlog.nxdl.xml")
  process(xml, xslt, "transformednxdl")

  nexus = os.path.join(doc_root, "test/NXmonopd.hdf")
  nxconvert("NXmonopd", nexus, "reducednexus")<commit_msg>Added verbose and exception code to process as well. Refs #179.<commit_after>#!/usr/bin/env python
NXCONVERT = "nxconvert"
XSLT_PROC = "xsltproc"

def run(cmd, **kwargs):
  """Execute the supplied command and return the tuple (return value,
  stdout)"""
  shell = kwargs.get("shell", True)

  import subprocess as sub
  proc = sub.Popen(cmd, stdout=sub.PIPE, stderr=sub.STDOUT, shell=shell)
  (stdout, stderr) = proc.communicate()
  return_code = proc.wait()
  return (return_code, stdout)

def nxconvert(definition=None, input=None, output=None, verbose=0):
  command = "%s -d %s %s" % (NXCONVERT, input, output)
  if verbose > 1:
    print command
  (code, stdout) = run(command)
  if verbose > 1:
    print stdout
  if code != 0:
    raise RuntimeError(NXCONVERT + " returned " + code)
  

def process(xml=None, xslt=None, output=None, verbose=0):
  if xml is None:
    raise Exception("xml file is None")
  if xslt is None:
    raise Exception("xslt file is None")
  command = "%s -o %s %s %s" % (XSLT_PROC, output, xslt, xml)
  if verbose > 1:
    print command
  (code, stdout) = run(command)
  if verbose > 1:
    print stdout
  if code != 0:
    raise RuntimeError(XSLT_PROC + " returned " + code)

if __name__ == "__main__":
  import os
  doc_root = "~/code/nexus-dfn/"

  xslt = os.path.join(doc_root, "xslt/nxdl2xsd.xsl")
  xml = os.path.join(doc_root, "base_classes/NXlog.nxdl.xml")
  process(xml, xslt, "transformednxdl")

  nexus = os.path.join(doc_root, "test/NXmonopd.hdf")
  nxconvert("NXmonopd", nexus, "reducednexus")
{'fraction_non_alphanumeric': 0.14431934493346982, 'fraction_numerical': 0.005800068236096895, 'mean_word_length': 3.9948892674616694, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '3729471', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1103, 'n_words': 296}