2 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Recipes for NativeClient toolchain packages.
8 The real entry plumbing is in toolchain_main.py.
22 head, tail = os.path.split(path)
27 if not os.path.islink(path) and not os.path.exists(path):
28 if not os.path.exists(head):
34 if os.path.exists(path):
35 print "Removing: " + path
39 def Symlink(srcpath, dstpath):
40 if os.path.islink(dstpath):
41 linkinfo = os.readlink(dstpath)
42 if linkinfo == srcpath:
45 os.symlink(srcpath, dstpath)
49 def AcceptMatch(name, paterns, filters):
51 if fnmatch.fnmatch(name, pat):
54 if fnmatch.fnmatch(name, pat):
59 def UpdateText(dstpath, text):
60 if os.path.exists(dstpath):
61 old = open(dstpath, 'r').read()
65 Mkdir(os.path.dirname(dstpath))
66 with open(dstpath, 'w') as f:
71 def UpdateFile(srcpath, dstpath, verbose=False):
73 print '%s -> %s\n' % (dstpath, srcpath)
74 shutil.copy(srcpath, dstpath)
77 def NeedsUpdate(src, dst):
78 if not os.path.exists(dst):
81 stime = os.path.getmtime(src)
82 dtime = os.path.getmtime(dst)
83 return (stime > dtime)
86 def CopyOrLinkNewer(src, dst):
87 if not NeedsUpdate(src, dst):
90 if os.path.islink(src):
91 linkinfo = os.readlink(src)
92 if os.path.islink(dst):
93 if os.readlink(dst) == linkinfo:
96 os.symlink(linkinfo, dst)
102 def UpdateFromTo(src, dst, paterns=['*'], filters=[]):
103 if os.path.isfile(src):
104 if not AcceptMatch(src, paterns, filters):
107 Mkdir(os.path.dirname(dst))
108 return CopyOrLinkNewer(src, dst)
110 if not os.path.isdir(src):
111 print "SRC does not exist, skipping: " + src
114 pathlen = len(os.path.abspath(src))
117 for root, dirs, files in os.walk(src, followlinks=False):
118 relroot = root[path_offs+1:]
119 dstdir = os.path.join(dst, relroot)
122 # Don't travel down symlinks
123 if os.path.islink(root):
126 # Don't travel down filtered directories
127 if not AcceptMatch(srcdir, paterns=['*'], filters=filters):
131 for filename in files:
132 srcpath = os.path.join(srcdir, filename)
133 dstpath = os.path.join(dstdir, filename)
134 if not AcceptMatch(srcpath, paterns, filters):
136 if CopyOrLinkNewer(srcpath, dstpath):
137 print " %s -> %s" % (srcpath, dstpath)
140 for filename in dirs:
141 srcpath = os.path.abspath(os.path.join(root, filename))
142 dstrel = os.path.abspath(os.path.join(root, filename))[pathlen+1:]
143 dstpath = os.path.join(dst, dstrel)
144 if os.path.islink(srcpath):
145 if CopyOrLinkNewer(srcpath, dstpath):
147 print " %s -> %s" % (srcpath, dstpath)
149 print "Update From To %s -> %s" % (src, dst)