| Total Complexity | 68 |
| Total Lines | 424 |
| Duplicated Lines | 0 % |
Complex classes like buildtimetrend.travis.TravisData 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 | # vim: set expandtab sw=4 ts=4: |
||
| 306 | class TravisData(object): |
||
| 307 | |||
| 308 | """Gather data from Travis CI using the API.""" |
||
| 309 | |||
| 310 | def __init__(self, repo, build_id, connector=None): |
||
| 311 | """ |
||
| 312 | Retrieve Travis CI build data using the API. |
||
| 313 | |||
| 314 | Parameters: |
||
| 315 | - repo : github repository slug (fe. buildtimetrend/python-lib) |
||
| 316 | - build_id : Travis CI build id (fe. 158) |
||
| 317 | - connector : Travis Connector instance |
||
| 318 | """ |
||
| 319 | self.builds_data = {} |
||
| 320 | self.build_jobs = {} |
||
| 321 | self.current_build_data = {} |
||
| 322 | self.current_job = BuildJob() |
||
| 323 | self.travis_substage = None |
||
| 324 | self.repo = repo |
||
| 325 | self.build_id = str(build_id) |
||
| 326 | # set TravisConnector if it is defined |
||
| 327 | if connector is not None and type(connector) is TravisConnector: |
||
| 328 | self.connector = connector |
||
| 329 | # use Travis Org connector by default |
||
| 330 | else: |
||
| 331 | self.connector = TravisOrgConnector() |
||
| 332 | |||
| 333 | def get_build_data(self): |
||
| 334 | """ |
||
| 335 | Retrieve Travis CI build data. |
||
| 336 | |||
| 337 | Returns true if retrieving data was succesful, false on error. |
||
| 338 | """ |
||
| 339 | request = 'repos/{repo}/builds?number={build_id}'.format( |
||
| 340 | repo=self.repo, build_id=self.build_id |
||
| 341 | ) |
||
| 342 | try: |
||
| 343 | self.builds_data = self.connector.json_request(request) |
||
| 344 | except (HTTPError, URLError) as msg: |
||
| 345 | logger.error("Error getting build data from Travis CI: %s", msg) |
||
| 346 | return False |
||
| 347 | |||
| 348 | # log builds_data |
||
| 349 | logger.debug( |
||
| 350 | "Build #%s data : %s", |
||
| 351 | str(self.build_id), |
||
| 352 | json.dumps(self.builds_data, sort_keys=True, indent=2) |
||
| 353 | ) |
||
| 354 | |||
| 355 | return True |
||
| 356 | |||
| 357 | def get_substage_name(self, command): |
||
| 358 | """ |
||
| 359 | Resolve Travis CI substage name that corresponds to a cli command. |
||
| 360 | |||
| 361 | Parameters: |
||
| 362 | - command : cli command |
||
| 363 | """ |
||
| 364 | if not is_string(command): |
||
| 365 | return "" |
||
| 366 | |||
| 367 | if len(self.current_build_data) > 0 and \ |
||
| 368 | "config" in self.current_build_data: |
||
| 369 | build_config = self.current_build_data["config"] |
||
| 370 | else: |
||
| 371 | logger.warning( |
||
| 372 | "Travis CI build config is not set" |
||
| 373 | ) |
||
| 374 | return "" |
||
| 375 | |||
| 376 | # check if build_config collection is empty |
||
| 377 | if build_config: |
||
| 378 | for stage_name, commands in build_config.items(): |
||
| 379 | if type(commands) is list and command in commands: |
||
| 380 | substage_number = commands.index(command) + 1 |
||
| 381 | substage_name = "{stage}.{substage:d}".format( |
||
| 382 | stage=stage_name, substage=substage_number |
||
| 383 | ) |
||
| 384 | logger.debug( |
||
| 385 | "Substage %s corresponds to '%s'", |
||
| 386 | substage_name, command |
||
| 387 | ) |
||
| 388 | return substage_name |
||
| 389 | |||
| 390 | return "" |
||
| 391 | |||
| 392 | def process_build_jobs(self): |
||
| 393 | """ |
||
| 394 | Retrieve Travis CI build job data. |
||
| 395 | |||
| 396 | Method is a generator, iterate result to get each processed build job. |
||
| 397 | """ |
||
| 398 | if len(self.builds_data) > 0 and "builds" in self.builds_data: |
||
| 399 | for build in self.builds_data['builds']: |
||
| 400 | self.current_build_data = build |
||
| 401 | |||
| 402 | if "job_ids" in build: |
||
| 403 | for job_id in build['job_ids']: |
||
| 404 | yield self.process_build_job(job_id) |
||
| 405 | |||
| 406 | # reset current_build_data after builds are processed |
||
| 407 | self.current_build_data = {} |
||
| 408 | |||
| 409 | def process_build_job(self, job_id): |
||
| 410 | """ |
||
| 411 | Retrieve Travis CI build job data. |
||
| 412 | |||
| 413 | Parameters: |
||
| 414 | - job_id : ID of the job to process |
||
| 415 | """ |
||
| 416 | if job_id is None: |
||
| 417 | return None |
||
| 418 | |||
| 419 | # retrieve job data from Travis CI |
||
| 420 | job_data = self.get_job_data(job_id) |
||
| 421 | # process build/job data |
||
| 422 | self.process_job_data(job_data) |
||
| 423 | # parse Travis CI job log file |
||
| 424 | self.parse_job_log(job_id) |
||
| 425 | |||
| 426 | # store build job |
||
| 427 | self.build_jobs[str(job_id)] = self.current_job |
||
| 428 | # create new build job instance |
||
| 429 | self.current_job = BuildJob() |
||
| 430 | |||
| 431 | # return processed build job |
||
| 432 | return self.build_jobs[str(job_id)] |
||
| 433 | |||
| 434 | def get_job_data(self, job_id): |
||
| 435 | """ |
||
| 436 | Retrieve Travis CI job data. |
||
| 437 | |||
| 438 | Parameters: |
||
| 439 | - job_id : ID of the job to process |
||
| 440 | """ |
||
| 441 | request = 'jobs/{:s}'.format(str(job_id)) |
||
| 442 | job_data = self.connector.json_request(request) |
||
| 443 | |||
| 444 | # log job_data |
||
| 445 | logger.debug( |
||
| 446 | "Job #%s data : %s", |
||
| 447 | str(job_id), |
||
| 448 | json.dumps(job_data, sort_keys=True, indent=2) |
||
| 449 | ) |
||
| 450 | |||
| 451 | return job_data |
||
| 452 | |||
| 453 | def process_job_data(self, job_data): |
||
| 454 | """ |
||
| 455 | Process Job/build data. |
||
| 456 | |||
| 457 | Set build/job properties : |
||
| 458 | - Build/job ID |
||
| 459 | - build result : passed, failed, errored |
||
| 460 | - git repo |
||
| 461 | - git branch |
||
| 462 | - CI platform : Travis |
||
| 463 | - build matrix (language, language version, compiler, ...) |
||
| 464 | - build_trigger : push, pull_request |
||
| 465 | - pull_request (is_pull_request, title, number) |
||
| 466 | |||
| 467 | Parameters: |
||
| 468 | - job_data : dictionary with Travis CI job data |
||
| 469 | """ |
||
| 470 | self.current_job.add_property( |
||
| 471 | "build", |
||
| 472 | # buildnumber is part before "." of job number |
||
| 473 | job_data['job']['number'].split(".")[0] |
||
| 474 | ) |
||
| 475 | self.current_job.add_property("job", job_data['job']['number']) |
||
| 476 | self.current_job.add_property("branch", job_data['commit']['branch']) |
||
| 477 | self.current_job.add_property( |
||
| 478 | "repo", |
||
| 479 | job_data['job']['repository_slug'] |
||
| 480 | ) |
||
| 481 | self.current_job.add_property("ci_platform", 'travis') |
||
| 482 | self.current_job.add_property("result", job_data['job']['state']) |
||
| 483 | |||
| 484 | self.set_build_matrix(job_data) |
||
| 485 | |||
| 486 | self.process_pull_request_data() |
||
| 487 | |||
| 488 | self.current_job.set_started_at(job_data['job']['started_at']) |
||
| 489 | self.current_job.set_finished_at(job_data['job']['finished_at']) |
||
| 490 | |||
| 491 | # calculate job duration from start and finished timestamps |
||
| 492 | # if no timing tags are available |
||
| 493 | if not self.has_timing_tags(): |
||
| 494 | self.current_job.add_property("duration", self.get_job_duration()) |
||
| 495 | |||
| 496 | def set_build_matrix(self, job_data): |
||
| 497 | """ |
||
| 498 | Retrieve build matrix data from job data and store in properties. |
||
| 499 | |||
| 500 | Properties : |
||
| 501 | - language |
||
| 502 | - language version (if applicable) |
||
| 503 | - compiler (if applicable) |
||
| 504 | - operating system |
||
| 505 | - environment parameters |
||
| 506 | |||
| 507 | Parameters: |
||
| 508 | - job_data : dictionary with Travis CI job data |
||
| 509 | """ |
||
| 510 | build_matrix = Collection() |
||
| 511 | |||
| 512 | job_config = job_data['job']['config'] |
||
| 513 | |||
| 514 | language = job_config['language'] |
||
| 515 | build_matrix.add_item("language", language) |
||
| 516 | |||
| 517 | # set language version |
||
| 518 | # ('d', 'dart', 'go', 'perl', 'php', 'python', 'rust') |
||
| 519 | if language in job_config: |
||
| 520 | if language == "android": |
||
| 521 | build_matrix.add_item( |
||
| 522 | "language_components", |
||
| 523 | " ".join(job_config[language]["components"]) |
||
| 524 | ) |
||
| 525 | else: |
||
| 526 | build_matrix.add_item( |
||
| 527 | "language_version", |
||
| 528 | str(job_config[language]) |
||
| 529 | ) |
||
| 530 | |||
| 531 | # language specific build matrix parameters |
||
| 532 | parameters = { |
||
| 533 | 'ghc': 'ghc', # Haskell |
||
| 534 | 'jdk': 'jdk', # Java, Android, Groovy, Ruby, Scala |
||
| 535 | 'lein': 'lein', # Clojure |
||
| 536 | 'mono': 'mono', # C#, F#, Visual Basic |
||
| 537 | 'node_js': 'node_js', # Javascript |
||
| 538 | 'otp_release': 'otp_release', # Erlang |
||
| 539 | 'rvm': 'rvm', # Ruby, Objective-C |
||
| 540 | 'gemfile': 'gemfile', # Ruby, Objective-C |
||
| 541 | 'xcode_sdk': 'xcode_sdk', # Objective-C |
||
| 542 | 'xcode_scheme': 'xcode_scheme', # Objective-C |
||
| 543 | 'compiler': 'compiler', # C, C++ |
||
| 544 | 'os': 'os', |
||
| 545 | 'env': 'parameters' |
||
| 546 | } |
||
| 547 | for parameter, name in parameters.items(): |
||
| 548 | if parameter in job_config: |
||
| 549 | build_matrix.add_item(name, str(job_config[parameter])) |
||
| 550 | |||
| 551 | self.current_job.add_property( |
||
| 552 | "build_matrix", |
||
| 553 | build_matrix.get_items_with_summary() |
||
| 554 | ) |
||
| 555 | |||
| 556 | def process_pull_request_data(self): |
||
| 557 | """Retrieve pull request data from Travis CI API.""" |
||
| 558 | # check if collection is empty |
||
| 559 | if self.current_build_data: |
||
| 560 | if "event_type" in self.current_build_data: |
||
| 561 | # build trigger (push or pull_request) |
||
| 562 | self.current_job.add_property( |
||
| 563 | "build_trigger", |
||
| 564 | self.current_build_data["event_type"] |
||
| 565 | ) |
||
| 566 | |||
| 567 | # pull_request |
||
| 568 | pull_request_data = {} |
||
| 569 | if "pull_request" in self.current_build_data: |
||
| 570 | pull_request_data["is_pull_request"] = \ |
||
| 571 | self.current_build_data["pull_request"] |
||
| 572 | else: |
||
| 573 | pull_request_data["is_pull_request"] = False |
||
| 574 | |||
| 575 | if "pull_request_title" in self.current_build_data: |
||
| 576 | pull_request_data["title"] = \ |
||
| 577 | self.current_build_data["pull_request_title"] |
||
| 578 | |||
| 579 | if "pull_request_number" in self.current_build_data: |
||
| 580 | pull_request_data["number"] = \ |
||
| 581 | self.current_build_data["pull_request_number"] |
||
| 582 | |||
| 583 | self.current_job.add_property("pull_request", pull_request_data) |
||
| 584 | |||
| 585 | def parse_job_log(self, job_id): |
||
| 586 | """ |
||
| 587 | Parse Travis CI job log. |
||
| 588 | |||
| 589 | Parameters: |
||
| 590 | - job_id : ID of the job to process |
||
| 591 | """ |
||
| 592 | self.parse_job_log_stream(self.connector.download_job_log(job_id)) |
||
| 593 | |||
| 594 | def parse_job_log_file(self, filename): |
||
| 595 | """ |
||
| 596 | Open a Travis CI log file and parse it. |
||
| 597 | |||
| 598 | Parameters : |
||
| 599 | - filename : filename of Travis CI log |
||
| 600 | Returns false if file doesn't exist, true if it was read successfully. |
||
| 601 | """ |
||
| 602 | # load timestamps file |
||
| 603 | if not check_file(filename): |
||
| 604 | return False |
||
| 605 | |||
| 606 | # read timestamps, calculate stage duration |
||
| 607 | with open(filename, 'rb') as file_stream: |
||
| 608 | self.parse_job_log_stream(file_stream) |
||
| 609 | |||
| 610 | return True |
||
| 611 | |||
| 612 | def parse_job_log_stream(self, stream): |
||
| 613 | """ |
||
| 614 | Parse Travis CI job log stream. |
||
| 615 | |||
| 616 | Parameters: |
||
| 617 | - stream : stream of job log file |
||
| 618 | """ |
||
| 619 | self.travis_substage = TravisSubstage() |
||
| 620 | check_timing_tags = self.has_timing_tags() |
||
| 621 | |||
| 622 | for line in stream: |
||
| 623 | # convert to str if line is bytes type |
||
| 624 | if isinstance(line, bytes): |
||
| 625 | line = line.decode('utf-8') |
||
| 626 | # parse Travis CI timing tags |
||
| 627 | if check_timing_tags and 'travis_' in line: |
||
| 628 | self.parse_travis_time_tag(line) |
||
| 629 | # parse Travis CI worker tag |
||
| 630 | if 'Using worker:' in line: |
||
| 631 | self.parse_travis_worker_tag(line) |
||
| 632 | |||
| 633 | def parse_travis_time_tag(self, line): |
||
| 634 | """ |
||
| 635 | Parse and process Travis CI timing tags. |
||
| 636 | |||
| 637 | Parameters: |
||
| 638 | - line : line from logfile containing Travis CI tags |
||
| 639 | """ |
||
| 640 | if self.travis_substage is None: |
||
| 641 | self.travis_substage = TravisSubstage() |
||
| 642 | |||
| 643 | escaped_line = line.replace('\x0d', '*').replace('\x1b', 'ESC') |
||
| 644 | logger.debug('line : %s', escaped_line) |
||
| 645 | |||
| 646 | # parse Travis CI timing tags |
||
| 647 | for parse_string in TRAVIS_LOG_PARSE_TIMING_STRINGS: |
||
| 648 | result = re.search(parse_string, line) |
||
| 649 | if result: |
||
| 650 | self.travis_substage.process_parsed_tags(result.groupdict()) |
||
| 651 | |||
| 652 | # when finished : log stage and create a new instance |
||
| 653 | if self.travis_substage.has_finished(): |
||
| 654 | # set substage name, if it is not set |
||
| 655 | if not self.travis_substage.has_name() and \ |
||
| 656 | self.travis_substage.has_command(): |
||
| 657 | self.travis_substage.set_name( |
||
| 658 | self.get_substage_name( |
||
| 659 | self.travis_substage.get_command() |
||
| 660 | ) |
||
| 661 | ) |
||
| 662 | |||
| 663 | # only log complete substages |
||
| 664 | if not self.travis_substage.finished_incomplete: |
||
| 665 | self.current_job.add_stage(self.travis_substage.stage) |
||
| 666 | self.travis_substage = TravisSubstage() |
||
| 667 | |||
| 668 | def parse_travis_worker_tag(self, line): |
||
| 669 | """ |
||
| 670 | Parse and process Travis CI worker tag. |
||
| 671 | |||
| 672 | Parameters: |
||
| 673 | - line : line from logfile containing Travis CI tags |
||
| 674 | """ |
||
| 675 | logger.debug('line : %s', line) |
||
| 676 | |||
| 677 | # parse Travis CI worker tags |
||
| 678 | result = re.search(TRAVIS_LOG_PARSE_WORKER_STRING, line) |
||
| 679 | if not result: |
||
| 680 | return |
||
| 681 | |||
| 682 | worker_tags = result.groupdict() |
||
| 683 | |||
| 684 | # check if parameter worker_tags is a dictionary and |
||
| 685 | # if it contains all required tags |
||
| 686 | tag_list = list({'hostname', 'os'}) |
||
| 687 | if check_dict(worker_tags, "worker_tags", tag_list): |
||
| 688 | logger.debug("Worker tags : %s", worker_tags) |
||
| 689 | self.current_job.add_property("worker", worker_tags) |
||
| 690 | |||
| 691 | def has_timing_tags(self): |
||
| 692 | """ |
||
| 693 | Check if Travis CI job log has timing tags. |
||
| 694 | |||
| 695 | Timing tags were introduced on Travis CI starting 2014-08-07, |
||
| 696 | check if started_at is more recent. |
||
| 697 | """ |
||
| 698 | started_at = self.current_job.get_property("started_at") |
||
| 699 | if started_at is None or "timestamp_seconds" not in started_at: |
||
| 700 | return False |
||
| 701 | |||
| 702 | # 1407369600 is epoch timestamp of 2014-08-07T00:00:00Z |
||
| 703 | return started_at["timestamp_seconds"] > 1407369600 |
||
| 704 | |||
| 705 | def get_job_duration(self): |
||
| 706 | """Calculate build job duration.""" |
||
| 707 | started_at = self.current_job.get_property("started_at") |
||
| 708 | finished_at = self.current_job.get_property("finished_at") |
||
| 709 | if started_at is None or "timestamp_seconds" not in started_at or \ |
||
| 710 | finished_at is None or "timestamp_seconds" not in finished_at: |
||
| 711 | return 0.0 |
||
| 712 | |||
| 713 | timestamp_start = float(started_at["timestamp_seconds"]) |
||
| 714 | timestamp_end = float(finished_at["timestamp_seconds"]) |
||
| 715 | return timestamp_end - timestamp_start |
||
| 716 | |||
| 717 | def get_started_at(self): |
||
| 718 | """Retrieve timestamp when build was started.""" |
||
| 719 | if check_dict(self.current_build_data, key_list=["started_at"]): |
||
| 720 | return self.current_build_data['started_at'] |
||
| 721 | else: |
||
| 722 | return None |
||
| 723 | |||
| 724 | def get_finished_at(self): |
||
| 725 | """Retrieve timestamp when build finished.""" |
||
| 726 | if check_dict(self.current_build_data, key_list=["finished_at"]): |
||
| 727 | return self.current_build_data['finished_at'] |
||
| 728 | else: |
||
| 729 | return None |
||
| 730 | |||
| 1014 |