chiark / gitweb /
Merge branch 'newcomers' into 'master'
[fdroidserver.git] / fdroidserver / common.py
index c8fdaad529de3f49e1d9bb12b00d649a2da40583..a39b671a7560685e5c091338f4f288cbe41b0d13 100644 (file)
@@ -49,6 +49,7 @@ from pyasn1.error import PyAsn1Error
 from distutils.util import strtobool
 
 import fdroidserver.metadata
+from fdroidserver import _
 from fdroidserver.exception import FDroidException, VCSException, BuildException
 from .asynchronousfilereader import AsynchronousFileReader
 
@@ -75,7 +76,7 @@ default_config = {
         'r12b': "$ANDROID_NDK",
         'r13b': None,
         'r14b': None,
-        'r15b': None,
+        'r15c': None,
     },
     'qt_sdk_path': None,
     'build_tools': "25.0.2",
@@ -123,9 +124,9 @@ default_config = {
 
 def setup_global_opts(parser):
     parser.add_argument("-v", "--verbose", action="store_true", default=False,
-                        help="Spew out even more information than normal")
+                        help=_("Spew out even more information than normal"))
     parser.add_argument("-q", "--quiet", action="store_true", default=False,
-                        help="Restrict output to warnings and errors")
+                        help=_("Restrict output to warnings and errors"))
 
 
 def fill_config_defaults(thisconfig):
@@ -238,8 +239,8 @@ def read_config(opts, config_file='config.py'):
         with io.open(config_file, "rb") as f:
             code = compile(f.read(), config_file, 'exec')
             exec(code, None, config)
-    elif len(get_local_metadata_files()) == 0:
-        raise FDroidException("Missing config file - is this a repo directory?")
+    else:
+        logging.debug("No config.py found - using defaults.")
 
     for k in ('mirrors', 'install_list', 'uninstall_list', 'serverwebroot', 'servergitroot'):
         if k in config:
@@ -260,7 +261,7 @@ def read_config(opts, config_file='config.py'):
     if any(k in config for k in ["keystore", "keystorepass", "keypass"]):
         st = os.stat(config_file)
         if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
-            logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
+            logging.warning("unsafe permissions on {0} (should be 0600)!".format(config_file))
 
     fill_config_defaults(config)
 
@@ -1022,9 +1023,9 @@ def retrieve_string(app_dir, string, xmlfiles=None):
             os.path.join(app_dir, 'res'),
             os.path.join(app_dir, 'src', 'main', 'res'),
         ]:
-            for r, d, f in os.walk(res_dir):
-                if os.path.basename(r) == 'values':
-                    xmlfiles += [os.path.join(r, x) for x in f if x.endswith('.xml')]
+            for root, dirs, files in os.walk(res_dir):
+                if os.path.basename(root) == 'values':
+                    xmlfiles += [os.path.join(root, x) for x in files if x.endswith('.xml')]
 
     name = string[len('@string/'):]
 
@@ -1317,21 +1318,21 @@ def getsrclib(spec, srclib_dir, subdir=None, basepath=False,
 gradle_version_regex = re.compile(r"[^/]*'com\.android\.tools\.build:gradle:([^\.]+\.[^\.]+).*'.*")
 
 
-# Prepare the source code for a particular build
-#  'vcs'         - the appropriate vcs object for the application
-#  'app'         - the application details from the metadata
-#  'build'       - the build details from the metadata
-#  'build_dir'   - the path to the build directory, usually
-#                   'build/app.id'
-#  'srclib_dir'  - the path to the source libraries directory, usually
-#                   'build/srclib'
-#  'extlib_dir'  - the path to the external libraries directory, usually
-#                   'build/extlib'
-# Returns the (root, srclibpaths) where:
-#   'root' is the root directory, which may be the same as 'build_dir' or may
-#          be a subdirectory of it.
-#   'srclibpaths' is information on the srclibs being used
 def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=False, refresh=True):
+    """ Prepare the source code for a particular build
+
+    :param vcs: the appropriate vcs object for the application
+    :param app: the application details from the metadata
+    :param build: the build details from the metadata
+    :param build_dir: the path to the build directory, usually 'build/app.id'
+    :param srclib_dir: the path to the source libraries directory, usually 'build/srclib'
+    :param extlib_dir: the path to the external libraries directory, usually 'build/extlib'
+
+    Returns the (root, srclibpaths) where:
+    :param root: is the root directory, which may be the same as 'build_dir' or may
+                 be a subdirectory of it.
+    :param srclibpaths: is information on the srclibs being used
+    """
 
     # Optionally, the actual app source can be in a subdirectory
     if build.subdir:
@@ -1706,6 +1707,21 @@ def isApkAndDebuggable(apkfile):
         return get_apk_debuggable_androguard(apkfile)
 
 
+def get_apk_id_aapt(apkfile):
+    """Extrat identification information from APK using aapt.
+
+    :param apkfile: path to an APK file.
+    :returns: triplet (appid, version code, version name)
+    """
+    r = re.compile("package: name='(?P<appid>.*)' versionCode='(?P<vercode>.*)' versionName='(?P<vername>.*)' platformBuildVersionName='.*'")
+    p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False)
+    for line in p.output.splitlines():
+        m = r.match(line)
+        if m:
+            return m.group('appid'), m.group('vercode'), m.group('vername')
+    raise FDroidException("reading identification failed, APK invalid: '{}'".format(apkfile))
+
+
 class PopenResult:
     def __init__(self):
         self.returncode = None
@@ -1789,6 +1805,12 @@ def FDroidPopenBytes(commands, cwd=None, envs=None, output=True, stderr_to_stdou
     result.returncode = p.wait()
     result.output = buf.getvalue()
     buf.close()
+    # make sure all filestreams of the subprocess are closed
+    for streamvar in ['stdin', 'stdout', 'stderr']:
+        if hasattr(p, streamvar):
+            stream = getattr(p, streamvar)
+            if stream:
+                stream.close()
     return result
 
 
@@ -1964,6 +1986,30 @@ def place_srclib(root_dir, number, libpath):
 apk_sigfile = re.compile(r'META-INF/[0-9A-Za-z]+\.(SF|RSA|DSA|EC)')
 
 
+def metadata_get_sigdir(appid, vercode=None):
+    """Get signature directory for app"""
+    if vercode:
+        return os.path.join('metadata', appid, 'signatures', vercode)
+    else:
+        return os.path.join('metadata', appid, 'signatures')
+
+
+def apk_extract_signatures(apkpath, outdir, manifest=True):
+    """Extracts a signature files from APK and puts them into target directory.
+
+    :param apkpath: location of the apk
+    :param outdir: folder where the extracted signature files will be stored
+    :param manifest: (optionally) disable extracting manifest file
+    """
+    with ZipFile(apkpath, 'r') as in_apk:
+        for f in in_apk.infolist():
+            if apk_sigfile.match(f.filename) or \
+                    (manifest and f.filename == 'META-INF/MANIFEST.MF'):
+                newpath = os.path.join(outdir, os.path.basename(f.filename))
+                with open(newpath, 'wb') as out_file:
+                    out_file.write(in_apk.read(f.filename))
+
+
 def verify_apks(signed_apk, unsigned_apk, tmp_dir):
     """Verify that two apks are the same
 
@@ -2269,7 +2315,10 @@ def write_to_config(thisconfig, key, value=None, config_file=None):
         value = thisconfig[origkey] if origkey in thisconfig else thisconfig[key]
     cfg = config_file if config_file else 'config.py'
 
-    # load config file
+    # load config file, create one if it doesn't exist
+    if not os.path.exists(cfg):
+        os.mknod(cfg)
+        logging.info("Creating empty " + cfg)
     with open(cfg, 'r', encoding="utf-8") as f:
         lines = f.readlines()