Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimize] refactor cmd package command #118

Merged
merged 4 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Change Logs:
# Date Author Notes
# 2018-5-28 SummerGift Add copyright information
# 2020-4-10 SummerGift Code clear up
#

import tarfile
Expand All @@ -31,27 +32,27 @@
import shutil


def unpack(archive_fn, path, pkg, pkgs_name_in_json):
def unpack(archive_fn, path, pkg, package_name):
pkg_ver = pkg['ver']
flag = True

iswindows = False

if platform.system() == "Windows":
iswindows = True
is_windows = True
else:
is_windows = False

if ".tar.bz2" in archive_fn:
arch = tarfile.open(archive_fn, "r:bz2")
for tarinfo in arch:
arch.extract(tarinfo, path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
if iswindows:
if is_windows:
right_path = a.replace('/', '\\')
else:
right_path = a
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
pkgsdb.savetodb(a, archive_fn)
pkgsdb.save_to_database(a, archive_fn)
arch.close()

if ".tar.gz" in archive_fn:
Expand All @@ -60,39 +61,39 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json):
arch.extract(tarinfo, path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
if iswindows:
if is_windows:
right_path = a.replace('/', '\\')
else:
right_path = a
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
pkgsdb.savetodb(a, archive_fn)
pkgsdb.save_to_database(a, archive_fn)
arch.close()

if ".zip" in archive_fn:
arch = zipfile.ZipFile(archive_fn, "r")
for item in arch.namelist():
arch.extract(item, path)
if not os.path.isdir(os.path.join(path, item)):
if iswindows:
if is_windows:
right_path = item.replace('/', '\\')
else:
right_path = item

# Gets the folder name and change_dirname only once
if flag:
dir_name = os.path.split(right_path)[0]
change_dirname = pkgs_name_in_json + '-' + pkg_ver
change_dirname = package_name + '-' + pkg_ver
flag = False

right_name_to_db = right_path.replace(dir_name, change_dirname, 1)
pkgsdb.savetodb(right_name_to_db, archive_fn, right_path)
pkgsdb.save_to_database(right_name_to_db, archive_fn, right_path)
arch.close()

# Change the folder name
change_dirname = pkgs_name_in_json + '-' + pkg_ver
change_dirname = package_name + '-' + pkg_ver

if os.path.isdir(os.path.join(path, change_dirname)):
if iswindows:
if is_windows:
cmd = 'rd /s /q ' + os.path.join(path, change_dirname)
os.system(cmd)
else:
Expand All @@ -101,7 +102,7 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json):
os.rename(os.path.join(path, dir_name), os.path.join(path, change_dirname))


def packtest(path):
def package_integrity_test(path):
ret = True

if path.find(".zip") != -1:
Expand All @@ -116,7 +117,7 @@ def packtest(path):
ret = False
print('package check error. \n')
except Exception as e:
print('packtest error message:%s\t' % e)
print('Package test error message:%s\t' % e)
print("The archive package is broken. \n")
arch.close()
ret = False
Expand All @@ -127,6 +128,7 @@ def packtest(path):
if not tarfile.is_tarfile(path):
ret = False
except Exception as e:
print('Error message:%s' % e)
ret = False

# if ".tar.gz" in path:
Expand All @@ -135,6 +137,7 @@ def packtest(path):
if not tarfile.is_tarfile(path):
ret = False
except Exception as e:
print('Error message:%s' % e)
ret = False

return ret
2 changes: 1 addition & 1 deletion cmds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"* $ pip install requests\n"
"* command install step:\n"
"* $ sudo apt-get install python-requests\n"
"****************************************\n")
"****************************************\n")
56 changes: 6 additions & 50 deletions cmds/cmd_menuconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import os
import platform
import re
from vars import Import, Export
from vars import Import
from .cmd_package.cmd_package_utils import find_macro_in_config


def is_pkg_special_config(config_str):
Expand All @@ -44,6 +45,7 @@ def mk_rtconfig(filename):
try:
config = open(filename, 'r')
except Exception as e:
print('Error message:%s' % e)
print('open config:%s failed' % filename)
return

Expand Down Expand Up @@ -99,52 +101,6 @@ def mk_rtconfig(filename):
rtconfig.close()


def find_macro_in_config(filename, macro_name):
try:
config = open(filename, "r")
except Exception as e:
print('open .config failed')
return

empty_line = 1

for line in config:
line = line.lstrip(' ').replace('\n', '').replace('\r', '')

if len(line) == 0:
continue

if line[0] == '#':
if len(line) == 1:
if empty_line:
continue

empty_line = 1
continue

# comment_line = line[1:]
if line.startswith('# CONFIG_'):
line = ' ' + line[9:]
else:
line = line[1:]

# print line

empty_line = 0
else:
empty_line = 0
setting = line.split('=')
if len(setting) >= 2:
if setting[0].startswith('CONFIG_'):
setting[0] = setting[0][7:]

if setting[0] == macro_name and setting[1] == 'y':
return True

config.close()
return False


def cmd(args):
env_root = Import('env_root')
os_version = platform.platform(True)[10:13]
Expand Down Expand Up @@ -194,7 +150,7 @@ def cmd(args):
os.system('kconfig-mconf Kconfig -n')

elif args.menuconfig_setting:
env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
env_kconfig_path = os.path.join(env_root, r'tools\scripts\cmds')
beforepath = os.getcwd()
os.chdir(env_kconfig_path)

Expand Down Expand Up @@ -228,7 +184,7 @@ def cmd(args):
mk_rtconfig(fn)

if platform.system() == "Windows":
env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
env_kconfig_path = os.path.join(env_root, r'tools\scripts\cmds')
fn = os.path.join(env_kconfig_path, '.config')

if not os.path.isfile(fn):
Expand Down Expand Up @@ -276,7 +232,7 @@ def add_parser(sub):
dest='menuconfig_setting')

parser.add_argument('--easy',
help='easy mode,place kconfig file everywhere,just modify the option env="RTT_ROOT" default "../.."',
help='easy mode, place kconfig everywhere, modify the option env="RTT_ROOT" default "../.."',
action='store_true',
default=False,
dest='menuconfig_easy')
Expand Down
1 change: 1 addition & 0 deletions cmds/cmd_package.py → cmds/cmd_package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ def add_parser(sub):
dest='package_print_env')

parser.set_defaults(func=run_env_cmd)

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def package_print_env():
if platform.system() != 'Windows':
env_root = os.path.join(os.getenv('HOME'), '.env')

print("ENV_ROOT:%s" % (env_root))
print("ENV_ROOT:%s" % env_root)


def package_print_help():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import requests
from package import Package, Bridge_SConscript
from vars import Import, Export
from .cmd_package_utils import get_url_from_mirror_server, execute_command, git_pull_repo, user_input
from .cmd_menuconfig import find_macro_in_config
from .cmd_package_utils import get_url_from_mirror_server, execute_command, git_pull_repo, user_input, find_macro_in_config


def determine_support_chinese(env_root):
Expand Down Expand Up @@ -194,6 +193,7 @@ def install_pkg(env_root, pkgs_root, bsp_root, pkg, force_update):
git_check_cmd = 'git checkout -q ' + ver_sha
execute_command(git_check_cmd, cwd=repo_path)
except Exception as e:
print('Error message:%s' % e)
print("\nFailed to download software package with git. Please check the network connection.")
return False

Expand Down Expand Up @@ -240,7 +240,7 @@ def install_pkg(env_root, pkgs_root, bsp_root, pkg, force_update):
pkg_dir = os.path.splitext(pkg_dir)[0]
package_path = os.path.join(local_pkgs_path, package.get_filename(pkg['ver']))

if not archive.packtest(package_path):
if not archive.package_integrity_test(package_path):
print("package : %s is invalid" % package_path.encode("utf-8"))
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

import os
from vars import Import
from .cmd_package_utils import git_pull_repo, get_url_from_mirror_server
from .cmd_menuconfig import find_macro_in_config
from .cmd_package_utils import git_pull_repo, get_url_from_mirror_server, find_macro_in_config


def upgrade_packages_index():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,51 @@ def user_input(msg=None):
value = input()

return value


def find_macro_in_config(filename, macro_name):
try:
config = open(filename, "r")
except Exception as e:
print('Error message:%s' % e)
print('open .config failed')
return

empty_line = 1

for line in config:
line = line.lstrip(' ').replace('\n', '').replace('\r', '')

if len(line) == 0:
continue

if line[0] == '#':
if len(line) == 1:
if empty_line:
continue

empty_line = 1
continue

# comment_line = line[1:]
if line.startswith('# CONFIG_'):
line = ' ' + line[9:]
else:
line = line[1:]

# print line

empty_line = 0
else:
empty_line = 0
setting = line.split('=')
if len(setting) >= 2:
if setting[0].startswith('CONFIG_'):
setting[0] = setting[0][7:]

if setting[0] == macro_name and setting[1] == 'y':
return True

config.close()
return False

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import os
import re
import sys
from package import Kconfig_file, Package_json_file
from string import Template
from .cmd_package_utils import user_input
Expand Down Expand Up @@ -153,3 +152,4 @@ def package_wizard():
print('\nThe package index has been created \033[1;32;40msuccessfully\033[0m.')
print('Please \033[5;34;40mupdate\033[0m other information of this package '
'based on Kconfig and package.json in directory ' + name + '.')

13 changes: 5 additions & 8 deletions cmds/cmd_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ def cmd(args):
if args.system_update:
dir_list = os.listdir(packages_root)

kconfig = file(os.path.join(packages_root, 'Kconfig'), 'w')

for item in dir_list:
if os.path.isfile(os.path.join(packages_root, item, 'Kconfig')):
kconfig.write('source "$PKGS_DIR/' + item + '/Kconfig"')
kconfig.write('\n')

kconfig.close()
with open(os.path.join(packages_root, 'Kconfig'), 'w') as kconfig:
for item in dir_list:
if os.path.isfile(os.path.join(packages_root, item, 'Kconfig')):
kconfig.write('source "$PKGS_DIR/' + item + '/Kconfig"')
kconfig.write('\n')


def add_parser(sub):
Expand Down
12 changes: 6 additions & 6 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from cmds import *
from vars import Export

__version__ = 'rt-thread packages v1.1.0'
__version__ = 'rt-thread packages v1.2.0'


def init_argparse():
Expand Down Expand Up @@ -60,7 +60,7 @@ def main():
env_root = os.path.join(os.getenv('USERPROFILE'), '.env')

sys.path = sys.path + [os.path.join(script_root)]

pkgs_root = os.getenv("PKGS_ROOT")
if pkgs_root is None:
pkgs_root = os.path.join(env_root, 'packages')
Expand All @@ -76,10 +76,10 @@ def main():
if platform.system() == "Windows":
os.system('chcp 65001 > nul')

print ("\n\033[1;31;40m警告:\033[0m")
print ("\033[1;31;40m当前路径不支持非英文字符,请修改当前路径为纯英文路径。\033[0m")
print ("\033[1;31;40mThe current path does not support non-English characters.\033[0m")
print ("\033[1;31;40mPlease modify the current path to a pure English path.\033[0m")
print("\n\033[1;31;40m警告:\033[0m")
print("\033[1;31;40m当前路径不支持非英文字符,请修改当前路径为纯英文路径。\033[0m")
print("\033[1;31;40mThe current path does not support non-English characters.\033[0m")
print("\033[1;31;40mPlease modify the current path to a pure English path.\033[0m")

if platform.system() == "Windows":
os.system('chcp 437 > nul')
Expand Down
Loading