Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-for-android/pythonforandroid/util.py at develop · gg582/python-for-android · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
gg582
/
python-for-android
Public
forked from
kivy/python-for-android
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
python-for-android
/
pythonforandroid
/
util.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
179 lines (139 loc) · 5.69 KB
develop
Breadcrumbs
python-for-android
/
pythonforandroid
/
util.py
Copy path
Top
File metadata and controls
Code
Blame
179 lines (139 loc) · 5.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import
contextlib
from
unittest
import
mock
from
fnmatch
import
fnmatch
import
logging
from
os
.
path
import
exists
,
join
from
os
import
getcwd
,
chdir
,
makedirs
,
walk
from
pathlib
import
Path
from
platform
import
uname
import
shutil
from
tempfile
import
mkdtemp
import
packaging
.
version
from
pythonforandroid
.
logger
import
(
logger
,
Err_Fore
,
error
,
info
)
LOGGER
=
logging
.
getLogger
(
"p4a.util"
)
build_platform
=
"{system}-{machine}"
.
format
(
system
=
uname
().
system
,
machine
=
uname
().
machine
).
lower
()
"""the build platform in the format `system-machine`. We use
this string to define the right build system when compiling some recipes or
to get the right path for clang compiler"""
@
contextlib
.
contextmanager
def
current_directory
(
new_dir
):
cur_dir
=
getcwd
()
logger
.
info
(
''
.
join
((
Err_Fore
.
CYAN
,
'-> directory context '
,
new_dir
,
Err_Fore
.
RESET
)))
chdir
(
new_dir
)
yield
logger
.
info
(
''
.
join
((
Err_Fore
.
CYAN
,
'<- directory context '
,
cur_dir
,
Err_Fore
.
RESET
)))
chdir
(
cur_dir
)
@
contextlib
.
contextmanager
def
temp_directory
():
temp_dir
=
mkdtemp
()
try
:
logger
.
debug
(
''
.
join
((
Err_Fore
.
CYAN
,
' + temp directory used '
,
temp_dir
,
Err_Fore
.
RESET
)))
yield
temp_dir
finally
:
shutil
.
rmtree
(
temp_dir
)
logger
.
debug
(
''
.
join
((
Err_Fore
.
CYAN
,
' - temp directory deleted '
,
temp_dir
,
Err_Fore
.
RESET
)))
def
walk_valid_filens
(
base_dir
,
invalid_dir_names
,
invalid_file_patterns
):
"""Recursively walks all the files and directories in ``dirn``,
ignoring directories that match any pattern in ``invalid_dirns``
and files that patch any pattern in ``invalid_filens``.
``invalid_dirns`` and ``invalid_filens`` should both be lists of
strings to match. ``invalid_dir_patterns`` expects a list of
invalid directory names, while ``invalid_file_patterns`` expects a
list of glob patterns compared against the full filepath.
File and directory paths are evaluated as full paths relative to ``dirn``.
"""
for
dirn
,
subdirs
,
filens
in
walk
(
base_dir
):
# Remove invalid subdirs so that they will not be walked
for
i
in
reversed
(
range
(
len
(
subdirs
))):
subdir
=
subdirs
[
i
]
if
subdir
in
invalid_dir_names
:
subdirs
.
pop
(
i
)
for
filen
in
filens
:
for
pattern
in
invalid_file_patterns
:
if
fnmatch
(
filen
,
pattern
):
break
else
:
yield
join
(
dirn
,
filen
)
def
load_source
(
module
,
filename
):
# Python 3.5+
import
importlib
.
util
if
hasattr
(
importlib
.
util
,
'module_from_spec'
):
spec
=
importlib
.
util
.
spec_from_file_location
(
module
,
filename
)
mod
=
importlib
.
util
.
module_from_spec
(
spec
)
spec
.
loader
.
exec_module
(
mod
)
return
mod
else
:
# Python 3.3 and 3.4:
from
importlib
.
machinery
import
SourceFileLoader
return
SourceFileLoader
(
module
,
filename
).
load_module
()
class
BuildInterruptingException
(
Exception
):
def
__init__
(
self
,
message
,
instructions
=
None
):
super
().
__init__
(
message
,
instructions
)
self
.
message
=
message
self
.
instructions
=
instructions
def
handle_build_exception
(
exception
):
"""
Handles a raised BuildInterruptingException by printing its error
message and associated instructions, if any, then exiting.
"""
error
(
'Build failed: {}'
.
format
(
exception
.
message
))
if
exception
.
instructions
is
not
None
:
info
(
'Instructions: {}'
.
format
(
exception
.
instructions
))
exit
(
1
)
def
rmdir
(
dn
,
ignore_errors
=
False
):
if
not
exists
(
dn
):
return
LOGGER
.
debug
(
"Remove directory and subdirectory {}"
.
format
(
dn
))
shutil
.
rmtree
(
dn
,
ignore_errors
)
def
ensure_dir
(
dn
):
if
exists
(
dn
):
return
LOGGER
.
debug
(
"Create directory {0}"
.
format
(
dn
))
makedirs
(
dn
)
def
move
(
source
,
destination
):
LOGGER
.
debug
(
"Moving {} to {}"
.
format
(
source
,
destination
))
shutil
.
move
(
source
,
destination
)
def
touch
(
filename
):
Path
(
filename
).
touch
()
def
build_tools_version_sort_key
(
version_string
:
str
,
)
->
packaging
.
version
.
Version
:
"""
Returns a packaging.version.Version object for comparison purposes.
It includes canonicalization of the version string to allow for
comparison of versions with spaces in them (historically, RC candidates)
If the version string is invalid, it returns a version object with
version 0, which will be sorted at worst position.
"""
try
:
# Historically, Android build release candidates have had
# spaces in the version number.
return
packaging
.
version
.
Version
(
version_string
.
replace
(
" "
,
""
))
except
packaging
.
version
.
InvalidVersion
:
# Put badly named versions at worst position.
return
packaging
.
version
.
Version
(
"0"
)
def
max_build_tool_version
(
build_tools_versions
:
list
,
)
->
str
:
"""
Returns the maximum build tools version from a list of build tools
versions. It uses the :meth:`build_tools_version_sort_key` function to
canonicalize the version strings and then returns the maximum version.
"""
return
max
(
build_tools_versions
,
key
=
build_tools_version_sort_key
)
def
patch_wheel_setuptools_logging
():
"""
When setuptools is not present and the root logger has no handlers,
Wheels would configure the root logger with DEBUG level, refs:
- https://github.com/pypa/wheel/blob/0.44.0/src/wheel/util.py
- https://github.com/pypa/wheel/blob/0.44.0/src/wheel/_setuptools_logging.py
Both of these conditions are met in our CI, leading to very verbose
and unreadable `sh` logs. Patching it prevents that.
"""
return
mock
.
patch
(
"wheel._setuptools_logging.configure"
)
You can’t perform that action at this time.