|
43 | 43 | bootstrap = APIRouter(prefix="/bootstrap", tags=["bootstrap"]) |
44 | 44 | cygwin = APIRouter(prefix="/cygwin", tags=["bootstrap"]) |
45 | 45 | msys2 = APIRouter(prefix="/msys2", tags=["bootstrap"]) |
46 | | -windows_terminal = APIRouter(prefix="/microsoft/terminal", tags=["bootstrap"]) |
47 | 46 | pypi = APIRouter(prefix="/pypi", tags=["bootstrap"]) |
48 | 47 | plugins = APIRouter(prefix="/plugins", tags=["bootstrap"]) |
49 | 48 |
|
@@ -565,219 +564,6 @@ def get_msys2_package_file( |
565 | 564 | raise HTTPException(status_code=package_file.status_code) |
566 | 565 |
|
567 | 566 |
|
568 | | -""" |
569 | | -======================================================================================= |
570 | | -WINDOWS TERMINAL-RELATED FUNCTIONS AND ENDPOINTS |
571 | | -======================================================================================= |
572 | | -""" |
573 | | - |
574 | | -windows_terminal_url = "https://github.com/microsoft/terminal/releases" |
575 | | - |
576 | | - |
577 | | -def get_number_of_github_pages(url) -> int: |
578 | | - """ |
579 | | - Parses the main GitHub releases page to find the number of pages present in the |
580 | | - repository. |
581 | | - """ |
582 | | - |
583 | | - response = requests.get(url) |
584 | | - headers = response.headers |
585 | | - if not headers["content-type"].startswith("text/html"): |
586 | | - raise HTTPException("Unable to parse non-HTML page for page numbers") |
587 | | - |
588 | | - # Find the number of pages present in this release |
589 | | - text = response.text |
590 | | - pattern = r'aria-label="Page ([0-9]+)"' |
591 | | - matches = re.findall(pattern, text) |
592 | | - if len(matches) == 0: |
593 | | - raise HTTPException("No page numbers found") |
594 | | - pages = [int(item) for item in matches] |
595 | | - pages.sort(reverse=True) |
596 | | - return pages[0] |
597 | | - |
598 | | - |
599 | | -@windows_terminal.get("/releases", response_class=Response) |
600 | | -def get_windows_terminal_releases(request: Request): |
601 | | - """ |
602 | | - Returns a list of stable Windows Terminal releases from the GitHub repository. |
603 | | - """ |
604 | | - |
605 | | - num_pages = get_number_of_github_pages(windows_terminal_url) |
606 | | - |
607 | | - # Get list of release versions |
608 | | - versions: list[str] = [] |
609 | | - |
610 | | - # RegEx patterns to parse HTML file with |
611 | | - # https://github.com/{owner}/{repo}/releases/expanded_assets/{version} leads to a |
612 | | - # HTML page with the assets for that particular version |
613 | | - release_pattern = ( |
614 | | - r'src="' + f"{windows_terminal_url}" + r'/expanded_assets/([v0-9\.]+)"' |
615 | | - ) |
616 | | - # Pre-release label follows after link to version tag |
617 | | - prerelease_pattern = ( |
618 | | - r'[\s]*<span data-view-component="true" class="f1 text-bold d-inline mr-3"><a href="/microsoft/terminal/releases/tag/([\w\.]+)" data-view-component="true" class="Link--primary Link">[\w\s\.\-]+</a></span>' |
619 | | - r"[\s]*<span>" |
620 | | - r'[\s]*<span data-view-component="true" class="Label Label--warning Label--large v-align-text-bottom d-none d-md-inline-block">Pre-release</span>' |
621 | | - ) |
622 | | - # Older packages in the repo are named "Color Tools"; omit them |
623 | | - colortool_pattern = r'<span data-view-component="true" class="f1 text-bold d-inline mr-3"><a href="/microsoft/terminal/releases/tag/([\w\.]+)" data-view-component="true" class="Link--primary Link">Color Tool[\w\s]+</a></span>' |
624 | | - |
625 | | - # Iterate through repository pages |
626 | | - for p in range(num_pages): |
627 | | - url = f"{windows_terminal_url}?page={p + 1}" |
628 | | - response = requests.get(url) |
629 | | - headers = response.headers |
630 | | - if not headers["content-type"].startswith("text/html"): |
631 | | - raise HTTPException("Unable to parse non-HTML page for package versions") |
632 | | - text = response.text |
633 | | - |
634 | | - # Collect only stable releases |
635 | | - releases = re.findall(release_pattern, text) |
636 | | - prereleases = re.findall(prerelease_pattern, text) |
637 | | - colortool = re.findall(colortool_pattern, text) |
638 | | - stable = set(releases) - (set(prereleases) | set(colortool)) |
639 | | - versions.extend(stable) |
640 | | - |
641 | | - # Construct HTML document for available versions |
642 | | - html_head = "\n".join( |
643 | | - ( |
644 | | - "<!DOCTYPE html>", |
645 | | - "<html>", |
646 | | - "<head>", |
647 | | - " <title>Links to Windows Terminal Versions</title>", |
648 | | - "</head>", |
649 | | - "<body>", |
650 | | - " <h1>Links to Windows Terminal Versions</h1>", |
651 | | - ) |
652 | | - ) |
653 | | - # Construct hyperlinks |
654 | | - link_list = [] |
655 | | - base_url = str(request.base_url).strip("/") # Remove trailing '/' |
656 | | - path = request.url.path.strip("/") # Remove leading '/' |
657 | | - |
658 | | - for v in range(len(versions)): |
659 | | - version = versions[v] |
660 | | - hyperlink = f'<a href="{base_url}/{path}/{quote(version, safe="")}">{quote(version, safe="")}</a><br />' |
661 | | - link_list.append(hyperlink) |
662 | | - hyperlinks = "\n".join(link_list) |
663 | | - |
664 | | - html_tail = "\n".join( |
665 | | - ( |
666 | | - "</body>", |
667 | | - "</html>", |
668 | | - ) |
669 | | - ) |
670 | | - |
671 | | - # Combine |
672 | | - content = "\n".join((html_head, hyperlinks, html_tail)) |
673 | | - |
674 | | - # Return FastAPI response |
675 | | - return Response( |
676 | | - content=content.encode("utf-8"), |
677 | | - status_code=response.status_code, |
678 | | - media_type="text/html", |
679 | | - ) |
680 | | - |
681 | | - |
682 | | -@windows_terminal.get("/releases/{version}", response_class=Response) |
683 | | -def get_windows_terminal_version_assets( |
684 | | - version: str, |
685 | | - request: Request, |
686 | | -): |
687 | | - """ |
688 | | - Returns a list of packages for the selected version of Windows Terminal. |
689 | | - """ |
690 | | - |
691 | | - # Validate inputs |
692 | | - if bool(re.match(r"^[\w\-\.]+$", version)) is False: |
693 | | - raise HTTPException("Invalid version format") |
694 | | - |
695 | | - # https://github.com/{owner}/{repo}/releases/expanded_assets/{version} |
696 | | - url = f'{windows_terminal_url}/expanded_assets/{quote(version, safe="")}' |
697 | | - |
698 | | - response = requests.get(url) |
699 | | - headers = response.headers |
700 | | - if not headers["content-type"].startswith("text/html"): |
701 | | - raise HTTPException("Unable to parse non-HTML page for page numbers") |
702 | | - text = response.text |
703 | | - |
704 | | - # Find hyperlinks |
705 | | - pattern = ( |
706 | | - r'href="[/\w\.]+/releases/download/' |
707 | | - + f'{quote(version, safe="")}' |
708 | | - + r'/([\w\.\-]+)"' |
709 | | - ) |
710 | | - assets = re.findall(pattern, text) |
711 | | - |
712 | | - # Construct HTML document for available assets |
713 | | - html_head = "\n".join( |
714 | | - ( |
715 | | - "<!DOCTYPE html>", |
716 | | - "<html>", |
717 | | - "<head>", |
718 | | - f' <title>Links to Windows Terminal {quote(version, safe="")} Assets</title>', |
719 | | - "</head>", |
720 | | - "<body>", |
721 | | - f' <h1>Links to Windows Terminal {quote(version, safe="")} Assets</h1>', |
722 | | - ) |
723 | | - ) |
724 | | - # Construct hyperlinks |
725 | | - link_list = [] |
726 | | - base_url = str(request.base_url).strip("/") # Remove trailing '/' |
727 | | - path = request.url.path.strip("/") # Remove leading '/' |
728 | | - |
729 | | - for a in range(len(assets)): |
730 | | - asset = assets[a] |
731 | | - hyperlink = f'<a href="{base_url}/{path}/{quote(asset, safe="")}">{quote(asset, safe="")}</a><br />' |
732 | | - link_list.append(hyperlink) |
733 | | - hyperlinks = "\n".join(link_list) |
734 | | - |
735 | | - html_tail = "\n".join( |
736 | | - ( |
737 | | - "</body>", |
738 | | - "</html>", |
739 | | - ) |
740 | | - ) |
741 | | - |
742 | | - # Combine |
743 | | - content = "\n".join((html_head, hyperlinks, html_tail)) |
744 | | - |
745 | | - # Return FastAPI response |
746 | | - return Response( |
747 | | - content=content.encode("utf-8"), |
748 | | - status_code=response.status_code, |
749 | | - media_type="text/html", |
750 | | - ) |
751 | | - |
752 | | - |
753 | | -@windows_terminal.get("/releases/{version}/{file_name}", response_class=Response) |
754 | | -def get_windows_terminal_package_file( |
755 | | - version: str, |
756 | | - file_name: str, |
757 | | -): |
758 | | - """ |
759 | | - Returns a package from the GitHub repository. |
760 | | - """ |
761 | | - |
762 | | - # Validate version and file names |
763 | | - if bool(re.match(r"^[\w\.\-]+$", version)) is False: |
764 | | - raise HTTPException("Invalid version format") |
765 | | - if bool(re.match(r"^[\w\.\-]+$", file_name)) is False: |
766 | | - raise HTTPException("Invalid file name") |
767 | | - |
768 | | - # https://github.com/{owner}/{repo}/releases/download/{version}/{file_name} |
769 | | - url = f'{windows_terminal_url}/download/{quote(version, safe="")}/{quote(file_name, safe="")}' |
770 | | - response = requests.get(url) |
771 | | - if response.status_code == 200: |
772 | | - return Response( |
773 | | - content=response.content, |
774 | | - status_code=response.status_code, |
775 | | - headers=response.headers, |
776 | | - ) |
777 | | - else: |
778 | | - raise HTTPException(status_code=response.status_code) |
779 | | - |
780 | | - |
781 | 567 | """ |
782 | 568 | ======================================================================================= |
783 | 569 | PYPI-RELATED FUNCTIONS AND ENDPOINTS |
|
0 commit comments