From f504c07bdb9ad2cc88586b4ea7833359b2badf34 Mon Sep 17 00:00:00 2001 From: Gene Reese Date: Mon, 19 Mar 2018 02:29:46 -0500 Subject: [PATCH] Added support for %config(noreplace) file flag --- root/usr/bin/togo | 136 +++++++++++++++++++++++++++++++++------------- spec/changelog | 3 + spec/header | 2 +- 3 files changed, 103 insertions(+), 38 deletions(-) diff --git a/root/usr/bin/togo b/root/usr/bin/togo index 28a7b48..2d3cdad 100755 --- a/root/usr/bin/togo +++ b/root/usr/bin/togo @@ -46,7 +46,7 @@ class Application: # Set up some class-wide defaults path_root = os.path.abspath(os.path.curdir) path_database = os.path.join(path_root, APPLICATION_DATABASE_NAME) - preference_path_database = os.path.join(os.path.expanduser('~/.togo'), 'preferences.db') + path_preference_database = os.path.join(os.path.expanduser('~/.togo'), 'preferences.db') def __init__(self): """ Application initialization """ @@ -148,24 +148,34 @@ class Application: help="Specify directories to exclude") # Create the file flag parser and subparser - p_file_flag = sp_file.add_parser('flag', help="Flag commands") + p_file_flag = sp_file.add_parser('flag', help="Flag files") sp_file_flag = p_file_flag.add_subparsers(dest="sp_file_flag") # Create the file flag normal parser and subparser - p_file_flag_normal = sp_file_flag.add_parser('normal', help="Flag file(s) as normal") - p_file_flag_normal.add_argument('file_flag_normal_files', metavar="files", + p_file_flag_normal = sp_file_flag.add_parser('normal', help="DEPRECATED: Use 'togo file unflag '") + p_file_flag_normal.add_argument('file_flag_files', metavar="files", nargs='*', help="Specify files to flag") # Create the file flag config parser and subparser p_file_flag_config = sp_file_flag.add_parser('config', help="Flag file(s) as %%config") - p_file_flag_config.add_argument('file_flag_config_files', metavar="files", + p_file_flag_config.add_argument('file_flag_files', metavar="files", nargs='*', help="Specify files to flag") + # Create the file flag config(noreplace) parser and subparser + p_file_flag_config = sp_file_flag.add_parser('config-nr', help="Flag file(s) as %%config(noreplace)") + p_file_flag_config.add_argument('file_flag_files', metavar="files", + nargs='*', help="Specify files to flag") + # Create the file flag doc parser and subparser p_file_flag_doc = sp_file_flag.add_parser('doc', help="Flag file(s) as %%doc") - p_file_flag_doc.add_argument('file_flag_doc_files', metavar="files", + p_file_flag_doc.add_argument('file_flag_files', metavar="files", nargs='*', help="Specify files to flag") + # Create the file unflag parser + p_file_unflag = sp_file.add_parser('unflag', help="Unflag files") + p_file_unflag.add_argument('file_unflag_files', metavar="files", + nargs='*', help="Specify files to unflag") + # Create the file database parser and subparser p_file_database = sp_file.add_parser('database', help="File database commands") @@ -272,8 +282,11 @@ class Application: # If we're adding files elif (args.sp_file == 'include'): self.updateFileDatabase() - for f in args.file_include_files: - self.includeItem(f) + if (args.file_include_files): + for f in args.file_include_files: + self.includeItem(f) + else: + p_file_include.print_help() # If we're removing files elif (args.sp_file == 'exclude'): @@ -286,16 +299,31 @@ class Application: # If we're flagging files elif (args.sp_file == 'flag'): + self.verifyDirectiveData() self.updateFileDatabase() - if (args.sp_file_flag == 'normal'): - for f in args.file_flag_normal_files: - self.flagItemNormal(f) - elif (args.sp_file_flag == 'config'): - for f in args.file_flag_config_files: - self.flagItemConfig(f) - elif (args.sp_file_flag == 'doc'): - for f in args.file_flag_doc_files: - self.flagItemDoc(f) + if (args.file_flag_files): + if (args.sp_file_flag == 'normal'): + for f in args.file_flag_files: + self.flagItemNormal(f) + elif (args.sp_file_flag == 'config'): + for f in args.file_flag_files: + self.flagItemConfig(f) + elif (args.sp_file_flag == 'config-nr'): + for f in args.file_flag_files: + self.flagItemConfigNoReplace(f) + elif (args.sp_file_flag == 'doc'): + for f in args.file_flag_files: + self.flagItemDoc(f) + else: + p_file_flag.print_help() + + # If we're unflagging files + elif (args.sp_file == 'unflag'): + if (args.file_unflag_files): + for f in args.file_unflag_files: + self.unflagItem(f) + else: + p_file_unflag.print_help() # If we're messing with the file database elif (args.sp_file == 'database'): @@ -340,12 +368,12 @@ class Application: # Check to see if the preference database exists # If it doesn't, set it to be created - Dir(os.path.split(self.preference_path_database)[0]) - if not (os.path.exists(self.preference_path_database)): + Dir(os.path.split(self.path_preference_database)[0]) + if not (os.path.exists(self.path_preference_database)): initialize = True # Set up the connection information for the sql object - connection_string = 'sqlite://%s' % (self.preference_path_database) + connection_string = 'sqlite://%s' % (self.path_preference_database) self.connection = connectionForURI(connection_string) sqlhub.processConnection = self.connection @@ -386,7 +414,7 @@ class Application: self.connection = connectionForURI(connection_string) sqlhub.processConnection = self.connection self.transaction = self.connection.transaction() - + def initializeDatabase(self): """ Initializes the package database """ self.connectToDatabase() @@ -416,16 +444,33 @@ class Application: package_path_spec=p.package_path_spec, package_path_meta=p.package_path_meta, package_path_rpms=p.package_path_rpms) + + # Verify/setup initial data values + self.verifyDirectiveData() - # Initialize the directive types - Directive(name="%config", - description="Configuration files.") - Directive(name="%dir", - description="Directories.") - Directive(name="%doc", - description="Documentation.") - Directive(name="%docdir", - description="Documentation directories.") + def verifyDirectiveData(self): + """ Check the database for inconsistencies and fix any found issues """ + self.connectToDatabase() + + ## Verify the presence of the directive types + # Set up the desired directives + directives = [{'name': '%config', + 'description': "Configuration files."}, + {'name': '%config(noreplace)', + 'description': "Configuration files w/ noreplace modifier."}, + {'name': '%dir', + 'description': 'Directories.'}, + {'name': '%doc', + 'description': 'Documentation'}, + {'name': '%docdir', + 'description': 'Documentation directories.'}] + + # Iterate through the directives and add the missing ones + for directive in directives: + log.debug("Searching for: %s" % directive) + if not (list(Directive.selectBy(name=directive['name']))): + log.debug(" Adding directive to database: %s" % (directive['name'])) + Directive(name=directive['name'], description=directive['description']) def createProject(self, name): """ Creates a new package and package database """ @@ -553,6 +598,10 @@ class Application: sys.exit(2) else: self.transaction.commit() + + def listFiles(self): + p = Package.get(1) + p.listFiles() def includeItem(self, item): p = Package.get(1) @@ -563,6 +612,7 @@ class Application: p.excludeItem(item) def flagItemNormal(self, item): + log.info("DEPRECATED: Please use 'togo file unflag ") p = Package.get(1) p.unflagItem(item) @@ -570,18 +620,25 @@ class Application: p = Package.get(1) p.flagItem(item, "%config") + def flagItemConfigNoReplace(self, item): + p = Package.get(1) + p.flagItem(item, "%config(noreplace)") + def flagItemDoc(self, item): p = Package.get(1) p.flagItem(item, "%doc") - - def listFiles(self): + + def unflagItem(self, item): p = Package.get(1) - p.listFiles() + p.unflagItem(item) def clearAllFlags(self): p = Package.get(1) p.clearAllFlags() - + + def fileChown(self, user_group, item): + p = Package.get(1) + p.fileChown(user_group, item) # Define the Preference object class Preference(SQLObject): @@ -603,6 +660,7 @@ class Package(SQLObject): spec_section_extension = '' def _init(self, *args, **keys): + SQLObject._init(self, *args, **keys) self.spec_var_name = self.package_name @@ -977,7 +1035,7 @@ class Package(SQLObject): break def unflagItem(self, item): item = self.getPackageFile(item) - log.debug("Unflagging '%s'..." % (item.path)) + log.info(" Unflagging '%s'" % (item.path)) relative_path = "%s%s" % (self.package_path_root, item.path) for d in item.directives: item.removeDirective(d) @@ -989,13 +1047,17 @@ class Package(SQLObject): def flagItem(self, item, directive): item = self.getPackageFile(item) - log.debug("Flagging '%s' as '%s'..." % (item.path, directive)) + log.info(" Flagging '%s' as '%s'" % (item.path, directive)) relative_path = "%s%s" % (self.package_path_root, item.path) if (directive == "%doc"): if (os.path.isdir(relative_path)): directive = "%docdir" - directive = Directive.select(Directive.q.name==directive)[0] + + try: + directive = Directive.select(Directive.q.name==directive)[0] + except: + raise Exception("Does not exist.") # Clear the existing flag for d in item.directives: diff --git a/spec/changelog b/spec/changelog index 1e73dc8..ff58fa5 100644 --- a/spec/changelog +++ b/spec/changelog @@ -1,4 +1,7 @@ %changelog +* Sun Mar 18 2018 Gene Reese +- Added support for %config(noreplace) file flag + * Tue Jul 25 2017 Gene Reese - Build package option will now also build the associated source RPM diff --git a/spec/header b/spec/header index 42d158f..95b0d00 100644 --- a/spec/header +++ b/spec/header @@ -5,7 +5,7 @@ Name: togo Summary: A quick, easy, and powerful RPM authoring tool # The version of your package -Version: 2.4 +Version: 2.5 # The release number of your package Release: 1