1 | #! /usr/bin/env python |
---|
2 | |
---|
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 | |
---|
8 | from socket import gethostname |
---|
9 | import sys |
---|
10 | import os |
---|
11 | import re |
---|
12 | |
---|
13 | #------------------------------------------------------------------------ |
---|
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 | #------------------------------------------------------------------------ |
---|
20 | def newer(source,target) : |
---|
21 | if sys.version[:6]<'2.5.0' : |
---|
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 |
---|
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) |
---|
35 | #-rwxrwxrwx 1 jpf4 None 12309 2007-10-21 16:13:47.395793600 -0400 nightlyBuild.py |
---|
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 |
---|
42 | |
---|
43 | else : |
---|
44 | import distutils.dep_util |
---|
45 | return distutils.dep_util.newer(source,target) |
---|
46 | |
---|
47 | |
---|
48 | #------------------------------------------------------------------------ |
---|
49 | # Run an OS command in another process. |
---|
50 | # Examples might be 'svn checkout', 'make test'. |
---|
51 | # Return: command's return code, stdout messages, & stderr messages |
---|
52 | #------------------------------------------------------------------------ |
---|
53 | def run(cmd) : |
---|
54 | |
---|
55 | if sys.version[:6]<'2.4.0' : |
---|
56 | |
---|
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 | |
---|
82 | |
---|
83 | |
---|