summary refs log tree commit diff
path: root/src/etc/snapshot.py
blob: b44adc040b7437ae0c68f50b9f57c56c02cb7448 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Copyright 2011-2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import re, os, sys, glob, tarfile, shutil, subprocess, tempfile, distutils.spawn

try:
  import hashlib
  sha_func = hashlib.sha1
except ImportError:
  import sha
  sha_func = sha.new

def scrub(b):
  if sys.version_info >= (3,) and type(b) == bytes:
    return b.decode('ascii')
  else:
    return b

src_dir = scrub(os.getenv("CFG_SRC_DIR"))
if not src_dir:
  raise Exception("missing env var CFG_SRC_DIR")

snapshotfile = os.path.join(src_dir, "src", "snapshots.txt")
download_url_base = "http://static.rust-lang.org/stage0-snapshots"
download_dir_base = "dl"
download_unpack_base = os.path.join(download_dir_base, "unpack")

snapshot_files = {
    "linux": ["bin/rustc"],
    "macos": ["bin/rustc"],
    "winnt": ["bin/rustc.exe"],
    "freebsd": ["bin/rustc"],
    }

winnt_runtime_deps = ["libgcc_s_dw2-1.dll",
                      "libstdc++-6.dll"]

def parse_line(n, line):
  global snapshotfile

  if re.match(r"\s*$", line): return None

  if re.match(r"^T\s*$", line): return None

  match = re.match(r"\s+([\w_-]+) ([a-fA-F\d]{40})\s*$", line)
  if match:
    return { "type": "file",
             "platform": match.group(1),
             "hash": match.group(2).lower() }

  match = re.match(r"([ST]) (\d{4}-\d{2}-\d{2}) ([a-fA-F\d]+)\s*$", line);
  if (not match):
    raise Exception("%s:%d:E syntax error: " % (snapshotfile, n))
  return {"type": "snapshot",
          "date": match.group(2),
          "rev": match.group(3)}


def partial_snapshot_name(date, rev, platform):
  return ("rust-stage0-%s-%s-%s.tar.bz2"
          % (date, rev, platform))

def full_snapshot_name(date, rev, platform, hsh):
  return ("rust-stage0-%s-%s-%s-%s.tar.bz2"
          % (date, rev, platform, hsh))


def get_kernel(triple):
    os_name = triple.split('-')[-1]
    #scrub(os.getenv("CFG_ENABLE_MINGW_CROSS")):
    if os_name == "nt" or os_name == "mingw32":
        return "winnt"
    if os_name == "darwin":
        return "macos"
    if os_name == "freebsd":
        return "freebsd"
    return "linux"

def get_cpu(triple):
    arch = triple.split('-')[0]
    if arch == "i686":
      return "i386"
    return arch

def get_platform(triple):
  return "%s-%s" % (get_kernel(triple), get_cpu(triple))


def cmd_out(cmdline):
    p = subprocess.Popen(cmdline,
                         stdout=subprocess.PIPE)
    return scrub(p.communicate()[0].strip())


def local_rev_info(field):
    return cmd_out(["git", "--git-dir=" + os.path.join(src_dir, ".git"),
                    "log", "-n", "1",
                    "--format=%%%s" % field, "HEAD"])


def local_rev_full_sha():
    return local_rev_info("H").split()[0]


def local_rev_short_sha():
    return local_rev_info("h").split()[0]


def local_rev_committer_date():
    return local_rev_info("ci")

def get_url_to_file(u,f):
    # no security issue, just to stop partial download leaving a stale file
    tmpf = f + '.tmp'

    returncode = -1
    if distutils.spawn.find_executable("curl"):
        returncode = subprocess.call(["curl", "-o", tmpf, u])
    elif distutils.spawn.find_executable("wget"):
        returncode = subprocess.call(["wget", "-O", tmpf, u])

    if returncode != 0:
        try:
            os.unlink(tmpf)
        except OSError as e:
            pass
        raise Exception("failed to fetch url")
    os.rename(tmpf, f)

def snap_filename_hash_part(snap):
  match = re.match(r".*([a-fA-F\d]{40}).tar.bz2$", snap)
  if not match:
    raise Exception("unable to find hash in filename: " + snap)
  return match.group(1)

def hash_file(x):
    h = sha_func()
    h.update(open(x, "rb").read())
    return scrub(h.hexdigest())

# Returns a list of paths of Rust's system runtime dependencies
def get_winnt_runtime_deps():
    runtime_deps = []
    path_dirs = os.environ["PATH"].split(';')
    for name in winnt_runtime_deps:
      for dir in path_dirs:
        matches = glob.glob(os.path.join(dir, name))
        if matches:
          runtime_deps.append(matches[0])
          break
      else:
        raise Exception("Could not find runtime dependency: %s" % name)
    return runtime_deps

def make_snapshot(stage, triple):
    kernel = get_kernel(triple)
    platform = get_platform(triple)
    rev = local_rev_short_sha()
    date = local_rev_committer_date().split()[0]

    file0 = partial_snapshot_name(date, rev, platform)

    def in_tar_name(fn):
      cs = re.split(r"[\\/]", fn)
      if len(cs) >= 2:
        return os.sep.join(cs[-2:])

    tar = tarfile.open(file0, "w:bz2")

    for name in snapshot_files[kernel]:
      dir = stage
      if stage == "stage1" and re.match(r"^lib/(lib)?std.*", name):
        dir = "stage0"
      fn_glob = os.path.join(triple, dir, name)
      matches = glob.glob(fn_glob)
      if not matches:
        raise Exception("Not found file with name like " + fn_glob)
      if len(matches) == 1:
        tar.add(matches[0], "rust-stage0/" + in_tar_name(matches[0]))
      else:
        raise Exception("Found stale files: \n  %s\n"
                        "Please make a clean build." % "\n  ".join(matches))

    if kernel=="winnt":
      for path in get_winnt_runtime_deps():
        tar.add(path, "rust-stage0/bin/" + os.path.basename(path))
      tar.add(os.path.join(os.path.dirname(__file__), "third-party"),
              "rust-stage0/bin/third-party")

    tar.close()

    h = hash_file(file0)
    file1 = full_snapshot_name(date, rev, platform, h)

    shutil.move(file0, file1)

    return file1

def curr_snapshot_rev():
  i = 0
  found_snap = False
  date = None
  rev = None

  f = open(snapshotfile)
  for line in f.readlines():
    i += 1
    parsed = parse_line(i, line)
    if (not parsed): continue

    if parsed["type"] == "snapshot":
      date = parsed["date"]
      rev = parsed["rev"]
      found_snap = True
      break

  if not found_snap:
    raise Exception("no snapshot entries in file")

  return (date, rev)

def determine_curr_snapshot(triple):
  i = 0
  platform = get_platform(triple)

  found_file = False
  found_snap = False
  hsh = None
  date = None
  rev = None

  f = open(snapshotfile)
  for line in f.readlines():
    i += 1
    parsed = parse_line(i, line)
    if (not parsed): continue

    if found_snap and parsed["type"] == "file":
      if parsed["platform"] == platform:
        hsh = parsed["hash"]
        found_file = True
        break;
    elif parsed["type"] == "snapshot":
      date = parsed["date"]
      rev = parsed["rev"]
      found_snap = True

  if not found_snap:
    raise Exception("no snapshot entries in file")

  if not found_file:
    raise Exception("no snapshot file found for platform %s, rev %s" %
                    (platform, rev))

  return full_snapshot_name(date, rev, platform, hsh)