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

[update] refactoring and add some useful feature for user package management #122

Merged
merged 56 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d5cf840
[optimize] code clear up
SummerGift Apr 7, 2020
08910db
[optimize] Code improvement
SummerGift Apr 7, 2020
e32ed12
[optimize] code improvement
SummerGift Apr 7, 2020
3be7a9d
[optimize] clear up
SummerGift Apr 7, 2020
8e4bc4d
[update] cmds/cmd_package.py
SummerGift Apr 7, 2020
28fdab1
[optimize] Optimize program structure
SummerGift Apr 8, 2020
8cb4f59
[update] cmds/cmd_package_list.py
SummerGift Apr 8, 2020
73ca2ac
[add] cmd_package_wizard.py
SummerGift Apr 8, 2020
f3cfd2b
[add] cmds/cmd_package_printenv.py
SummerGift Apr 8, 2020
662ed0b
[optimize] add cmd_package_upgrade.py and cmd_package_utils.py
SummerGift Apr 8, 2020
e4ed841
[update] cmd_package.py
SummerGift Apr 8, 2020
cfda04f
[optimize] error message
SummerGift Apr 8, 2020
4a7a9ff
[optimize] extract user_input to cmd_package_utils.py
SummerGift Apr 8, 2020
9b26ef4
[optimize] cmds/cmd_package.py
SummerGift Apr 8, 2020
01a62b4
[optimize] add cmd_package_update.py and code clear up
SummerGift Apr 8, 2020
6449f2f
[optimize] cmds/cmd_package_update.py
SummerGift Apr 8, 2020
41b5943
[add new feature] The env tool adds a new feature that if the user re…
SummerGift Apr 8, 2020
a1bd958
[fix] Solve the problem that when the user deletes the .git folder, c…
SummerGift Apr 8, 2020
3b1161f
Merge pull request #116 from SummerLife/fix_change_upstream_error
armink Apr 9, 2020
5ea388a
[optimize] package update progress
SummerGift Apr 9, 2020
e3fde60
[optimize] cmds/cmd_package_update.py
SummerGift Apr 9, 2020
cd9bbf7
Merge pull request #117 from SummerLife/fix_change_upstream_error
armink Apr 9, 2020
7186ed6
[optimize] code clear up
SummerGift Apr 10, 2020
d0aaee0
[optimize] package database operation
SummerGift Apr 10, 2020
a265aa9
[optimize] code clear up
SummerGift Apr 10, 2020
0014f2e
[optimize] convert cmd package feature to a package
SummerGift Apr 11, 2020
8a6ac99
Merge pull request #118 from SummerLife/fix_change_upstream_error
armink Apr 11, 2020
abdff81
[update] using user_input to replace raw_inuput or input
SummerGift Apr 13, 2020
da7fe3c
[update] reduce some unnecessary log
SummerGift Apr 13, 2020
28f7218
[optimize] code clear up
SummerGift Apr 13, 2020
f8555de
[optimize] code clear up and add logging system
SummerGift Apr 13, 2020
f6a7f5c
[optimize] add need_using_mirror_download function to simplify code
SummerGift Apr 13, 2020
31d9106
[update] add some basic logging and optimize code
SummerGift Apr 13, 2020
176966b
[add] is_git_url function
SummerGift Apr 13, 2020
ad28b19
[optimize] code clear up
SummerGift Apr 13, 2020
ab9c945
[update] change default log level
SummerGift Apr 13, 2020
2dd4b05
[optimize] install_pkg function
SummerGift Apr 13, 2020
94b1f79
[optimize] extract git_package_install and non_git_package_install pr…
SummerGift Apr 13, 2020
ce04de2
[update] change pathname to filename
SummerGift Apr 13, 2020
b5a5493
[update] rename Package class
SummerGift Apr 13, 2020
9e09486
[update] fix overwrite package problem
SummerGift Apr 13, 2020
f745068
Merge pull request #119 from SummerLife/fix_change_upstream_error
armink Apr 13, 2020
b423e83
[optimize] code clear up
SummerGift Apr 14, 2020
9f9490b
[update] logging
SummerGift Apr 20, 2020
e3eedc7
[optimize] package unpack process
SummerGift Apr 20, 2020
60912ff
[optimize] update logging
SummerGift Apr 20, 2020
d2d68dd
Merge pull request #120 from SummerLife/fix_change_upstream_error
armink Apr 20, 2020
97cf8ec
[fix] package update error
SummerGift Apr 20, 2020
188f960
Merge pull request #121 from SummerLife/fix_change_upstream_error
armink Apr 20, 2020
a0f8bf1
[update] code clear up
SummerGift Apr 21, 2020
cc5ca61
[optimize] remove_folder function
SummerGift Apr 21, 2020
528783d
[optimize] package install process, add more error handle
SummerGift Apr 21, 2020
6655508
[optimize] move package function
SummerGift Apr 21, 2020
c8c9357
Merge pull request #123 from SummerLife/fix_change_upstream_error
armink Apr 21, 2020
0674cdb
[optimize] package install handle
SummerGift Apr 22, 2020
bdcbf21
Merge pull request #124 from SummerLife/fix_change_upstream_error
armink Apr 22, 2020
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
152 changes: 102 additions & 50 deletions archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,87 +21,137 @@
# Change Logs:
# Date Author Notes
# 2018-5-28 SummerGift Add copyright information
# 2020-4-10 SummerGift Code clear up
#

import logging
import os
import shutil
import tarfile
import zipfile
import os
import pkgsdb
import platform
import shutil

from cmds.cmd_package.cmd_package_utils import is_windows, remove_folder

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

iswindows = False

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

if ".tar.bz2" in archive_fn:
arch = tarfile.open(archive_fn, "r:bz2")
def unpack(archive_filename, bsp_package_path, package_info, package_name):
if ".tar.bz2" in archive_filename:
arch = tarfile.open(archive_filename, "r:bz2")
for tarinfo in arch:
arch.extract(tarinfo, path)
arch.extract(tarinfo, bsp_package_path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
if iswindows:
if not os.path.isdir(os.path.join(bsp_package_path, a)):
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_filename)
arch.close()

if ".tar.gz" in archive_fn:
arch = tarfile.open(archive_fn, "r:gz")
if ".tar.gz" in archive_filename:
arch = tarfile.open(archive_filename, "r:gz")
for tarinfo in arch:
arch.extract(tarinfo, path)
arch.extract(tarinfo, bsp_package_path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
if iswindows:
if not os.path.isdir(os.path.join(bsp_package_path, a)):
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_filename)
arch.close()

if ".zip" in archive_fn:
arch = zipfile.ZipFile(archive_fn, "r")
if ".zip" in archive_filename:
if not handle_zip_package(archive_filename, bsp_package_path, package_name, package_info):
return False

return True


def handle_zip_package(archive_filename, bsp_package_path, package_name, package_info):
package_version = package_info['ver']
package_temp_path = os.path.join(bsp_package_path, "package_temp")

try:
if remove_folder(package_temp_path):
os.makedirs(package_temp_path)
except Exception as e:
logging.warning('Error message : {0}'.format(e))

logging.info("BSP packages path {0}".format(bsp_package_path))
logging.info("BSP package temp path: {0}".format(package_temp_path))
logging.info("archive filename : {0}".format(archive_filename))

try:
flag = True
package_folder_name = ""
package_name_with_version = ""
arch = zipfile.ZipFile(archive_filename, "r")
for item in arch.namelist():
arch.extract(item, path)
if not os.path.isdir(os.path.join(path, item)):
if iswindows:
arch.extract(item, package_temp_path)
if not os.path.isdir(os.path.join(package_temp_path, item)):
if is_windows():
right_path = item.replace('/', '\\')
else:
right_path = item

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

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

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

if os.path.isdir(os.path.join(path, change_dirname)):
if iswindows:
cmd = 'rd /s /q ' + os.path.join(path, change_dirname)
os.system(cmd)
else:
shutil.rmtree(os.path.join(path, change_dirname))

os.rename(os.path.join(path, dir_name),os.path.join(path, change_dirname))

right_name_to_db = right_path.replace(package_folder_name, package_name_with_version, 1)
right_path = os.path.join("package_temp", right_path)
pkgsdb.save_to_database(right_name_to_db, archive_filename, right_path)
arch.close()

def packtest(path):
if not move_package_to_bsp_packages(package_folder_name, package_name, package_temp_path, package_version,
bsp_package_path):
return False
except Exception as e:
logging.warning('unpack error message : {0}'.format(e))
logging.warning('unpack {0} failed'.format(os.path.basename(archive_filename)))
# remove temp folder and archive file
remove_folder(package_temp_path)
os.remove(archive_filename)
return False

return True


def move_package_to_bsp_packages(package_folder_name, package_name, package_temp_path, package_version,
bsp_packages_path):
"""move package in temp folder to bsp packages folder."""
origin_package_folder_path = os.path.join(package_temp_path, package_folder_name)
package_name_with_version = package_name + '-' + package_version
package_folder_in_temp = os.path.join(package_temp_path, package_name_with_version)
bsp_package_path = os.path.join(bsp_packages_path, package_name_with_version)
logging.info("origin name: {0}".format(origin_package_folder_path))
logging.info("rename name: {0}".format(package_folder_in_temp))

result = True
try:
# rename package folder name to package name with version
os.rename(origin_package_folder_path, package_folder_in_temp)

# if there is no specified version package in the bsp package path,
# then move package from package_folder_in_temp to bsp_package_path
if not os.path.isdir(bsp_package_path):
shutil.move(package_folder_in_temp, bsp_package_path)
except Exception as e:
logging.warning('{0}'.format(e))
result = False
finally:
# must remove temp folder
remove_folder(package_temp_path)

return result


def package_integrity_test(path):
ret = True

if path.find(".zip") != -1:
Expand All @@ -116,7 +166,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 +177,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 +186,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
12 changes: 12 additions & 0 deletions cmds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@
#

__all__ = ['cmd_package', 'cmd_system', 'cmd_menuconfig']

try:
import requests
except ImportError:
print("****************************************\n"
"* Import requests module error.\n"
"* Please install requests module first.\n"
"* pip install step:\n"
"* $ pip install requests\n"
"* command install step:\n"
"* $ sudo apt-get install python-requests\n"
"****************************************\n")
77 changes: 16 additions & 61 deletions cmds/cmd_menuconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
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):
''' judge if it's CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER'''
"""judge if it's CONFIG_PKG_XX_PATH or CONFIG_PKG_XX_VER"""

if type(config_str) == type('a'):
if isinstance(config_str, str):
if config_str.startswith("PKG_") and (config_str.endswith('_PATH') or config_str.endswith('_VER')):
return True
return False
Expand All @@ -43,7 +44,8 @@ def is_pkg_special_config(config_str):
def mk_rtconfig(filename):
try:
config = open(filename, 'r')
except:
except Exception as e:
print('Error message:%s' % e)
print('open config:%s failed' % filename)
return

Expand Down Expand Up @@ -89,7 +91,7 @@ def mk_rtconfig(filename):
if setting[1] == 'y':
rtconfig.write('#define %s\n' % setting[0])
else:
rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$",line)[0]))
rtconfig.write('#define %s %s\n' % (setting[0], re.findall(r"^.*?=(.*)$", line)[0]))

if os.path.isfile('rtconfig_project.h'):
rtconfig.write('#include "rtconfig_project.h"\n')
Expand All @@ -99,54 +101,7 @@ def mk_rtconfig(filename):
rtconfig.close()


def find_macro_in_config(filename, macro_name):
try:
config = open(filename, "r")
except:
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]
kconfig_win7_path = os.path.join(
Expand All @@ -159,9 +114,9 @@ def cmd(args):
print("\n\033[1;31;40m<menuconfig> 命令应当在某一特定 BSP 目录下执行,例如:\"rt-thread/bsp/stm32/stm32f091-st-nucleo\"\033[0m")
print("\033[1;31;40m请确保当前目录为 BSP 根目录,并且该目录中有 Kconfig 文件。\033[0m\n")

print ("<menuconfig> command should be used in a bsp root path with a Kconfig file.")
print ("Example: \"rt-thread/bsp/stm32/stm32f091-st-nucleo\"")
print ("You should check if there is a Kconfig file in your bsp root first.")
print("<menuconfig> command should be used in a bsp root path with a Kconfig file.")
print("Example: \"rt-thread/bsp/stm32/stm32f091-st-nucleo\"")
print("You should check if there is a Kconfig file in your bsp root first.")

if platform.system() == "Windows":
os.system('chcp 437 > nul')
Expand Down Expand Up @@ -195,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 @@ -229,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 All @@ -242,13 +197,13 @@ def cmd(args):
if find_macro_in_config(fn, 'SYS_CREATE_MDK_IAR_PROJECT'):
if find_macro_in_config(fn, 'SYS_CREATE_MDK4'):
os.system('scons --target=mdk4 -s')
print("Create mdk4 project done")
print("Create mdk4 project done")
elif find_macro_in_config(fn, 'SYS_CREATE_MDK5'):
os.system('scons --target=mdk5 -s')
print("Create mdk5 project done")
print("Create mdk5 project done")
elif find_macro_in_config(fn, 'SYS_CREATE_IAR'):
os.system('scons --target=iar -s')
print("Create iar project done")
print("Create iar project done")


def add_parser(sub):
Expand Down Expand Up @@ -277,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
Loading