[yocto-autobuilder-helper,v3] Scripts to automatically add banner to html files in tarball

Message ID 20220415231315.31561-1-abongwabonalais@gmail.com
State New
Headers show
Series [yocto-autobuilder-helper,v3] Scripts to automatically add banner to html files in tarball | expand

Commit Message

Abongwa Bonalais April 15, 2022, 11:13 p.m. UTC
Signed-off-by: Abongwa Bonalais Amahnui <abongwabonalais@gmail.com>
---
 trial.py      | 44 +++++++++++++++++++++++++++++++
 trialstyle.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+)
 create mode 100644 trial.py
 create mode 100644 trialstyle.py

Comments

Richard Purdie April 16, 2022, 10:36 a.m. UTC | #1
On Sat, 2022-04-16 at 00:13 +0100, Abongwa Amahnui Bonalais wrote:
> Signed-off-by: Abongwa Bonalais Amahnui <abongwabonalais@gmail.com>
> ---
>  trial.py      | 44 +++++++++++++++++++++++++++++++
>  trialstyle.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 115 insertions(+)
>  create mode 100644 trial.py
>  create mode 100644 trialstyle.py
> 
> diff --git a/trial.py b/trial.py
> new file mode 100644
> index 0000000..bc60433
> --- /dev/null
> +++ b/trial.py
> @@ -0,0 +1,44 @@
> +#!usr/bin/env python3
> +#LICENSE = "GPL-2.0 & LGPL-2.0"
> +#PackageOriginator: Abongwa Bonalais Amahnui

This is heading the right way and I'm glad the encoding change helped. Please
re-read what I said about the header though as the above isn't what I said,
there are at least two issues.

Also, why are you picking "GPL-2.0 & LGPL-2.0" as that seems like a strange
choice for this script. Most of our code is either MIT or GPL-2.0-only. You need
to use SPDX names for licenses. I do also now notice we're missing a lot of
license identifiers in that repository but there are some.

> +
> +# function to append to the content of an html file below the body tag
> +import os
> +
> +
> +content = '''
> +<div id="outdated-warning">This document is for outdated, you should select the <a href="https://docs.yoctoproject.org/">latest release version</a> in this series.</div>
> +
> +'''
> +
> +def append_to_body(html_file, content):
> +    # open the html file in read mode
> +    with open(html_file, 'r', encoding="ISO-8859-1") as f:
> +        # read the content of the html file
> +        html_content = f.read()
> +    # open the html file in write mode
> +    with open(html_file, 'w') as f:
> +        # write the content of the html file
> +        f.write(html_content.replace('<body>', '<body>' + content ))
> +
> +
> +
> +# function to loop through all html files in the current directory and call the append_to_body function
> +def loop_through_html_files(directory):
> +    # loop through all files in the directory
> +    for file in os.listdir(directory):
> +        # check if the file is an html file
> +        if file.endswith('.html'):
> +            # call the append_to_body function
> +            append_to_body(os.path.join(directory, file), content)
> +#loop_through_html_files('.')
> +
> +
> +# function to loop through all sub directories and recursively call above function
> +def loop_through_directories(directory):
> +    # loop through all sub directories in the directory
> +    for dir in os.listdir(directory):
> +        # check if the sub directory is a directory
> +        if os.path.isdir(os.path.join(directory, dir)):
> +            # call the loop_through_html_files function
> +            loop_through_html_files(os.path.join(directory, dir))
> +            # call the loop_through_directories function again via recursion
> +            loop_through_directories(os.path.join(directory, dir))
> +loop_through_directories('.')

Please use os.walk(). There are plenty of examples in google and in our own
code.

Cheers,

Richard
Abongwa Bonalais April 16, 2022, 10:58 a.m. UTC | #2
Hi Richard, can I use a topic like " Added a banner on the old documentation docs " as written in the bugzilla bug?

And I will also do some research how to implement the os.walk() module.

Patch

diff --git a/trial.py b/trial.py
new file mode 100644
index 0000000..bc60433
--- /dev/null
+++ b/trial.py
@@ -0,0 +1,44 @@ 
+#!usr/bin/env python3
+#LICENSE = "GPL-2.0 & LGPL-2.0"
+#PackageOriginator: Abongwa Bonalais Amahnui
+
+# function to append to the content of an html file below the body tag
+import os
+
+
+content = '''
+<div id="outdated-warning">This document is for outdated, you should select the <a href="https://docs.yoctoproject.org/">latest release version</a> in this series.</div>
+
+'''
+
+def append_to_body(html_file, content):
+    # open the html file in read mode
+    with open(html_file, 'r', encoding="ISO-8859-1") as f:
+        # read the content of the html file
+        html_content = f.read()
+    # open the html file in write mode
+    with open(html_file, 'w') as f:
+        # write the content of the html file
+        f.write(html_content.replace('<body>', '<body>' + content ))
+
+
+
+# function to loop through all html files in the current directory and call the append_to_body function
+def loop_through_html_files(directory):
+    # loop through all files in the directory
+    for file in os.listdir(directory):
+        # check if the file is an html file
+        if file.endswith('.html'):
+            # call the append_to_body function
+            append_to_body(os.path.join(directory, file), content)
+#loop_through_html_files('.')
+
+
+# function to loop through all sub directories and recursively call above function
+def loop_through_directories(directory):
+    # loop through all sub directories in the directory
+    for dir in os.listdir(directory):
+        # check if the sub directory is a directory
+        if os.path.isdir(os.path.join(directory, dir)):
+            # call the loop_through_html_files function
+            loop_through_html_files(os.path.join(directory, dir))
+            # call the loop_through_directories function again via recursion
+            loop_through_directories(os.path.join(directory, dir))
+loop_through_directories('.')
diff --git a/trialstyle.py b/trialstyle.py
new file mode 100644
index 0000000..941005b
--- /dev/null
+++ b/trialstyle.py
@@ -0,0 +1,71 @@ 
+#!usr/bin/env python3
+# function to append to the content of an html file below the body tag
+
+import os
+
+
+content = '''
+ 
+  font-family: Verdana, Sans, sans-serif;
+
+  /*min-width: 640px;*/
+  width: 100%;
+  margin:  0;
+  padding: 0;
+  color: #333;
+  overflow-x: hidden;
+  }
+ 
+ /*added books too*/
+.book{
+margin:  0 auto;
+min-width: 640px;
+padding: 0 5em 5em 5em;
+}
+/* added the id below to make the banner show and be fixed*/
+#outdated-warning{
+text-align: center;
+background-color: rgb(255, 186, 186); 
+color: rgb(106, 14, 14); 
+padding: 0.5em 0; 
+width: 100%;
+position: fixed;
+top: 0;
+
+
+'''
+def append_to_body(html_file, content):
+    # open the html file in read mode
+    with open(html_file, 'r', encoding="ISO-8859-1") as f:
+        # read the content of the html file
+        html_content = f.read()
+        html_content.find('body {')
+    # open the html file in write mode
+    with open(html_file, 'w') as f:
+        # write the content of the html file
+        f.write(html_content.replace((html_content[html_content.find('body {'):html_content.find('}')]), 'body {' + content ))
+
+
+
+# function to loop through all html files in the current directory and call the append_to_body function
+def loop_through_html_files(directory):
+    # loop through all files in the directory
+    for file in os.listdir(directory):
+        # check if the file is an html file
+        if file.endswith('.css'):
+            # call the append_to_body function
+            append_to_body(os.path.join(directory, file), content)
+#loop_through_html_files('.')
+
+
+# function to loop through all sub directories and recursively call above function
+def loop_through_directories(directory):
+    # loop through all sub directories in the directory
+    for dir in os.listdir(directory):
+        # check if the sub directory is a directory
+        if os.path.isdir(os.path.join(directory, dir)):
+            # call the loop_through_html_files function
+            loop_through_html_files(os.path.join(directory, dir))
+            # call the loop_through_directories function again via recursion
+            loop_through_directories(os.path.join(directory, dir))
+loop_through_directories('.')
\ No newline at end of file