source: branches/testScripts/NBosCommand.py @ 761

Last change on this file since 761 was 718, checked in by jpfasano, 13 years ago

Added comment about licensing

  • Property svn:eol-style set to native
File size: 3.0 KB
RevLine 
[626]1#! /usr/bin/env python
2
[718]3#------------------------------------------------------------------------
4# This file is distributed under the Common Public License.
5# It is part of the BuildTools project in COIN-OR (www.coin-or.org)
6#------------------------------------------------------------------------
7
[631]8from socket import gethostname
[632]9import sys
[656]10import os
11import re
[626]12
13#------------------------------------------------------------------------
[656]14# newer(source, target)
15# Return true if source exists and is more recently modified than target,
16# or if source exists and target doesn't.
17# Return false if both exist and target is the same age or newer than source.
18# Raise DistutilsFileError if source does not exist.
19#------------------------------------------------------------------------
20def newer(source,target) :
[657]21  if sys.version[:6]<'2.5.0' :
[656]22    # Version of python being used does not have distutils
23    if not os.path.isfile(source) : sys.exit(1)
24    if os.name!="posix" :
25      # Always assume target is out of date
26      return True
27    else :
28      # running on posix so should be able to use ls command
29      if not os.path.isfile(target) : return True
[663]30      statsource = os.stat(source)
31      stattarget = os.stat(target)
32      return statsource.st_mtime > stattarget.st_mtime
33#      lsSource=run("ls --full-time "+source)
34#      lsTarget=run("ls --full-time "+target)
[656]35      #-rwxrwxrwx 1 jpf4 None 12309 2007-10-21 16:13:47.395793600 -0400 nightlyBuild.py
[663]36#      rexBase=r"(-|r|w|x){10} . (.+) (.+) (.+) (\d\d\d\d-\d\d-\d\d .+) "
37#      rexSource=rexBase+source
38#      rexTarget=rexBase+target
39#      timeSource=(re.findall(rexSource,lsSource['stdout']))[0][4]
40#      timeTarget=(re.findall(rexTarget,lsTarget['stdout']))[0][4]
41#      return timeSource > timeTarget
[656]42     
43  else :
44    import distutils.dep_util 
45    return distutils.dep_util.newer(source,target)
46
47
48#------------------------------------------------------------------------
[647]49# Run an OS command in another process.
[626]50# Examples might be 'svn checkout', 'make test'.
51# Return: command's return code, stdout messages, & stderr messages
52#------------------------------------------------------------------------
53def run(cmd) :
54
[632]55  if sys.version[:6]<'2.4.0' :
[626]56
[631]57    # this machine has a back level of python, so must use an older
58    # techniques to implement this function.  This implementation
59    # runs the command in the same process as the script.
60    # This has the problem that if the command crashes, it will bring
61    # down the script. Another problem is that stderr and stdout are
62    # mingled together
63
64    import commands
65    result = commands.getstatusoutput(cmd)
66    retVal = { 'returnCode':result[0], 'stdout':result[1], 'stderr':'' }
67    return retVal
68
69  else :
70
71    import subprocess
72 
73    p=subprocess.Popen(cmd,shell=True,\
74                       stdout=subprocess.PIPE,\
75                       stderr=subprocess.PIPE)
76    cmdStdout,cmdStderr=p.communicate()
77    cmdRc=p.returncode
78    retVal = { 'returnCode':cmdRc, 'stdout':cmdStdout, 'stderr':cmdStderr }
79    return retVal
80
81
[626]82
[656]83
Note: See TracBrowser for help on using the repository browser.