1. Henrik Tunedal
  2. fdroidserver

Commits

Henrik Tunedal  committed e1e23f1

Added support for rewriting license metadata.

  • Participants
  • Parent commits b6ee195
  • Branches default

Comments (0)

Files changed (1)

File check_license.py

View file
 def parse_package_metadata(filename):
     f = open(filename, "r")
     multiline = None
+    current_key = None
     data = {}
+    original_data = []
     for rad in f:
+        original_line = rad
         rad = rad.rstrip()
-        if rad.startswith("#"):
+        if rad.startswith("#") or not rad:
+            original_data.append((None, original_line))
             continue
         elif multiline:
+            original_data.append((current_key, original_line))
             if rad == ".":
                 data[multiline] = "\n".join(data[multiline])
                 multiline = None
+                current_key = None
             else:
                 data[multiline].append(rad)
         elif rad:
             k, v = rad.split(":", 1)
             k = k.lower()
+            current_key = k
+            original_data.append((current_key, original_line))
             if k == "description":
                 multiline = k
                 data[k] = []
     f.close()
     id = os.path.splitext(os.path.basename(filename).replace("/", "_"))[0]
     data["package id"] = id
+    data["original metadata"] = original_data
     return data
 
 def metadata_for_id(package_id):
             pass
     raise ValueError, "invalid package name: '%s'" % name
 
+def rewrite_license(metadata, new_license):
+    for i, rad in enumerate(metadata["original metadata"]):
+        if rad[0] == "license":
+            license_index = i
+            break
+    else:
+        license_index = None
+    new_meta = ("license", "License:%s\n" % new_license)
+    if license_index is None:
+        metadata["original metadata"].append(new_meta)
+    else:
+        metadata["original metadata"][license_index] = new_meta
+    metadata["license"] = new_license
+    filename = os.path.join(metadata_root, metadata["package id"]) + ".txt"
+    temp_filename = filename + ".temp"
+    try:
+        f = open(temp_filename, "w")
+        f.writelines(v for k, v in metadata["original metadata"])
+        f.flush()
+        os.fsync(f.fileno())
+        f.close()
+        os.rename(temp_filename, filename)
+    except IOError, e:
+        print "Metadata update failed:", e
+        try:
+            os.remove(temp_filename)
+        except OSError:
+            pass                # not much we can do
+
 def interactive_mode(packages):
     def print_info(meta, res):
         print "%i/%i: %s " % (i+1, len(packages), meta["package id"])
-        print "Summary: %s" % meta["summary"]
-        print "License: %s" % meta["license"]
-        print "Website: %s" % meta["web site"]
+        print "Summary: %s" % meta.get("summary")
+        print "License: %s" % meta.get("license")
+        print "Website: %s" % meta.get("web site")
         print "File analysis:",
         if sourcedir_for_id(meta["package id"]):
             print
                 input = "n"
             args = input.split()
             action = args[0]
-            if action == "m":
+            if action == "m":   # print metadata
                 print_info(meta, res)
-            elif action == "n":
+            elif action == "n": # next package
                 i += 1; break
-            elif action == "p":
+            elif action == "om": # print original metadata
+                for rad in meta["original metadata"]:
+                    print rad
+            elif action == "p": # previous package
                 if i == 0:
                     print "Already at first package."
                 else:
                     i -= 1; break
-            elif action == "q":
+            elif action == "q": # quit
                 print "Quit."
                 return
-            elif action == "r":
-                print "Re-licensing not implemented yet."
-            elif action == "v":
+            elif action == "r": # rewrite license in metadata file
+                if len(args) > 1:
+                    new_lic = " ".join(args[1:]).strip()
+                    old_lic = meta.get("license")
+                    print "Rewriting license: %s -> %s" % (old_lic, new_lic)
+                    rewrite_license(meta, new_lic)
+                else:
+                    print "You must supply a new license name."
+            elif action == "v": # view files
                 key = len(args) > 1 and args[1].strip().lower() or "unknown"
                 matches = [v for k, v in res.items() if k.lower() == key]
                 if matches:
                     subprocess.call(["less"] + matches[0])
                 else:
                     print "No files under that license."
-            elif action == "w":
+            elif action == "w": # open web page
                 print "Opening '%s'..." % meta["web site"]
                 import webbrowser
                 webbrowser.open(meta["web site"])