• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.74
/src/python/pants/fs/fs.py
1
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
import hashlib
1✔
5
import os
1✔
6

7
# The max filename length for HFS+, extX and NTFS is 255, but many systems also have limits on the
8
# total path length (made up of multiple filenames), so we include some additional buffer.
9
_MAX_FILENAME_LENGTH = 100
1✔
10

11

12
def safe_filename(name, extension=None, digest=None, max_length=_MAX_FILENAME_LENGTH):
1✔
13
    """Creates filename from name and extension ensuring that the final length is within the
14
    max_length constraint.
15

16
    By default the length is capped to work on most filesystems and the fallback to achieve
17
    shortening is a sha1 hash of the proposed name.
18

19
    Raises ValueError if the proposed name is not a simple filename but a file path.
20
    Also raises ValueError when the name is simple but cannot be satisfactorily shortened with the
21
    given digest.
22

23
    :API: public
24

25
    name:       the proposed filename without extension
26
    extension:  an optional extension to append to the filename
27
    digest:     the digest to fall back on for too-long name, extension concatenations - should
28
                support the hashlib digest api of update(string) and hexdigest
29
    max_length: the maximum desired file name length
30
    """
UNCOV
31
    if os.path.basename(name) != name:
×
UNCOV
32
        raise ValueError(f"Name must be a filename, handed a path: {name}")
×
33

UNCOV
34
    ext = extension or ""
×
UNCOV
35
    filename = name + ext
×
UNCOV
36
    if len(filename) <= max_length:
×
UNCOV
37
        return filename
×
38
    else:
UNCOV
39
        digest = digest or hashlib.sha1()
×
UNCOV
40
        digest.update(filename.encode())
×
UNCOV
41
        hexdigest = digest.hexdigest()[:16]
×
42

43
        # Prefix and suffix length: max length less 2 periods, the extension length, and the digest length.
UNCOV
44
        ps_len = max(0, (max_length - (2 + len(ext) + len(hexdigest))) // 2)
×
UNCOV
45
        sep = "." if ps_len > 0 else ""
×
UNCOV
46
        prefix = name[:ps_len]
×
UNCOV
47
        suffix = name[-ps_len:] if ps_len > 0 else ""
×
48

UNCOV
49
        safe_name = f"{prefix}{sep}{hexdigest}{sep}{suffix}{ext}"
×
UNCOV
50
        if len(safe_name) > max_length:
×
UNCOV
51
            raise ValueError(
×
52
                "Digest {} failed to produce a filename <= {} characters for {} - got {}".format(
53
                    digest, max_length, filename, safe_name
54
                )
55
            )
UNCOV
56
        return safe_name
×
57

58

59
def safe_filename_from_path(path, **kwargs):
1✔
60
    """As for `safe_filename`, but takes a path.
61

62
    First converts it into a name by replacing separator characters, and then calls safe_filename.
63
    """
UNCOV
64
    return safe_filename(path.strip(os.path.sep).replace(os.path.sep, "."), **kwargs)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc