#51 First shot at handling local crates
Closed 5 years ago by ignatenkobrain. Opened 6 years ago by andersblomdell.
Unknown source master  into  master

file modified
+40 -24
@@ -81,36 +81,21 @@

      t = datetime.fromtimestamp(os.stat(path).st_mtime, timezone.utc)

      return t.astimezone().isoformat()

  

- def main():

-     parser = argparse.ArgumentParser()

-     parser.add_argument("-", "--stdout", action="store_true",

-                         help="Print spec and patches into stdout")

-     parser.add_argument("-t", "--target", action="store",

-                         choices=("plain", "fedora", "mageia", "opensuse"), default=get_default_target(),

-                         help="Distribution target")

-     parser.add_argument("-p", "--patch", action="store_true",

-                         help="Do initial patching of Cargo.toml")

-     parser.add_argument("crate", help="crates.io name")

-     parser.add_argument("version", nargs="?", help="crates.io version")

-     args = parser.parse_args()

- 

-     if args.patch:

-         editor = detect_editor()

- 

-     if args.version is None:

+ def download(crate, version):

+     if version is None:

          # Now we need to get latest version

-         url = requests.compat.urljoin(API_URL, "crates/{}/versions".format(args.crate))

+         url = requests.compat.urljoin(API_URL, "crates/{}/versions".format(crate))

          req = requests.get(url)

          req.raise_for_status()

          versions = req.json()["versions"]

-         args.version = next(version["num"] for version in versions if not version["yanked"])

+         version = next(version["num"] for version in versions if not version["yanked"])

  

      if not os.path.isdir(CACHEDIR):

          os.mkdir(CACHEDIR)

-     cratef_base = "{}-{}.crate".format(args.crate, args.version)

+     cratef_base = "{}-{}.crate".format(crate, version)

      cratef = os.path.join(CACHEDIR, cratef_base)

      if not os.path.isfile(cratef):

-         url = requests.compat.urljoin(API_URL, "crates/{}/{}/download#".format(args.crate, args.version))

+         url = requests.compat.urljoin(API_URL, "crates/{}/{}/download#".format(crate, version))

          req = requests.get(url, stream=True)

          req.raise_for_status()

          total = int(req.headers["Content-Length"])
@@ -118,6 +103,37 @@

              for chunk in tqdm.tqdm(req.iter_content(), "Downloading {}".format(cratef_base),

                                     total=total, unit="B", unit_scale=True):

                  f.write(chunk)

+     return cratef, crate, version

+ 

+ def local(crate, version):

+     if version != None:

if version is not None

+         raise Exception("Don't specify version when using local crates!")

+     assert os.path.isfile(crate)

+     assert crate.endswith('.crate')

I thought that you want to use Cargo.toml

I was thinking along the line that I have to create the .crate for Source0 and this way the SRPM would be in sync, i.e. I imagine the workflow to be:
* run 'cargo package'
* copy crate to rpmbuild/SOURCE
* run rust2rpm on rpmbuild/SOURCE/name-version.crate
* edit the specfile
* run 'rpmbuild -bs'

+     cratename,version = os.path.basename(crate)[0:-6].rsplit('-', 1)

+     cratef = crate

+     return cratef, cratename, version

+ 

+ def main():

+     parser = argparse.ArgumentParser()

+     parser.add_argument("-", "--stdout", action="store_true",

+                         help="Print spec and patches into stdout")

+     parser.add_argument("-t", "--target", action="store",

+                         choices=("plain", "fedora", "mageia", "opensuse"), default=get_default_target(),

+                         help="Distribution target")

+     parser.add_argument("-p", "--patch", action="store_true",

+                         help="Do initial patching of Cargo.toml")

+     parser.add_argument("crate", help="crates.io name")

+     parser.add_argument("version", nargs="?", help="crates.io version")

+     args = parser.parse_args()

+ 

+     if args.patch:

+         editor = detect_editor()

+ 

+     if os.path.isfile(args.crate) and args.crate.endswith('.crate'):

+         cratef,crate,version = local(args.crate, args.version)

+     else:

+         cratef,crate,version = download(args.crate, args.version)

  

      with tempfile.TemporaryDirectory() as tmpdir:

          target_dir = "{}/".format(tmpdir)
@@ -126,7 +142,7 @@

                  if not os.path.abspath(os.path.join(target_dir, n)).startswith(target_dir):

                      raise Exception("Unsafe filenames!")

              archive.extractall(target_dir)

-         toml_relpath = "{}-{}/Cargo.toml".format(args.crate, args.version)

+         toml_relpath = "{}-{}/Cargo.toml".format(crate, version)

          toml = "{}/{}".format(tmpdir, toml_relpath)

          assert os.path.isfile(toml)

  
@@ -147,7 +163,7 @@

      template = JINJA_ENV.get_template("main.spec")

  

      if args.patch and len(diff) > 0:

-         patch_file = "{}-{}-fix-metadata.diff".format(args.crate, args.version)

+         patch_file = "{}-{}-fix-metadata.diff".format(crate, version)

      else:

          patch_file = None

  
@@ -193,7 +209,7 @@

          kwargs["date"] = time.strftime("%a %b %d %Y")

      kwargs["packager"] = detect_packager()

  

-     spec_file = "rust-{}.spec".format(args.crate)

+     spec_file = "rust-{}.spec".format(crate)

      spec_contents = template.render(md=metadata, patch_file=patch_file, **kwargs)

      if args.stdout:

          print("# {}".format(spec_file))

no initial comment

I thought that you want to use Cargo.toml

I was thinking along the line that I have to create the .crate for Source0 and this way the SRPM would be in sync, i.e. I imagine the workflow to be:
* run 'cargo package'
* copy crate to rpmbuild/SOURCE
* run rust2rpm on rpmbuild/SOURCE/name-version.crate
* edit the specfile
* run 'rpmbuild -bs'

Pull-Request has been closed by ignatenkobrain

5 years ago
Metadata