summaryrefslogtreecommitdiffstats
path: root/debian/scripts/guess-crate-copyright
blob: 15f35f634c3a82576941d4aa2c7c972975d45f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python3
# Copyright: 2015-2017 The Debian Project
# License: MIT or Apache-2.0
#
# Guess the copyright of a cargo crate by looking at its git history.

import datetime
import toml
import os
import subprocess
import sys

this_year = datetime.datetime.now().year
crates = sys.argv[1:]
get_initial_commit = len(crates) == 1

for crate in crates:
	with open(os.path.join(crate, "Cargo.toml")) as fp:
		data = toml.load(fp)
		repo = data["package"].get("repository", None)
		if get_initial_commit and repo:
			output = subprocess.check_output(
				"""git clone -q --bare "%s" tmp.crate-copyright >&2 &&
cd tmp.crate-copyright &&
git log --format=%%cI --reverse | head -n1 | cut -b1-4 &&
git log --format=%%cI           | head -n1 | cut -b1-4 &&
cd .. &&
rm -rf tmp.crate-copyright""" % repo, shell=True).decode("utf-8")
			first_year, last_year = output.strip().split(maxsplit=2)
		else:
			first_year = "20XX"
			last_year = this_year

		authors = data["package"].get("authors", ["UNKNOWN AUTHORS"])

		print("""Files: {0}
Copyright: {1}
License: {2}
Comment: see {3}
""".format(
		os.path.join(crate, "*"),
		"\n           ".join("%s-%s %s" % (first_year, last_year, a.replace(" <>", "")) for a in authors),
		data["package"].get("license", "???").replace("/", " or "),
		repo or "???"
	))