ya2 · news · projects · code · about

refactoring of ya2.build.build
authorFlavio Calva <f.calva@gmail.com>
Mon, 4 Jul 2022 18:14:16 +0000 (19:14 +0100)
committerFlavio Calva <f.calva@gmail.com>
Mon, 4 Jul 2022 18:14:16 +0000 (19:14 +0100)
setup.py
tests/test_functional.py
ya2/build/build.py

index 426a1ea5c5146b6a7037f97ef8a9732104d6165c..11c8fbed6a9aa8eef351f22a980ff71f7445d6f4 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,7 @@ from setuptools import setup, Command
 from setuptools.command.develop import develop
 from multiprocessing import cpu_count
 from direct.dist.commands import bdist_apps
 from setuptools.command.develop import develop
 from multiprocessing import cpu_count
 from direct.dist.commands import bdist_apps
-from ya2.build.build import branch, files, ver, files, bld_dpath
+from ya2.build.build import _branch, files, _ver, files
 #from ya2.build.docs import bld_docs
 from ya2.build.models import ModelsBuilder
 from ya2.build.images import bld_images
 #from ya2.build.docs import bld_docs
 from ya2.build.models import ModelsBuilder
 from ya2.build.images import bld_images
@@ -126,10 +126,10 @@ class BDistAppsCmd(bdist_apps):
         system(cmd)
         bdist_apps.run(self)
         if not self.nolinux:
         system(cmd)
         bdist_apps.run(self)
         if not self.nolinux:
-            hbranch = {'master': 'alpha', 'rc': 'rc', 'stable': ''}[branch]
+            hbranch = {'master': 'alpha', 'rc': 'rc', 'stable': ''}[_branch()]
             AppImageBuilder(self).build(longname, hbranch,
                                         'https://www.ya2.it/downloads/')
             AppImageBuilder(self).build(longname, hbranch,
                                         'https://www.ya2.it/downloads/')
-            fbranch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[branch]
+            fbranch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
             # bld = FlatpakBuilder(
             #     self,
             #     'it.ya2.Pmachines',
             # bld = FlatpakBuilder(
             #     self,
             #     'it.ya2.Pmachines',
index 428b69e565e4c505b20409b6a97247d6dfddcbd3..fa8ce9af6a334d335af3c871b4a41db232a054ed 100644 (file)
@@ -12,7 +12,7 @@ from os import system, remove, environ
 from os.path import exists, basename, join
 from glob import glob
 from panda3d.core import Filename
 from os.path import exists, basename, join
 from glob import glob
 from panda3d.core import Filename
-from ya2.build.build import exec_cmd, _branch, ver
+from ya2.build.build import exec_cmd, _branch, _ver
 
 
 class FunctionalTests(TestCase):
 
 
 class FunctionalTests(TestCase):
@@ -171,11 +171,11 @@ class FunctionalTests(TestCase):
         for line in lines:
             if line.strip().split()[0] == _branch():
                 commit = line.strip().split()[1][:7]
         for line in lines:
             if line.strip().split()[0] == _branch():
                 commit = line.strip().split()[1][:7]
-        _ver = ver
+        __ver = _ver()
         if _branch() == 'stable':
             with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
         if _branch() == 'stable':
             with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
-                _ver = fver.read().strip() + '-'
-        exp = '%s-%s' % (_ver, commit)
+                __ver = fver.read().strip() + '-'
+        exp = '%s-%s' % (__ver, commit)
         cmds = [
             ('timeout 720s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
             ('timeout 720s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
         cmds = [
             ('timeout 720s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
             ('timeout 720s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
index 52242e5854ac7737791c63331f49e33b5e5d80a8..d9b77a402eb8bda7adc96b0a8a04c5e67484dd43 100644 (file)
@@ -14,41 +14,42 @@ def exec_cmd(cmd):
     '''Synchronously executes a command and returns its output.'''
     # ret = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True).communicate()
     # return ret[0].decode('utf-8').strip()
     '''Synchronously executes a command and returns its output.'''
     # ret = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True).communicate()
     # return ret[0].decode('utf-8').strip()
-    proc = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
+    proc = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=True)
     return proc.stdout.strip() + proc.stderr.strip()
 
 
 def _branch():
     '''Returns the current branch.'''
     git_branch = exec_cmd('git symbolic-ref HEAD').split('/')[-1].strip()
     return proc.stdout.strip() + proc.stderr.strip()
 
 
 def _branch():
     '''Returns the current branch.'''
     git_branch = exec_cmd('git symbolic-ref HEAD').split('/')[-1].strip()
-    print('git_branch result: %s' % git_branch)
+    print(f'git_branch result: {git_branch}')
     branches = ['master', 'rc', 'stable']
     if git_branch in branches:
     branches = ['master', 'rc', 'stable']
     if git_branch in branches:
-        print('git_branch: %s' % git_branch)
+        print(f'git_branch: {git_branch}')
         return git_branch
     root = str(Path(dirname(dirname(__file__))).parent) + '/'
     if 'itch' in __file__.split(sep):
         root = str(Path(dirname(__file__))) + '/'
     if __file__ == '/app/bin/pmachines':  # flatpak
         root = '/app/bin/'
         return git_branch
     root = str(Path(dirname(dirname(__file__))).parent) + '/'
     if 'itch' in __file__.split(sep):
         root = str(Path(dirname(__file__))) + '/'
     if __file__ == '/app/bin/pmachines':  # flatpak
         root = '/app/bin/'
-    for branch in branches:
+    for __branch in branches:
         try:
         try:
-            print('try: %s' % root + 'assets/bld_version.txt')
-            with open(root + 'assets/bld_version.txt') as fver:
-                ver = fver.read()
-                print('ver: %s' % ver)
-            # if branch in ver:
+            print(f'try: {root}' + 'assets/bld_version.txt')
+            with open(root + 'assets/bld_version.txt', encoding='utf8') as fver:
+                __ver = fver.read()
+                print(f'ver: {__ver}')
+            # if __branch in __ver:
             b2c = {'master': 'a', 'rc': 'r', 'stable': '.'}
             b2c = {'master': 'a', 'rc': 'r', 'stable': '.'}
-            if ver[1] == b2c[branch]:
-                return branch
+            if __ver[1] == b2c[__branch]:
+                return __branch
         except FileNotFoundError:
         except FileNotFoundError:
-            print('file not found: %s' % root + 'assets/bld_version.txt')
+            print(f'file not found: {root}' + 'assets/bld_version.txt')
+    return ''
 
 
 def _commit():
     '''Returns the current branch.'''
     git_commit = exec_cmd('git rev-parse HEAD')[:7]
 
 
 def _commit():
     '''Returns the current branch.'''
     git_commit = exec_cmd('git rev-parse HEAD')[:7]
-    print('git_commit result: %s' % git_commit)
+    print(f'git_commit result: {git_commit}')
     if not git_commit.startswith("Can't r"):
         return git_commit
     root = str(Path(dirname(dirname(__file__))).parent) + '/'
     if not git_commit.startswith("Can't r"):
         return git_commit
     root = str(Path(dirname(dirname(__file__))).parent) + '/'
@@ -57,13 +58,14 @@ def _commit():
     if __file__ == '/app/bin/pmachines':  # flatpak
         root = '/app/bin/'
     try:
     if __file__ == '/app/bin/pmachines':  # flatpak
         root = '/app/bin/'
     try:
-        print('try: %s' % root + 'assets/bld_version.txt')
-        with open(root + 'assets/bld_version.txt') as fver:
-            ver = fver.read()
-            print('ver: %s' % ver)
-            return ver.split('-')[1]
+        print(f'try: {root}' + 'assets/bld_version.txt')
+        with open(root + 'assets/bld_version.txt', encoding='utf8') as fver:
+            __ver = fver.read()
+            print(f'ver: {__ver}')
+            return __ver.split('-')[1]
     except FileNotFoundError:
     except FileNotFoundError:
-        print('file not found: %s' % root + 'assets/bld_version.txt')
+        print(f'file not found: {root}' + 'assets/bld_version.txt')
+    return ''
 
 
 def _version():
 
 
 def _version():
@@ -75,7 +77,7 @@ def _version():
     if _branch() == 'stable':
         pref, _ver = '', ''
         if exists(root + 'assets/version.txt'):
     if _branch() == 'stable':
         pref, _ver = '', ''
         if exists(root + 'assets/version.txt'):
-            with open(root + 'assets/version.txt') as fver:
+            with open(root + 'assets/version.txt', encoding='utf8') as fver:
                 pref = fver.read().strip() + '-'  # + _branch() + '-'
                 _ver = fver.read().strip()
         ret_ver = _ver or ('0.' + day)
                 pref = fver.read().strip() + '-'  # + _branch() + '-'
                 _ver = fver.read().strip()
         ret_ver = _ver or ('0.' + day)
@@ -84,11 +86,11 @@ def _version():
         pref = {'master': 'a', 'rc': 'rc', '': 'runtime'}[_branch()]
         # except KeyError:
         #    pref = 'notfound'
         pref = {'master': 'a', 'rc': 'rc', '': 'runtime'}[_branch()]
         # except KeyError:
         #    pref = 'notfound'
-        ret_ver = '0%s%s' % (pref, day)
+        ret_ver = f'0{pref}{day}'
         pref = ret_ver
     bld_ver = pref + '-' + _commit()
     try:
         pref = ret_ver
     bld_ver = pref + '-' + _commit()
     try:
-        with open(root + 'assets/bld_version.txt', 'w') as fver:
+        with open(root + 'assets/bld_version.txt', 'w', encoding='utf8') as fver:
             fver.write(bld_ver)
     except OSError:
         print("we can't write inside flatpaks, but we don't need it")
             fver.write(bld_ver)
     except OSError:
         print("we can't write inside flatpaks, but we don't need it")
@@ -111,30 +113,30 @@ def __files_ext(fnames, _extensions):
 
 def __to_be_built_single(src, tgt):
     if getmtime(tgt) > getmtime(src):
 
 def __to_be_built_single(src, tgt):
     if getmtime(tgt) > getmtime(src):
-        print('%s is newer than %s: do not build' % (tgt, src))
+        print(f'{tgt} is newer than {src}: do not build')
         return False
         return False
-    with open(src, 'rb') as f:
-        src_content = f.read()
-    with open(tgt, 'rb') as f:
-        tgt_content = f.read()
+    with open(src, 'rb') as fsrc:
+        src_content = fsrc.read()
+    with open(tgt, 'rb') as ftgt:
+        tgt_content = ftgt.read()
     hash_src = md5(src_content).hexdigest()
     hash_tgt = md5(tgt_content).hexdigest()
     cache = {}
     lines = []
     if exists('hash_cache.txt'):
     hash_src = md5(src_content).hexdigest()
     hash_tgt = md5(tgt_content).hexdigest()
     cache = {}
     lines = []
     if exists('hash_cache.txt'):
-        with open('hash_cache.txt') as f:
-            lines = f.readlines()
+        with open('hash_cache.txt', encoding='utf8') as fhash:
+            lines = fhash.readlines()
     for line in lines:
         line_spl = line.split()
     for line in lines:
         line_spl = line.split()
-        hash = line_spl[-1]
+        _hash = line_spl[-1]
         fname = ' '.join(line_spl[:-1])
         fname = ' '.join(line_spl[:-1])
-        cache[fname] = hash
+        cache[fname] = _hash
     if src in cache and tgt in cache:
         if hash_src == cache[src] and \
            hash_tgt == cache[tgt]:
     if src in cache and tgt in cache:
         if hash_src == cache[src] and \
            hash_tgt == cache[tgt]:
-            print('%s and %s are in the cache: do not build' % (tgt, src))
+            print(f'{tgt} and {src} are in the cache: do not build')
             return False
             return False
-    print('%s and %s are not up-to-date: building...' % (src, tgt))
+    print(f'{src} and {tgt} are not up-to-date: building...')
     return True
 
 
     return True
 
 
@@ -160,11 +162,11 @@ class InsideDir:
         chdir(self.old_dir)
 
 
         chdir(self.old_dir)
 
 
-bld_dpath = 'build/'
-branch = _branch()
-ver = _version()
-win_fpath = '{dst_dir}{appname}-%s-windows.exe' % branch
+bld_dpath = 'build/'
+branch = _branch()
+ver = _version()
+# win_fpath = '{dst_dir}{appname}-' + f'{branch}-windows.exe'
 # osx_fpath = '{dst_dir}{appname}-%s-osx.zip' % branch
 # flatpak_fpath = '{dst_dir}{appname}-%s-flatpak' % branch
 # osx_fpath = '{dst_dir}{appname}-%s-osx.zip' % branch
 # flatpak_fpath = '{dst_dir}{appname}-%s-flatpak' % branch
-appimage_fpath = '{dst_dir}{appname}-%s-appimage' % branch
+# appimage_fpath = '{dst_dir}{appname}-' + f'{branch}-appimage'
 # docs_fpath = '{dst_dir}{appname}-%s-docs.tar.gz' % branch
 # docs_fpath = '{dst_dir}{appname}-%s-docs.tar.gz' % branch