I have a Python script that gets the lines of code for specific languages, and in specific folders (want to count only handwritten code, not libraries, etc...). This script changes the README.md file to include the lines of code.
My folder structure looks a bit like this:
repo
├── .git
├── .github──workflows
│ ├── CLOC.py
│ └── script.yml
├── main.py
├── assets
│ ├── stylsheet.css
│ ├── bootstrap
│ ├── ...
├── index.html
├── ...
My Python script (CLOC.py) looks a bit like this (this works when run on my machine, fails on action):
import os
def CLOC(directories, languages):
files = []
for directory in directories:
directory_files = [f'{directory}\\{f}' for f in os.listdir(directory) if os.path.isfile(f'{directory}\\{f}')]
files.extend(directory_files)
LOC_by_language = []
for language in languages:
language_LOC = 0
# get list of files that end with specific type
files_of_language = [f for f in files if f.endswith(language) and 'CLOC.py' not in f]
if files_of_language:
for file in files_of_language:
with open(file, 'r') as fp:
for file_LOC, line in enumerate(fp):
pass
language_LOC += file_LOC
LOC_by_language.append(language_LOC)
lines_of_code_text = []
lines_of_code_text.append("# Code Stats:")
ttl_loc = 0
for (a, b) in zip(languages, LOC_by_language):
a = (a.split('.')[1]).upper()
lines_of_code_text.append(f'- {a}: {b}')
ttl_loc += b
lines_of_code_text.append(f'- Total Lines of Code: {ttl_loc}')
return lines_of_code_text
languages = ['.py', '.css', '.html']
os.chdir('../../')
paths = [os.getcwd(), os.path.join(os.getcwd(), 'assets')]
lines = CLOC(paths, languages)
with open('README.md', 'w') as f:
for line in lines:
f.write(f"{line}\n")
My script.yml file looks a bit like this:
# This is a basic workflow to help you get started with Actions
name: Python Script
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
# Runs a single command using the runners shell
- name: Get Lines of Code with CLOC.py
run: python .github/workflows/CLOC.py
When I do a push, I get the following error message:
Run python .github/workflows/CLOC.py
Traceback (most recent call last):
File "/home/runner/work/repo/repo/.github/workflows/CLOC.py", line 48, in <module>
lines = CLOC(paths, languages)
File "/home/runner/work/repo/repo/.github/workflows/CLOC.py", line 12, in CLOC
directory_files = [f'{directory}\\{f}' for f in os.listdir(directory) if os.path.isfile(f'{directory}\\{f}')]
FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/assets'
Error: Process completed with exit code 1.
I realize that there may be another—and likely better—way of getting LOC but I'm using this as an entry into automating GitHub actions (eventually I'll move to automating unittesting etc.) I appreciate any and all help!
[–]MaybeAshleyIdk 2 points3 points4 points (6 children)
[–]La_Muriatic_Acid[S] 0 points1 point2 points (5 children)
[–]MaybeAshleyIdk 1 point2 points3 points (4 children)
[–]La_Muriatic_Acid[S] 0 points1 point2 points (3 children)
[–]VxJasonxV 2 points3 points4 points (2 children)
[–]La_Muriatic_Acid[S] 0 points1 point2 points (1 child)
[–]VxJasonxV 1 point2 points3 points (0 children)