| Conditions | 15 |
| Total Lines | 124 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 lazyloader_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 |
||
| 217 | |||
| 218 | def lazyloader_main(device, osversion, radioversion=None, softwareversion=None, localdir=None, autoloader=False, download=True, altsw=None, core=False): |
||
|
2 ignored issues
–
show
|
|||
| 219 | """ |
||
| 220 | Wrap the tools necessary to make one autoloader. |
||
| 221 | |||
| 222 | :param device: Device family to create loader for. |
||
| 223 | :type device: int |
||
| 224 | |||
| 225 | :param osversion: OS version, 10.x.y.zzzz. |
||
| 226 | :type osversion: str |
||
| 227 | |||
| 228 | :param radioversion: Radio version, 10.x.y.zzzz. |
||
| 229 | :type radioversion: str |
||
| 230 | |||
| 231 | :param softwareversion: Software version, 10.x.y.zzzz. |
||
| 232 | :type softwareversion: str |
||
| 233 | |||
| 234 | :param localdir: Working path. Default is local dir. |
||
| 235 | :type localdir: str |
||
| 236 | |||
| 237 | :param autoloader: Whether to run loader. Default is false. Windows-only. |
||
| 238 | :type autoloader: bool |
||
| 239 | |||
| 240 | :param download: Whether to download files. Default is true. |
||
| 241 | :type download: bool |
||
| 242 | |||
| 243 | :param altsw: Radio software release, if not the same as OS. |
||
| 244 | :type altsw: str |
||
| 245 | |||
| 246 | :param core: Whether to create a core/radio loader. Default is false. |
||
| 247 | :type core: bool |
||
| 248 | """ |
||
| 249 | radioversion = scriptutils.return_radio_version(osversion, radioversion) |
||
| 250 | softwareversion, swc = scriptutils.return_sw_checked(softwareversion, osversion) |
||
| 251 | if altsw == "checkme": |
||
| 252 | altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion) |
||
| 253 | scriptutils.standard_preamble("lazyloader", osversion, softwareversion, radioversion, altsw) |
||
| 254 | print("DEVICE: {0}".format(bbconstants.DEVICES[device])) |
||
| 255 | |||
| 256 | # Make dirs |
||
| 257 | bd_o, bd_r, ld_o, ld_r, zd_o, zd_r = barutils.make_dirs(localdir, osversion, radioversion) |
||
| 258 | osurl = radiourl = None |
||
| 259 | |||
| 260 | # Create download URLs |
||
| 261 | baseurl = utilities.create_base_url(softwareversion) |
||
| 262 | if altsw: |
||
| 263 | alturl = utilities.create_base_url(altsw) |
||
| 264 | osurl, radiourl = utilities.generate_lazy_urls(softwareversion, osversion, radioversion, device) |
||
| 265 | if altsw: |
||
| 266 | radiourl = radiourl.replace(baseurl, alturl) |
||
|
1 ignored issue
–
show
|
|||
| 267 | if core: |
||
| 268 | osurl = osurl.replace(".desktop", "") |
||
| 269 | |||
| 270 | #Terminate if device is STL100-1 and OS >= 10.3.3 |
||
| 271 | splitos = [int(i) for i in osversion.split(".")] |
||
| 272 | if device == 0 and utilities.newer_103(splitos, 3): |
||
| 273 | print("STL100-1 UNSUPPORTED IN 10.3.3+!") |
||
| 274 | print("\nEXITING...") |
||
| 275 | raise SystemExit |
||
| 276 | |||
| 277 | if download: |
||
| 278 | # Check availability of software releases |
||
| 279 | scriptutils.check_sw(baseurl, softwareversion, swc) |
||
| 280 | if altsw: |
||
| 281 | scriptutils.check_radio_sw(alturl, altsw, altchecked) |
||
|
1 ignored issue
–
show
|
|||
| 282 | |||
| 283 | # Check availability of OS, radio |
||
| 284 | scriptutils.check_os_single(osurl, osversion, device) |
||
| 285 | radiourl, radioversion = scriptutils.check_radio_single(radiourl, radioversion) |
||
| 286 | dllist = [osurl, radiourl] |
||
| 287 | |||
| 288 | # Check cached |
||
| 289 | osfile = os.path.join(localdir, bd_o, os.path.basename(osurl)) |
||
| 290 | radfile = os.path.join(localdir, bd_r, os.path.basename(radiourl)) |
||
| 291 | |||
| 292 | if download: |
||
| 293 | # Download files |
||
| 294 | print("DOWNLOADING...") |
||
| 295 | sess = requests.Session() |
||
| 296 | networkutils.download_bootstrap(dllist, outdir=localdir, workers=2, session=sess) |
||
| 297 | elif all(os.path.exists(x) for x in [osfile, radfile]): |
||
| 298 | # Already downloaded in previous session |
||
| 299 | print("USING CACHED OS/RADIO...") |
||
| 300 | barutils.replace_bar_pair(localdir, osfile, radfile) |
||
| 301 | |||
| 302 | # Test bar files |
||
| 303 | scriptutils.test_bar_files(localdir, dllist) |
||
| 304 | |||
| 305 | # Extract bar files |
||
| 306 | print("EXTRACTING...") |
||
| 307 | barutils.extract_bars(localdir) |
||
| 308 | |||
| 309 | # Test signed files |
||
| 310 | scriptutils.test_signed_files(localdir) |
||
| 311 | |||
| 312 | # Move bar files |
||
| 313 | print("MOVING BAR FILES...") |
||
| 314 | barutils.move_bars(localdir, bd_o, bd_r) |
||
| 315 | |||
| 316 | # Generate loader |
||
| 317 | altradio = radioversion if altsw else None |
||
| 318 | loadergen.generate_lazy_loader(osversion, device, os.path.abspath(localdir), altradio, core) |
||
| 319 | |||
| 320 | # Test loader |
||
| 321 | suffix = loadergen.format_suffix(bool(altradio), altradio, core) |
||
| 322 | loadername = loadergen.generate_filename(device, osversion, suffix) |
||
| 323 | loaderpath = os.path.join(localdir, loadername) |
||
| 324 | scriptutils.test_single_loader(loaderpath) |
||
| 325 | |||
| 326 | # Remove signed files |
||
| 327 | print("REMOVING SIGNED FILES...") |
||
| 328 | barutils.remove_signed_files(localdir) |
||
| 329 | |||
| 330 | # Move loaders |
||
| 331 | print("MOVING LOADERS...") |
||
| 332 | barutils.move_loaders(localdir, ld_o, ld_r, zd_o, zd_r) |
||
| 333 | loadername = os.path.join(ld_o, loadername) |
||
| 334 | |||
| 335 | # Delete empty folders |
||
| 336 | print("REMOVING EMPTY FOLDERS...") |
||
| 337 | barutils.remove_empty_folders(localdir) |
||
| 338 | |||
| 339 | if autoloader: |
||
| 340 | subprocess.call(loadername) |
||
| 341 | print("\nFINISHED!!!") |
||
| 347 |