diff --git a/archive.py b/archive.py index b0ea9f1b..a5c992d8 100644 --- a/archive.py +++ b/archive.py @@ -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 @@ -31,14 +32,14 @@ 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") @@ -46,12 +47,12 @@ 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 ".tar.gz" in archive_fn: @@ -60,12 +61,12 @@ 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: @@ -73,7 +74,7 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json): 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 @@ -81,18 +82,18 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json): # 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: @@ -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: @@ -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 @@ -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: @@ -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 diff --git a/cmds/__init__.py b/cmds/__init__.py index def30d7e..c5688f7c 100644 --- a/cmds/__init__.py +++ b/cmds/__init__.py @@ -35,4 +35,4 @@ "* $ pip install requests\n" "* command install step:\n" "* $ sudo apt-get install python-requests\n" - "****************************************\n") \ No newline at end of file + "****************************************\n") diff --git a/cmds/cmd_menuconfig.py b/cmds/cmd_menuconfig.py index 7cb61081..541278ec 100644 --- a/cmds/cmd_menuconfig.py +++ b/cmds/cmd_menuconfig.py @@ -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): @@ -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 @@ -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] @@ -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) @@ -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): @@ -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') diff --git a/cmds/cmd_package.py b/cmds/cmd_package/__init__.py similarity index 99% rename from cmds/cmd_package.py rename to cmds/cmd_package/__init__.py index 1ec8fe9b..68361a40 100644 --- a/cmds/cmd_package.py +++ b/cmds/cmd_package/__init__.py @@ -94,3 +94,4 @@ def add_parser(sub): dest='package_print_env') parser.set_defaults(func=run_env_cmd) + diff --git a/cmds/cmd_package_list.py b/cmds/cmd_package/cmd_package_list.py similarity index 100% rename from cmds/cmd_package_list.py rename to cmds/cmd_package/cmd_package_list.py diff --git a/cmds/cmd_package_printenv.py b/cmds/cmd_package/cmd_package_printenv.py similarity index 97% rename from cmds/cmd_package_printenv.py rename to cmds/cmd_package/cmd_package_printenv.py index 377ead46..36291da5 100644 --- a/cmds/cmd_package_printenv.py +++ b/cmds/cmd_package/cmd_package_printenv.py @@ -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(): diff --git a/cmds/cmd_package_update.py b/cmds/cmd_package/cmd_package_update.py similarity index 99% rename from cmds/cmd_package_update.py rename to cmds/cmd_package/cmd_package_update.py index 88ae72ab..07ac4f1f 100644 --- a/cmds/cmd_package_update.py +++ b/cmds/cmd_package/cmd_package_update.py @@ -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): @@ -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 @@ -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 diff --git a/cmds/cmd_package_upgrade.py b/cmds/cmd_package/cmd_package_upgrade.py similarity index 98% rename from cmds/cmd_package_upgrade.py rename to cmds/cmd_package/cmd_package_upgrade.py index 982cfe5d..fd46a212 100644 --- a/cmds/cmd_package_upgrade.py +++ b/cmds/cmd_package/cmd_package_upgrade.py @@ -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(): diff --git a/cmds/cmd_package_utils.py b/cmds/cmd_package/cmd_package_utils.py similarity index 79% rename from cmds/cmd_package_utils.py rename to cmds/cmd_package/cmd_package_utils.py index 920cca3f..7e7433d9 100644 --- a/cmds/cmd_package_utils.py +++ b/cmds/cmd_package/cmd_package_utils.py @@ -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 + diff --git a/cmds/cmd_package_wizard.py b/cmds/cmd_package/cmd_package_wizard.py similarity index 99% rename from cmds/cmd_package_wizard.py rename to cmds/cmd_package/cmd_package_wizard.py index 949a3ef6..41b6a8e8 100644 --- a/cmds/cmd_package_wizard.py +++ b/cmds/cmd_package/cmd_package_wizard.py @@ -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 @@ -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 + '.') + diff --git a/cmds/cmd_system.py b/cmds/cmd_system.py index 4b116202..4f682744 100644 --- a/cmds/cmd_system.py +++ b/cmds/cmd_system.py @@ -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): diff --git a/env.py b/env.py index 3dbb3e9a..c122eb9d 100644 --- a/env.py +++ b/env.py @@ -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(): @@ -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') @@ -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') diff --git a/package.py b/package.py index 027bf529..824a9b4e 100644 --- a/package.py +++ b/package.py @@ -186,7 +186,7 @@ def download(self, ver, path, url_from_srv): if not os.path.getsize(path): os.remove(path) else: - if archive.packtest(path): + if archive.package_integrity_test(path): # print "The file is rigit." return True else: @@ -219,7 +219,7 @@ def download(self, ver, path, url_from_srv): retry_count = retry_count + 1 - if archive.packtest(path): # make sure the file is right + if archive.package_integrity_test(path): # make sure the file is right ret = True print("\rDownloded %d KB " % flush_count) print('Start to unpack. Please wait...') diff --git a/pkgsdb.py b/pkgsdb.py index 715d54cb..b556b881 100644 --- a/pkgsdb.py +++ b/pkgsdb.py @@ -21,6 +21,7 @@ # Change Logs: # Date Author Notes # 2018-5-28 SummerGift Add copyright information +# 2020-4-10 SummerGift Code clear up # import sqlite3 @@ -33,27 +34,25 @@ SHOW_SQL = False -def GetFileMd5(filename): +def get_file_md5(filename): if not os.path.isfile(filename): return - myhash = hashlib.md5() + hash_value = hashlib.md5() f = open(filename, 'rb') while True: b = f.read(8096) if not b: break - myhash.update(b) + hash_value.update(b) f.close() - return myhash.hexdigest() + return hash_value.hexdigest() def get_conn(path): conn = sqlite3.connect(path) if os.path.exists(path) and os.path.isfile(path): - # print('on disk:[{}]'.format(path)) return conn else: - conn = None print('on memory:[:memory:]') return sqlite3.connect(':memory:') @@ -74,17 +73,16 @@ def create_table(conn, sql): if sql is not None and sql != '': cu = get_cursor(conn) if SHOW_SQL: - print('执行sql:[{}]'.format(sql)) + print('execute :[{}]'.format(sql)) cu.execute(sql) conn.commit() - # print('create data table successful!') close_all(conn) else: print('the [{}] is empty or equal None!'.format(sql)) def save(conn, sql, data): - '''insert data to database''' + """insert data to database""" if sql is not None and sql != '': if data is not None: cu = get_cursor(conn) @@ -100,14 +98,15 @@ def save(conn, sql, data): def isdataexist(pathname): ret = True - dbpathname = Import('dbsqlite_pathname') + dbfilename = Import('dbsqlite_pathname') - conn = get_conn(dbpathname) + conn = get_conn(dbfilename) c = get_cursor(conn) sql = 'SELECT md5 from packagefile where pathname = "' + pathname + '"' cursor = c.execute(sql) for row in cursor: dbmd5 = row[0] + if dbmd5: ret = False conn.close() @@ -115,29 +114,26 @@ def isdataexist(pathname): # 将数据添加到数据库,如果数据库中已经存在则不重复添加 -def savetodb(pathname, pkgspathname, before_change_name): - dbpathname = Import('dbsqlite_pathname') +def save_to_database(pathname, package_pathname, before_change_name): + db_pathname = Import('dbsqlite_pathname') bsp_root = Import('bsp_root') - bsppkgs = os.path.join(bsp_root, 'packages') + bsp_packages_path = os.path.join(bsp_root, 'packages') - conn = get_conn(dbpathname) + conn = get_conn(db_pathname) save_sql = '''insert into packagefile values (?, ?, ?)''' - package = os.path.basename(pkgspathname) - md5pathname = os.path.join(bsppkgs, before_change_name) - # print("pathname to save : %s"%pathname) - # print("md5pathname : %s"%md5pathname) + package = os.path.basename(package_pathname) + md5pathname = os.path.join(bsp_packages_path, before_change_name) if not os.path.isfile(md5pathname): print("md5pathname is Invalid") - md5 = GetFileMd5(md5pathname) - # print("md5 to save : %s"%md5) + md5 = get_file_md5(md5pathname) data = [(pathname, package, md5)] save(conn, save_sql, data) -def dbdump(dbpathname): - conn = get_conn(dbpathname) +def dbdump(dbfilename): + conn = get_conn(dbfilename) c = get_cursor(conn) cursor = c.execute("SELECT pathname, package, md5 from packagefile") for row in cursor: @@ -147,39 +143,33 @@ def dbdump(dbpathname): conn.close() -def remove_unchangedfile(pathname, dbpathname, dbsqlname): +def remove_unchanged_file(pathname, dbfilename, dbsqlname): """delete unchanged files""" flag = True - conn = get_conn(dbpathname) + conn = get_conn(dbfilename) c = get_cursor(conn) - - # print('pathname : %s'%pathname) - # print('dbsqlname : %s'%dbsqlname) - - filemd5 = GetFileMd5(pathname) - # print("filemd5 : %s"%filemd5) + filemd5 = get_file_md5(pathname) dbmd5 = 0 sql = 'SELECT md5 from packagefile where pathname = "' + dbsqlname + '"' # print sql cursor = c.execute(sql) for row in cursor: - dbmd5 = row[0] # fetch md5 from databas - # print("dbmd5 : %s"%dbmd5) + # fetch md5 from database + dbmd5 = row[0] if dbmd5 == filemd5: # delete file info from database sql = "DELETE from packagefile where pathname = '" + dbsqlname + "'" - cursor = c.execute(sql) conn.commit() os.remove(pathname) else: print("%s has been changed." % pathname) print('Are you sure you want to permanently delete the file: %s?' % os.path.basename(pathname)) - print( - 'If you choose to keep the changed file,you should copy the file to another folder. \nbecaues it may be covered by the next update.') + print('If you choose to keep the changed file,you should copy the file to another folder. ' + '\nbecaues it may be covered by the next update.') if sys.version_info < (3, 0): rc = raw_inuput('Press the Y Key to delete the file or just press Enter to keep the file.') @@ -187,7 +177,6 @@ def remove_unchangedfile(pathname, dbpathname, dbsqlname): rc = input('Press the Y Key to delete the file or just press Enter to keep the file.') if rc == 'y' or rc == 'Y': sql = "DELETE from packagefile where pathname = '" + dbsqlname + "'" - cursor = c.execute(sql) conn.commit() os.remove(pathname) print("%s has been removed.\n" % pathname) @@ -202,7 +191,7 @@ def remove_unchangedfile(pathname, dbpathname, dbsqlname): def deletepackdir(dirpath, dbpathname): flag = getdirdisplay(dirpath, dbpathname) - if flag == True: + if flag: if os.path.exists(dirpath): for root, dirs, files in os.walk(dirpath, topdown=False): for name in files: @@ -226,8 +215,7 @@ def displaydir(filepath, basepath, length, dbpathname): else: pathname = os.path.join(filepath, fi_d) dbsqlname = basepath + os.path.join(filepath, fi_d)[length:] - # print("dbsqlname : %s"%dbsqlname) - if not remove_unchangedfile(pathname, dbpathname, dbsqlname): + if not remove_unchanged_file(pathname, dbpathname, dbsqlname): flag = False return flag diff --git a/vars.py b/vars.py index 6d443b01..b70c3dbc 100644 --- a/vars.py +++ b/vars.py @@ -27,9 +27,11 @@ env_vars = {} + def Export(var): f = sys._getframe(1).f_locals env_vars[var] = f[var] + def Import(var): return env_vars[var]