{"id":13419365,"url":"https://github.com/pre-commit/identify","last_synced_at":"2025-03-15T05:30:52.946Z","repository":{"id":37942951,"uuid":"81786752","full_name":"pre-commit/identify","owner":"pre-commit","description":"File identification library for Python","archived":false,"fork":false,"pushed_at":"2025-03-08T15:54:15.000Z","size":636,"stargazers_count":265,"open_issues_count":4,"forks_count":154,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-08T16:31:08.012Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pre-commit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"asottile","open_collective":"pre-commit","tidelift":"pypi/pre-commit"}},"created_at":"2017-02-13T05:02:37.000Z","updated_at":"2025-03-08T16:09:12.000Z","dependencies_parsed_at":"2023-10-16T06:47:56.571Z","dependency_job_id":"41743253-1b05-4373-b341-47688682d045","html_url":"https://github.com/pre-commit/identify","commit_stats":{"total_commits":561,"total_committers":132,"mean_commits":4.25,"dds":0.554367201426025,"last_synced_commit":"6fd6f6bea8fedb15e54f1477a49325ea82b73911"},"previous_names":[],"tags_count":175,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pre-commit%2Fidentify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pre-commit%2Fidentify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pre-commit%2Fidentify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pre-commit%2Fidentify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pre-commit","download_url":"https://codeload.github.com/pre-commit/identify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242980730,"owners_count":20216287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:01:14.938Z","updated_at":"2025-03-15T05:30:52.574Z","avatar_url":"https://github.com/pre-commit.png","language":"Python","readme":"[![build status](https://github.com/pre-commit/identify/actions/workflows/main.yml/badge.svg)](https://github.com/pre-commit/identify/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pre-commit/identify/main.svg)](https://results.pre-commit.ci/latest/github/pre-commit/identify/main)\n\nidentify\n========\n\nFile identification library for Python.\n\nGiven a file (or some information about a file), return a set of standardized\ntags identifying what the file is.\n\n## Installation\n\n```bash\npip install identify\n```\n\n## Usage\n### With a file on disk\n\nIf you have an actual file on disk, you can get the most information possible\n(a superset of all other methods):\n\n```python\n\u003e\u003e\u003e from identify import identify\n\u003e\u003e\u003e identify.tags_from_path('/path/to/file.py')\n{'file', 'text', 'python', 'non-executable'}\n\u003e\u003e\u003e identify.tags_from_path('/path/to/file-with-shebang')\n{'file', 'text', 'shell', 'bash', 'executable'}\n\u003e\u003e\u003e identify.tags_from_path('/bin/bash')\n{'file', 'binary', 'executable'}\n\u003e\u003e\u003e identify.tags_from_path('/path/to/directory')\n{'directory'}\n\u003e\u003e\u003e identify.tags_from_path('/path/to/symlink')\n{'symlink'}\n```\n\nWhen using a file on disk, the checks performed are:\n\n* File type (file, symlink, directory, socket)\n* Mode (is it executable?)\n* File name (mostly based on extension)\n* If executable, the shebang is read and the interpreter interpreted\n\n\n### If you only have the filename\n\n```python\n\u003e\u003e\u003e identify.tags_from_filename('file.py')\n{'text', 'python'}\n```\n\n\n### If you only have the interpreter\n\n```python\n\u003e\u003e\u003e identify.tags_from_interpreter('python3.5')\n{'python', 'python3'}\n\u003e\u003e\u003e identify.tags_from_interpreter('bash')\n{'shell', 'bash'}\n\u003e\u003e\u003e identify.tags_from_interpreter('some-unrecognized-thing')\nset()\n```\n\n### As a cli\n\n```\n$ identify-cli --help\nusage: identify-cli [-h] [--filename-only] path\n\npositional arguments:\n  path\n\noptional arguments:\n  -h, --help       show this help message and exit\n  --filename-only\n```\n\n```console\n$ identify-cli setup.py; echo $?\n[\"file\", \"non-executable\", \"python\", \"text\"]\n0\n$ identify-cli setup.py --filename-only; echo $?\n[\"python\", \"text\"]\n0\n$ identify-cli wat.wat; echo $?\nwat.wat does not exist.\n1\n$ identify-cli wat.wat --filename-only; echo $?\n1\n```\n\n### Identifying LICENSE files\n\n`identify` also has an api for determining what type of license is contained\nin a file.  This routine is roughly based on the approaches used by\n[licensee] (the ruby gem that github uses to figure out the license for a\nrepo).\n\nThe approach that `identify` uses is as follows:\n\n1. Strip the copyright line\n2. Normalize all whitespace\n3. Return any exact matches\n4. Return the closest by edit distance (where edit distance \u003c 5%)\n\nTo use the api, install via `pip install identify[license]`\n\n```pycon\n\u003e\u003e\u003e from identify import identify\n\u003e\u003e\u003e identify.license_id('LICENSE')\n'MIT'\n```\n\nThe return value of the `license_id` function is an [SPDX] id.  Currently\nlicenses are sourced from [choosealicense.com].\n\n[licensee]: https://github.com/benbalter/licensee\n[SPDX]: https://spdx.org/licenses/\n[choosealicense.com]: https://github.com/github/choosealicense.com\n\n## How it works\n\nA call to `tags_from_path` does this:\n\n1. What is the type: file, symlink, directory? If it's not file, stop here.\n2. Is it executable? Add the appropriate tag.\n3. Do we recognize the file extension? If so, add the appropriate tags, stop\n   here. These tags would include binary/text.\n4. Peek at the first X bytes of the file. Use these to determine whether it is\n   binary or text, add the appropriate tag.\n5. If identified as text above, try to read and interpret the shebang, and add\n   appropriate tags.\n\nBy design, this means we don't need to partially read files where we recognize\nthe file extension.\n","funding_links":["https://github.com/sponsors/asottile","https://opencollective.com/pre-commit","https://tidelift.com/funding/github/pypi/pre-commit"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpre-commit%2Fidentify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpre-commit%2Fidentify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpre-commit%2Fidentify/lists"}