Conditions | 24 |
Total Lines | 172 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like archivist_main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python3 |
||
186 | def archivist_main(osversion, radioversion=None, softwareversion=None, |
||
187 | localdir=None, radios=True, compressed=True, deleted=True, |
||
188 | hashed=True, hashdict=None, download=True, |
||
189 | extract=True, signed=True, compmethod="7z", |
||
190 | gpg=False, integrity=True, altsw=None, |
||
191 | core=False): |
||
192 | """ |
||
193 | Wrap around multi-autoloader creation code. |
||
194 | Some combination of creating, downloading, hashing, |
||
195 | compressing and moving autoloaders. |
||
196 | |||
197 | :param osversion: OS version, 10.x.y.zzzz. Required. |
||
198 | :type osversion: str |
||
199 | |||
200 | :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed. |
||
201 | :type radioversion: str |
||
202 | |||
203 | :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed. |
||
204 | :type softwareversion: str |
||
205 | |||
206 | :param localdir: Working directory. Local by default. |
||
207 | :type localdir: str |
||
208 | |||
209 | :param radios: Whether to create radio autoloaders. True by default. |
||
210 | :type radios: bool |
||
211 | |||
212 | :param compressed: Whether to compress files. True by default. |
||
213 | :type compressed: bool |
||
214 | |||
215 | :param deleted: Whether to delete uncompressed files. True by default. |
||
216 | :type deleted: bool |
||
217 | |||
218 | :param hashed: Whether to hash files. True by default. |
||
219 | :type hashed: bool |
||
220 | |||
221 | :param hashdict: Dictionary of hash rules, in ~\bbarchivist.ini. |
||
222 | :type hashdict: dict({str: bool}) |
||
223 | |||
224 | :param download: Whether to download bar files. True by default. |
||
225 | :type download: bool |
||
226 | |||
227 | :param extract: Whether to extract bar files. True by default. |
||
228 | :type extract: bool |
||
229 | |||
230 | :param signed: Whether to delete signed files. True by default. |
||
231 | :type signed: bool |
||
232 | |||
233 | :param compmethod: Compression method. Default is "7z", fallback "zip". |
||
234 | :type compmethod: str |
||
235 | |||
236 | :param gpg: Whether to use GnuPG verification. False by default. |
||
237 | :type gpg: bool |
||
238 | |||
239 | :param integrity: Whether to test downloaded bar files. True by default. |
||
240 | :type integrity: bool |
||
241 | |||
242 | :param altsw: Radio software release, if not the same as OS. |
||
243 | :type altsw: str |
||
244 | |||
245 | :param core: Whether to create a core/radio loader. Default is false. |
||
246 | :type core: bool |
||
247 | """ |
||
248 | radioversion = scriptutils.return_radio_version(osversion, radioversion) |
||
249 | softwareversion, swchecked = scriptutils.return_sw_checked(softwareversion, osversion) |
||
250 | if altsw == "checkme": |
||
251 | altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion) |
||
252 | if localdir is None: |
||
253 | localdir = os.getcwd() |
||
254 | if hashed and hashdict is None: |
||
255 | hashdict = hashutils.verifier_config_loader() |
||
256 | hashutils.verifier_config_writer(hashdict) |
||
257 | scriptutils.standard_preamble("archivist", osversion, softwareversion, radioversion, altsw) |
||
258 | |||
259 | # Generate download URLs |
||
260 | baseurl, alturl = scriptutils.get_baseurls(softwareversion, altsw) |
||
261 | osurls, radiourls, cores = utilities.generate_urls(baseurl, osversion, radioversion, True) |
||
262 | osurls = cores if core else osurls |
||
263 | for idx, url in enumerate(osurls): |
||
264 | if "qc8960.factory_sfi" in url: |
||
265 | vzwurl = url |
||
266 | vzwindex = idx |
||
267 | break |
||
268 | if not networkutils.availability(vzwurl): |
||
269 | osurls[vzwindex] = osurls[vzwindex].replace("qc8960.factory_sfi", "qc8960.verizon_sfi") |
||
270 | osurls = list(set(osurls)) # pop duplicates |
||
271 | if altsw: |
||
272 | radiourls2 = [x.replace(baseurl, alturl) for x in radiourls] |
||
273 | radiourls = radiourls2 |
||
274 | del radiourls2 |
||
275 | |||
276 | # Check availability of software releases |
||
277 | scriptutils.check_sw(baseurl, softwareversion, swchecked) |
||
278 | if altsw: |
||
279 | scriptutils.check_radio_sw(alturl, altsw, altchecked) |
||
280 | |||
281 | # Check availability of OS, radio |
||
282 | scriptutils.check_os_bulk(osurls) |
||
283 | radiourls, radioversion = scriptutils.check_radio_bulk(radiourls, radioversion) |
||
284 | |||
285 | # Get 7z executable |
||
286 | compmethod, szexe = scriptutils.get_sz_executable(compmethod) |
||
287 | |||
288 | # Make dirs: bd_o, bd_r, ld_o, ld_r, zd_o, zd_r |
||
289 | dirs = barutils.make_dirs(localdir, osversion, radioversion) |
||
290 | |||
291 | # Download files |
||
292 | if download: |
||
293 | print("BEGIN DOWNLOADING...") |
||
294 | networkutils.download_bootstrap(radiourls + osurls, localdir, 3) |
||
295 | print("ALL FILES DOWNLOADED") |
||
296 | |||
297 | # Test bar files |
||
298 | if integrity: |
||
299 | urllist = osurls + radiourls |
||
300 | scriptutils.test_bar_files(localdir, urllist) |
||
301 | |||
302 | # Extract bar files |
||
303 | if extract: |
||
304 | print("EXTRACTING...") |
||
305 | barutils.extract_bars(localdir) |
||
306 | |||
307 | # Test signed files |
||
308 | if integrity: |
||
309 | scriptutils.test_signed_files(localdir) |
||
310 | |||
311 | # Move bar files |
||
312 | print("MOVING BAR FILES...") |
||
313 | barutils.move_bars(localdir, dirs[0], dirs[1]) |
||
314 | |||
315 | # Create loaders |
||
316 | print("GENERATING LOADERS...") |
||
317 | altradio = altsw is not None |
||
318 | loadergen.generate_loaders(osversion, radioversion, radios, localdir, altradio, core) |
||
319 | |||
320 | # Test loader files |
||
321 | if integrity: |
||
322 | scriptutils.test_loader_files(localdir) |
||
323 | |||
324 | # Remove .signed files |
||
325 | if signed: |
||
326 | print("REMOVING SIGNED FILES...") |
||
327 | barutils.remove_signed_files(localdir) |
||
328 | |||
329 | # If compression = true, compress |
||
330 | if compressed: |
||
331 | print("COMPRESSING...") |
||
332 | archiveutils.compress(localdir, compmethod, szexe, True) |
||
333 | |||
334 | if integrity and compressed: |
||
335 | print("TESTING ARCHIVES...") |
||
336 | archiveutils.verify(localdir, compmethod, szexe, True) |
||
337 | |||
338 | # Move zipped/unzipped loaders |
||
339 | print("MOVING LOADERS...") |
||
340 | barutils.move_loaders(localdir, dirs[2], dirs[3], dirs[4], dirs[5]) |
||
341 | |||
342 | # Get hashes/signatures (if specified) |
||
343 | if hashed: |
||
344 | scriptutils.bulk_hash(dirs, compressed, deleted, radios, hashdict) |
||
345 | if gpg: |
||
346 | scriptutils.bulk_verify(dirs, compressed, deleted, radios) |
||
347 | |||
348 | # Remove uncompressed loaders (if specified) |
||
349 | if deleted: |
||
350 | print("DELETING UNCOMPRESSED LOADERS...") |
||
351 | barutils.remove_unpacked_loaders(dirs[2], dirs[3], radios) |
||
352 | |||
353 | # Delete empty folders |
||
354 | print("REMOVING EMPTY FOLDERS...") |
||
355 | barutils.remove_empty_folders(localdir) |
||
356 | |||
357 | print("\nFINISHED!") |
||
358 | |||
363 |