From dec9fab768e43a5c75456bb61c21701502db6de6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 5 Feb 2018 23:43:53 +0100 Subject: [PATCH] Convert python script to rust --- src/Cargo.lock | 4 ++ src/Cargo.toml | 1 + src/bootstrap/test.rs | 19 ++++---- src/bootstrap/tool.rs | 1 + src/tools/rustdoc-themes/Cargo.toml | 8 ++++ src/tools/rustdoc-themes/main.rs | 59 +++++++++++++++++++++++++ src/tools/rustdoc-themes/test-themes.py | 52 ---------------------- 7 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 src/tools/rustdoc-themes/Cargo.toml create mode 100644 src/tools/rustdoc-themes/main.rs delete mode 100644 src/tools/rustdoc-themes/test-themes.py diff --git a/src/Cargo.lock b/src/Cargo.lock index 52ed134c01e..afe7f841f25 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -2217,6 +2217,10 @@ dependencies = [ "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rustdoc-themes" +version = "0.1.0" + [[package]] name = "rustdoc-tool" version = "0.0.0" diff --git a/src/Cargo.toml b/src/Cargo.toml index c22ba7a37c8..c03301852cd 100644 --- a/src/Cargo.toml +++ b/src/Cargo.toml @@ -22,6 +22,7 @@ members = [ "tools/rls", "tools/rustfmt", "tools/miri", + "tools/rustdoc-themes", # FIXME(https://github.com/rust-lang/cargo/issues/4089): move these to exclude "tools/rls/test_data/bin_lib", "tools/rls/test_data/borrow_error", diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 351d10df28d..eae8ec1311d 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -113,7 +113,7 @@ impl Step for Linkcheck { let _time = util::timeit(); try_run(build, builder.tool_cmd(Tool::Linkchecker) - .arg(build.out.join(host).join("doc"))); + .arg(build.out.join(host).join("doc"))); } fn should_run(run: ShouldRun) -> ShouldRun { @@ -427,7 +427,6 @@ fn path_for_cargo(builder: &Builder, compiler: Compiler) -> OsString { #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct RustdocTheme { pub compiler: Compiler, - pub host: Interned, } impl Step for RustdocTheme { @@ -444,27 +443,25 @@ impl Step for RustdocTheme { run.builder.ensure(RustdocTheme { compiler: compiler, - host: run.builder.build.build, }); } fn run(self, builder: &Builder) { let rustdoc = builder.rustdoc(self.compiler.host); - let mut cmd = Command::new(builder.config.python.clone().expect("python not defined")); - cmd.args(&[builder.src.join("src/tools/rustdoc-themes/test-themes.py").to_str().unwrap(), - rustdoc.to_str().unwrap(), - builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap()]); - cmd.env("RUSTC_STAGE", self.compiler.stage.to_string()) + let mut cmd = builder.tool_cmd(Tool::RustdocTheme); + cmd.arg(rustdoc.to_str().unwrap()) + .arg(builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap()) + .env("RUSTC_STAGE", self.compiler.stage.to_string()) .env("RUSTC_SYSROOT", builder.sysroot(self.compiler)) .env("RUSTDOC_LIBDIR", builder.sysroot_libdir(self.compiler, self.compiler.host)) .env("CFG_RELEASE_CHANNEL", &builder.build.config.channel) - .env("RUSTDOC_REAL", builder.rustdoc(self.host)) + .env("RUSTDOC_REAL", builder.rustdoc(self.compiler.host)) .env("RUSTDOC_CRATE_VERSION", builder.build.rust_version()) .env("RUSTC_BOOTSTRAP", "1"); - if let Some(linker) = builder.build.linker(self.host) { + if let Some(linker) = builder.build.linker(self.compiler.host) { cmd.env("RUSTC_TARGET_LINKER", linker); } - builder.run(&mut cmd); + try_run(builder.build, &mut cmd); } } diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs index ea055cb5d1b..9036eb044b5 100644 --- a/src/bootstrap/tool.rs +++ b/src/bootstrap/tool.rs @@ -260,6 +260,7 @@ tool!( BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd; RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd; RustInstaller, "src/tools/rust-installer", "fabricate", Mode::Libstd; + RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes", Mode::Libstd; ); #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/src/tools/rustdoc-themes/Cargo.toml b/src/tools/rustdoc-themes/Cargo.toml new file mode 100644 index 00000000000..c0e2f527301 --- /dev/null +++ b/src/tools/rustdoc-themes/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rustdoc-themes" +version = "0.1.0" +authors = ["Guillaume Gomez "] + +[[bin]] +name = "rustdoc-themes" +path = "main.rs" diff --git a/src/tools/rustdoc-themes/main.rs b/src/tools/rustdoc-themes/main.rs new file mode 100644 index 00000000000..4028cb4e8b6 --- /dev/null +++ b/src/tools/rustdoc-themes/main.rs @@ -0,0 +1,59 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::env::args; +use std::fs::read_dir; +use std::path::Path; +use std::process::{Command, exit}; + +const FILES_TO_IGNORE: &[&str] = &["main.css"]; + +fn get_folders>(folder_path: P) -> Vec { + let mut ret = Vec::with_capacity(10); + + for entry in read_dir(folder_path.as_ref()).expect("read_dir failed") { + let entry = entry.expect("Couldn't unwrap entry"); + let path = entry.path(); + + if !path.is_file() { + continue + } + let filename = path.file_name().expect("file_name failed"); + if FILES_TO_IGNORE.iter().any(|x| x == &filename) { + continue + } + ret.push(format!("{}", path.display())); + } + ret +} + +fn main() { + let argv: Vec = args().collect(); + + if argv.len() < 3 { + eprintln!("Needs rustdoc binary path"); + exit(1); + } + let rustdoc_bin = &argv[1]; + let themes_folder = &argv[2]; + let themes = get_folders(&themes_folder); + if themes.is_empty() { + eprintln!("No theme found in \"{}\"...", themes_folder); + exit(1); + } + let status = Command::new(rustdoc_bin) + .args(&["-Z", "unstable-options", "--theme-checker"]) + .args(&themes) + .status() + .expect("failed to execute child"); + if !status.success() { + exit(1); + } +} diff --git a/src/tools/rustdoc-themes/test-themes.py b/src/tools/rustdoc-themes/test-themes.py deleted file mode 100644 index 31591277ce3..00000000000 --- a/src/tools/rustdoc-themes/test-themes.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# Copyright 2018 The Rust Project Developers. See the COPYRIGHT -# file at the top-level directory of this distribution and at -# http://rust-lang.org/COPYRIGHT. -# -# Licensed under the Apache License, Version 2.0 or the MIT license -# , at your -# option. This file may not be copied, modified, or distributed -# except according to those terms. - -from os import listdir -from os.path import isfile, join -import subprocess -import sys - -FILES_TO_IGNORE = ['main.css'] - - -def print_err(msg): - sys.stderr.write('{}\n'.format(msg)) - - -def exec_command(command): - child = subprocess.Popen(command) - stdout, stderr = child.communicate() - return child.returncode - - -def main(argv): - if len(argv) < 2: - print_err("Needs rustdoc binary path") - return 1 - rustdoc_bin = argv[0] - themes_folder = argv[1] - themes = [join(themes_folder, f) for f in listdir(themes_folder) - if isfile(join(themes_folder, f)) and f not in FILES_TO_IGNORE] - if len(themes) < 1: - print_err('No theme found in "{}"...'.format(themes_folder)) - return 1 - args = [rustdoc_bin, '-Z', 'unstable-options', '--theme-checker'] - args.extend(themes) - return exec_command(args) - - -if __name__ != '__main__': - print_err("Needs to be run as main") - sys.exit(1) -else: - sys.exit(main(sys.argv[1:]))