Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ddd SPDX IDs to repository files #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# http://editorconfig.org/
root = yes

Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
* text eol=lf
*.jpg binary
*.odt binary
Expand Down
2 changes: 2 additions & 0 deletions Bylaws.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->

# Apertium Bylaws

## Mission
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,35 @@ This is a placeholder for whatever we make the introductory and formal entry-poi
* [Issues suitable for newcomers](https://github.com/issues?q=is%3Aopen+is%3Aissue+org%3Aapertium+archived%3Afalse+label%3A%22good+first+issue%22) ([un-authed link](https://github.com/search?q=org%3Aapertium+label%3A%22good+first+issue%22&state=open&type=Issues))
* [Open issues across Apertium](https://github.com/issues?q=is%3Aopen+is%3Aissue+org%3Aapertium+archived%3Afalse) ([un-authed link](https://github.com/search?q=org%3Aapertium&state=open&type=Issues))
* [Open pull requests](https://github.com/issues?q=is%3Aopen+is%3Apr+org%3Aapertium+archived%3Afalse) ([un-authed link](https://github.com/search?q=org%3Aapertium+type%3apr&state=open&type=Issues))

## Helper Script for Adding SPDX IDs

We have created a helper script named `add_spdx_ids.py` to automatically add SPDX IDs to files based on their file type and existing license information. This script helps ensure that all files in the repository have the appropriate SPDX IDs.

### Usage Instructions

To use the `add_spdx_ids.py` script, follow these steps:

1. Ensure you have Python installed on your system.
2. Download the `add_spdx_ids.py` script from the repository.
3. Open a terminal or command prompt.
4. Navigate to the directory where the script is located.
5. Run the script with the file path as an argument:

```sh
python add_spdx_ids.py <file_path>
```

Replace `<file_path>` with the path to the file you want to add the SPDX ID to.

### Example

```sh
python add_spdx_ids.py README.md
```

This command will add the appropriate SPDX ID to the `README.md` file.

### Dependencies

The `add_spdx_ids.py` script requires Python 3.x to run. Ensure you have Python 3.x installed on your system before running the script.
81 changes: 81 additions & 0 deletions app_spdx_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import sys
import os
import logging

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')

# Define comment styles for different file types
xml_comment = '<!-- %s -->\n'
hfst_comment = '! %s\n'
cpp_comment = '// %s\n'
other_comment = '# %s\n'

# Define SPDX license identifiers
license_identifiers = {
'GPL-3.0-or-later': 'SPDX-License-Identifier: GPL-3.0-or-later',
'GPL-2.0-or-later': 'SPDX-License-Identifier: GPL-2.0-or-later',
'GPL-3.0-only': 'SPDX-License-Identifier: GPL-3.0-only',
'CC0-1.0': 'SPDX-License-Identifier: CC0-1.0'
}

def get_license_identifier(file_path):
# Check for existing license in the file
with open(file_path, 'r') as f:
lines = f.readlines()
for line in lines:
for license_id in license_identifiers.values():
if license_id in line:
return license_id

# Check for existing license in the repository COPYING file
repo_root = os.path.dirname(os.path.abspath(file_path))
copying_file = os.path.join(repo_root, 'COPYING')
if os.path.exists(copying_file):
with open(copying_file, 'r') as f:
content = f.read()
for license_id in license_identifiers.values():
if license_id in content:
return license_id

# Default to GPL-3.0-or-later if no existing license is found
return license_identifiers['GPL-3.0-or-later']

def add_spdx_id(file_path):
ext = os.path.splitext(file_path)[-1]
license_id = get_license_identifier(file_path)
lines = []

with open(file_path, 'r') as f:
lines = f.readlines()

if ext in ['.xml', '.svg']:
if '<?xml' not in lines[0].lower():
lines.insert(0, '<?xml version="1.0" encoding="UTF-8"?>\n')
lines.insert(1, xml_comment % license_id)
elif ext in ['.hfst']:
lines.insert(0, hfst_comment % license_id)
elif ext in ['.cpp', '.c', '.h']:
lines.insert(0, cpp_comment % license_id)
else:
n = 0
if lines[0].startswith('#!/'):
n = 1
lines.insert(n, other_comment % license_id)

with open(file_path, 'w') as f:
f.write(''.join(lines))

logging.info(f'Added SPDX ID to {file_path}')

if __name__ == '__main__':
if len(sys.argv) < 2:
logging.error('Usage: python add_spdx_ids.py <file_path>')
sys.exit(1)

file_path = sys.argv[1]
if not os.path.exists(file_path):
logging.error(f'File not found: {file_path}')
sys.exit(1)

add_spdx_id(file_path)
2 changes: 2 additions & 0 deletions brochure/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->

# Apertium brochure

The Apertium 2-sided A4 brochure (for conferences and other events) needs to be updated periodically.
Expand Down
1 change: 1 addition & 0 deletions posters/eamt-2016/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
# EAMT 2016 Apertium poster, recovered from Overleaf.
6 changes: 4 additions & 2 deletions posters/lt4all/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
apertium-at-lt4all.pdf: apertium-at-lt4all.tex Images/pairs.pdf
xelatex apertium-at-lt4all.tex
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
# Apertium poster for LT4All
4-6 December 2019, UNESCO, Paris.
In progress.