diff --git a/README b/README index 2fc93af..a6bc7c5 100644 --- a/README +++ b/README @@ -63,9 +63,11 @@ 3.3 Global Settings 1) In Cross-Platform case, make sure the required commands can be invoked preferentially. - For example, you can do it by the following command for Yocto-User + Install toolchain includes the appropriate target architecture into the host development + system. Use the following command for Yocto user to install toolchain. [ARCH] means the + appropriate target architecture. ----------------------------------------------------------------------- - $ source TOOLCHAIN/environment-setup-i586-poky-linux + $ source TOOLCHAIN/environment-setup-[ARCH]-poky-linux ----------------------------------------------------------------------- 2) Make sure the following environment variable point to correct path. diff --git a/smart/commands/init.py b/smart/commands/init.py new file mode 100644 index 0000000..aad2c48 --- /dev/null +++ b/smart/commands/init.py @@ -0,0 +1,83 @@ +# +# Copyright (C) 2016 FUJITSU LIMITED +# +# Written by Fan Xin +# 2016-08-28 +# +# This file is part of Smart2 Package Manager +# +# This file provides the init command +# Usage: smart init +# + +from smart.transaction import Transaction, PolicyInstall, sortUpgrades +from smart.transaction import INSTALL, REINSTALL +from smart.option import OptionParser +from smart.cache import Package +from smart import * +import string +import re +import os +import pdb +import sys + +#use debug module +#pdb.set_trace() + +USAGE=_("smart init [pacakge repository path] [rootfs path] ") + +DESCRIPTION=_(""" +This command will initial the environment before using smart2. +""") + +EXAMPLES=_(""" +smart install pkgname +smart install '*kgna*' +smart install pkgname-1.0 +smart install pkgname-1.0-1 +smart install pkgname1 pkgname2 +smart install ./somepackage.file +smart install http://some.url/some/path/somepackage.file +""") + +def add (num1=0, num2=0): + return int(num1) + int(num2) + +def sub(num1=0, num2=0): + return int(num1) - int(num2) +#def main(ctrl, opts): + +def option_parser(): + parser = OptionParser(usage=USAGE, + description=DESCRIPTION, + examples=EXAMPLES) + parser.add_option("--info", action="store_true", + help=_("Fan Xin")) + parser.add_option("--paths", action="store_true", + help=_("show path list")) + parser.add_option("--changelog", action="store_true", + help=_("show change log")) + return parser + + +def parse_options(argv): + parser = option_parser() + opts, args = parser.parse_args(argv) + opts.args = args + return opts + +def main(ctrl, opts): + + print sys.argv + #pdb.set_trace() #break point added here + addition = add(sys.argv[1], sys.argv[2]) + print addition + subtraction = sub(sys.argv[1], sys.argv[2]) + print subtraction + + print "Hello, this is smart init command." + +#if __name__ == '__main__': +# main() + +# vim:ts=4:sw=4:et diff --git a/smart/interfaces/tgui/interactive.py b/smart/interfaces/tgui/interactive.py index f117759..fbd798b 100644 --- a/smart/interfaces/tgui/interactive.py +++ b/smart/interfaces/tgui/interactive.py @@ -6,21 +6,39 @@ from smart.interfaces.tgui.interface import TguiInterface from smart.transaction import * from smart.report import Report -import sys, os, copy, textwrap, snack, string, time, re +import sys, os, copy, textwrap, snack, string, time, re, shutil from snack import * +import tarfile +import datetime _TXT_ROOT_TITLE = "Package Installer" -Install_types = [("Install All", "If you choose it, all packages are selected."), \ - ("Busybox base", "Busybox base sample."), \ - ("Customize", "Default, no packages are selected, your can choose packages that you want.")] -INSTALL_ALL = 0 -INSTALL_BUSYBOX = 1 -INSTALL_CUSTOMIZE = 2 +Install_actions = [("Install", "Choose it to install packages."), \ + ("Remove", "Choose it to remove packages"), \ + ("Upgrade", "Choose it to upgrade packages"), \ + ("Create source archive", "Choose it to create source archive"), \ + ("Create spdx archive", "Choose it to Create SPDX archive"), \ + ("Append local file into Root FileSystem", "Choose it to Append local file into RootFS") \ + ] -CONFIRM_EXIT = 0 -CONFIRM_INSTALL = 1 -CONFIRM_LICENSE = 2 +ACTION_INSTALL = 0 +ACTION_REMOVE = 1 +ACTION_UPGRADE = 2 +ACTION_GET_SOURCE = 3 +ACTION_GET_SPDX = 4 +ACTION_GET_LOCALFILE = 5 + +CONFIRM_EXIT = 0 +CONFIRM_INSTALL = 1 +CONFIRM_LICENSE = 2 +CONFIRM_REMOVE = 3 +CONFIRM_UPGRADE = 4 +CONFIRM_GET_SOURCE = 5 +CONFIRM_GET_SPDX = 6 +CONFIRM_APPEND_LOCALFILE = 7 + +ATTENTON_NONE = 0 +ATTENTON_HAVE_UPGRADE = 1 class TguiInteractiveInterface(TguiInterface): @@ -40,6 +58,7 @@ def PKGINSTDispMain(self): STAGE_CUST_LIC = 3 STAGE_PACKAGE = 4 STAGE_PACKAGE_SPEC = 5 + STAGE_PROCESS = 6 screen = None no_gpl3 = False @@ -55,7 +74,7 @@ def PKGINSTDispMain(self): if screen == None: sys.exit(1) - install_type = INSTALL_ALL + install_type = ACTION_INSTALL stage = STAGE_INSTALL_TYPE def __init_pkg_type(): @@ -74,39 +93,122 @@ def __init_pkg_type(): pkgType_ptest = pkgType("ptest", False, "If select, you can see/select *-ptest packages in the next step.") pkgTypeList.append(pkgType_ptest) - return pkgTypeList pkgTypeList = __init_pkg_type() selected_pkgs = [] selected_pkgs_spec = [] pkgs_spec = [] + src_path="" + output_path="" + local_path="" + while True: #============================== # select install type #============================== if stage == STAGE_INSTALL_TYPE: - install_type = PKGINSTTypeWindowCtrl(screen, Install_types, install_type) - stage = STAGE_PACKAGE + + install_type = PKGINSTActionWindowCtrl(screen, Install_actions, install_type) + + if install_type==ACTION_GET_LOCALFILE: + local_path=LocalFileInputWindow(screen, \ + True, \ + " Input Path ", \ + "Specify the path where the local archives you want to append:") + if not local_path == None: + stage = STAGE_PROCESS + else: + continue + + + elif install_type==ACTION_GET_SOURCE: + src_path=PKGINSTPathInputWindow(screen, \ + True, \ + " Input Path ", \ + "Specify the path where the source archives you want to download:") + if not src_path == None: + output_path = PKGINSTPathInputWindow(screen, \ + False, \ + " Input Path ", \ + " Specify the output path: ", \ + "./source") + if not output_path == None: + stage = STAGE_PACKAGE + else: + continue + else: + continue + + + elif install_type== ACTION_GET_SPDX: + src_path=PKGINSTPathInputWindow(screen, \ + True, \ + " Input Path ", \ + "Specify the path where the SPDX files you want to download:") + if not src_path == None: + output_path = PKGINSTPathInputWindow(screen, \ + False, \ + " Input Path ", \ + " Specify the output path: ", \ + "./spdx") + if not output_path == None: + stage = STAGE_PACKAGE + else: + continue + else: + continue + else: + if install_type==ACTION_REMOVE: + transaction.setPolicy(PolicyRemove) + elif install_type==ACTION_UPGRADE: + transaction.setPolicy(PolicyUpgrade) + + stage = STAGE_PACKAGE + + selected_pkgs = [] selected_pkgs_spec = [] pkgs_spec = [] - #if install_type != INSTALL_BUSYBOX: - result = HotkeyExitWindow(screen, confirm_type=CONFIRM_LICENSE) - if result == "y": - no_gpl3 = False + + if install_type == ACTION_INSTALL: + result = HotkeyExitWindow(screen, confirm_type=CONFIRM_LICENSE) + if result == "y": + no_gpl3 = False + else: + no_gpl3 = True else: - no_gpl3 = True + no_gpl3 = False + + + #============================== # select package #============================== elif stage == STAGE_PACKAGE: - (result, selected_pkgs, pkgs_spec) = self.PKGINSTWindowCtrl(screen, install_type, None, no_gpl3, None, selected_pkgs) + (result, selected_pkgs, pkgs_spec) = self.PKGINSTWindowCtrl(screen, install_type, None, no_gpl3, \ + None, selected_pkgs) if result == "b": # back stage = STAGE_INSTALL_TYPE + elif result == "n": - stage = STAGE_PKG_TYPE + if install_type == ACTION_INSTALL: + stage = STAGE_PKG_TYPE + else: + #confirm if or not continue process function + if install_type == ACTION_REMOVE : confirm_type = CONFIRM_REMOVE + elif install_type == ACTION_UPGRADE : confirm_type = CONFIRM_UPGRADE + elif install_type == ACTION_GET_SOURCE : confirm_type = CONFIRM_GET_SOURCE + elif install_type == ACTION_GET_SPDX : confirm_type = CONFIRM_GET_SPDX + elif install_type == ACTION_GET_LOCALFILE : confirm_type = CONFIRM_APPEND_LOCALFILE + + hkey = HotkeyExitWindow(screen, confirm_type) + if hkey == "y": + stage = STAGE_PROCESS + elif hkey == "n": + stage = STAGE_PACKAGE + #============================== # select package type #============================== @@ -121,25 +223,62 @@ def __init_pkg_type(): # select special packages(local, dev, dbg, doc) #============================== elif stage == STAGE_PACKAGE_SPEC: - (result, selected_pkgs_spec, pkgs_temp) = self.PKGINSTWindowCtrl(screen, install_type, pkgTypeList, no_gpl3, pkgs_spec, selected_pkgs_spec) + (result, selected_pkgs_spec, pkgs_temp) = self.PKGINSTWindowCtrl(screen, install_type, pkgTypeList, \ + no_gpl3, pkgs_spec, selected_pkgs_spec) if result == "b": # back stage = STAGE_PKG_TYPE elif result == "k": - stage = STAGE_PKG_TYPE + stage = STAGE_PKG_TYPE elif result == "n": - for pkg in selected_pkgs: - transaction.enqueue(pkg, INSTALL) - for pkg in selected_pkgs_spec: - transaction.enqueue(pkg, INSTALL) + stage = STAGE_PROCESS + + # ============================== + # Process function + # ============================== + elif stage == STAGE_PROCESS: + + if install_type==ACTION_GET_SOURCE or install_type==ACTION_GET_SPDX or install_type==ACTION_GET_LOCALFILE: + + if screen != None: + StopHotkeyScreen(screen) + screen = None + + if install_type==ACTION_GET_SOURCE: + self.installSrc(selected_pkgs, src_path, output_path) + elif install_type==ACTION_GET_SPDX: + self.installSPDX(selected_pkgs, src_path, output_path) + elif install_type==ACTION_GET_LOCALFILE: + #local_path is the changeset path + print local_path + #self.appendLocalFile(local_path) + break + + else: + for pkg in selected_pkgs: #selected_pkgs + if install_type==ACTION_INSTALL: + transaction.enqueue(pkg, INSTALL) + elif install_type==ACTION_REMOVE: + transaction.enqueue(pkg, REMOVE) + elif install_type==ACTION_UPGRADE: + transaction.enqueue(pkg, UPGRADE) + if install_type == ACTION_INSTALL: #selected_pkgs_spec + for pkg in selected_pkgs_spec: + transaction.enqueue(pkg, INSTALL) + transaction.run() + if no_gpl3: oldchangeset = self._changeset - newchangeset = transaction.getChangeSet() + newchangeset = transaction.getChangeSet() result = self.confirmChange(screen, oldchangeset, newchangeset) #continue to install if result == "y": - hkey = HotkeyExitWindow(screen, confirm_type=CONFIRM_INSTALL) + if install_type == ACTION_INSTALL: + confirm_type = CONFIRM_INSTALL + + hkey = HotkeyExitWindow(screen, confirm_type) + if hkey == "y": if screen != None: StopHotkeyScreen(screen) @@ -158,11 +297,33 @@ def __init_pkg_type(): self._ctrl.commitTransaction(transaction, confirm=True) break + #update version info + print "The local_path is "+local_path + if len(local_path) == 0: + print "There is no local chagnese, Only update the version info" + self.appendLocalFile(local_path) if screen != None: StopHotkeyScreen(screen) screen = None + def _DeleteUpgrade(self,packages=None,display_pkgs=[]): + haveUpgrade=False + for i, pkg in enumerate(display_pkgs[:-1]): + for pkg_oth in display_pkgs[i+1:]: + if pkg.name==pkg_oth.name: + haveUpgrade=True + break + if haveUpgrade : + break + ctn=0 + if(haveUpgrade): + for pkg in packages: + if (not pkg.installed) and (pkg in display_pkgs): + ctn+=1 + display_pkgs.remove(pkg) + return haveUpgrade + def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages=None, selected_pkgs=[]): STAGE_SELECT = 1 STAGE_PKG_TYPE = 2 @@ -264,20 +425,33 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages else: display_pkgs = [] - if install_type == INSTALL_ALL: - selected_pkgs = copy.copy(display_pkgs) + if (install_type==ACTION_REMOVE) or (install_type==ACTION_UPGRADE) or (install_type==ACTION_GET_SOURCE) \ + or (install_type==ACTION_GET_SPDX) : + for pkg in packages: + if not pkg.installed: + if pkg in display_pkgs: + display_pkgs.remove(pkg) + elif install_type==ACTION_INSTALL: + if(self._DeleteUpgrade(packages,display_pkgs)): + hkey = HotkeyAttentionWindow(screen, ATTENTON_HAVE_UPGRADE) if len(display_pkgs) == 0: if not no_gpl3: - hkey = HotkeyExitWindow(screen, confirm_type=CONFIRM_INSTALL) - if hkey == "y": - return ("n", selected_pkgs, packages) - elif hkey == "n": - return ("k", selected_pkgs, packages) + if install_type == ACTION_INSTALL : + confirm_type = CONFIRM_INSTALL + hkey = HotkeyExitWindow(screen, confirm_type) + if hkey == "y": + return ("n", selected_pkgs, packages) + elif hkey == "n": + return ("k", selected_pkgs, packages) + else: + hkey=HotkeyAttentionWindow(screen,ATTENTON_NONE) + return ("b", selected_pkgs, packages) else: return ("n", selected_pkgs, packages) else: - for pkg in packages: + if install_type == ACTION_INSTALL : + for pkg in packages: if "-locale-" in pkg.name: display_pkgs.remove(pkg) pkgs_spec.append(pkg) @@ -299,19 +473,24 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages elif pkg.name.endswith('-ptest'): display_pkgs.remove(pkg) pkgs_spec.append(pkg) - if install_type == INSTALL_ALL: - selected_pkgs = copy.copy(display_pkgs) - elif install_type == INSTALL_BUSYBOX: - selected_pkgs = [] - first_busybox_position = 0 - for pkg in display_pkgs: - if pkg.name.startswith('busybox'): - selected_pkgs.append(pkg) - if first_busybox_position == 0: - first_busybox_position = position - position += 1 - position = first_busybox_position - + + if(self._DeleteUpgrade(packages,display_pkgs)): + hkey = HotkeyAttentionWindow(screen, ATTENTON_HAVE_UPGRADE) + + else: + for pkg in packages: + if not pkg.installed: + if pkg in display_pkgs: + display_pkgs.remove(pkg) + + + if len(display_pkgs)==0: + if install_type==ACTION_INSTALL: + stage = STAGE_NEXT + else: + hkey = HotkeyAttentionWindow(screen, ATTENTON_NONE) + return ("b", selected_pkgs, packages) + while True: if stage == STAGE_SELECT: if search == None: @@ -321,7 +500,8 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages position, \ iTargetSize, \ iHostSize, \ - search) + search, \ + install_type) else: (hkey, search_position, pkglist) = PKGINSTPackageWindow(screen, \ searched_ret, \ @@ -329,7 +509,8 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages search_position, \ iTargetSize, \ iHostSize, \ - search) + search, \ + install_type) if hkey == "n": stage = STAGE_NEXT @@ -349,7 +530,9 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages #if in special type packages(dev,doc,locale) select Interface: else: if not no_gpl3: - hkey = HotkeyExitWindow(screen, confirm_type=CONFIRM_INSTALL) + if install_type == ACTION_INSTALL : confirm_type = CONFIRM_INSTALL + + hkey = HotkeyExitWindow(screen, confirm_type) if hkey == "y": return ("n", selected_pkgs, packages) elif hkey == "n": @@ -365,7 +548,7 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages elif stage == STAGE_INFO: ctrl = self._ctrl if not search == None: - PKGINSTPackageInfoWindow(screen, ctrl, searched_ret[position]) + PKGINSTPackageInfoWindow(screen, ctrl, searched_ret[search_position]) else: PKGINSTPackageInfoWindow(screen, ctrl, display_pkgs[position]) stage = STAGE_SELECT @@ -384,6 +567,7 @@ def PKGINSTWindowCtrl(self, screen, install_type, pkgTypeList, no_gpl3, packages if not search == None: def __search_pkgs(keyword, pkgs): searched_pgks = [] + keyword = re.escape(keyword) for pkg in pkgs: if re.compile(keyword, re.IGNORECASE).search(pkg.name): searched_pgks.append(pkg) @@ -400,7 +584,6 @@ def showChangeSet(self, screen, changeset): gplv3_pkgs = [] report = Report(changeset) report.compute() - pkgs = report.installing.keys() for pkg in pkgs: for loader in pkg.loaders: @@ -418,3 +601,198 @@ def showChangeSet(self, screen, changeset): return "n" else: return "y" + + def installSrc(self, selected_pkgs , src_dir, output_dir): + + sys.stdout.write("Preparing...\n") + ctn=0 + ctnget=0 + numpkg=len(selected_pkgs) + + for pkg in selected_pkgs: + + ctn += 1 + + for loader in pkg.loaders: + + info = loader.getInfo(pkg) + src = info.getSource() + lic = info.getLicense() + srcdir = src_dir + + if srcdir: + + srcpath = srcdir + "/" + src + ".src.rpm" + srcdpath = output_dir + if not os.path.exists(srcdpath): + os.mkdir(srcdpath) + srcdpath = srcdpath + "/" + lic + if not os.path.exists(srcdpath): + os.mkdir(srcdpath) + + srcdpath = srcdpath + '/' + src + ".src.rpm" + if srcdir.startswith("/"): + + ctnget += 1 + str = "[" + ("%d" % int(ctn * 100 / numpkg)).rjust(3) + "%]" + ("%d" % ctnget).rjust(4) + \ + ": Getting: " + src + ".src.rpm" + if len(str) > 43: + str = str[:40] + "..." + + str=str.ljust(43) + + if os.path.exists(srcpath): + + if not os.path.exists(srcdpath): + shutil.copyfile(srcpath, srcdpath) + sys.stdout.write(str + " OK\n") + break + else: + sys.stdout.write(str + " Exists\n") + break + else: + sys.stdout.write(str + " No source rpm\n") + sys.stderr.write("Source rpm: " + srcpath + " does not exists....\n") + break + elif srcdir.startswith("http"): + if not os.path.exists(srcdpath): + command = "wget -q --no-proxy " + srcpath + rec = subprocess.call(command, shell=True) + if (rec != 0): + sys.stderr.write("Source rpm: " + src + ".src.rpm fetch failed!\n") + if (rec == 0): + src = src + ".src.rpm" + shutil.move(src, srcdpath) + elif srcdir.startswith("ftp"): + if not os.path.exists(srcdpath): + command = "wget -q --no-proxy " + srcpath + rec = subprocess.call(command, shell=True) + if (rec != 0): + sys.stderr.write("Source rpm: " + src + ".src.rpm fetch failed!\n") + if (rec == 0): + src = src + ".src.rpm" + shutil.move(src, srcdpath) + else: + sys.stderr.write("Src directory is invalid!\n") + else: + break + + #Append the local archive file into the Root File System + def appendLocalFile(self,src_dir): + print "In appendLocalFile function" + srcdir = src_dir + #get rootfs path + rootfs=sysconf.get("data-dir") + sys.stdout.write("rootfs path is "+rootfs[:-14]+"\n") + if len(srcdir) != 0: + sys.stdout.write("\nPreparing applying ChangeSet into rootfs...\n") + #output the changeset path + sys.stdout.write("ChangeSet path is"+srcdir+"\n") + #open the archive file and output into rootfs + tar = tarfile.open(srcdir) + names = tar.getnames() + for name in names: + tar.extract(name,rootfs[:-14] ) + tar.close() + #display finish info + sys.stdout.write("\nThe Local Files have been appended into the Root File System\n") + sys.stdout.write("\nThen Update the version info\n") + else: + sys.stdout.write("\nThere is no Change Set file, Only Updte the version info") + + #get version file path + rootpath = rootfs[:-14] + rootfs_version_path = rootpath+".rootfs_release" + sys.stdout.write("Version file path is "+rootfs_version_path+"\n") + #check version exists or not + #if not exists, create one + if os.path.isfile(rootfs_version_path) and os.access(rootfs_version_path, os.R_OK | os.W_OK): + print "File exists and is readable" + else: + print "file is not exists" + open(rootfs_version_path, 'a').close() + print ".rootfs_release file is create" + + #else if exists, update + #open version file and update date and number + rf = open(rootfs_version_path) + rline= rf.readline() + wf = open(rootfs_version_path,'w') + sys.stdout.write("The rootfs version file content is "+rline) + #prepare the version info + + version_date_str = datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y") + #sys.stdout.write(version_date_str+"\n") + if len(rline) == 0: + print "The rootfs version file is empty, add new version into it" + wf.write("PSC-RFS #1 "+version_date_str) + else: + print "The rootfs version file is not empty, update the version info" + version_num_str = "#"+str(int(rline.split()[1][1:])+1)+" " + print "The old rootfs version is "+ rline.split()[1] + print "The new rootfs version is "+ version_num_str+" "+ version_date_str + version_update_str = "PSC-RFS "+ version_num_str + version_date_str + wf.write(version_update_str) + #close version file + rf.close + wf.close + #display finish info + sys.stdout.write("Version file has been updated") + + + + def installSPDX(self, selected_pkgs, src_dir, output_dir): + + sys.stdout.write("Preparing...\n") + ctn = 0 + ctnget = 0 + numpkg = len(selected_pkgs) + + for pkg in selected_pkgs: + + ctn += 1 + + for loader in pkg.loaders: + + info = loader.getInfo(pkg) + src = info.getSource() + src = "-".join(src.split("-")[:-1]) + + lic = info.getLicense() + srcdir = src_dir + + if srcdir: + srcpath = srcdir + "/" + src + ".spdx" + srcdpath = output_dir + if not os.path.exists(srcdpath): + os.mkdir(srcdpath) + srcdpath = srcdpath + "/" + lic + if not os.path.exists(srcdpath): + os.mkdir(srcdpath) + + srcdpath = srcdpath + '/' + src + ".spdx" + + ctnget += 1 + str = "[" + ("%d" % int(ctn * 100 / numpkg)).rjust(3) + "%]" + ("%d" % ctnget).rjust(4) + \ + ": Getting: " + src + ".spdx" + if len(str) > 43: + str = str[:40] + "..." + + str=str.ljust(43) + + if srcdir.startswith("/"): + if os.path.exists(srcpath): + if not os.path.exists(srcdpath): + shutil.copyfile(srcpath, srcdpath) + sys.stdout.write(str + " OK\n") + break + else: + sys.stdout.write(str + " Exists\n") + break + else: + sys.stdout.write(str + " No SPDX file\n") + sys.stderr.write("Source SPDX file: " + srcpath + " does not exist....\n") + break + else: + break +