source: branches/testScripts/NBosCommand.py @ 681

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

Added svn property svn:eol-style native

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