# Support for pythontex in v. 0.16 or higher, with latexmk 4.62 or higher
#
# What these definitions provide/do:
# 1. Variable specifying command string for invoking the pythontex program.
# 2. Addition to %extra_rule_spec of template for pythontex rule. This
#    tells latexmk to create the rule when it is initializing its rule
#    network for processing a .tex file.
# 3. A subroutine mypythontex that the pythontex rule is defined to
#    call. This runs pythontex and then sets dependency information.
# 4. Settings for the files generated by the pythontex package and the
#    pythontex program so that the files are deleted in a clean-up
#    operation.
#
# 5. There are two parts to the dependency information
#    a. The files **generated** by pythontex.
#       This happens without any special assistance from the document.
#    b. The files **read** by python.
#       At present this needs support with some code in the document.
#       See the document at the end of this file for an example.


$clean_ext .= " pythontex-files-%R/* pythontex-files-%R";
push @generated_exts, 'pytxcode';

$pythontex = 'pythontex %O %S';

# Specify a rule for running the pythontex program.
# Because of the unusual structure of input and output files, I've used
# an internal hash variable %extra_rule_spec of latexmk to specify the
# rule.  Currently the only documentation is in the latexmk source code.
# Probably the following line is stable against future changes in latexmk,
# but that isn't guaranteed.
$extra_rule_spec{'pythontex'}  = [ 'internal', '', 'mypythontex', "%Y%R.pytxcode",  "%Ypythontex-files-%R/%R.pytxmcr",    "%R", 1 ];

sub mypythontex {
   my $result_dir = $aux_dir1."pythontex-files-$$Pbase";
   my $ret = Run_subst( $pythontex, 2 );
   rdb_add_generated( glob "$result_dir/*" );
   open( my $fh, "<", $$Pdest );
   if ($fh) {
      while (<$fh>) {
         if ( /^%PythonTeX dependency:\s+'([^']+)';/ ) {
	     print "Found pythontex dependency '$1'\n";
             rdb_ensure_file( $rule, $aux_dir1.$1 );
	 }
      }
      undef $fh;
   }
   else {
       warn "mypythontex: I could not read '$$Pdest'\n",
            "  to check dependencies\n";
   }
   return $ret;
}

# ==============================================
#  Sample document that writes dependency information
#  To use it you need to save this file as e.g., test.tex,
#  and to create a data file name data.txt, with whatever content you like.
#
#\documentclass{article}
#\usepackage{pythontex}
#
#%================
#% Macro to write list of dependent files to where latexmk will find it,
#% when configured with the code in pythontex_latexmkrc.
#\newcommand{\pycachedeps}{\py{cache_dependencies()}}
#%\AtEndDocument{\pycachedeps}
#
#% Python code used by above.
#\begin{pycode}
#def cache_dependencies():
#  s = '';
#  for item in pytex._dependencies:
#    s += "%PythonTeX dependency: '" + item + "';\n"
#  return s;
#\end{pycode}
#%================
#
#\begin{document}
#
#Test of using pythontex with dependency information.
#
#% Python code to access a particular file, together with use of a pythontex
#% subroutine to add the file to pythontex's list of source files.
#\begin{pycode}
#pytex.add_dependencies("data.txt")
#n = "data.txt"
#f = open( n, "r" )
#print( "From '", n, "', first line is: \\\\", f.readline(), "" )
#f.close
#\end{pycode}
#
#A big loop, which takes a few seconds to run in python.  This is just to illustra
#\begin{pycode}
#j = -20
#for i in range(0,15000000):
#  j = j + i
#print(j)
#\end{pycode}
#
#% Cache dependencies (in .pytxmcr file):
#\pycachedeps
#\end{document}