Conditions | 25 |
Total Lines | 179 |
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 |
||
193 | def archivist_main(osversion, radioversion=None, softwareversion=None, |
||
194 | localdir=None, radios=True, compressed=True, deleted=True, |
||
195 | hashed=True, hashdict=None, download=True, |
||
196 | extract=True, signed=True, compmethod="7z", |
||
197 | gpg=False, integrity=True, altsw=None, |
||
198 | core=False, oldstyle=False): |
||
199 | """ |
||
200 | Wrap around multi-autoloader creation code. |
||
201 | Some combination of creating, downloading, hashing, |
||
202 | compressing and moving autoloaders. |
||
203 | |||
204 | :param osversion: OS version, 10.x.y.zzzz. Required. |
||
205 | :type osversion: str |
||
206 | |||
207 | :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed. |
||
208 | :type radioversion: str |
||
209 | |||
210 | :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed. |
||
211 | :type softwareversion: str |
||
212 | |||
213 | :param localdir: Working directory. Local by default. |
||
214 | :type localdir: str |
||
215 | |||
216 | :param radios: Whether to create radio autoloaders. True by default. |
||
217 | :type radios: bool |
||
218 | |||
219 | :param compressed: Whether to compress files. True by default. |
||
220 | :type compressed: bool |
||
221 | |||
222 | :param deleted: Whether to delete uncompressed files. True by default. |
||
223 | :type deleted: bool |
||
224 | |||
225 | :param hashed: Whether to hash files. True by default. |
||
226 | :type hashed: bool |
||
227 | |||
228 | :param hashdict: Dictionary of hash rules, in ~\bbarchivist.ini. |
||
229 | :type hashdict: dict({str: bool}) |
||
230 | |||
231 | :param download: Whether to download bar files. True by default. |
||
232 | :type download: bool |
||
233 | |||
234 | :param extract: Whether to extract bar files. True by default. |
||
235 | :type extract: bool |
||
236 | |||
237 | :param signed: Whether to delete signed files. True by default. |
||
238 | :type signed: bool |
||
239 | |||
240 | :param compmethod: Compression method. Default is "7z", fallback "zip". |
||
241 | :type compmethod: str |
||
242 | |||
243 | :param gpg: Whether to use GnuPG verification. False by default. |
||
244 | :type gpg: bool |
||
245 | |||
246 | :param integrity: Whether to test downloaded bar files. True by default. |
||
247 | :type integrity: bool |
||
248 | |||
249 | :param altsw: Radio software release, if not the same as OS. |
||
250 | :type altsw: str |
||
251 | |||
252 | :param core: Whether to create a core/radio loader. Default is false. |
||
253 | :type core: bool |
||
254 | |||
255 | :param oldstyle: Whether to make old-style checksum files. Default is false. |
||
256 | :type oldstyle: bool |
||
257 | """ |
||
258 | radioversion = scriptutils.return_radio_version(osversion, radioversion) |
||
259 | softwareversion, swchecked = scriptutils.return_sw_checked(softwareversion, osversion) |
||
260 | if altsw == "checkme": |
||
261 | altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion) |
||
262 | if localdir is None: |
||
263 | localdir = os.getcwd() |
||
264 | if hashed and hashdict is None: |
||
265 | hashdict = hashutils.verifier_config_loader() |
||
266 | hashutils.verifier_config_writer(hashdict) |
||
267 | scriptutils.standard_preamble("archivist", osversion, softwareversion, radioversion, altsw) |
||
268 | |||
269 | # Generate download URLs |
||
270 | baseurl, alturl = scriptutils.get_baseurls(softwareversion, altsw) |
||
271 | osurls, radiourls, cores = utilities.generate_urls(baseurl, osversion, radioversion, True) |
||
272 | osurls = cores if core else osurls |
||
273 | for idx, url in enumerate(osurls): |
||
274 | if "qc8960.factory_sfi" in url: |
||
275 | vzwurl = url |
||
276 | vzwindex = idx |
||
277 | break |
||
278 | if not networkutils.availability(vzwurl): |
||
279 | osurls[vzwindex] = osurls[vzwindex].replace("qc8960.factory_sfi", "qc8960.verizon_sfi") |
||
280 | osurls = list(set(osurls)) # pop duplicates |
||
281 | if altsw: |
||
282 | radiourls2 = [x.replace(baseurl, alturl) for x in radiourls] |
||
283 | radiourls = radiourls2 |
||
284 | del radiourls2 |
||
285 | |||
286 | # Check availability of software releases |
||
287 | scriptutils.check_sw(baseurl, softwareversion, swchecked) |
||
288 | if altsw: |
||
289 | scriptutils.check_radio_sw(alturl, altsw, altchecked) |
||
290 | |||
291 | # Check availability of OS, radio |
||
292 | scriptutils.check_os_bulk(osurls) |
||
293 | radiourls, radioversion = scriptutils.check_radio_bulk(radiourls, radioversion) |
||
294 | |||
295 | # Get 7z executable |
||
296 | compmethod, szexe = scriptutils.get_sz_executable(compmethod) |
||
297 | |||
298 | # Make dirs: bd_o, bd_r, ld_o, ld_r, zd_o, zd_r |
||
299 | dirs = barutils.make_dirs(localdir, osversion, radioversion) |
||
300 | |||
301 | # Download files |
||
302 | if download: |
||
303 | print("BEGIN DOWNLOADING...") |
||
304 | networkutils.download_bootstrap(radiourls + osurls, localdir, 3) |
||
305 | print("ALL FILES DOWNLOADED") |
||
306 | |||
307 | # Test bar files |
||
308 | if integrity: |
||
309 | urllist = osurls + radiourls |
||
310 | scriptutils.test_bar_files(localdir, urllist) |
||
311 | |||
312 | # Extract bar files |
||
313 | if extract: |
||
314 | print("EXTRACTING...") |
||
315 | barutils.extract_bars(localdir) |
||
316 | |||
317 | # Test signed files |
||
318 | if integrity: |
||
319 | scriptutils.test_signed_files(localdir) |
||
320 | |||
321 | # Move bar files |
||
322 | print("MOVING BAR FILES...") |
||
323 | barutils.move_bars(localdir, dirs[0], dirs[1]) |
||
324 | |||
325 | # Create loaders |
||
326 | print("GENERATING LOADERS...") |
||
327 | altradio = altsw is not None |
||
328 | loadergen.generate_loaders(osversion, radioversion, radios, localdir, altradio, core) |
||
329 | |||
330 | # Test loader files |
||
331 | if integrity: |
||
332 | scriptutils.test_loader_files(localdir) |
||
333 | |||
334 | # Remove .signed files |
||
335 | if signed: |
||
336 | print("REMOVING SIGNED FILES...") |
||
337 | barutils.remove_signed_files(localdir) |
||
338 | |||
339 | # If compression = true, compress |
||
340 | if compressed: |
||
341 | print("COMPRESSING...") |
||
342 | archiveutils.compress(localdir, compmethod, szexe, True) |
||
343 | |||
344 | if integrity and compressed: |
||
345 | print("TESTING ARCHIVES...") |
||
346 | archiveutils.verify(localdir, compmethod, szexe, True) |
||
347 | |||
348 | # Move zipped/unzipped loaders |
||
349 | print("MOVING LOADERS...") |
||
350 | barutils.move_loaders(localdir, dirs[2], dirs[3], dirs[4], dirs[5]) |
||
351 | |||
352 | # Get hashes/signatures (if specified) |
||
353 | if hashed: |
||
354 | if oldstyle: |
||
355 | scriptutils.bulk_hash(dirs, compressed, deleted, radios, hashdict) |
||
356 | else: |
||
357 | scriptutils.bulk_info(dirs, osversion, compressed, deleted, radios, |
||
358 | radioversion, softwareversion) |
||
359 | if gpg: |
||
360 | scriptutils.bulk_verify(dirs, compressed, deleted, radios) |
||
361 | |||
362 | # Remove uncompressed loaders (if specified) |
||
363 | if deleted: |
||
364 | print("DELETING UNCOMPRESSED LOADERS...") |
||
365 | barutils.remove_unpacked_loaders(dirs[2], dirs[3], radios) |
||
366 | |||
367 | # Delete empty folders |
||
368 | print("REMOVING EMPTY FOLDERS...") |
||
369 | barutils.remove_empty_folders(localdir) |
||
370 | |||
371 | print("\nFINISHED!") |
||
372 | |||
377 |