1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5import hashlib
6import json
7import os
8
9from mozpack.files import FileFinder
10import mozpack.path as mozpath
11
12
13def sha512_digest(data):
14    '''
15    Generate the SHA-512 digest of `data` and return it as a hex string.
16    '''
17    return hashlib.sha512(data).hexdigest()
18
19
20def get_filename_with_digest(name, contents):
21    '''
22    Return the filename that will be used to store the generated file
23    in the S3 bucket, consisting of the SHA-512 digest of `contents`
24    joined with the relative path `name`.
25    '''
26    digest = sha512_digest(contents)
27    return mozpath.join(digest, name)
28
29
30def get_generated_sources():
31    '''
32    Yield tuples of `(objdir-rel-path, file)` for generated source files
33    in this objdir, where `file` is either an absolute path to the file or
34    a `mozpack.File` instance.
35    '''
36    import buildconfig
37
38    # First, get the list of generated sources produced by the build backend.
39    gen_sources = os.path.join(buildconfig.topobjdir, 'generated-sources.json')
40    with open(gen_sources, 'rb') as f:
41        data = json.load(f)
42    for f in data['sources']:
43        yield f, mozpath.join(buildconfig.topobjdir, f)
44    # Next, return all the files in $objdir/ipc/ipdl/_ipdlheaders.
45    base = 'ipc/ipdl/_ipdlheaders'
46    finder = FileFinder(mozpath.join(buildconfig.topobjdir, base))
47    for p, f in finder.find('**/*.h'):
48        yield mozpath.join(base, p), f
49    # Next, return any Rust source files that were generated into the Rust
50    # object directory.
51    rust_build_kind = 'debug' if buildconfig.substs.get('MOZ_DEBUG_RUST') else 'release'
52    base = mozpath.join('toolkit/library',
53                        buildconfig.substs['RUST_TARGET'],
54                        rust_build_kind,
55                        'build')
56    finder = FileFinder(mozpath.join(buildconfig.topobjdir, base))
57    for p, f in finder.find('**/*.rs'):
58        yield mozpath.join(base, p), f
59
60
61def get_s3_region_and_bucket():
62    '''
63    Return a tuple of (region, bucket) giving the AWS region and S3
64    bucket to which generated sources should be uploaded.
65    '''
66    region = 'us-west-2'
67    level = os.environ.get('MOZ_SCM_LEVEL', '1')
68    bucket = {
69        '1': 'gecko-generated-sources-l1',
70        '2': 'gecko-generated-sources-l2',
71        '3': 'gecko-generated-sources',
72    }[level]
73    return (region, bucket)
74