Package psicons :: Package core :: Module commands
[hide private]
[frames] | no frames]

Source Code for Module psicons.core.commands

  1  #! /usr/bin/env python
 
  2  # -*- coding: utf-8 -*-
 
  3  """
 
  4  New scons commands.
 
  5  
 
  6  The process of installing a new tool or module for use by SCons is fiddly, 
 
  7  involves copying libraries in a tool directory, registering 
 
  8  their use in build files, voids the ease of using ``easy_install`` and makes 
 
  9  development a pain. Thus, these additional "commands" are real scons commands, so
 
 10  much as functions that generate commands. But they are easy to use.
 
 11  
 
 12  
 
 13  """ 
 14  
 
 15  __docformat__ = 'restructuredtext en' 
 16  
 
 17  
 
 18  ### IMPORTS ###
 
 19  
 
 20  import impl 
 21  
 
 22  
 
 23  ### CONSTANTS & DEFINES ###
 
 24  
 
 25  ### IMPLEMENTATION ###  
 
 26  
 
27 -def External (env, exe, args=[], infiles=[], output=[], depends=[], 28 capture_stdout=None):
29 """ 30 Create a task for doing analysis with an external (fixed) program. 31 32 :Parameters: 33 env : Environment 34 The environment created for the build 35 exe : str 36 name of executable to be called 37 args : str or list 38 either a string that is used to pass arguments to the executable 39 (e.g. ``"--foo 5 -t one.txt"``) or a list of strings that can be joined to 40 do the same (e.g. ``["--foo", "5", "-t", "one.txt"]``). 41 infiles : str or list 42 a string or list of strings giving the paths of the files that this 43 task depends on. 44 output : str or list 45 a string or list of strings giving the paths of the files that this 46 task produces. 47 capture_stdout : None or str 48 Record the output of the tool. If a string is provided, this will be 49 used as the path of the file to save it in. 50 51 This is a command that runs an executable, intended for processing input 52 datafiles to output data files. It calls the named executable with the 53 passed arguments. Infiles and any others listed in `depends` are recorded 54 as dependencies. outfiles and the captured output are listed as outputs. 55 These will be used in dependency tracking. 56 57 """ 58 # TODO: should be "outputs"? 59 infiles = impl.make_list (infiles or []) 60 args = impl.make_list (args or []) 61 cmdline = exe % { 62 'args': ' '.join (args), 63 'infiles': ' '.join (infiles) 64 } 65 output = impl.make_list (output or []) 66 new_command = env.Command ( 67 output, 68 infiles, 69 cmdline, 70 ) 71 depends = depends + infiles 72 env.Depends (new_command, depends) 73 return new_command
74 75 76
77 -def Script (env, path, interpreter=None, args=[], infiles=[], output=[], 78 depends=[], capture_stdout=None):
79 """ 80 Create a task for running a local script. 81 82 :Parameters: 83 path : str 84 Pathway to the script 85 interpreter 86 Interpreter to run the script with. If None, assume it is python. 87 88 89 90 This is a command that runs a local script, intended for processing input 91 datafiles to output data files. It work like `External`, except that the 92 script itself is made a dependency for this step. Thus ,alterations in the 93 script will trigger rerunning. All undescribed parameters are as per `External`. 94 95 For example:: 96 97 >>> env = SconsEnv() 98 >>> make_clean_data = Script ('Scripts/clean.py', 99 ... args = ['--save-as', 'output.txt'], 100 ... infiles = ['indata.txt'], 101 ... output = 'output.txt', 102 ... ) 103 >>> type_data = Script ('count_types.pl', 104 ... interpreter = 'perl', 105 ... args = ['--use-types', 'type_data.csv'], 106 ... infiles = ['clean_data.txt'], 107 ... depends = 'type_config.txt', 108 ... capture_stdout='captured-types.txt', 109 ... ) 110 111 """ 112 interpreter = interpreter or '$PY' 113 infiles = impl.make_list (infiles or []) 114 # TODO: flatten tuples to make arg list like [('-a', 'b'), ('--c', 'd')] to 115 # ['-a', 'b', '--c', 'd']? 116 # if dict do "--x=foo" 117 # TODO: autoescape/quote strings? 118 # TODO: have a "SrcFile" class for use in arguments that can be picked up 119 # in a scan 120 # TOOD: make a "chain" where every script depends on the output of the previous 121 args = impl.make_list (args or []) 122 cmdline = ' '.join ([interpreter, path] + args + infiles) 123 if capture_stdout: 124 cmdline = ' '.join ([cmdline, '>', capture_stdout]) 125 output.append (capture_stdout) 126 new_command = env.Command ( 127 output, 128 [path] + infiles, 129 cmdline, 130 ) 131 depends = depends + infiles 132 env.Depends (new_command, depends) 133 return new_command
134 135 136 137 ## DEBUG & TEST ### 138 139 if __name__ == "__main__": 140 import doctest 141 doctest.testmod() 142 143 144 145 ### END ####################################################################### 146