Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / native_client / toolchain_build / file_update.py
1 #!/usr/bin/python
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.
5
6 """Recipes for NativeClient toolchain packages.
7
8 The real entry plumbing is in toolchain_main.py.
9 """
10
11 import fnmatch
12 import os
13 import optparse
14 import process
15 import shutil
16 import StringIO
17 import sys
18
19 def Mkdir(path):
20   if path[-1] == '/':
21     path = path[:-1]
22   head, tail = os.path.split(path)
23
24   if not head:
25     head = '.'
26
27   if not os.path.islink(path) and not os.path.exists(path):
28     if not os.path.exists(head):
29       Mkdir(head)
30     os.mkdir(path)
31
32
33 def Rmdir(path):
34   if os.path.exists(path):
35     print "Removing: " + path
36     shutil.rmtree(path)
37
38
39 def Symlink(srcpath, dstpath):
40   if os.path.islink(dstpath):
41     linkinfo = os.readlink(dstpath)
42     if linkinfo == srcpath:
43       return False
44   Rmdir(dstpath)
45   os.symlink(srcpath, dstpath)
46   return True
47
48
49 def AcceptMatch(name, paterns, filters):
50   for pat in filters:
51     if fnmatch.fnmatch(name, pat):
52       return False
53   for pat in paterns:
54     if fnmatch.fnmatch(name, pat):
55       return True
56   return False
57
58
59 def UpdateText(dstpath, text):
60   if os.path.exists(dstpath):
61     old = open(dstpath, 'r').read()
62     if old == text:
63       return False
64
65   Mkdir(os.path.dirname(dstpath))
66   with open(dstpath, 'w') as f:
67     f.write(text)
68   return True
69
70
71 def UpdateFile(srcpath, dstpath, verbose=False):
72   if verbose:
73     print '%s -> %s\n' % (dstpath, srcpath)
74   shutil.copy(srcpath, dstpath)
75
76
77 def NeedsUpdate(src, dst):
78   if not os.path.exists(dst):
79     return True
80
81   stime = os.path.getmtime(src)
82   dtime = os.path.getmtime(dst)
83   return (stime > dtime)
84
85
86 def CopyOrLinkNewer(src, dst):
87   if not NeedsUpdate(src, dst):
88     return False
89
90   if os.path.islink(src):
91     linkinfo = os.readlink(src)
92     if os.path.islink(dst):
93       if os.readlink(dst) == linkinfo:
94         return False
95       os.remove(dst)
96     os.symlink(linkinfo, dst)
97   else:
98     UpdateFile(src, dst)
99   return True
100
101
102 def UpdateFromTo(src, dst, paterns=['*'], filters=[]):
103   if os.path.isfile(src):
104     if not AcceptMatch(src, paterns, filters):
105       return False
106
107     Mkdir(os.path.dirname(dst))
108     return CopyOrLinkNewer(src, dst)
109
110   if not os.path.isdir(src):
111     print "SRC does not exist, skipping: " + src
112     return False
113
114   pathlen = len(os.path.abspath(src))
115   modified = False
116   path_offs = len(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)
120     srcdir = root
121
122     # Don't travel down symlinks
123     if os.path.islink(root):
124       continue
125
126     # Don't travel down filtered directories
127     if not AcceptMatch(srcdir, paterns=['*'], filters=filters):
128       continue
129
130     Mkdir(dstdir)
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):
135         continue
136       if CopyOrLinkNewer(srcpath, dstpath):
137         print "  %s -> %s" % (srcpath, dstpath)
138         modified = True
139
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):
146           modified = True
147           print "  %s -> %s" % (srcpath, dstpath)
148   if modified:
149     print "Update From To %s -> %s" % (src, dst)
150   return modified