2017-01-30 23:44:25 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import jinja2
|
|
|
|
import requests
|
2017-02-05 13:20:55 +00:00
|
|
|
import tqdm
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
import cargodeps
|
|
|
|
|
2017-02-05 12:56:50 +00:00
|
|
|
XDG_CACHE_HOME = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache"))
|
|
|
|
CACHEDIR = os.path.join(XDG_CACHE_HOME, "rust2rpm")
|
2017-01-30 23:44:25 +00:00
|
|
|
API_URL = "https://crates.io/api/v1/"
|
|
|
|
TEMPLATE = """# Generated by rust2rpm
|
2017-02-03 09:05:26 +00:00
|
|
|
%bcond_without check
|
|
|
|
|
2017-02-03 09:39:37 +00:00
|
|
|
%global crate {{ md.name }}
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
Name: rust-%{crate}
|
2017-02-03 09:39:37 +00:00
|
|
|
Version: {{ md.version }}
|
2017-01-30 23:44:25 +00:00
|
|
|
Release: 1%{?dist}
|
|
|
|
Summary: # FIXME
|
|
|
|
|
2017-02-05 11:10:50 +00:00
|
|
|
License: {{ md.license|default("# FIXME") }}
|
2017-02-03 09:39:37 +00:00
|
|
|
URL: https://crates.io/crates/{{ md.name }}
|
2017-01-30 23:44:25 +00:00
|
|
|
Source0: https://crates.io/api/v1/crates/%{crate}/%{version}/download#/%{crate}-%{version}.crate
|
|
|
|
|
|
|
|
ExclusiveArch: %{rust_arches}
|
|
|
|
|
|
|
|
BuildRequires: rust
|
|
|
|
BuildRequires: cargo
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for req in md.build_requires %}
|
|
|
|
BuildRequires: {{ req }}
|
2017-01-30 23:54:56 +00:00
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for con in md.build_conflicts %}
|
|
|
|
BuildConflicts: {{ con }}
|
2017-01-30 23:44:25 +00:00
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% if md.test_requires|length > 0 %}
|
2017-02-03 09:05:26 +00:00
|
|
|
%if %{with check}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for req in md.test_requires %}
|
|
|
|
BuildRequires: {{ req }}
|
2017-02-03 09:05:26 +00:00
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for con in md.test_conflicts %}
|
|
|
|
BuildConflicts: {{ con }}
|
2017-02-03 09:05:26 +00:00
|
|
|
{% endfor %}
|
|
|
|
%endif
|
|
|
|
{% endif %}
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
%description
|
|
|
|
%{summary}.
|
|
|
|
|
|
|
|
%package devel
|
|
|
|
Summary: %{summary}
|
|
|
|
BuildArch: noarch
|
2017-02-03 09:39:37 +00:00
|
|
|
{% if target == "epel-7" %}
|
|
|
|
{% for prv in md.provides %}
|
|
|
|
Provides: {{ prv }}
|
2017-01-30 23:44:25 +00:00
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for req in md.requires %}
|
2017-01-30 23:44:25 +00:00
|
|
|
Requires: {{ req }}
|
2017-01-30 23:54:56 +00:00
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% for con in md.conflicts %}
|
2017-01-30 23:44:25 +00:00
|
|
|
Conflicts: {{ con }}
|
|
|
|
{% endfor %}
|
2017-02-03 09:39:37 +00:00
|
|
|
{% endif %}
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
%description devel
|
|
|
|
%{summary}.
|
|
|
|
|
|
|
|
%prep
|
|
|
|
%autosetup -n %{crate}-%{version}
|
|
|
|
%cargo_prep
|
|
|
|
|
|
|
|
%install
|
|
|
|
%cargo_install_crate %{crate}-%{version}
|
|
|
|
|
2017-02-03 09:05:26 +00:00
|
|
|
%if %{with check}
|
2017-01-30 23:44:25 +00:00
|
|
|
%check
|
|
|
|
%cargo_test
|
2017-02-03 09:05:26 +00:00
|
|
|
%endif
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
%files devel
|
2017-02-05 11:07:49 +00:00
|
|
|
{% if md.license_file is not none %}
|
|
|
|
%license {{ md.license_file }}
|
|
|
|
{% endif %}
|
2017-01-30 23:44:25 +00:00
|
|
|
%{cargo_registry}/%{crate}-%{version}/
|
|
|
|
|
|
|
|
%changelog
|
|
|
|
"""
|
|
|
|
JINJA_ENV = jinja2.Environment(undefined=jinja2.StrictUndefined,
|
|
|
|
trim_blocks=True, lstrip_blocks=True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-t", "--target", choices=("epel-7", "fedora-26"), required=True,
|
|
|
|
help="Distribution target")
|
|
|
|
parser.add_argument("crate", help="crates.io name")
|
|
|
|
parser.add_argument("version", nargs="?", help="crates.io version")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.version is None:
|
|
|
|
# Now we need to get latest version
|
|
|
|
url = requests.compat.urljoin(API_URL, "crates/{}/versions".format(args.crate))
|
|
|
|
req = requests.get(url)
|
|
|
|
req.raise_for_status()
|
|
|
|
args.version = req.json()["versions"][0]["num"]
|
|
|
|
|
2017-02-05 12:56:50 +00:00
|
|
|
if not os.path.isdir(CACHEDIR):
|
|
|
|
os.mkdir(CACHEDIR)
|
2017-02-05 13:20:55 +00:00
|
|
|
cratef_base = "{}-{}.crate".format(args.crate, args.version)
|
|
|
|
cratef = os.path.join(CACHEDIR, cratef_base)
|
2017-01-30 23:44:25 +00:00
|
|
|
if not os.path.isfile(cratef):
|
|
|
|
url = requests.compat.urljoin(API_URL, "crates/{}/{}/download#".format(args.crate, args.version))
|
|
|
|
req = requests.get(url, stream=True)
|
|
|
|
req.raise_for_status()
|
2017-02-05 13:20:55 +00:00
|
|
|
total = int(req.headers["Content-Length"])
|
2017-01-30 23:44:25 +00:00
|
|
|
with open(cratef, "wb") as f:
|
2017-02-05 13:20:55 +00:00
|
|
|
for chunk in tqdm.tqdm(req.iter_content(), "Downloading {}".format(cratef_base),
|
|
|
|
total=total, unit="B", unit_scale=True):
|
2017-01-30 23:44:25 +00:00
|
|
|
f.write(chunk)
|
|
|
|
|
|
|
|
files = []
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
target_dir = "{}/".format(tmpdir)
|
|
|
|
with tarfile.open(cratef, "r") as archive:
|
|
|
|
for n in archive.getnames():
|
|
|
|
if not os.path.abspath(os.path.join(target_dir, n)).startswith(target_dir):
|
|
|
|
raise Exception("Unsafe filenames!")
|
|
|
|
archive.extractall(target_dir)
|
|
|
|
toml = "{}/{}-{}/Cargo.toml".format(tmpdir, args.crate, args.version)
|
|
|
|
assert os.path.isfile(toml)
|
|
|
|
|
2017-02-03 09:39:37 +00:00
|
|
|
metadata = cargodeps.Metadata.from_file(toml)
|
2017-01-30 23:44:25 +00:00
|
|
|
|
|
|
|
template = JINJA_ENV.from_string(TEMPLATE)
|
2017-02-03 09:39:37 +00:00
|
|
|
print(template.render(target=args.target, md=metadata))
|