2
2
# SPDX-FileCopyrightText: 2020 Daniel Fullmer and robotnix contributors
3
3
# SPDX-License-Identifier: MIT
4
4
5
+ from typing import Any , Dict , List , Tuple
5
6
import argparse
6
7
import json
7
8
import os
8
9
import subprocess
9
- import urllib .request
10
10
11
11
# A full run took approximately 12 minutes total. Needed to set TMPDIR=/tmp
12
12
#
18
18
VENDOR_REPO_BASE = "https://github.com/TheMuppets"
19
19
ROBOTNIX_GIT_MIRRORS = os .environ .get ('ROBOTNIX_GIT_MIRRORS' , '' )
20
20
if ROBOTNIX_GIT_MIRRORS :
21
- MIRRORS = dict (mirror .split ("=" ) for mirror in ROBOTNIX_GIT_MIRRORS ).split ('|' ))
21
+ MIRRORS : Dict [str , str ] = dict (
22
+ (mirror .split ("=" )[0 ], mirror .split ("=" )[1 ])
23
+ for mirror in ROBOTNIX_GIT_MIRRORS .split ('|' )
24
+ )
22
25
else :
23
26
MIRRORS = {}
24
- REMOTE_REFS = {} # url: { ref: rev }
27
+ REMOTE_REFS : Dict [ str , Dict [ str , str ]] = {} # url: { ref: rev }
25
28
26
- def save (filename , data ):
29
+
30
+ def save (filename : str , data : str ) -> None :
27
31
open (filename , 'w' ).write (json .dumps (data , sort_keys = True , indent = 2 , separators = (',' , ': ' )))
28
32
29
- def get_mirrored_url (url ):
33
+
34
+ def get_mirrored_url (url : str ) -> str :
30
35
for mirror_url , mirror_path in MIRRORS .items ():
31
36
if url .startswith (mirror_url ):
32
37
url = url .replace (mirror_url , mirror_path )
33
38
return url
34
39
35
- def ls_remote (url ):
40
+
41
+ def ls_remote (url : str ) -> Dict [str , str ]:
36
42
if url in REMOTE_REFS :
37
43
return REMOTE_REFS [url ]
38
44
39
45
orig_url = url
40
46
url = get_mirrored_url (url )
41
47
42
- remote_info = subprocess .check_output ([ "git" , "ls-remote" , url ]).decode ()
43
- REMOTE_REFS [orig_url ] = dict (reversed (line .split ('\t ' )) for line in remote_info .split ('\n ' ) if line )
48
+ remote_info = subprocess .check_output (["git" , "ls-remote" , url ]).decode ()
49
+ REMOTE_REFS [orig_url ] = {}
50
+ for line in remote_info .split ('\n ' ):
51
+ if line :
52
+ ref , rev = reversed (line .split ('\t ' ))
53
+ REMOTE_REFS [orig_url ][ref ] = rev
44
54
return REMOTE_REFS [orig_url ]
45
55
46
- def checkout_git (url , rev ):
56
+
57
+ def checkout_git (url : str , rev : str ) -> Any :
47
58
print ("Checking out %s %s" % (url , rev ))
48
- json_text = subprocess .check_output ([ "nix-prefetch-git" , "--url" , url , "--rev" , rev ]).decode ()
59
+ json_text = subprocess .check_output (["nix-prefetch-git" , "--url" , url , "--rev" , rev ]).decode ()
49
60
return json .loads (json_text )
50
61
51
- def fetch_relpath (dirs , relpath , url , branch ):
62
+
63
+ def fetch_relpath (dirs : Dict [str , Any ], relpath : str , url : str , branch : str ) -> Any :
52
64
orig_url = url
53
65
url = get_mirrored_url (url )
54
66
55
67
current_rev = dirs .get (relpath , {}).get ('rev' , None )
56
68
refs = ls_remote (url )
57
69
ref = f'refs/heads/{ branch } '
58
70
if ref not in refs :
59
- raise Exception (f'{ url } is missing { ref } ' )
71
+ raise ValueError (f'{ url } is missing { ref } ' )
60
72
newest_rev = refs [ref ]
61
73
if (current_rev != newest_rev
62
74
or ('path' not in dirs [relpath ])
@@ -70,14 +82,14 @@ def fetch_relpath(dirs, relpath, url, branch):
70
82
71
83
72
84
# Fetch device source trees for devices in metadata and save their information into filename
73
- def fetch_device_dirs (metadata , filename , branch ) :
85
+ def fetch_device_dirs (metadata : Any , filename : str , branch : str ) -> Tuple [ Any , Dict [ str , List [ str ]]] :
74
86
if os .path .exists (filename ):
75
87
dirs = json .load (open (filename ))
76
88
else :
77
89
dirs = {}
78
90
79
- dirs_to_fetch = set () # Pairs of (relpath, url)
80
- dirs_fetched = set () # Just strings of relpath
91
+ dirs_to_fetch = set () # Pairs of (relpath, url)
92
+ dirs_fetched = set () # Just strings of relpath
81
93
for device , data in metadata .items ():
82
94
vendor = data ['vendor' ]
83
95
url = f'{ LINEAGE_REPO_BASE } /android_device_{ vendor } _{ device } '
@@ -88,12 +100,12 @@ def fetch_device_dirs(metadata, filename, branch):
88
100
else :
89
101
print (f'SKIP: { branch } branch does not exist for { device } ' )
90
102
91
- dir_dependencies = {} # key -> [ values ].
103
+ dir_dependencies : Dict [ str , List [ str ]] = {} # key -> [ values ].
92
104
while len (dirs_to_fetch ) > 0 :
93
105
relpath , url = dirs_to_fetch .pop ()
94
106
try :
95
107
dir_info = fetch_relpath (dirs , relpath , url , branch )
96
- except :
108
+ except ValueError :
97
109
continue
98
110
99
111
# Also grab any dirs that this one depends on
@@ -105,16 +117,17 @@ def fetch_device_dirs(metadata, filename, branch):
105
117
if dep ['target_path' ] not in dirs_fetched :
106
118
dirs_to_fetch .add ((dep ['target_path' ], f"{ LINEAGE_REPO_BASE } /{ dep ['repository' ]} " ))
107
119
108
- dir_info ['deps' ] = [ dep ['target_path' ] for dep in lineage_dependencies ]
120
+ dir_info ['deps' ] = [dep ['target_path' ] for dep in lineage_dependencies ]
109
121
else :
110
122
dir_info ['deps' ] = []
111
123
112
- save (filename , dirs ) # Save after every step, for resuming
124
+ save (filename , dirs ) # Save after every step, for resuming
113
125
dirs_fetched .add (relpath )
114
126
115
127
return dirs , dir_dependencies
116
128
117
- def fetch_vendor_dirs (metadata , filename , branch ):
129
+
130
+ def fetch_vendor_dirs (metadata : Any , filename : str , branch : str ) -> Any :
118
131
required_vendor = set ()
119
132
for device , data in metadata .items ():
120
133
if 'vendor' in data :
@@ -151,11 +164,13 @@ def fetch_vendor_dirs(metadata, filename, branch):
151
164
152
165
return dirs
153
166
154
- def main ():
167
+
168
+ def main () -> None :
155
169
parser = argparse .ArgumentParser ()
156
170
parser .add_argument ('--branch' , help = "lineageos version" )
157
171
parser .add_argument ('product' , nargs = '*' ,
158
- help = 'product to fetch directory metadata for, specified by <vendor>_<device> (example: google_crosshatch) '
172
+ help = 'product to fetch directory metadata for, specified by <vendor>_<device> '
173
+ '(example: google_crosshatch) '
159
174
'If no products are specified, all products in device-metadata.json will be updated' )
160
175
args = parser .parse_args ()
161
176
@@ -165,10 +180,11 @@ def main():
165
180
metadata = {}
166
181
for product in args .product :
167
182
vendor , device = product .split ('_' , 1 )
168
- metadata [device ] = { 'vendor' : vendor }
183
+ metadata [device ] = {'vendor' : vendor }
184
+
185
+ fetch_device_dirs (metadata , os .path .join (args .branch , 'device-dirs.json' ), args .branch )
186
+ fetch_vendor_dirs (metadata , os .path .join (args .branch , 'vendor-dirs.json' ), args .branch )
169
187
170
- device_dirs , dir_dependencies = fetch_device_dirs (metadata , os .path .join (args .branch , 'device-dirs.json' ), args .branch )
171
- vendor_dirs = fetch_vendor_dirs (metadata , os .path .join (args .branch , 'vendor-dirs.json' ), args .branch )
172
188
173
189
if __name__ == '__main__' :
174
190
main ()
0 commit comments