chiark / gitweb /
use common.regsub_file() instead of Popen(sed)
authorHans-Christoph Steiner <hans@eds.org>
Thu, 30 Jul 2015 20:13:12 +0000 (22:13 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Fri, 31 Jul 2015 22:38:41 +0000 (00:38 +0200)
Python libraries work better in Python than running external commands, and
it also makes the code much more portable.  For example, the GNU and BSD
sed commands have different, and sometimes conflicting, flags.

This also reworks the regexp patterns to be more tightly focused, and not
change the same variable name in comments or elsewhere.

fdroidserver/build.py
fdroidserver/common.py

index 84af7f41c5b0497e422a36bf6aa7833771a59227..74004d29b07c2a8b944f301d7630847eb5a1025d 100644 (file)
@@ -436,10 +436,9 @@ def adapt_gradle(build_dir):
             if not os.path.isfile(path):
                 continue
             logging.debug("Adapting %s at %s" % (filename, path))
-
-            FDroidPopen(['sed', '-i',
-                         r's@buildToolsVersion\([ =]\+\).*@buildToolsVersion\1"'
-                         + config['build_tools'] + '"@g', path])
+            common.regsub_file(r"""(\s*)buildToolsVersion[\s'"=]+.*""",
+                               r"""\1buildToolsVersion '%s'""" % config['build_tools'],
+                               path)
 
 
 def capitalize_intact(string):
@@ -631,17 +630,13 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
                   'package']
         if thisbuild['target']:
             target = thisbuild["target"].split('-')[1]
-            FDroidPopen(['sed', '-i',
-                         's@<platform>[0-9]*</platform>@<platform>'
-                         + target + '</platform>@g',
-                         'pom.xml'],
-                        cwd=root_dir)
+            common.regsub_file(r'<platform>[0-9]*</platform>',
+                               r'<platform>%s</platform>' % target,
+                               os.path.join(root_dir, 'pom.xml'))
             if '@' in thisbuild['maven']:
-                FDroidPopen(['sed', '-i',
-                             's@<platform>[0-9]*</platform>@<platform>'
-                             + target + '</platform>@g',
-                             'pom.xml'],
-                            cwd=maven_dir)
+                common.regsub_file(r'<platform>[0-9]*</platform>',
+                                   r'<platform>%s</platform>' % target,
+                                   os.path.join(maven_dir, 'pom.xml'))
 
         p = FDroidPopen(mvncmd, cwd=maven_dir)
 
index 3c672728a860544a7d74a72783503927e93b8cc9..e630602066ca947601aa84e6281f2d54df5e76b5 100644 (file)
@@ -121,6 +121,14 @@ def fill_config_defaults(thisconfig):
                 thisconfig[k][k2 + '_orig'] = v
 
 
+def regsub_file(pattern, repl, path):
+    with open(path, 'r') as f:
+        text = f.read()
+    text = re.sub(pattern, repl, text)
+    with open(path, 'w') as f:
+        f.write(text)
+
+
 def read_config(opts, config_file='config.py'):
     """Read the repository config
 
@@ -965,10 +973,9 @@ def remove_debuggable_flags(root_dir):
     logging.debug("Removing debuggable flags from %s" % root_dir)
     for root, dirs, files in os.walk(root_dir):
         if 'AndroidManifest.xml' in files:
-            path = os.path.join(root, 'AndroidManifest.xml')
-            p = FDroidPopen(['sed', '-i', 's/android:debuggable="[^"]*"//g', path], output=False)
-            if p.returncode != 0:
-                raise BuildException("Failed to remove debuggable flags of %s" % path)
+            regsub_file(r'android:debuggable="[^"]*"',
+                        '',
+                        os.path.join(root, 'AndroidManifest.xml'))
 
 
 # Extract some information from the AndroidManifest.xml at the given path.
@@ -1305,9 +1312,9 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
 
         if build['target']:
             n = build["target"].split('-')[1]
-            FDroidPopen(['sed', '-i',
-                         's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g',
-                         'build.gradle'], cwd=root_dir, output=False)
+            regsub_file(r'compileSdkVersion[ =]+[0-9]+',
+                        r'compileSdkVersion %s' % n,
+                        os.path.join(root_dir, 'build.gradle'))
 
     # Remove forced debuggable flags
     remove_debuggable_flags(root_dir)
@@ -1319,34 +1326,27 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
             if not os.path.isfile(path):
                 continue
             if has_extension(path, 'xml'):
-                p = FDroidPopen(['sed', '-i',
-                                 's/android:versionName="[^"]*"/android:versionName="' + build['version'] + '"/g',
-                                 path], output=False)
-                if p.returncode != 0:
-                    raise BuildException("Failed to amend manifest")
+                regsub_file(r'android:versionName="[^"]*"',
+                            r'android:versionName="%s"' % build['version'],
+                            path)
             elif has_extension(path, 'gradle'):
-                p = FDroidPopen(['sed', '-i',
-                                 's/versionName *=* *.*/versionName = "' + build['version'] + '"/g',
-                                 path], output=False)
-                if p.returncode != 0:
-                    raise BuildException("Failed to amend build.gradle")
+                regsub_file(r"""(\s*)versionName[\s'"=]+.*""",
+                            r"""\1versionName '%s'""" % build['version'],
+                            path)
+
     if build['forcevercode']:
         logging.info("Changing the version code")
         for path in manifest_paths(root_dir, flavours):
             if not os.path.isfile(path):
                 continue
             if has_extension(path, 'xml'):
-                p = FDroidPopen(['sed', '-i',
-                                 's/android:versionCode="[^"]*"/android:versionCode="' + build['vercode'] + '"/g',
-                                 path], output=False)
-                if p.returncode != 0:
-                    raise BuildException("Failed to amend manifest")
+                regsub_file(r'android:versionCode="[^"]*"',
+                            r'android:versionCode="%s"' % build['vercode'],
+                            path)
             elif has_extension(path, 'gradle'):
-                p = FDroidPopen(['sed', '-i',
-                                 's/versionCode *=* *[0-9]*/versionCode = ' + build['vercode'] + '/g',
-                                 path], output=False)
-                if p.returncode != 0:
-                    raise BuildException("Failed to amend build.gradle")
+                regsub_file(r'versionCode[ =]+[0-9]+',
+                            r'versionCode %s' % build['vercode'],
+                            path)
 
     # Delete unwanted files
     if build['rm']: