Skip to content

Instantly share code, notes, and snippets.

@dmgolembiowski
Created May 19, 2024 01:50
Show Gist options
  • Save dmgolembiowski/bdde73127e87f69349637c3dfd2ec933 to your computer and use it in GitHub Desktop.
Save dmgolembiowski/bdde73127e87f69349637c3dfd2ec933 to your computer and use it in GitHub Desktop.
2nd Order Automation
#!/usr/bin/env python
import textwrap
from string import Template
def template(dir: str, full_pkg: str, ver: str = "0.1.0") -> str:
backslash = '\\'
backslash_newline = '\\n'
pkg_src = full_pkg + '_SOURCES=' + full_pkg + '.c'
it = textwrap.dedent('''\
#!/bin/bash
# This script was automatically generated by auto-automate.
# Do not modify it unless you know what you are doing.
if [ -e $dir ]; then rm -r $dir; fi
mkdir -p $dir
cd $dir
cat > $full_pkg <<$backslash
"----------------"
#include <stdio.h>
int main() {
printf("Hi.$backslash_newline");
}
----------------
cat > Makefile.am <<$backslash
"----------------"
bin_PROGRAMS=$full_pkg
$pkg_src
----------------
autoscan
sed -e 's/FULL-PACKAGE-NAME/$full_pkg/' $backslash
-e 's/VERSION/$ver/' $backslash
-e 's|BUG-REPORT-ADDRESS|/dev/null|' $backslash
-e '10i$backslash
AM_INIT_AUTOMAKE' $backslash
< configure.scan > configure.ac
touch NEWS README AUTHORS ChangeLog INSTALL'''
)
temp: Template = Template(it)
return (
temp
.substitute(
ver=ver,
dir=dir,
full_pkg=full_pkg,
pkg_src=pkg_src,
backslash=backslash,
backslash_newline=backslash_newline,
)
)
def main():
import os
ident_dir: str = ""
ident_full_pkg: str = ""
ident_ver: str = ""
ident_dir += input("Enter the directory name: ")
ident_full_pkg += input("Enter the full package name: ")
ident_ver += input("Enter the semver value (or leave blank for 0.1.0): ")
if not ident_ver:
ident_ver += "0.1.0"
print("Preparing your C-project...")
import tempfile
named_temp = tempfile.NamedTemporaryFile(
prefix='auto-automata-',
suffix='.sh',
delete=False,
)
with open(named_temp.name, 'w') as f:
body = template(
dir=ident_dir,
full_pkg=ident_full_pkg,
ver=ident_ver,
)
f.write(body)
os.system(f"chmod +x {named_temp.name}")
print(f"Execute the script at {named_temp.name}")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment