Commits

Michał Górny committed 0bf2363

Handle trailing comments in package.* properly.

Issue reported by yaccz.

Fixes: https://bitbucket.org/mgorny/flaggie/issue/3/flaggie-wont-add-use-flag-properly-if-the

  • Participants
  • Parent commits a0adfc4

Comments (0)

Files changed (1)

File lib/flaggie/packagefile.py

 # (C) 2010 Michał Górny <gentoo@mgorny.alt.pl>
 # Released under the terms of the 2-clause BSD license.
 
-import codecs, os, os.path, shutil, tempfile
+import codecs, os, os.path, re, shutil, tempfile
 
 from portage import VERSION as portage_ver
 from portage.versions import vercmp
 
+# comments start with '#' following whitespace
+comment_regexp = re.compile(r'\s#.*$')
+
 class PackageFileSet(object):
 	class PackageFile(list):
 		class PackageEntry(object):
 				self.as_str = l
 				self.modified = False
 				self.package = sl.pop(0)
-				self.flags = [self.PackageFlag(x) for x in sl]
+				self.flags = []
+
+				for x in sl:
+					if x.startswith('#'):
+						break
+					self.flags.append(self.PackageFlag(x))
+
+				m = comment_regexp.search(l)
+				if m:
+					self.trailing_whitespace = m.group(0) + '\n'
+				else:
+					self.trailing_whitespace = '\n'
 
 			def toString(self):
 				ret = ''.join(self.whitespace)
 				if not self.modified:
 					ret += self.as_str
 				else:
-					ret += ' '.join([self.package] + \
-							[x.toString() for x in self.flags]) + '\n'
+					ret += ' '.join([self.package] \
+							+ [x.toString() for x in self.flags]) \
+							+ self.trailing_whitespace
 				return ret
 
 			def append(self, flag):