util: Add reviewers using account ID's when possible
[gem5.git] / util / gerrit-bot / extract_gitcookies.py
1 # Copyright (c) 2020 The Regents of the University of California
2 # All Rights Reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 import argparse
28
29 def parse_gitcookies_line(raw):
30 # if this is a line from .gitcookies, the delimiter is `\t`
31 auth_info = raw.strip().split('\t')
32 if len(auth_info) < 7:
33 # if this is a line from auth script, the delimiter is `,`
34 auth_info = raw.strip().split(',')
35 if len(auth_info) != 7:
36 return None, None
37 auth_info = auth_info[-1]
38 auth_info = auth_info[4:].split("=")
39 username = auth_info[0]
40 password = auth_info[1]
41 return username, password
42
43 def parse_gitcookies(input_path):
44 username_password_dict = {}
45 with open(input_path, "r") as input_stream:
46 for line in input_stream:
47 username, password = parse_gitcookies_line(line)
48 if not username:
49 continue
50 username_password_dict[username] = password
51 return username_password_dict
52
53 if __name__ == "__main__":
54 parser = argparse.ArgumentParser(
55 description=("Extract username and password from .gitcookies"
56 "or from the script used to write .gitcookies file"))
57 parser.add_argument("input",
58 help = ("Path to a .gitcookies file or a file with "
59 "a similar format"))
60 parser.add_argument("output", help="Path to the output file")
61 args = parser.parse_args()
62 username_password_dict = parse_gitcookies(args.input)
63 with open(args.output, "w") as output_stream:
64 for username, password in username_password_dict.items():
65 output_stream.write(f"{username}\n{password}\n")