1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#!/usr/bin/python
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Recipes for NativeClient toolchain packages.
The real entry plumbing is in toolchain_main.py.
"""
import fnmatch
import os
import optparse
import process
import shutil
import StringIO
import sys
def Mkdir(path):
if path[-1] == '/':
path = path[:-1]
head, tail = os.path.split(path)
if not head:
head = '.'
if not os.path.islink(path) and not os.path.exists(path):
if not os.path.exists(head):
Mkdir(head)
os.mkdir(path)
def Rmdir(path):
if os.path.exists(path):
print "Removing: " + path
shutil.rmtree(path)
def Symlink(srcpath, dstpath):
if os.path.islink(dstpath):
linkinfo = os.readlink(dstpath)
if linkinfo == srcpath:
return False
Rmdir(dstpath)
os.symlink(srcpath, dstpath)
return True
def AcceptMatch(name, paterns, filters):
for pat in filters:
if fnmatch.fnmatch(name, pat):
return False
for pat in paterns:
if fnmatch.fnmatch(name, pat):
return True
return False
def UpdateText(dstpath, text):
if os.path.exists(dstpath):
old = open(dstpath, 'r').read()
if old == text:
return False
Mkdir(os.path.dirname(dstpath))
with open(dstpath, 'w') as f:
f.write(text)
return True
def UpdateFile(srcpath, dstpath, verbose=False):
if verbose:
print '%s -> %s\n' % (dstpath, srcpath)
shutil.copy(srcpath, dstpath)
def NeedsUpdate(src, dst):
if not os.path.exists(dst):
return True
stime = os.path.getmtime(src)
dtime = os.path.getmtime(dst)
return (stime > dtime)
def CopyOrLinkNewer(src, dst):
if not NeedsUpdate(src, dst):
return False
if os.path.islink(src):
linkinfo = os.readlink(src)
if os.path.islink(dst):
if os.readlink(dst) == linkinfo:
return False
os.remove(dst)
os.symlink(linkinfo, dst)
else:
UpdateFile(src, dst)
return True
def UpdateFromTo(src, dst, paterns=['*'], filters=[]):
if os.path.isfile(src):
if not AcceptMatch(src, paterns, filters):
return False
Mkdir(os.path.dirname(dst))
return CopyOrLinkNewer(src, dst)
if not os.path.isdir(src):
print "SRC does not exist, skipping: " + src
return False
pathlen = len(os.path.abspath(src))
modified = False
path_offs = len(src)
for root, dirs, files in os.walk(src, followlinks=False):
relroot = root[path_offs+1:]
dstdir = os.path.join(dst, relroot)
srcdir = root
# Don't travel down symlinks
if os.path.islink(root):
continue
# Don't travel down filtered directories
if not AcceptMatch(srcdir, paterns=['*'], filters=filters):
continue
Mkdir(dstdir)
for filename in files:
srcpath = os.path.join(srcdir, filename)
dstpath = os.path.join(dstdir, filename)
if not AcceptMatch(srcpath, paterns, filters):
continue
if CopyOrLinkNewer(srcpath, dstpath):
print " %s -> %s" % (srcpath, dstpath)
modified = True
for filename in dirs:
srcpath = os.path.abspath(os.path.join(root, filename))
dstrel = os.path.abspath(os.path.join(root, filename))[pathlen+1:]
dstpath = os.path.join(dst, dstrel)
if os.path.islink(srcpath):
if CopyOrLinkNewer(srcpath, dstpath):
modified = True
print " %s -> %s" % (srcpath, dstpath)
if modified:
print "Update From To %s -> %s" % (src, dst)
return modified
|