Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2018 The Chromium 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 | import os |
| 7 | import subprocess |
| 8 | import json |
| 9 | import sys |
| 10 | |
| 11 | from third_party import httplib2 |
| 12 | |
| 13 | import ninjalog_uploader |
| 14 | |
| 15 | THIS_DIR = os.path.dirname(__file__) |
| 16 | UPLOADER = os.path.join(THIS_DIR, 'ninjalog_uploader.py') |
| 17 | CONFIG = os.path.join(THIS_DIR, 'ninjalog.cfg') |
| 18 | VERSION = 1 |
| 19 | |
| 20 | |
| 21 | def LoadConfig(): |
| 22 | if os.path.isfile(CONFIG): |
| 23 | with open(CONFIG, 'rb') as f: |
| 24 | config = json.load(f) |
| 25 | if config['version'] == VERSION: |
| 26 | config['countdown'] -= 1 |
| 27 | return config |
| 28 | |
| 29 | return { |
| 30 | 'is-googler': ninjalog_uploader.IsGoogler( |
| 31 | 'chromium-build-stats.appspot.com'), |
| 32 | 'countdown': 10, |
| 33 | 'version': VERSION, |
| 34 | } |
| 35 | |
| 36 | |
| 37 | def SaveConfig(config): |
| 38 | with open(CONFIG, 'wb') as f: |
| 39 | json.dump(config, f) |
| 40 | |
| 41 | |
| 42 | def ShowMessage(countdown): |
| 43 | print """ |
| 44 | Your ninjalog will be uploaded to build stats server. Uploaded log will be used |
| 45 | to analyze user side build performance. |
| 46 | |
| 47 | The following information will be uploaded with ninjalog. |
| 48 | * OS (e.g. Win, Mac or Linux) |
| 49 | * build directory (e.g. /home/foo/chromium/src/out/Release) |
| 50 | * hostname |
| 51 | * number of cpu cores of building machine |
| 52 | * cmdline passed to ninja (e.g. ninja -C out/Default -j1024 chrome) |
| 53 | * build config (e.g. use_goma=true, is_component_build=true, etc) |
| 54 | |
| 55 | Uploading ninjalog will be started after you run autoninja another %d time. |
| 56 | |
| 57 | If you don't want to upload ninjalog, please run following command. |
| 58 | $ %s opt-out |
| 59 | |
| 60 | If you allow upload ninjalog from next autoninja run, please run the following |
| 61 | command. |
| 62 | $ %s opt-in |
| 63 | |
| 64 | If you have question about this, please send mail to infra-dev@chromium.org |
| 65 | |
| 66 | """ % (countdown, __file__, __file__) |
| 67 | |
| 68 | |
| 69 | def main(): |
| 70 | config = LoadConfig() |
| 71 | |
| 72 | if len(sys.argv) == 2 and sys.argv[1] == 'opt-in': |
| 73 | config['opt-in'] = True |
| 74 | config['countdown'] = 0 |
| 75 | SaveConfig(config) |
| 76 | print('ninjalog upload is opted in.') |
| 77 | return 0 |
| 78 | |
| 79 | if len(sys.argv) == 2 and sys.argv[1] == 'opt-out': |
| 80 | config['opt-in'] = False |
| 81 | SaveConfig(config) |
| 82 | print('ninjalog upload is opted out.') |
| 83 | return 0 |
| 84 | |
| 85 | SaveConfig(config) |
| 86 | |
| 87 | if 'opt-in' in config and not config['opt-in']: |
| 88 | # Upload is opted out. |
| 89 | return 0 |
| 90 | |
| 91 | if not config.get("is-googler", False): |
| 92 | # Not googler. |
| 93 | return 0 |
| 94 | |
| 95 | if config.get("countdown", 0) > 0: |
| 96 | # Need to show message. |
| 97 | ShowMessage(config["countdown"]) |
| 98 | return 0 |
| 99 | |
| 100 | if len(sys.argv) == 1: |
| 101 | # dry-run for debugging. |
| 102 | print("upload ninjalog dry-run") |
| 103 | return 0 |
| 104 | |
| 105 | # Run upload script without wait. |
| 106 | devnull = open(os.devnull, "w") |
| 107 | subprocess.Popen([sys.executable, UPLOADER] + sys.argv[1:], |
| 108 | stdout=devnull, stderr=devnull) |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | sys.exit(main()) |