[Checkins] SVN: zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/ 1. Check for newlines at the end of file before inserting another one.

Satchidanand Haridas satchit at zope.com
Mon May 17 15:38:53 EDT 2010


Log message for revision 112427:
  1. Check for newlines at the end of file before inserting another one.
  
  2. Better cleanup on uninstall.
  
  3. Assume "text" mode.
  
  

Changed:
  U   zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/README.txt
  U   zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/__init__.py

-=-
Modified: zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/README.txt
===================================================================
--- zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/README.txt	2010-05-17 19:19:42 UTC (rev 112426)
+++ zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/README.txt	2010-05-17 19:38:52 UTC (rev 112427)
@@ -701,10 +701,10 @@
     Some
     existing
     configuration
-    <BLANKLINE>
     Some
     additional
     configuration
+    <BLANKLINE>
     #[foo_y.cfg DO NOT MODIFY LINES FROM HERE#
     111
     222
@@ -794,13 +794,127 @@
     Some
     existing
     configuration
-    <BLANKLINE>
     Some
     additional
     configuration
+    <BLANKLINE>
 
     >>> os.path.exists(join(sample_buildout, 'etc', 'z.cfg'))
     True
 
     >>> print open(join(sample_buildout, 'etc', 'z.cfg'), 'r').read()
     <BLANKLINE>
+
+
+Edgecases
+---------
+
+The SharedConfig recipe checks to see if the current data in the file ends with
+a new line. If it doesn't exist it adds one. This is in addition to the blank
+line the recipe adds before it adds the section to enhance readability.
+
+    >>> open('anotherconfig.cfg', 'w').write('one')
+    >>> write('buildout.cfg',
+    ... '''
+    ... [buildout]
+    ... parts = foo y.cfg
+    ...
+    ... [foo]
+    ... recipe = zc.recipe.deployment
+    ... prefix = %s
+    ... user = %s
+    ... etc-user = %s
+    ...
+    ... [y.cfg]
+    ... recipe = zc.recipe.deployment:sharedconfig
+    ... path = anotherconfig.cfg
+    ... deployment = foo
+    ... text = I predict that there will be a blank line above this.
+    ... ''' % (sample_buildout, user, user))
+    >>> print system(join('bin', 'buildout')), # doctest: +NORMALIZE_WHITESPACE
+    Installing foo.
+    zc.recipe.deployment:
+        Creating 'PREFIX/etc/foo',
+        mode 755, user 'USER', group 'GROUP'
+    zc.recipe.deployment:
+        Creating 'PREFIX/var/log/foo',
+        mode 755, user 'USER', group 'GROUP'
+    zc.recipe.deployment:
+        Creating 'PREFIX/var/run/foo',
+        mode 750, user 'USER', group 'GROUP'
+    zc.recipe.deployment:
+        Creating 'PREFIX/etc/cron.d',
+        mode 755, user 'USER', group 'GROUP'
+    zc.recipe.deployment:
+        Creating 'PREFIX/etc/init.d',
+        mode 755, user 'USER', group 'GROUP'
+    zc.recipe.deployment:
+        Creating 'PREFIX/etc/logrotate.d',
+        mode 755, user 'USER', group 'GROUP'
+    Installing y.cfg.
+
+    >>> print open('anotherconfig.cfg').read()
+    one
+    <BLANKLINE>
+    #[foo_y.cfg DO NOT MODIFY LINES FROM HERE#
+    I predict that there will be a blank line above this.
+    #TILL HERE foo_y.cfg]#
+    <BLANKLINE>
+
+But the recipe doesn't add a new line if there was one already at the end.
+
+    >>> open('anotherconfig.cfg', 'w').write('ends with a new line\n')
+    >>> print open('anotherconfig.cfg').read()
+    ends with a new line
+    <BLANKLINE>
+
+We modify the buildout configuration so that "install" is invoked again:
+
+    >>> write('buildout.cfg',
+    ... '''
+    ... [buildout]
+    ... parts = foo y.cfg
+    ...
+    ... [foo]
+    ... recipe = zc.recipe.deployment
+    ... prefix = %s
+    ... user = %s
+    ... etc-user = %s
+    ...
+    ... [y.cfg]
+    ... recipe = zc.recipe.deployment:sharedconfig
+    ... path = anotherconfig.cfg
+    ... deployment = foo
+    ... text = there will still be only a single blank line above.
+    ... ''' % (sample_buildout, user, user))
+    >>> print system(join('bin', 'buildout')), # doctest: +NORMALIZE_WHITESPACE
+    Uninstalling y.cfg.
+    Running uninstall recipe.
+    Updating foo.
+    Installing y.cfg.
+
+    >>> print open('anotherconfig.cfg').read()
+    ends with a new line
+    <BLANKLINE>
+    #[foo_y.cfg DO NOT MODIFY LINES FROM HERE#
+    there will still be only a single blank line above.
+    #TILL HERE foo_y.cfg]#
+    <BLANKLINE>
+
+If we uninstall the file, the data will be the same as "original_data":
+
+    >>> print system(join('bin', 'buildout')+' buildout:parts='),
+    Uninstalling y.cfg.
+    Running uninstall recipe.
+    Uninstalling foo.
+    Running uninstall recipe.
+    zc.recipe.deployment: Removing 'PREFIX/etc/foo'
+    zc.recipe.deployment: Removing 'PREFIX/etc/cron.d'.
+    zc.recipe.deployment: Removing 'PREFIX/etc/init.d'.
+    zc.recipe.deployment: Removing 'PREFIX/etc/logrotate.d'.
+    zc.recipe.deployment: Removing 'PREFIX/var/log/foo'.
+    zc.recipe.deployment: Removing 'PREFIX/var/run/foo'.
+
+    >>> print open('anotherconfig.cfg').read()
+    ends with a new line
+    <BLANKLINE>

Modified: zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/__init__.py
===================================================================
--- zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/__init__.py	2010-05-17 19:19:42 UTC (rev 112426)
+++ zc.recipe.deployment/branches/satchit-sharedconfig-recipe/src/zc/recipe/deployment/__init__.py	2010-05-17 19:38:52 UTC (rev 112427)
@@ -179,16 +179,21 @@
 
     def install(self):
         options = self.options
-        mode = options.get('mode', '')
         if 'file' in options:
             if 'text' in options:
                 raise zc.buildout.UserError(
                     "Cannot specify both file and text options")
-            text = open(options['file'], 'r'+mode).read()
+            text = open(options['file'], 'r').read()
         else:
             text = options['text']
-        open(options['location'], 'a'+mode).write(
-            self._wrap_with_comments(options['entry_name'], text))
+        config_file = open(options['location'], 'r+')
+        current_data = config_file.read()
+        new_data = ''
+        if current_data and current_data[-1] != '\n':
+            new_data += '\n'
+        new_data += self._wrap_with_comments(options['entry_name'], text)
+        config_file.write(new_data)
+        config_file.close()
         return ()
 
     def _wrap_with_comments(self, entry_name, text):
@@ -198,15 +203,19 @@
     def update(self):
         pass
 
+
 def uninstall_shared_config(name, options):
-    old_config = open(options['location'], 'r').read()
+    old_config = open(options['location'], 'r').readlines()
     new_config = []
     block_start = False
-    for line in old_config.splitlines():
+    for line in old_config:
         if line.startswith('#[%s' % options['entry_name']):
+            # remove the newline we have added
+            if new_config[-1] == '\n':
+                new_config = new_config[:-1]
             block_start = True
             continue
-        elif line.endswith('%s]#' % options['entry_name']):
+        elif line.strip().endswith('%s]#' % options['entry_name']):
             block_start = False
             continue
         else:
@@ -215,6 +224,5 @@
             else:
                 new_config.append(line)
 
-    mode = options.get('mode', '')
-    open(options['location'], 'w'+mode).write('\n'.join(new_config))
+    open(options['location'], 'w').write(''.join(new_config))
 



More information about the checkins mailing list