Merge pull request #20590 from Chocobo1/py

GHA CI: check python scripts
This commit is contained in:
Chocobo1 2024-03-25 13:42:09 +08:00 committed by GitHub
commit f2d6129db3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 124 additions and 117 deletions

79
.github/workflows/ci_python.yaml vendored Normal file
View file

@ -0,0 +1,79 @@
name: CI - Python
on: [pull_request, push]
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: ${{ github.head_ref != '' }}
jobs:
ci:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup python (auxiliary scripts)
uses: actions/setup-python@v5
with:
python-version: '3' # use default version
- name: Install tools (auxiliary scripts)
run: pip install bandit pycodestyle pyflakes
- name: Gather files (auxiliary scripts)
run: |
export "PY_FILES=$(find . -type f -name '*.py' ! -path '*searchengine*' -printf '%p ')"
echo $PY_FILES
echo "PY_FILES=$PY_FILES" >> "$GITHUB_ENV"
- name: Lint code (auxiliary scripts)
run: |
pyflakes $PY_FILES
bandit --skip B314,B405 $PY_FILES
- name: Format code (auxiliary scripts)
run: |
pycodestyle \
--max-line-length=1000 \
--statistics \
$PY_FILES
- name: Build code (auxiliary scripts)
run: |
python -m compileall $PY_FILES
- name: Setup python (search engine)
uses: actions/setup-python@v5
with:
python-version: '3.7'
- name: Install tools (search engine)
run: pip install bandit pycodestyle pyflakes
- name: Gather files (search engine)
run: |
export "PY_FILES=$(find . -type f -name '*.py' -path '*searchengine*' ! -name 'socks.py' -printf '%p ')"
echo $PY_FILES
echo "PY_FILES=$PY_FILES" >> "$GITHUB_ENV"
- name: Lint code (search engine)
run: |
pyflakes $PY_FILES
bandit --skip B110,B310,B314,B405 $PY_FILES
- name: Format code (search engine)
run: |
pycodestyle \
--ignore=E265,E402 \
--max-line-length=1000 \
--statistics \
$PY_FILES
- name: Build code (search engine)
run: |
python -m compileall $PY_FILES

View file

@ -41,7 +41,7 @@ jobs:
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
config-file: ./.github/workflows/helper/codeql/js.yaml
config-file: .github/workflows/helper/codeql/js.yaml
languages: javascript
- name: Run CodeQL analysis

View file

@ -30,6 +30,7 @@ from typing import Optional, Sequence
import argparse
import re
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to check')
@ -52,7 +53,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
f"Part: \"{match.group()}\"\n\n")
line_counter += 1
except UnicodeDecodeError as error:
except UnicodeDecodeError:
# not a text file, skip
continue
@ -64,5 +65,6 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
return 0
if __name__ == '__main__':
exit(main())

View file

@ -7,9 +7,11 @@ import shutil
import sys
from typing import List
def isNotStub(path: str) -> bool:
return (os.path.getsize(path) >= (10 * 1024))
def main() -> int:
parser = argparse.ArgumentParser(description='Gather valid Qt translations for NSIS packaging.')
parser.add_argument("qt_translations_folder", help="Qt's translations folder")
@ -27,5 +29,6 @@ def main() -> int:
return 0
if __name__ == '__main__':
sys.exit(main())

View file

@ -1,4 +1,4 @@
#VERSION: 1.46
#VERSION: 1.47
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
@ -45,13 +45,13 @@ def anySizeToBytes(size_string):
# separate integer from unit
try:
size, unit = size_string.split()
except:
except Exception:
try:
size = size_string.strip()
unit = ''.join([c for c in size if c.isalpha()])
if len(unit) > 0:
size = size[:-len(unit)]
except:
except Exception:
return -1
if len(size) == 0:
return -1

View file

@ -1,90 +0,0 @@
#!/usr/bin/env python
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Small script to update qrc files (lang.qrc, icons.qrc)
# by Christophe Dumez <chris@qbittorrent.org>
import os
from os.path import splitext, join
# update languages files directory
languages_list = [x for x in os.listdir('lang') if x.endswith('.qm')]
output = '''<!DOCTYPE RCC><RCC version="1.0">
<qresource>
'''
for language in languages_list:
output += ' <file>%s</file>'%('lang'+os.sep+language)
output += os.linesep
output += '''</qresource>
</RCC>'''
lang_file = open('lang.qrc', 'w')
lang_file.write(output)
lang_file.close()
# update search_engine directory
os.chdir('gui/searchengine')
search_list = []
for root, dirs, files in os.walk('nova3'):
for file in files:
if file.startswith("__"):
continue
if splitext(file)[-1] in ('.py', '.png'):
search_list.append(join(root, file))
output = '''<!DOCTYPE RCC><RCC version="1.0">
<qresource>
'''
for file in search_list:
output += ' <file>%s</file>'%(file)
output += os.linesep
output += '''</qresource>
</RCC>'''
search_file = open('search.qrc', 'w')
search_file.write(output)
search_file.close()
os.chdir('../..');
# update icons files directory
icons_list = []
for root, dirs, files in os.walk('icons'):
if 'skin_unused' in dirs:
dirs.remove('skin_unused')
for file in files:
if splitext(file)[-1] in ('.png', '.jpg', '.gif'):
icons_list.append(join(root, file))
output = '''<!DOCTYPE RCC><RCC version="1.0">
<qresource>
'''
for icon in icons_list:
output += ' <file>%s</file>'%(icon)
output += os.linesep
output += '''</qresource>
</RCC>'''
icons_file = open('icons.qrc', 'w')
icons_file.write(output)
icons_file.close()

View file

@ -16,6 +16,7 @@ import os
from pathlib import PurePath
import sys
def updateJson(json_file: str, source: dict[str, str]) -> None:
trimmed_path: str = json_file
path_parts = PurePath(json_file).parts
@ -42,6 +43,7 @@ def updateJson(json_file: str, source: dict[str, str]) -> None:
file.write("\n")
file.truncate()
def splitJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -> None:
locales: list[str] = glob.glob("*.json", root_dir=transifex_dir)
locales = [x for x in locales if x != "en.json"]
@ -65,6 +67,7 @@ def splitJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -
updateJson(public_file, transifex_json)
updateJson(private_file, transifex_json)
def mergeJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -> None:
transifex_en_file: str = f"{transifex_dir}/en.json"
public_en_file: str = f"{json_public_dir}/en.json"
@ -92,6 +95,7 @@ def mergeJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -
json.dump(transifex_en_json, file, ensure_ascii=False, indent=2, sort_keys=True)
file.write("\n")
def main() -> int:
script_path: str = os.path.dirname(os.path.realpath(__file__))
transifex_dir: str = f"{script_path}/transifex"
@ -124,5 +128,6 @@ def main() -> int:
return 0
if __name__ == '__main__':
sys.exit(main())

View file

@ -14,6 +14,7 @@ import os
import sys
import xml.etree.ElementTree as ET
def getTsStrings(ts_file: str, key: str) -> list[str]:
tr_strings: list[str] = []
tree = ET.parse(ts_file)
@ -33,6 +34,7 @@ def getTsStrings(ts_file: str, key: str) -> list[str]:
return tr_strings
def migrate2Json(ts_dir: str, json_dir: str, locale: str) -> None:
ts_file: str = f"{ts_dir}/webui_{locale}.ts"
js_file: str = f"{json_dir}/{locale}.json"
@ -71,6 +73,7 @@ def migrate2Json(ts_dir: str, json_dir: str, locale: str) -> None:
print("\tFinished.")
def main() -> int:
script_path: str = os.path.dirname(os.path.realpath(__file__))
ts_dir: str = f"{script_path}/translations"
@ -97,5 +100,6 @@ def main() -> int:
return 0
if __name__ == '__main__':
sys.exit(main())

View file

@ -41,9 +41,10 @@ no_obsolete = False
www_folder = "."
ts_folder = os.path.join(www_folder, "translations")
def parseSource(filename, sources):
print("Parsing %s..." % (os.path.normpath(filename)))
with open(filename, encoding = 'utf-8', mode = 'r') as file:
with open(filename, encoding='utf-8', mode='r') as file:
regex = re.compile(
r"QBT_TR\((([^\)]|\)(?!QBT_TR))+)\)QBT_TR\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\]")
for match in regex.finditer(file.read()):
@ -54,11 +55,12 @@ def parseSource(filename, sources):
sources[context] = set()
sources[context].add(string)
def processTranslation(filename, sources):
print('Processing %s...' % (os.path.normpath(filename)))
try:
tree = ET.ElementTree(file = filename)
tree = ET.ElementTree(file=filename)
except Exception:
print('\tFailed to parse %s!' % (os.path.normpath(filename)))
return
@ -117,13 +119,15 @@ def processTranslation(filename, sources):
context.tail = '\n'
context.find('./name').tail = '\n' + indent
messages = context.findall('./message')
if len(messages) == 0: continue
if len(messages) == 0:
continue
for message in messages:
message.text = '\n' + (indent * 2)
message.tail = '\n' + indent
elems = message.findall('./')
if len(elems) == 0: continue
if len(elems) == 0:
continue
for elem in elems:
elem.tail = '\n' + (indent * 2)
@ -131,24 +135,25 @@ def processTranslation(filename, sources):
messages[-1:][0].tail = '\n'
try:
with open(filename, mode = 'wb') as file:
with open(filename, mode='wb') as file:
file.write(b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<!DOCTYPE TS>\n')
tree.write(file, encoding = 'utf-8')
tree.write(file, encoding='utf-8')
except Exception:
print('\tFailed to write %s!' % (os.path.normpath(filename)))
argp = argparse.ArgumentParser(
prog = 'tstool.py', description = 'Update qBittorrent WebUI translation files.')
argp.add_argument('--no-obsolete', dest = 'no_obsolete', action = 'store_true',
default = no_obsolete,
help = 'remove obsolete messages (default: mark them as obsolete)')
argp.add_argument('--www-folder', dest = 'www_folder', action = 'store',
default = www_folder,
help = 'folder with WebUI source files (default: "%s")' % (www_folder))
argp.add_argument('--ts-folder', dest = 'ts_folder', action = 'store',
default = ts_folder,
help = 'folder with WebUI translation files (default: "%s")' % (ts_folder))
prog='tstool.py', description='Update qBittorrent WebUI translation files.')
argp.add_argument('--no-obsolete', dest='no_obsolete', action='store_true',
default=no_obsolete,
help='remove obsolete messages (default: mark them as obsolete)')
argp.add_argument('--www-folder', dest='www_folder', action='store',
default=www_folder,
help='folder with WebUI source files (default: "%s")' % (www_folder))
argp.add_argument('--ts-folder', dest='ts_folder', action='store',
default=ts_folder,
help='folder with WebUI translation files (default: "%s")' % (ts_folder))
args = argp.parse_args()
no_obsolete = args.no_obsolete
@ -174,8 +179,7 @@ print("")
print("Processing translation files...")
for entry in os.scandir(ts_folder):
if (entry.is_file() and entry.name.startswith('webui_')
and entry.name.endswith(".ts")):
if (entry.is_file() and entry.name.startswith('webui_') and entry.name.endswith(".ts")):
processTranslation(entry.path, copy.deepcopy(source_ts))
print("Done!")