chiark / gitweb /
Use origin/HEAD as the default checkout for git
[fdroidserver.git] / fdroidserver / common.py
index 92e559e1f778adc567aba33e3ce0f4e41702611f..48ee6dfe392ecfd3c74c956b005ffb4503a2dd6f 100644 (file)
@@ -92,6 +92,11 @@ def read_config(opts, config_file='config.py'):
                                       'sun.security.pkcs11.SunPKCS11',
                                       '-providerArg', 'opensc-fdroid.cfg']
 
+    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))
+
     defconfig = get_default_config()
     for k, v in defconfig.items():
         if k not in config:
@@ -107,11 +112,6 @@ def read_config(opts, config_file='config.py'):
     if not test_sdk_exists(config):
         sys.exit(3)
 
-    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))
-
     for k in ["keystorepass", "keypass"]:
         if k in config:
             write_password_file(k)
@@ -351,7 +351,9 @@ class vcs:
                     writeback = False
                 else:
                     deleterepo = True
-                    logging.info("Repository details changed - deleting")
+                    logging.info(
+                            "Repository details for {0} changed - deleting"
+                            .format(self.local))
             else:
                 deleterepo = True
                 logging.info("Repository details missing - deleting")
@@ -434,8 +436,9 @@ class vcs_git(vcs):
                 if p.returncode != 0:
                     raise VCSException("Git fetch failed")
                 self.refreshed = True
-        # Check out the appropriate revision
-        rev = str(rev if rev else 'origin/master')
+        # origin/HEAD is the HEAD of the remote, e.g. the "default branch" on
+        # a github repo. Most of the time this is the same as origin/master.
+        rev = str(rev if rev else 'origin/HEAD')
         p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local)
         if p.returncode != 0:
             raise VCSException("Git checkout failed")
@@ -861,12 +864,12 @@ def parse_androidmanifests(paths, ignoreversions=None):
     max_version = None
     max_vercode = None
     max_package = None
-    version = None
-    vercode = None
 
     for path in paths:
 
         gradle = has_extension(path, 'gradle')
+        version = None
+        vercode = None
         # Remember package name, may be defined separately from version+vercode
         package = max_package
 
@@ -893,20 +896,26 @@ def parse_androidmanifests(paths, ignoreversions=None):
                 if matches:
                     vercode = matches.group(1)
 
-        # Better some package name than nothing
-        if max_package is None:
+        # Always grab the package name and version name in case they are not
+        # together with the highest version code
+        if max_package is None and package is not None:
             max_package = package
+        if max_version is None and version is not None:
+            max_version = version
 
         if max_vercode is None or (vercode is not None and vercode > max_vercode):
             if not ignoresearch or not ignoresearch(version):
-                max_version = version
-                max_vercode = vercode
-                max_package = package
+                if version is not None:
+                    max_version = version
+                if vercode is not None:
+                    max_vercode = vercode
+                if package is not None:
+                    max_package = package
             else:
                 max_version = "Ignore"
 
     if max_version is None:
-        max_version = version if version else "Unknown"
+        max_version = "Unknown"
 
     return (max_version, max_vercode, max_package)
 
@@ -1136,23 +1145,37 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
         version_regex = re.compile(r".*'com\.android\.tools\.build:gradle:([^\.]+\.[^\.]+).*'.*")
         gradlepluginver = None
 
-        with open(os.path.join(root_dir, 'build.gradle')) as f:
-            for line in f:
-                match = version_regex.match(line)
-                if match:
-                    gradlepluginver = match.group(1)
-                    break
-        if '@' in build['gradle'] and not gradlepluginver:
-            gradle_dir = os.path.join(root_dir, build['gradle'].split('@', 1)[1])
-            gradle_dir = os.path.normpath(gradle_dir)
-            with open(os.path.join(root_dir, 'build.gradle')) as f:
+        gradle_files = [os.path.join(root_dir, 'build.gradle')]
+
+        # Parent dir build.gradle
+        parent_dir = os.path.normpath(os.path.join(root_dir, '..'))
+        if parent_dir.startswith(build_dir):
+            gradle_files.append(os.path.join(parent_dir, 'build.gradle'))
+
+        # Gradle execution dir build.gradle
+        if '@' in build['gradle']:
+            gradle_file = os.path.join(root_dir, build['gradle'].split('@', 1)[1], 'build.gradle')
+            gradle_file = os.path.normpath(gradle_file)
+            if gradle_file not in gradle_files:
+                gradle_files.append(gradle_file)
+
+        for path in gradle_files:
+            if gradlepluginver:
+                break
+            if not os.path.isfile(path):
+                continue
+            with open(path) as f:
                 for line in f:
                     match = version_regex.match(line)
                     if match:
                         gradlepluginver = match.group(1)
                         break
 
-        build['gradlepluginver'] = LooseVersion(gradlepluginver)
+        if gradlepluginver:
+            build['gradlepluginver'] = LooseVersion(gradlepluginver)
+        else:
+            logging.warn("Could not fetch the gradle plugin version, defaulting to 0.11")
+            build['gradlepluginver'] = LooseVersion('0.11')
 
         if build['target']:
             n = build["target"].split('-')[1]
@@ -1436,7 +1459,7 @@ def scan_source(build_dir, root_dir, thisbuild):
     # buildjni=no to bypass this check)
     if (os.path.exists(os.path.join(root_dir, 'jni')) and
             not thisbuild['buildjni']):
-        logging.error('Found jni directory, but buildjni is not enabled')
+        logging.error('Found jni directory, but buildjni is not enabled. Set it to \'no\' to ignore.')
         count += 1
 
     return count
@@ -1556,7 +1579,7 @@ def SilentPopen(commands, cwd=None, shell=False):
     return FDroidPopen(commands, cwd=cwd, shell=shell, output=False)
 
 
-def FDroidPopen(commands, cwd=None, shell=False, output=False):
+def FDroidPopen(commands, cwd=None, shell=False, output=True):
     """
     Run a command and capture the possibly huge output.
 
@@ -1565,11 +1588,10 @@ def FDroidPopen(commands, cwd=None, shell=False, output=False):
     :returns: A PopenResult.
     """
 
-    if output:
-        if cwd:
-            cwd = os.path.normpath(cwd)
-            logging.info("Directory: %s" % cwd)
-        logging.info("> %s" % ' '.join(commands))
+    if cwd:
+        cwd = os.path.normpath(cwd)
+        logging.debug("Directory: %s" % cwd)
+    logging.debug("> %s" % ' '.join(commands))
 
     result = PopenResult()
     p = subprocess.Popen(commands, cwd=cwd, shell=shell,
@@ -1583,7 +1605,7 @@ def FDroidPopen(commands, cwd=None, shell=False, output=False):
     while not stdout_reader.eof():
         while not stdout_queue.empty():
             line = stdout_queue.get()
-            if output or options.verbose:
+            if output:
                 # Output directly to console
                 sys.stdout.write(line)
                 sys.stdout.flush()
@@ -1612,6 +1634,8 @@ def remove_signing_keys(build_dir):
             with open(path, "r") as o:
                 lines = o.readlines()
 
+            changed = False
+
             opened = 0
             with open(path, "w") as o:
                 for line in lines:
@@ -1624,16 +1648,19 @@ def remove_signing_keys(build_dir):
                         continue
 
                     if signing_configs.match(line):
+                        changed = True
                         opened += 1
                         continue
 
                     if any(s.match(line) for s in line_matches):
+                        changed = True
                         continue
 
                     if opened == 0:
                         o.write(line)
 
-            logging.info("Cleaned build.gradle of keysigning configs at %s" % path)
+            if changed:
+                logging.info("Cleaned build.gradle of keysigning configs at %s" % path)
 
         for propfile in [
                 'project.properties',
@@ -1647,15 +1674,18 @@ def remove_signing_keys(build_dir):
                 with open(path, "r") as o:
                     lines = o.readlines()
 
+                changed = False
+
                 with open(path, "w") as o:
                     for line in lines:
-                        if line.startswith('key.store'):
-                            continue
-                        if line.startswith('key.alias'):
+                        if any(line.startswith(s) for s in ('key.store', 'key.alias')):
+                            changed = True
                             continue
+
                         o.write(line)
 
-                logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
+                if changed:
+                    logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
 
 
 def replace_config_vars(cmd):