use common.regsub_file() instead of Popen(sed)

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.
This commit is contained in:
Hans-Christoph Steiner 2015-07-30 22:13:12 +02:00
parent 4a478528c2
commit 85febd40d1
2 changed files with 36 additions and 41 deletions

View file

@ -436,10 +436,9 @@ def adapt_gradle(build_dir):
if not os.path.isfile(path): if not os.path.isfile(path):
continue continue
logging.debug("Adapting %s at %s" % (filename, path)) logging.debug("Adapting %s at %s" % (filename, path))
common.regsub_file(r"""(\s*)buildToolsVersion[\s'"=]+.*""",
FDroidPopen(['sed', '-i', r"""\1buildToolsVersion '%s'""" % config['build_tools'],
r's@buildToolsVersion\([ =]\+\).*@buildToolsVersion\1"' path)
+ config['build_tools'] + '"@g', path])
def capitalize_intact(string): def capitalize_intact(string):
@ -631,17 +630,13 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
'package'] 'package']
if thisbuild['target']: if thisbuild['target']:
target = thisbuild["target"].split('-')[1] target = thisbuild["target"].split('-')[1]
FDroidPopen(['sed', '-i', common.regsub_file(r'<platform>[0-9]*</platform>',
's@<platform>[0-9]*</platform>@<platform>' r'<platform>%s</platform>' % target,
+ target + '</platform>@g', os.path.join(root_dir, 'pom.xml'))
'pom.xml'],
cwd=root_dir)
if '@' in thisbuild['maven']: if '@' in thisbuild['maven']:
FDroidPopen(['sed', '-i', common.regsub_file(r'<platform>[0-9]*</platform>',
's@<platform>[0-9]*</platform>@<platform>' r'<platform>%s</platform>' % target,
+ target + '</platform>@g', os.path.join(maven_dir, 'pom.xml'))
'pom.xml'],
cwd=maven_dir)
p = FDroidPopen(mvncmd, cwd=maven_dir) p = FDroidPopen(mvncmd, cwd=maven_dir)

View file

@ -121,6 +121,14 @@ def fill_config_defaults(thisconfig):
thisconfig[k][k2 + '_orig'] = v 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'): def read_config(opts, config_file='config.py'):
"""Read the repository config """Read the repository config
@ -965,10 +973,9 @@ def remove_debuggable_flags(root_dir):
logging.debug("Removing debuggable flags from %s" % root_dir) logging.debug("Removing debuggable flags from %s" % root_dir)
for root, dirs, files in os.walk(root_dir): for root, dirs, files in os.walk(root_dir):
if 'AndroidManifest.xml' in files: if 'AndroidManifest.xml' in files:
path = os.path.join(root, 'AndroidManifest.xml') regsub_file(r'android:debuggable="[^"]*"',
p = FDroidPopen(['sed', '-i', 's/android:debuggable="[^"]*"//g', path], output=False) '',
if p.returncode != 0: os.path.join(root, 'AndroidManifest.xml'))
raise BuildException("Failed to remove debuggable flags of %s" % path)
# Extract some information from the AndroidManifest.xml at the given path. # 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']: if build['target']:
n = build["target"].split('-')[1] n = build["target"].split('-')[1]
FDroidPopen(['sed', '-i', regsub_file(r'compileSdkVersion[ =]+[0-9]+',
's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g', r'compileSdkVersion %s' % n,
'build.gradle'], cwd=root_dir, output=False) os.path.join(root_dir, 'build.gradle'))
# Remove forced debuggable flags # Remove forced debuggable flags
remove_debuggable_flags(root_dir) 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): if not os.path.isfile(path):
continue continue
if has_extension(path, 'xml'): if has_extension(path, 'xml'):
p = FDroidPopen(['sed', '-i', regsub_file(r'android:versionName="[^"]*"',
's/android:versionName="[^"]*"/android:versionName="' + build['version'] + '"/g', r'android:versionName="%s"' % build['version'],
path], output=False) path)
if p.returncode != 0:
raise BuildException("Failed to amend manifest")
elif has_extension(path, 'gradle'): elif has_extension(path, 'gradle'):
p = FDroidPopen(['sed', '-i', regsub_file(r"""(\s*)versionName[\s'"=]+.*""",
's/versionName *=* *.*/versionName = "' + build['version'] + '"/g', r"""\1versionName '%s'""" % build['version'],
path], output=False) path)
if p.returncode != 0:
raise BuildException("Failed to amend build.gradle")
if build['forcevercode']: if build['forcevercode']:
logging.info("Changing the version code") logging.info("Changing the version code")
for path in manifest_paths(root_dir, flavours): for path in manifest_paths(root_dir, flavours):
if not os.path.isfile(path): if not os.path.isfile(path):
continue continue
if has_extension(path, 'xml'): if has_extension(path, 'xml'):
p = FDroidPopen(['sed', '-i', regsub_file(r'android:versionCode="[^"]*"',
's/android:versionCode="[^"]*"/android:versionCode="' + build['vercode'] + '"/g', r'android:versionCode="%s"' % build['vercode'],
path], output=False) path)
if p.returncode != 0:
raise BuildException("Failed to amend manifest")
elif has_extension(path, 'gradle'): elif has_extension(path, 'gradle'):
p = FDroidPopen(['sed', '-i', regsub_file(r'versionCode[ =]+[0-9]+',
's/versionCode *=* *[0-9]*/versionCode = ' + build['vercode'] + '/g', r'versionCode %s' % build['vercode'],
path], output=False) path)
if p.returncode != 0:
raise BuildException("Failed to amend build.gradle")
# Delete unwanted files # Delete unwanted files
if build['rm']: if build['rm']: