Last active
November 11, 2024 20:33
-
-
Save dmgolembiowski/bdde73127e87f69349637c3dfd2ec933 to your computer and use it in GitHub Desktop.
2nd Order Automation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import textwrap | |
from string import Template | |
from pathlib import Path | |
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" | |
if ident_dir == '': | |
ident_dir = os.getcwd() | |
ident_dir = os.path.expanduser(ident_dir) | |
ident_dir = os.path.expandvars(ident_dir) | |
ident_dir = ( | |
ident_dir | |
.replace('.', os.getcwd()) | |
.replace('..', str(Path(os.getcwd()).parent)) | |
) | |
ident_dir = next(map(lambda p: Path(str(Path(p).resolve())).name, [ident_dir])) | |
if ident_full_pkg == '' or ident_full_pkg == '.': | |
ident_full_pkg = ident_dir | |
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}") | |
import subprocess | |
subprocess.call(["bash", named_temp.name]) | |
os.remove(named_temp.name) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment