示例#1
0
def _add_header_to_file(
    path: PathLike,
    spdx_info: SpdxInfo,
    template: Template,
    template_is_commented: bool,
    style: Optional[str],
    force_multi: bool = False,
    out=sys.stdout,
) -> int:
    """Helper function."""
    # pylint: disable=too-many-arguments
    result = 0
    if style is not None:
        style = NAME_STYLE_MAP[style]
    else:
        style = _get_comment_style(path)
        if style is None:
            out.write(_("Skipped unrecognised file {path}").format(path=path))
            out.write("\n")
            return result

    with path.open("r", encoding="utf-8", newline="") as fp:
        text = fp.read()

    # Detect and remember line endings for later conversion.
    line_ending = detect_line_endings(text)
    # Normalise line endings.
    text = text.replace(line_ending, "\n")

    try:
        output = find_and_replace_header(
            text,
            spdx_info,
            template=template,
            template_is_commented=template_is_commented,
            style=style,
            force_multi=force_multi,
        )
    except CommentCreateError:
        out.write(
            _("Error: Could not create comment for '{path}'").format(
                path=path))
        out.write("\n")
        result = 1
    except MissingSpdxInfo:
        out.write(
            _("Error: Generated comment header for '{path}' is missing"
              " copyright lines or license expressions. The template is"
              " probably incorrect. Did not write new header.").format(
                  path=path))
        out.write("\n")
        result = 1
    else:
        with path.open("w", encoding="utf-8", newline=line_ending) as fp:
            fp.write(output)
        # TODO: This may need to be rephrased more elegantly.
        out.write(_("Successfully changed header of {path}").format(path=path))
        out.write("\n")

    return result
示例#2
0
def _add_header_to_file(
    path: PathLike,
    spdx_info: SpdxInfo,
    template: Template,
    template_is_commented: bool,
    style: Optional[str],
    out=sys.stdout,
) -> int:
    """Helper function."""
    # pylint: disable=too-many-arguments
    result = 0
    if style is not None:
        style = NAME_STYLE_MAP[style]
    else:
        style = _get_comment_style(path)

    with path.open("r") as fp:
        text = fp.read()

    try:
        output = find_and_replace_header(
            text,
            spdx_info,
            template=template,
            template_is_commented=template_is_commented,
            style=style,
        )
    except CommentCreateError:
        out.write(
            _("Error: Could not create comment for '{path}'").format(
                path=path))
        out.write("\n")
        result = 1
    except MissingSpdxInfo:
        out.write(
            _("Error: Generated comment header for '{path}' is missing"
              " copyright lines or license expressions. The template is"
              " probably incorrect. Did not write new header.").format(
                  path=path))
        out.write("\n")
        result = 1
    else:
        with path.open("w") as fp:
            fp.write(output)
        # TODO: This may need to be rephrased more elegantly.
        out.write(_("Successfully changed header of {path}").format(path=path))
        out.write("\n")

    return result
示例#3
0
def put_license_in_file(spdx_identifier: str,
                        root: PathLike = None,
                        destination: PathLike = None) -> None:
    """Download a license and put it in the correct file.

    This function exists solely for convenience.

    :param spdx_identifier: SPDX identifier of the license.
    :param root: The root of the project.
    :param destination: An override path for the destination of the license.
    :raises requests.RequestException: if the license could not be downloaded.
    :raises FileExistsError: if the license file already exists.
    """
    header = ""
    if destination is None:
        licenses_path = find_licenses_directory(root=root)
        licenses_path.mkdir(exist_ok=True)
        destination = licenses_path / "".join((spdx_identifier, ".txt"))
    else:
        is_exception = spdx_identifier in EXCEPTION_MAP
        header = ("Valid-{licexc}-Identifier: {identifier}\n"
                  "{licexc}-Text:\n\n".format(
                      identifier=spdx_identifier,
                      licexc="Exception" if is_exception else "License",
                  ))

    destination = Path(destination)
    if destination.exists():
        raise FileExistsError(errno.EEXIST, "File exists", str(destination))

    text = download_license(spdx_identifier)
    with destination.open("w") as fp:
        fp.write(header)
        fp.write(text)
def save_skeleton(path: PathLike, adjacency: List[List[int]],
                  coordinates: np.ndarray):
    path = Path(path)
    assert not path.exists()
    skeleton = {'adjacency': adjacency, 'coordinates': coordinates.tolist()}
    with path.open('wb') as file:
        pickle.dump(skeleton, file)
示例#5
0
 def load_data(data_path: PathLike) -> "AccountManager":
     try:
         data_path = Path(data_path).expanduser()
         am = pickle.load(data_path.open("rb"))
         _compat_account_manager(am)
         return am
     except Exception:
         return AccountManager(data_path=data_path)
示例#6
0
    def parse_file(self, path: os.PathLike):
        path = Path(path)

        with path.open(mode="r") as f:
            for line_number, line in enumerate(f, start=1):
                self.parse_line(line, line_number)

        return self
示例#7
0
文件: _util.py 项目: zvr/reuse-tool
def _checksum(path: PathLike) -> str:
    path = Path(path)

    file_sha1 = sha1()
    with path.open("rb") as fp:
        for chunk in iter(lambda: fp.read(128 * file_sha1.block_size), b""):
            file_sha1.update(chunk)

    return file_sha1.hexdigest()
示例#8
0
    def __init__(self, path: os.PathLike):
        self._path = path
        self._update = False

        if path.exists():
            with path.open("r") as fh:
                self._state = json.load(fh)
        else:
            self._state = None
示例#9
0
def create_opmap_from_file(file_path: os.PathLike) -> Dict[str, int]:
    """Return an opcode map dictionary of OPNAME : OPCODE from a JSON file.

    The JSON file must enumerate a complete opmap for the specified Python
    version. Even if only a few bytes have been swapped, all operations and
    opcodes must have a value for the version specified.

    Parameters
    ----------
    file_path : os.PathLike
        The path to the JSON remapping file. This file *must* follow this
        format.

        .. code-block::

            {
                "python_version": "<major>.<minor>(.<patch>)",
                "remapped_opcodes": [
                    {
                        "opcode": 1,
                        "opname": "POP_TOP",
                        "remapped_value": 5
                    },
                    {
                        "opcode": 2,
                        "opname": "ROT_TWO",
                        "remapped_value": 4
                    },
                    ...

    Returns
    -------
    Dict[str, int]
        A dictionary of OPNAME : OPCODE. For example::

        {
            'POP_TOP': 5,
            'ROT_TWO': 4,
            ...
        }
    """
    if not file_path.exists():
        raise FileNotFoundError(file_path)

    remappings: Dict[str, int] = {}
    with file_path.open("r") as remapping_file:
        file_json: str = json.loads(remapping_file.read())
        version: str = file_json["python_version"]
        subdict: Dict[str, Union[str, int]]
        for subdict in file_json["remapped_opcodes"]:
            remappings[subdict["opname"]] = subdict["remapped_value"]

    if not validate_opmap(version, remappings):
        raise RuntimeError("[!] Opcode map is not valid!")
    return remappings
示例#10
0
def gzip_file(input_path: PathLike, output_path: PathLike = None, keep=True):
    input_path = Path(input_path)
    if input_path.name.endswith('.gz'):
        raise ValueError(f'{input_path} is already gzipped')
    output_path = Path(
        output_path
    ) if output_path else input_path.parent / f"{input_path.name}.gz"
    with input_path.open('rb') as f_in, gzip.open(output_path, 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)
    if not keep:
        os.unlink(input_path)
示例#11
0
def readfile(path: os.PathLike, fmt: str = None) -> Molecule:
    path = Path(path)
    if not fmt:
        fmt = geom_formats.get(path.suffix[1:])
    if not fmt:
        if path.name.endswith('geometry.in'):
            fmt = 'aims'
        else:
            raise ValueError(f'Unknown format: {fmt!r}')
    with path.open() as f:
        return load(f, fmt)
示例#12
0
 def write(self, path: os.PathLike, fmt: str = None) -> None:
     path = Path(path)
     if not fmt:
         fmt = geom_formats.get(path.suffix[1:])
     if not fmt:
         if path.name.endswith('geometry.in'):
             fmt = 'aims'
         else:
             raise ValueError(f'Unknown format: {fmt!r}')
     with path.open('w') as f:
         self.dump(f, fmt)
示例#13
0
    def load(self, path: PathLike) -> None:
        """ Load deck from a ydk file. 

        Parameters
        ----------
        path : PathLike
            Deck will be loaded from this `path`.


        Raises
        ------
        FileNotFoundError
            raises when `path` does not exist

        ValueError
            raise when suffix of `path` is not ydk

        """
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f'File `{str(path)}` does not exist')

        if path.suffix != '.ydk':
            raise ValueError(
                f'The file extension is invalid. valid: ydk, got {path.suffix}'
            )

        box: List[int] = self.main
        with path.open() as f:
            for line in f.readlines():
                line = line.strip()
                if line == '#extra':
                    box = self.extra
                elif line == '!side':
                    box = self.side

                try:
                    id = int(line)
                    box.append(id)
                except ValueError as err:
                    pass
示例#14
0
def put_license_in_file(spdx_identifier: str, destination: PathLike) -> None:
    """Download a license and put it in the destination file.

    This function exists solely for convenience.

    :param spdx_identifier: SPDX License Identifier of the license.
    :param destination: Where to put the license.
    :raises requests.RequestException: if the license could not be downloaded.
    :raises FileExistsError: if the license file already exists.
    """
    header = ""
    destination = Path(destination)
    destination.parent.mkdir(exist_ok=True)

    if destination.exists():
        raise FileExistsError(errno.EEXIST, "File exists", str(destination))

    text = download_license(spdx_identifier)
    with destination.open("w", encoding="utf-8") as fp:
        fp.write(header)
        fp.write(text)
示例#15
0
def download_file(url: str, destination: os.PathLike, show_size: bool = False):
    """Download a file from a URL to a destination.

    Args:
        url (str): the URL
        destination (os.PathLike): the destination
        show_size (bool, optional): whether to display a progress bar. Defaults to False.
    """
    destination = URI(destination)
    logger.info(f"Downloading {url} to {destination}")
    response = requests.get(url, stream=True)
    total_size_in_bytes = int(response.headers.get('content-length', 0))
    block_size = 1024
    if show_size:
        progress_bar = tqdm(total=total_size_in_bytes,
                            unit='iB',
                            unit_scale=True)
    with destination.open("wb") as f:
        for data in response.iter_content(block_size):
            if show_size:
                progress_bar.update(len(data))
            f.write(data)
    if show_size:
        progress_bar.close()