Skip to content

Commit

Permalink
Optimized run_app.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-sung committed Jan 7, 2025
1 parent fea5421 commit d523d49
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
4 changes: 3 additions & 1 deletion cxupdater/commands/build_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def _create_build_exe_distribution(self) -> None:
icon=self.app_icon,
base='Win32GUI',
)
self.distribution.command_options['build_exe'] = {'build_exe': ('setup script', self.build_exe)}
self.excludes = ['logging', 'pydoc', 'unittest', 'http', 'xml']
self.includes = []
self.packages = []
self.distribution.executables = [executable]
self.distribution.commands = ['build_exe']

Expand Down
1 change: 0 additions & 1 deletion cxupdater/pipe_connecter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Callable, Union, Any

import pywintypes
import requests
import win32file
import win32pipe

Expand Down
32 changes: 29 additions & 3 deletions cxupdater/scripts/run_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import os
import re
import subprocess
import sys
from pathlib import Path

from cxupdater import VersionParser
from packaging.version import Version


VERSION_PATTERN = r'\d+\.\d+(?:\.\d+)*'
PREFIX = 'win-amd64' if sys.maxsize > 2**32 else 'win32'


def get_latest_exe_path_from_local_folder(src: Path, app_name: str) -> Path:
"""
Getting a path of the latest version from src path using name of the app.
Args:
src (Path): Path of the source folder.
app_name (str): Name of the app.
Returns:
Path of the latest app executable.
"""
found_package_names = [
(fname, re.search(VERSION_PATTERN, fname).group())
for fname in os.listdir(src)
if fname.startswith(app_name) and fname.endswith(PREFIX) and re.search(VERSION_PATTERN, fname)
]
latest_version = max(found_package_names, key=lambda x: Version(x[1]))
execute_name = app_name + '.exe'
return src / latest_version[0] / execute_name


def main(path: Path):
Expand All @@ -16,10 +43,9 @@ def main(path: Path):


if __name__ == "__main__":
parser = VersionParser()
path_to_executable = Path(sys.executable)
src_folder_path = path_to_executable.parent
name = path_to_executable.name
main_exe_path = parser.get_latest_exe_path_from_local_folder(src_folder_path, name[:-4])
main_exe_path = get_latest_exe_path_from_local_folder(src_folder_path, name[:-4])
main(main_exe_path)
sys.exit(0)
27 changes: 1 addition & 26 deletions cxupdater/version_parser.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
import os
import re
from pathlib import Path
from typing import Dict, Tuple, Union

import toml
from packaging.version import Version
from requests import Response

from cxupdater.config import UpdatePackage, ARCH_PREFIX, is_64bit
from cxupdater.config import UpdatePackage, is_64bit


class VersionParser:
VERSION_PATTERN = r'\d+\.\d+(?:\.\d+)*'

def __init__(self):
pass

def get_latest_exe_path_from_local_folder(self, src: Path, app_name: str) -> Path:
"""
Getting a path of the latest version from src path using name of the app.
Args:
src (Path): Path of the source folder.
app_name (str): Name of the app.
Returns:
Path of the latest app executable.
"""
found_package_names = [
UpdatePackage(name, None, re.search(self.VERSION_PATTERN, name).group())
for name in os.listdir(src)
if name.startswith(app_name) and name.endswith(ARCH_PREFIX) and re.search(self.VERSION_PATTERN, name)
]
latest_version = max(found_package_names, key=lambda x: Version(x.version))
execute_name = app_name + '.exe'
return src / latest_version.name / execute_name

def get_latest_version_from_response(self, response: Response) -> UpdatePackage:
"""
Getting the latest version from response and return the maximum available version.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "cxupdater"
version = "0.1.0"
version = "0.1.1"
authors = [
{ name="Oleg Sungurovsky", email="safasgasc.asfg@gmail.com" },
]
Expand Down

0 comments on commit d523d49

Please sign in to comment.