diff --git a/.gitignore b/.gitignore index 972f527046b2c..390a0deeb9deb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ *.elc *.epub *.exe -*.pdb *.fn *.html *.kdev4 @@ -29,6 +28,7 @@ *.orig *.out *.patch +*.pdb *.pdf *.pg *.pot @@ -50,6 +50,10 @@ .cproject .hg/ .hgignore +.idea +__pycache__/ +*.py[cod] +*$py.class .project .settings/ .valgrindrc @@ -64,6 +68,7 @@ /llvm/ /mingw-build/ /nd/ +/obj/ /rt/ /rustllvm/ /src/libunicode/DerivedCoreProperties.txt @@ -72,13 +77,10 @@ /src/libunicode/PropList.txt /src/libunicode/Scripts.txt /src/libunicode/UnicodeData.txt -/stage0/ -/stage1/ -/stage2/ -/stage3/ +/stage[0-9]+/ +/target /test/ /tmp/ -/obj/ TAGS TAGS.emacs TAGS.vi @@ -88,11 +90,9 @@ config.mk config.stamp keywords.md lexer.ml -src/.DS_Store src/etc/dl src/librustc_llvm/llvmdeps.rs tmp.*.rs version.md version.ml version.texi -/target diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index a05700c1af492..fba71f15a98b4 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python +# # Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT # file at the top-level directory of this distribution and at # http://rust-lang.org/COPYRIGHT. @@ -8,6 +10,8 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + import argparse import contextlib import hashlib @@ -17,12 +21,13 @@ import sys import tarfile + def get(url, path, verbose=False): print("downloading " + url) sha_url = url + ".sha256" sha_path = path + ".sha256" for _url, _path in ((url, path), (sha_url, sha_path)): - # see http://serverfault.com/questions/301128/how-to-download + # See http://serverfault.com/questions/301128/how-to-download if sys.platform == 'win32': run(["PowerShell.exe", "/nologo", "-Command", "(New-Object System.Net.WebClient)" @@ -33,16 +38,17 @@ def get(url, path, verbose=False): print("verifying " + path) with open(path, "rb") as f: found = hashlib.sha256(f.read()).hexdigest() - with open(sha_path, "r") as f: + with open(sha_path) as f: expected, _ = f.readline().split() if found != expected: err = ("invalid checksum:\n" " found: {}\n" - " expected: {}".format(found, expected)) + " expected: {}").format(found, expected) if verbose: raise RuntimeError(err) sys.exit(err) + def unpack(tarball, dst, verbose=False, match=None): print("extracting " + tarball) fname = os.path.basename(tarball).replace(".tar.gz", "") @@ -65,11 +71,12 @@ def unpack(tarball, dst, verbose=False, match=None): shutil.move(tp, fp) shutil.rmtree(os.path.join(dst, fname)) + def run(args, verbose=False): if verbose: print("running: " + ' '.join(args)) sys.stdout.flush() - # Use Popen here instead of call() as it apparently allows powershell on + # Use Popen here instead of call() as it apparently allows PowerShell on # Windows to not lock up waiting for input presumably. ret = subprocess.Popen(args) code = ret.wait() @@ -79,6 +86,7 @@ def run(args, verbose=False): raise RuntimeError(err) sys.exit(err) + def stage0_data(rust_root): nightlies = os.path.join(rust_root, "src/stage0.txt") with open(nightlies, 'r') as nightlies: @@ -90,7 +98,16 @@ def stage0_data(rust_root): data[a] = b return data + class RustBuild: + def __init__(self, build_dir, rust_root, config_toml='', config_mk='', + verbose=False): + self.build_dir = build_dir + self.config_mk = config_mk + self.config_toml = config_toml + self.rust_root = rust_root + self.verbose = verbose + def download_stage0(self): cache_dst = os.path.join(self.build_dir, "cache") rustc_cache = os.path.join(cache_dst, self.stage0_rustc_date()) @@ -100,13 +117,15 @@ def download_stage0(self): if not os.path.exists(cargo_cache): os.makedirs(cargo_cache) - if self.rustc().startswith(self.bin_root()) and \ - (not os.path.exists(self.rustc()) or self.rustc_out_of_date()): + if self.rustc().startswith(self.bin_root()) and ( + not os.path.exists( + self.rustc()) or self.rustc_out_of_date()): if os.path.exists(self.bin_root()): shutil.rmtree(self.bin_root()) channel = self.stage0_rustc_channel() filename = "rust-std-" + channel + "-" + self.build + ".tar.gz" - url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date() + url = "https://static.rust-lang.org/dist/" + \ + self.stage0_rustc_date() tarball = os.path.join(rustc_cache, filename) if not os.path.exists(tarball): get(url + "/" + filename, tarball, verbose=self.verbose) @@ -115,23 +134,28 @@ def download_stage0(self): verbose=self.verbose) filename = "rustc-" + channel + "-" + self.build + ".tar.gz" - url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date() + url = "https://static.rust-lang.org/dist/" + \ + self.stage0_rustc_date() tarball = os.path.join(rustc_cache, filename) if not os.path.exists(tarball): get(url + "/" + filename, tarball, verbose=self.verbose) - unpack(tarball, self.bin_root(), match="rustc", verbose=self.verbose) + unpack(tarball, self.bin_root(), match="rustc", + verbose=self.verbose) with open(self.rustc_stamp(), 'w') as f: f.write(self.stage0_rustc_date()) - if self.cargo().startswith(self.bin_root()) and \ - (not os.path.exists(self.cargo()) or self.cargo_out_of_date()): + if self.cargo().startswith(self.bin_root()) and ( + not os.path.exists( + self.cargo()) or self.cargo_out_of_date()): channel = self.stage0_cargo_channel() filename = "cargo-" + channel + "-" + self.build + ".tar.gz" - url = "https://static.rust-lang.org/cargo-dist/" + self.stage0_cargo_date() + url = "https://static.rust-lang.org/cargo-dist/" + \ + self.stage0_cargo_date() tarball = os.path.join(cargo_cache, filename) if not os.path.exists(tarball): get(url + "/" + filename, tarball, verbose=self.verbose) - unpack(tarball, self.bin_root(), match="cargo", verbose=self.verbose) + unpack(tarball, self.bin_root(), match="cargo", + verbose=self.verbose) with open(self.cargo_stamp(), 'w') as f: f.write(self.stage0_cargo_date()) @@ -172,19 +196,17 @@ def get_toml(self, key): for line in self.config_toml.splitlines(): if line.startswith(key + ' ='): return self.get_string(line) - return None def get_mk(self, key): for line in iter(self.config_mk.splitlines()): if line.startswith(key): return line[line.find(':=') + 2:].strip() - return None def cargo(self): config = self.get_toml('cargo') - if config: - return config - return os.path.join(self.bin_root(), "bin/cargo" + self.exe_suffix()) + + return config or os.path.join(self.bin_root(), + "bin/cargo" + self.exe_suffix()) def rustc(self): config = self.get_toml('rustc') @@ -197,8 +219,8 @@ def rustc(self): def get_string(self, line): start = line.find('"') - end = start + 1 + line[start+1:].find('"') - return line[start+1:end] + end = start + 1 + line[start + 1:].find('"') + return line[start + 1:end] def exe_suffix(self): if sys.platform == 'win32': @@ -212,14 +234,14 @@ def build_bootstrap(self): env["RUSTC"] = self.rustc() env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") - env["PATH"] = os.path.join(self.bin_root(), "bin") + \ - os.pathsep + env["PATH"] + env["PATH"] = os.path.join(self.bin_root(), "bin") + os.pathsep + env[ + "PATH"] self.run([self.cargo(), "build", "--manifest-path", os.path.join(self.rust_root, "src/bootstrap/Cargo.toml")], env) def run(self, args, env): - proc = subprocess.Popen(args, env = env) + proc = subprocess.Popen(args, env=env) ret = proc.wait() if ret != 0: sys.exit(ret) @@ -234,7 +256,7 @@ def build_triple(self): try: ostype = subprocess.check_output(['uname', '-s']).strip() cputype = subprocess.check_output(['uname', '-m']).strip() - except FileNotFoundError: + except OSError: if sys.platform == 'win32': return 'x86_64-pc-windows-msvc' else: @@ -310,6 +332,7 @@ def build_triple(self): return cputype + '-' + ostype + def main(): parser = argparse.ArgumentParser(description='Build rust') parser.add_argument('--config') @@ -318,45 +341,41 @@ def main(): args = [a for a in sys.argv if a != '-h'] args, _ = parser.parse_known_args(args) - # Configure initial bootstrap - rb = RustBuild() - rb.config_toml = '' - rb.config_mk = '' - rb.rust_root = os.path.abspath(os.path.join(__file__, '../../..')) - rb.build_dir = os.path.join(os.getcwd(), "build") - rb.verbose = args.verbose + rust_root = os.path.abspath(os.path.join(__file__, '../../..')) + build_dir = os.path.join(os.getcwd(), "build") try: with open(args.config or 'config.toml') as config: - rb.config_toml = config.read() - except: - pass - try: - rb.config_mk = open('config.mk').read() - except: + config_toml = config.read() + with open('config.mk') as config_mk_file: + config_mk = config_mk_file.read() + except OSError: pass + # Configure initial bootstrap. + rb = RustBuild(build_dir=build_dir, rust_root=rust_root, + config_toml=config_toml, config_mk=config_mk, + verbose=args.verbose) + data = stage0_data(rb.rust_root) rb._rustc_channel, rb._rustc_date = data['rustc'].split('-', 1) rb._cargo_channel, rb._cargo_date = data['cargo'].split('-', 1) - # Fetch/build the bootstrap + # Fetch/build the bootstrap. rb.build = rb.build_triple() rb.download_stage0() sys.stdout.flush() rb.build_bootstrap() sys.stdout.flush() - # Run the bootstrap + # Run the bootstrap. args = [os.path.join(rb.build_dir, "bootstrap/debug/bootstrap")] - args.append('--src') - args.append(rb.rust_root) - args.append('--build') - args.append(rb.build) + args += ['--src', rb.rust_root, '--build', rb.build] args.extend(sys.argv[1:]) env = os.environ.copy() env["BOOTSTRAP_PARENT_ID"] = str(os.getpid()) rb.run(args, env) + if __name__ == '__main__': main() diff --git a/src/etc/check-sanitycheck.py b/src/etc/check-sanitycheck.py index 0e103fbcffbad..ef3ab7eb524fe 100644 --- a/src/etc/check-sanitycheck.py +++ b/src/etc/check-sanitycheck.py @@ -10,28 +10,33 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +import functools import os import subprocess import sys -import functools STATUS = 0 + def error_unless_permitted(env_var, message): global STATUS if not os.getenv(env_var): sys.stderr.write(message) STATUS = 1 + def only_on(platforms): def decorator(func): @functools.wraps(func) def inner(): if any(map(lambda x: sys.platform.startswith(x), platforms)): func() + return inner + return decorator + @only_on(['linux', 'darwin', 'freebsd', 'openbsd']) def check_rlimit_core(): import resource @@ -41,7 +46,8 @@ def check_rlimit_core(): RLIMIT_CORE is set to a nonzero value (%d). During debuginfo, the test suite will segfault many rustc's, creating many potentially large core files. set ALLOW_NONZERO_RLIMIT_CORE to ignore this warning -""" % (soft)) +""" % soft) + @only_on(['win32']) def check_console_code_page(): @@ -49,10 +55,12 @@ def check_console_code_page(): sys.stderr.write('Warning: the console output code page is not UTF-8, \ some tests may fail. Use `cmd /c "chcp 65001"` to setup UTF-8 code page.\n') + def main(): check_console_code_page() check_rlimit_core() + if __name__ == '__main__': main() sys.exit(STATUS) diff --git a/src/etc/check-summary.py b/src/etc/check-summary.py index 9312b685c14a2..40caaa68963d1 100755 --- a/src/etc/check-summary.py +++ b/src/etc/check-summary.py @@ -10,12 +10,15 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + import glob import sys if __name__ == '__main__': summaries = [] + def summarise(fname): summary = {} with open(fname) as fd: @@ -33,9 +36,11 @@ def summarise(fname): summary[status].append(test) summaries.append((fname, summary)) + def count(t): return sum(map(lambda f: len(f[1].get(t, [])), summaries)) + logfiles = sys.argv[1:] for files in map(glob.glob, logfiles): map(summarise, files) @@ -52,6 +57,6 @@ def count(t): for f, s in summaries: failures = s.get('failed', []) if len(failures) > 0: - print(" %s:" % (f)) + print(" %s:" % f) for test in failures: - print(" %s" % (test)) + print(" %s" % test) diff --git a/src/etc/debugger_pretty_printers_common.py b/src/etc/debugger_pretty_printers_common.py index 06a83c75936fe..a4fe6a8d56424 100644 --- a/src/etc/debugger_pretty_printers_common.py +++ b/src/etc/debugger_pretty_printers_common.py @@ -20,31 +20,31 @@ # For example structs, tuples, fat pointers, or enum variants will all have # DWARF_TYPE_CODE_STRUCT. DWARF_TYPE_CODE_STRUCT = 1 -DWARF_TYPE_CODE_UNION = 2 -DWARF_TYPE_CODE_PTR = 3 -DWARF_TYPE_CODE_ARRAY = 4 -DWARF_TYPE_CODE_ENUM = 5 +DWARF_TYPE_CODE_UNION = 2 +DWARF_TYPE_CODE_PTR = 3 +DWARF_TYPE_CODE_ARRAY = 4 +DWARF_TYPE_CODE_ENUM = 5 # These constants specify the most specific kind of type that could be # determined for a given value. -TYPE_KIND_UNKNOWN = -1 -TYPE_KIND_EMPTY = 0 -TYPE_KIND_SLICE = 1 -TYPE_KIND_REGULAR_STRUCT = 2 -TYPE_KIND_TUPLE = 3 -TYPE_KIND_TUPLE_STRUCT = 4 -TYPE_KIND_CSTYLE_VARIANT = 5 -TYPE_KIND_TUPLE_VARIANT = 6 -TYPE_KIND_STRUCT_VARIANT = 7 -TYPE_KIND_STR_SLICE = 8 -TYPE_KIND_STD_VEC = 9 -TYPE_KIND_STD_STRING = 10 -TYPE_KIND_REGULAR_ENUM = 11 -TYPE_KIND_COMPRESSED_ENUM = 12 -TYPE_KIND_SINGLETON_ENUM = 13 -TYPE_KIND_CSTYLE_ENUM = 14 -TYPE_KIND_PTR = 15 -TYPE_KIND_FIXED_SIZE_VEC = 16 +TYPE_KIND_UNKNOWN = -1 +TYPE_KIND_EMPTY = 0 +TYPE_KIND_SLICE = 1 +TYPE_KIND_REGULAR_STRUCT = 2 +TYPE_KIND_TUPLE = 3 +TYPE_KIND_TUPLE_STRUCT = 4 +TYPE_KIND_CSTYLE_VARIANT = 5 +TYPE_KIND_TUPLE_VARIANT = 6 +TYPE_KIND_STRUCT_VARIANT = 7 +TYPE_KIND_STR_SLICE = 8 +TYPE_KIND_STD_VEC = 9 +TYPE_KIND_STD_STRING = 10 +TYPE_KIND_REGULAR_ENUM = 11 +TYPE_KIND_COMPRESSED_ENUM = 12 +TYPE_KIND_SINGLETON_ENUM = 13 +TYPE_KIND_CSTYLE_ENUM = 14 +TYPE_KIND_PTR = 15 +TYPE_KIND_FIXED_SIZE_VEC = 16 ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$" ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR" @@ -101,8 +101,8 @@ def get_fields(self): objects represent the variants of the enum. Field-objects must have a `name` attribute that gives their name as specified in DWARF. """ - assert ((self.get_dwarf_type_kind() == DWARF_TYPE_CODE_STRUCT) or - (self.get_dwarf_type_kind() == DWARF_TYPE_CODE_UNION)) + assert (self.get_dwarf_type_kind() == DWARF_TYPE_CODE_STRUCT) or ( + self.get_dwarf_type_kind() == DWARF_TYPE_CODE_UNION) raise NotImplementedError("Override this method") def get_wrapped_value(self): @@ -140,8 +140,8 @@ def __classify_struct(self): # REGULAR SLICE if (unqualified_type_name.startswith("&[") and - unqualified_type_name.endswith("]") and - self.__conforms_to_field_layout(SLICE_FIELD_NAMES)): + unqualified_type_name.endswith("]") and + self.__conforms_to_field_layout(SLICE_FIELD_NAMES)): return TYPE_KIND_SLICE fields = self.get_fields() @@ -153,12 +153,12 @@ def __classify_struct(self): # STD VEC if (unqualified_type_name.startswith("Vec<") and - self.__conforms_to_field_layout(STD_VEC_FIELD_NAMES)): + self.__conforms_to_field_layout(STD_VEC_FIELD_NAMES)): return TYPE_KIND_STD_VEC # STD STRING if (unqualified_type_name.startswith("String") and - self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)): + self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)): return TYPE_KIND_STD_STRING # ENUM VARIANTS @@ -180,7 +180,6 @@ def __classify_struct(self): # REGULAR STRUCT return TYPE_KIND_REGULAR_STRUCT - def __classify_union(self): assert self.get_dwarf_type_kind() == DWARF_TYPE_CODE_UNION @@ -198,7 +197,6 @@ def __classify_union(self): else: return TYPE_KIND_REGULAR_ENUM - def __conforms_to_field_layout(self, expected_fields): actual_fields = self.get_fields() actual_field_count = len(actual_fields) @@ -229,6 +227,7 @@ class Value(object): Sub-classes are supposed to wrap a debugger-specific value-object and provide implementations for the abstract methods in this class. """ + def __init__(self, ty): self.type = ty @@ -310,11 +309,12 @@ def extract_length_ptr_and_cap_from_std_vec(vec_val): unique_ptr_val = vec_ptr_val.get_child_at_index(0) data_ptr = unique_ptr_val.get_child_at_index(0) assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR - return (length, data_ptr, capacity) + return length, data_ptr, capacity + def extract_length_and_ptr_from_slice(slice_val): - assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE or - slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE) + assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE) or ( + slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE) length_field_index = SLICE_FIELD_NAMES.index(SLICE_FIELD_NAME_LENGTH) ptr_field_index = SLICE_FIELD_NAMES.index(SLICE_FIELD_NAME_DATA_PTR) @@ -323,4 +323,4 @@ def extract_length_and_ptr_from_slice(slice_val): data_ptr = slice_val.get_child_at_index(ptr_field_index) assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR - return (length, data_ptr) + return length, data_ptr diff --git a/src/etc/dec2flt_table.py b/src/etc/dec2flt_table.py index 9fdab1fcfca28..d984191aa9af7 100644 --- a/src/etc/dec2flt_table.py +++ b/src/etc/dec2flt_table.py @@ -24,11 +24,10 @@ even larger, and it's already uncomfortably large (6 KiB). """ from __future__ import print_function -import sys -from math import ceil, log -from fractions import Fraction -from collections import namedtuple +from collections import namedtuple +from fractions import Fraction +from math import ceil, log N = 64 # Size of the significand field in bits MIN_SIG = 2 ** (N - 1) @@ -92,6 +91,7 @@ def error(f, e, z): ulp_err = abs_err / Fraction(2) ** z.exp return float(ulp_err) + HEADER = """ // Copyright 2015 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at @@ -121,7 +121,7 @@ def main(): def print_proper_powers(): MIN_E = -305 MAX_E = 305 - e_range = range(MIN_E, MAX_E+1) + e_range = range(MIN_E, MAX_E + 1) powers = [] for e in e_range: z = algorithm_m(1, e) @@ -142,7 +142,7 @@ def print_proper_powers(): def print_short_powers(num_bits, significand_size): - max_sig = 2**significand_size - 1 + max_sig = 2 ** significand_size - 1 # The fast path bails out for exponents >= ceil(log5(max_sig)) max_e = int(ceil(log(max_sig, 5))) e_range = range(max_e) diff --git a/src/etc/gdb_load_rust_pretty_printers.py b/src/etc/gdb_load_rust_pretty_printers.py index 755cac153d10d..9595676c7005e 100644 --- a/src/etc/gdb_load_rust_pretty_printers.py +++ b/src/etc/gdb_load_rust_pretty_printers.py @@ -9,4 +9,5 @@ # except according to those terms. import gdb_rust_pretty_printing + gdb_rust_pretty_printing.register_printers(gdb.current_objfile()) diff --git a/src/etc/gdb_rust_pretty_printing.py b/src/etc/gdb_rust_pretty_printing.py index f93f3490215d1..43df640f78ee8 100755 --- a/src/etc/gdb_rust_pretty_printing.py +++ b/src/etc/gdb_rust_pretty_printing.py @@ -9,15 +9,15 @@ # except according to those terms. import gdb -import re + import debugger_pretty_printers_common as rustpp -#=============================================================================== + +# =============================================================================== # GDB Pretty Printing Module for Rust -#=============================================================================== +# =============================================================================== class GdbType(rustpp.Type): - def __init__(self, ty): super(GdbType, self).__init__() self.ty = ty @@ -45,8 +45,8 @@ def get_dwarf_type_kind(self): return rustpp.DWARF_TYPE_CODE_ENUM def get_fields(self): - assert ((self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT) or - (self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_UNION)) + assert (self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_STRUCT) or ( + self.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_UNION) if self.fields is None: self.fields = list(self.ty.fields()) return self.fields @@ -90,18 +90,17 @@ def rust_pretty_printer_lookup_function(gdb_val): val = GdbValue(gdb_val) type_kind = val.type.get_type_kind() - if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or - type_kind == rustpp.TYPE_KIND_EMPTY): + if type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or type_kind == rustpp.TYPE_KIND_EMPTY: return RustStructPrinter(val, - omit_first_field = False, - omit_type_name = False, - is_tuple_like = False) + omit_first_field=False, + omit_type_name=False, + is_tuple_like=False) if type_kind == rustpp.TYPE_KIND_STRUCT_VARIANT: return RustStructPrinter(val, - omit_first_field = True, - omit_type_name = False, - is_tuple_like = False) + omit_first_field=True, + omit_type_name=False, + is_tuple_like=False) if type_kind == rustpp.TYPE_KIND_SLICE: return RustSlicePrinter(val) @@ -117,24 +116,24 @@ def rust_pretty_printer_lookup_function(gdb_val): if type_kind == rustpp.TYPE_KIND_TUPLE: return RustStructPrinter(val, - omit_first_field = False, - omit_type_name = True, - is_tuple_like = True) + omit_first_field=False, + omit_type_name=True, + is_tuple_like=True) if type_kind == rustpp.TYPE_KIND_TUPLE_STRUCT: return RustStructPrinter(val, - omit_first_field = False, - omit_type_name = False, - is_tuple_like = True) + omit_first_field=False, + omit_type_name=False, + is_tuple_like=True) if type_kind == rustpp.TYPE_KIND_CSTYLE_VARIANT: return RustCStyleVariantPrinter(val.get_child_at_index(0)) if type_kind == rustpp.TYPE_KIND_TUPLE_VARIANT: return RustStructPrinter(val, - omit_first_field = True, - omit_type_name = False, - is_tuple_like = True) + omit_first_field=True, + omit_type_name=False, + is_tuple_like=True) if type_kind == rustpp.TYPE_KIND_SINGLETON_ENUM: variant = get_field_at_index(gdb_val, 0) @@ -158,9 +157,9 @@ def rust_pretty_printer_lookup_function(gdb_val): return None -#=------------------------------------------------------------------------------ +# =------------------------------------------------------------------------------ # Pretty Printer Classes -#=------------------------------------------------------------------------------ +# =------------------------------------------------------------------------------ class RustStructPrinter: def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like): self.__val = val diff --git a/src/etc/generate-deriving-span-tests.py b/src/etc/generate-deriving-span-tests.py index 790fc8942873e..3e51466bc16e6 100755 --- a/src/etc/generate-deriving-span-tests.py +++ b/src/etc/generate-deriving-span-tests.py @@ -18,7 +18,9 @@ sample usage: src/etc/generate-deriving-span-tests.py """ -import sys, os, datetime, stat +import datetime +import os +import stat TEST_DIR = os.path.abspath( os.path.join(os.path.dirname(__file__), '../test/compile-fail')) @@ -76,6 +78,7 @@ ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4) + def create_test_case(type, trait, super_traits, number_of_errors): string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type] all_traits = ','.join([trait] + super_traits) @@ -83,8 +86,9 @@ def create_test_case(type, trait, super_traits, number_of_errors): error_deriving = '#[derive(%s)]' % super_traits if super_traits else '' errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count)) - code = string.format(traits = all_traits, errors = errors) - return TEMPLATE.format(year = YEAR, error_deriving=error_deriving, code = code) + code = string.format(traits=all_traits, errors=errors) + return TEMPLATE.format(year=YEAR, error_deriving=error_deriving, code=code) + def write_file(name, string): test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name) @@ -97,8 +101,7 @@ def write_file(name, string): f.write(string) # mark file read-only - os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH) - + os.chmod(test_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) ENUM = 1 @@ -108,10 +111,10 @@ def write_file(name, string): traits = { 'Zero': (STRUCT, [], 1), 'Default': (STRUCT, [], 1), - 'FromPrimitive': (0, [], 0), # only works for C-like enums + 'FromPrimitive': (0, [], 0), # only works for C-like enums - 'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans - 'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans + 'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans + 'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans } for (trait, supers, errs) in [('Clone', [], 1), diff --git a/src/etc/generate-keyword-tests.py b/src/etc/generate-keyword-tests.py index 937c231a473e9..04209909fb09d 100755 --- a/src/etc/generate-keyword-tests.py +++ b/src/etc/generate-keyword-tests.py @@ -18,11 +18,10 @@ sample usage: src/etc/generate-keyword-tests.py as break """ -import sys -import os import datetime +import os import stat - +import sys template = """// Copyright %d The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at @@ -48,12 +47,12 @@ for kw in sys.argv[1:]: test_file = os.path.join(test_dir, 'keyword-%s-as-identifier.rs' % kw) - # set write permission if file exists, so it can be changed + # Set write permission if file exists, so it can be changed. if os.path.exists(test_file): os.chmod(test_file, stat.S_IWUSR) with open(test_file, 'wt') as f: f.write(template % (datetime.datetime.now().year, kw, kw)) - # mark file read-only + # Mark file read-only. os.chmod(test_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) diff --git a/src/etc/get-stage0.py b/src/etc/get-stage0.py index 3a609957faff1..18f084c189805 100644 --- a/src/etc/get-stage0.py +++ b/src/etc/get-stage0.py @@ -11,15 +11,14 @@ # except according to those terms. import os -import shutil import sys -import tarfile path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../bootstrap")) sys.path.append(path) import bootstrap + def main(argv): src_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) triple = argv[1] @@ -47,5 +46,6 @@ def main(argv): os.makedirs(stage0_dst) bootstrap.unpack(dst, stage0_dst, match='rustc', verbose=True) + if __name__ == '__main__': main(sys.argv) diff --git a/src/etc/htmldocck.py b/src/etc/htmldocck.py index 8362c239b655d..245cda7f24657 100644 --- a/src/etc/htmldocck.py +++ b/src/etc/htmldocck.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python2.7 +# # Copyright 2015 The Rust Project Developers. See the COPYRIGHT # file at the top-level directory of this distribution and at # http://rust-lang.org/COPYRIGHT. @@ -105,30 +107,33 @@ """ from __future__ import print_function -import sys + import os.path import re import shlex -from collections import namedtuple +import sys from HTMLParser import HTMLParser +from collections import namedtuple +from htmlentitydefs import entitydefs from xml.etree import cElementTree as ET # ⇤/⇥ are not in HTML 4 but are in HTML 5 -from htmlentitydefs import entitydefs entitydefs['larrb'] = u'\u21e4' entitydefs['rarrb'] = u'\u21e5' # "void elements" (no closing tag) from the HTML Standard section 12.1.2 -VOID_ELEMENTS = set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', - 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr']) +VOID_ELEMENTS = {'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', + 'param', 'source', 'track', 'wbr'} class CustomHTMLParser(HTMLParser): - """simplified HTML parser. + """Simplified HTML parser. - this is possible because we are dealing with very regular HTML from + This is possible because we are dealing with very regular HTML from rustdoc; we only have to deal with i) void elements and ii) empty - attributes.""" + attributes. + """ + def __init__(self, target=None): HTMLParser.__init__(self) self.__builder = target or ET.TreeBuilder() @@ -161,21 +166,26 @@ def close(self): HTMLParser.close(self) return self.__builder.close() + Command = namedtuple('Command', 'negated cmd args lineno context') + class FailedCheck(Exception): pass + class InvalidCheck(Exception): pass + def concat_multi_lines(f): - """returns a generator out of the file object, which + """Returns a generator out of the file object, which - removes `\\` then `\n` then a shared prefix with the previous line then optional whitespace; - keeps a line number (starting from 0) of the first line being - concatenated.""" - lastline = None # set to the last line when the last line has a backslash + concatenated. + """ + lastline = None # set to the last line when the last line has a backslash firstlineno = None catenated = '' for lineno, line in enumerate(f): @@ -204,6 +214,7 @@ def concat_multi_lines(f): if lastline is not None: print_err(lineno, line, 'Trailing backslash at the end of the file') + LINE_PATTERN = re.compile(r''' (?<=(?!?) (?P[A-Za-z]+(?:-[A-Za-z]+)*) @@ -225,7 +236,7 @@ def get_commands(template): print_err(lineno, line, 'Invalid template syntax') continue args = shlex.split(args) - yield Command(negated=negated, cmd=cmd, args=args, lineno=lineno+1, context=line) + yield Command(negated=negated, cmd=cmd, args=args, lineno=lineno + 1, context=line) def _flatten(node, acc): @@ -245,7 +256,7 @@ def flatten(node): def normalize_xpath(path): if path.startswith('//'): - return '.' + path # avoid warnings + return '.' + path # avoid warnings elif path.startswith('.//'): return path else: @@ -275,7 +286,7 @@ def get_file(self, path): return self.files[path] abspath = os.path.join(self.root, path) - if not(os.path.exists(abspath) and os.path.isfile(abspath)): + if not (os.path.exists(abspath) and os.path.isfile(abspath)): raise FailedCheck('File does not exist {!r}'.format(path)) with open(abspath) as f: @@ -289,7 +300,7 @@ def get_tree(self, path): return self.trees[path] abspath = os.path.join(self.root, path) - if not(os.path.exists(abspath) and os.path.isfile(abspath)): + if not (os.path.exists(abspath) and os.path.isfile(abspath)): raise FailedCheck('File does not exist {!r}'.format(path)) with open(abspath) as f: @@ -303,7 +314,7 @@ def get_tree(self, path): def check_string(data, pat, regexp): if not pat: - return True # special case a presence testing + return True # special case a presence testing elif regexp: return re.search(pat, data) is not None else: @@ -346,9 +357,11 @@ def check_tree_count(tree, path, count): path = normalize_xpath(path) return len(tree.findall(path)) == count + def stderr(*args): print(*args, file=sys.stderr) + def print_err(lineno, context, err, message=None): global ERR_COUNT ERR_COUNT += 1 @@ -359,31 +372,33 @@ def print_err(lineno, context, err, message=None): if context: stderr("\t{}".format(context)) + ERR_COUNT = 0 + def check_command(c, cache): try: cerr = "" - if c.cmd == 'has' or c.cmd == 'matches': # string test + if c.cmd == 'has' or c.cmd == 'matches': # string test regexp = (c.cmd == 'matches') - if len(c.args) == 1 and not regexp: # @has = file existence + if len(c.args) == 1 and not regexp: # @has = file existence try: cache.get_file(c.args[0]) ret = True except FailedCheck as err: cerr = err.message ret = False - elif len(c.args) == 2: # @has/matches = string test + elif len(c.args) == 2: # @has/matches = string test cerr = "`PATTERN` did not match" ret = check_string(cache.get_file(c.args[0]), c.args[1], regexp) - elif len(c.args) == 3: # @has/matches = XML tree test + elif len(c.args) == 3: # @has/matches = XML tree test cerr = "`XPATH PATTERN` did not match" tree = cache.get_tree(c.args[0]) pat, sep, attr = c.args[1].partition('/@') - if sep: # attribute + if sep: # attribute tree = cache.get_tree(c.args[0]) ret = check_tree_attr(tree, pat, attr, c.args[2], regexp) - else: # normalized text + else: # normalized text pat = c.args[1] if pat.endswith('/text()'): pat = pat[:-7] @@ -391,8 +406,8 @@ def check_command(c, cache): else: raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd)) - elif c.cmd == 'count': # count test - if len(c.args) == 3: # @count = count test + elif c.cmd == 'count': # count test + if len(c.args) == 3: # @count = count test ret = check_tree_count(cache.get_tree(c.args[0]), c.args[1], int(c.args[2])) else: raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd)) @@ -413,11 +428,13 @@ def check_command(c, cache): except InvalidCheck as err: print_err(c.lineno, c.context, err.message) + def check(target, commands): cache = CachedFiles(target) for c in commands: check_command(c, cache) + if __name__ == '__main__': if len(sys.argv) != 3: stderr('Usage: {}