52242e5854ac7737791c63331f49e33b5e5d80a8
1 '''Tools for making the builds.'''
2 from os
import walk
, chdir
, getcwd
3 from os
.path
import join
, exists
, dirname
, getmtime
, sep
4 from subprocess
import PIPE
, run
5 from time
import strftime
6 from pathlib
import Path
7 from hashlib
import md5
10 # TODO refactor: make class BuilderTools
14 '''Synchronously executes a command and returns its output.'''
15 # ret = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True).communicate()
16 # return ret[0].decode('utf-8').strip()
17 proc
= run(cmd
, shell
=True, stdout
=PIPE
, stderr
=PIPE
, universal_newlines
=True)
18 return proc
.stdout
.strip() + proc
.stderr
.strip()
22 '''Returns the current branch.'''
23 git_branch
= exec_cmd('git symbolic-ref HEAD').split('/')[-1].strip()
24 print('git_branch result: %s' % git_branch
)
25 branches
= ['master', 'rc', 'stable']
26 if git_branch
in branches
:
27 print('git_branch: %s' % git_branch
)
29 root
= str(Path(dirname(dirname(__file__
))).parent
) + '/'
30 if 'itch' in __file__
.split(sep
):
31 root
= str(Path(dirname(__file__
))) + '/'
32 if __file__
== '/app/bin/pmachines': # flatpak
34 for branch
in branches
:
36 print('try: %s' % root
+ 'assets/bld_version.txt')
37 with
open(root
+ 'assets/bld_version.txt') as fver
:
39 print('ver: %s' % ver
)
41 b2c
= {'master': 'a', 'rc': 'r', 'stable': '.'}
42 if ver
[1] == b2c
[branch
]:
44 except FileNotFoundError
:
45 print('file not found: %s' % root
+ 'assets/bld_version.txt')
49 '''Returns the current branch.'''
50 git_commit
= exec_cmd('git rev-parse HEAD')[:7]
51 print('git_commit result: %s' % git_commit
)
52 if not git_commit
.startswith("Can't r"):
54 root
= str(Path(dirname(dirname(__file__
))).parent
) + '/'
55 if 'itch' in __file__
.split(sep
):
56 root
= str(Path(dirname(__file__
))) + '/'
57 if __file__
== '/app/bin/pmachines': # flatpak
60 print('try: %s' % root
+ 'assets/bld_version.txt')
61 with
open(root
+ 'assets/bld_version.txt') as fver
:
63 print('ver: %s' % ver
)
64 return ver
.split('-')[1]
65 except FileNotFoundError
:
66 print('file not found: %s' % root
+ 'assets/bld_version.txt')
70 '''Computes the version of the current build.'''
71 day
= strftime('%y%m%d')
72 root
= str(Path(dirname(dirname(__file__
))).parent
) + '/'
73 if __file__
== '/app/bin/pmachines': # flatpak
75 if _branch() == 'stable':
77 if exists(root
+ 'assets/version.txt'):
78 with
open(root
+ 'assets/version.txt') as fver
:
79 pref
= fver
.read().strip() + '-' # + _branch() + '-'
80 _ver
= fver
.read().strip()
81 ret_ver
= _ver
or ('0.' + day
)
83 # try: we want an error!
84 pref
= {'master': 'a', 'rc': 'rc', '': 'runtime'}[_branch()]
87 ret_ver
= '0%s%s' % (pref
, day
)
89 bld_ver
= pref
+ '-' + _commit()
91 with
open(root
+ 'assets/bld_version.txt', 'w') as fver
:
94 print("we can't write inside flatpaks, but we don't need it")
98 def files(_extensions
, excl_dirs
=None, excl_ends_with
=None, root_path
='.'):
99 '''Retrieves filenames in root_path with _extensions, with filters.'''
100 return [join(root
, fname
)
101 for root
, _
, fnames
in walk(root_path
)
102 for fname
in __files_ext(fnames
, _extensions
)
103 if not any(e_d
in root
.split('/') for e_d
in excl_dirs
or []) and
104 not any(fname
.endswith(e_e
) for e_e
in excl_ends_with
or [])]
107 def __files_ext(fnames
, _extensions
):
108 return [fname
for fname
in fnames
109 if any(fname
.endswith('.' + ext
) for ext
in _extensions
)]
112 def __to_be_built_single(src
, tgt
):
113 if getmtime(tgt
) > getmtime(src
):
114 print('%s is newer than %s: do not build' % (tgt
, src
))
116 with
open(src
, 'rb') as f
:
117 src_content
= f
.read()
118 with
open(tgt
, 'rb') as f
:
119 tgt_content
= f
.read()
120 hash_src
= md5(src_content
).hexdigest()
121 hash_tgt
= md5(tgt_content
).hexdigest()
124 if exists('hash_cache.txt'):
125 with
open('hash_cache.txt') as f
:
126 lines
= f
.readlines()
128 line_spl
= line
.split()
130 fname
= ' '.join(line_spl
[:-1])
132 if src
in cache
and tgt
in cache
:
133 if hash_src
== cache
[src
] and \
134 hash_tgt
== cache
[tgt
]:
135 print('%s and %s are in the cache: do not build' % (tgt
, src
))
137 print('%s and %s are not up-to-date: building...' % (src
, tgt
))
141 def to_be_built(tgt
, srcs
):
142 '''Should tgt be built (i.e. is it older?) from sources srcs?'''
144 print(tgt
+ ' does not exist: building...')
146 return any(__to_be_built_single(src
, tgt
) for src
in srcs
)
150 '''Context manager for working inside a directory.'''
152 def __init__(self
, dir_
):
154 self
.old_dir
= getcwd()
159 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
166 win_fpath
= '{dst_dir}{appname}-%s-windows.exe' % branch
167 # osx_fpath = '{dst_dir}{appname}-%s-osx.zip' % branch
168 # flatpak_fpath = '{dst_dir}{appname}-%s-flatpak' % branch
169 appimage_fpath
= '{dst_dir}{appname}-%s-appimage' % branch
170 # docs_fpath = '{dst_dir}{appname}-%s-docs.tar.gz' % branch