|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright 2022 DeepMind Technologies Ltd. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Generate version information from replays.""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +from absl import app |
| 20 | + |
| 21 | +from pysc2 import run_configs |
| 22 | +from pysc2.lib import replay |
| 23 | + |
| 24 | + |
| 25 | +def main(argv): |
| 26 | + if len(argv) <= 1: |
| 27 | + raise app.UsageError( |
| 28 | + "Please give one or more replay files/directories to scan as argv.") |
| 29 | + |
| 30 | + run_config = run_configs.get() |
| 31 | + |
| 32 | + # Use a set over the full version struct to catch cases where Blizzard failed |
| 33 | + # to update the version field properly (eg 5.0.0). |
| 34 | + versions = set() |
| 35 | + |
| 36 | + def replay_version(replay_path): |
| 37 | + """Query a replay for information.""" |
| 38 | + if replay_path.lower().endswith("sc2replay"): |
| 39 | + data = run_config.replay_data(replay_path) |
| 40 | + try: |
| 41 | + version = replay.get_replay_version(data) |
| 42 | + except (ValueError, KeyError): |
| 43 | + pass # Either corrupt or just old. |
| 44 | + except Exception as e: # pylint: disable=broad-except |
| 45 | + print("Invalid replay:", replay_path, e) |
| 46 | + else: |
| 47 | + versions.add(version) |
| 48 | + |
| 49 | + try: |
| 50 | + for path in argv[1:]: |
| 51 | + if os.path.isdir(path): |
| 52 | + for root, _, files in os.walk(path): |
| 53 | + for file in files: |
| 54 | + replay_version(os.path.join(root, file)) |
| 55 | + else: |
| 56 | + replay_version(path) |
| 57 | + except KeyboardInterrupt: |
| 58 | + pass |
| 59 | + |
| 60 | + for version in sorted(versions): |
| 61 | + print(version) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + app.run(main) |
0 commit comments