From fbb42da9249396156f539196dc25999c1f7af61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=81?= Date: Wed, 27 Jan 2021 17:52:44 +0300 Subject: [PATCH] Improved rendering of the some formats. Shebang processing is fixed #39 --- calculate/templates/format/base_format.py | 2 ++ calculate/templates/format/raw_format.py | 19 ++++++-------- calculate/templates/format/templates/compiz | 12 ++++----- calculate/templates/format/templates/openrc | 10 ++++--- calculate/templates/format/templates/postfix | 10 ++++--- calculate/templates/format/templates/procmail | 10 ++++--- calculate/templates/format/templates/samba | 13 +++++----- tests/templates/format/test_openrc.py | 12 +-------- tests/templates/format/test_postfix.py | 8 ------ tests/templates/format/test_procmail.py | 6 ----- tests/templates/format/test_raw.py | 26 +++++++++++++++++++ tests/templates/format/test_samba.py | 8 ++++++ .../templates/format/testfiles/openrc/result | 3 --- .../templates/format/testfiles/postfix/result | 7 ----- .../format/testfiles/procmail/result | 5 ---- tests/templates/format/testfiles/samba/result | 4 +++ 16 files changed, 80 insertions(+), 75 deletions(-) diff --git a/calculate/templates/format/base_format.py b/calculate/templates/format/base_format.py index ba249cc..ecd585e 100644 --- a/calculate/templates/format/base_format.py +++ b/calculate/templates/format/base_format.py @@ -366,9 +366,11 @@ class Format: if check_shebang: # Удаление #! + print("FIND SHEBANG") shebang_regex = re.compile(self.SHEBANG_PATTERN) shebang_result = shebang_regex.search(input_text) if shebang_result is not None: + print("SHEBANG WAS FOUND") shebang = shebang_result.groupdict()['shebang'] input_text = shebang_regex.sub("", input_text) else: diff --git a/calculate/templates/format/raw_format.py b/calculate/templates/format/raw_format.py index b6b335e..0ddab1f 100644 --- a/calculate/templates/format/raw_format.py +++ b/calculate/templates/format/raw_format.py @@ -20,17 +20,12 @@ class RawFormat(Format): self.comment_symbol = parameters.comment or "" self._before = join_before - if add_header and not ignore_comments: - self.header, self._document_text, self.shebang =\ - self._get_header_and_document_text( - document_text, - template_path, - already_changed=already_changed, - check_shebang=True) - else: - self.header = '' - self.shebang = '' - self._document_text = document_text + self.header, self._document_text, self.shebang =\ + self._get_header_and_document_text( + document_text, + template_path, + already_changed=already_changed, + check_shebang=True) @property def document_text(self): @@ -52,6 +47,8 @@ class RawFormat(Format): self._document_text = '{0}{1}{2}'.format( self._document_text, sep, template._document_text) + if template.shebang: + self.shebang = template.shebang def __bool__(self): return bool(self.document_text.strip()) diff --git a/calculate/templates/format/templates/compiz b/calculate/templates/format/templates/compiz index f2c48ef..160c1c0 100644 --- a/calculate/templates/format/templates/compiz +++ b/calculate/templates/format/templates/compiz @@ -1,17 +1,17 @@ -{% for section_name in document_dictionary.keys() -%} +{% for section_name, section in document_dictionary.items() -%} {{ '' if loop.first else '\n' -}} -{% if '#' in document_dictionary[section_name].keys() -%} -{% for comment in document_dictionary[section_name]['#'] -%} +{% if '#' in section.keys() -%} +{% for comment in section['#'] -%} {{ comment }} {% endfor -%} {% endif -%} [{{ section_name | join('') }}] -{% for parameter_name in document_dictionary[section_name].keys() -%} +{% for parameter_name, parameter_value in section.items() -%} {% if parameter_name != '#' -%} -{% for comment in document_dictionary[section_name][parameter_name][:-1] -%} +{% for comment in parameter_value[:-1] -%} {{ comment }} {% endfor -%} -{{ parameter_name | join('') }}={{ document_dictionary[section_name][parameter_name][-1] }} +{{ parameter_name | join('') }}={{ parameter_value[-1] }} {% endif -%} {% endfor -%} {% endfor -%} diff --git a/calculate/templates/format/templates/openrc b/calculate/templates/format/templates/openrc index fd9cd87..400c75a 100644 --- a/calculate/templates/format/templates/openrc +++ b/calculate/templates/format/templates/openrc @@ -1,7 +1,9 @@ -{% for parameter_name in document_dictionary.keys() -%} -{% for comment in document_dictionary[parameter_name][:-1] -%} +{% for parameter_name, value in document_dictionary.items() -%} +{% if value[:-1] -%} +{{ "" if loop.first else "\n" -}} +{% endif -%} +{% for comment in value[:-1] -%} {{ comment }} {% endfor -%} -{{ parameter_name[0] + parameter_name[1] }}={{ document_dictionary[parameter_name][-1] -}} -{{ '\n' if loop.last else '\n\n' -}} +{{ parameter_name[0] + parameter_name[1] }}={{ value[-1] }} {% endfor -%} diff --git a/calculate/templates/format/templates/postfix b/calculate/templates/format/templates/postfix index 9a2d7d9..f03ae35 100644 --- a/calculate/templates/format/templates/postfix +++ b/calculate/templates/format/templates/postfix @@ -1,7 +1,9 @@ -{% for parameter_name in document_dictionary.keys() -%} -{% for comment in document_dictionary[parameter_name][:-1] -%} +{% for parameter_name, value in document_dictionary.items() -%} +{% if value[:-1] -%} +{{ "" if loop.first else "\n" -}} +{% endif -%} +{% for comment in value[:-1] -%} {{ comment }} {% endfor -%} -{{ parameter_name[0] + parameter_name[1] }} = {{ document_dictionary[parameter_name][-1] -}} -{{ '\n' if loop.last else '\n\n' -}} +{{ parameter_name[0] + parameter_name[1] }} = {{ value[-1] }} {% endfor -%} diff --git a/calculate/templates/format/templates/procmail b/calculate/templates/format/templates/procmail index fd9cd87..400c75a 100644 --- a/calculate/templates/format/templates/procmail +++ b/calculate/templates/format/templates/procmail @@ -1,7 +1,9 @@ -{% for parameter_name in document_dictionary.keys() -%} -{% for comment in document_dictionary[parameter_name][:-1] -%} +{% for parameter_name, value in document_dictionary.items() -%} +{% if value[:-1] -%} +{{ "" if loop.first else "\n" -}} +{% endif -%} +{% for comment in value[:-1] -%} {{ comment }} {% endfor -%} -{{ parameter_name[0] + parameter_name[1] }}={{ document_dictionary[parameter_name][-1] -}} -{{ '\n' if loop.last else '\n\n' -}} +{{ parameter_name[0] + parameter_name[1] }}={{ value[-1] }} {% endfor -%} diff --git a/calculate/templates/format/templates/samba b/calculate/templates/format/templates/samba index d0c5260..11581fb 100644 --- a/calculate/templates/format/templates/samba +++ b/calculate/templates/format/templates/samba @@ -1,16 +1,17 @@ -{% for section_name in document_dictionary.keys() -%} -{% if '#' in document_dictionary[section_name].keys() -%} -{% for comment in document_dictionary[section_name]['#'] -%} +{% for section_name, section in document_dictionary.items() -%} +{{ "" if loop.first else "\n" -}} +{% if '#' in section.keys() -%} +{% for comment in section['#'] -%} {{ comment }} {% endfor -%} {% endif -%} [{{ section_name | join('') }}] -{% for parameter_name in document_dictionary[section_name].keys() -%} +{% for parameter_name, parameter_value in section.items() -%} {% if parameter_name != '#' -%} -{% for comment in document_dictionary[section_name][parameter_name][:-1] -%} +{% for comment in parameter_value[:-1] -%} {{ " " + comment }} {% endfor -%} -{{ " " + parameter_name | join('') }} = {{ document_dictionary[section_name][parameter_name][-1] }} +{{ " " + parameter_name | join('') }} = {{ parameter_value[-1] }} {% endif -%} {% endfor -%} {% endfor -%} diff --git a/tests/templates/format/test_openrc.py b/tests/templates/format/test_openrc.py index 2f2ebb6..af35793 100644 --- a/tests/templates/format/test_openrc.py +++ b/tests/templates/format/test_openrc.py @@ -186,11 +186,8 @@ rc_cgroup_memory_use_hierarchy="YES" # For example, you would use this to set the maximum memory and maximum # number of pids for a service. rc_cgroup_settings="10485760" - instance="openldap${SVCNAME#slapd}" - rc_cgroup_cleanup="NO" - rc_send_sighup="YES" ''' @@ -222,7 +219,6 @@ OPTS_CONF="-f /etc/${INSTANCE}/slapd.conf" #------------------------------------------------------------------------------- # If you have multiple slapd instances per #376699, this will provide a default config rc_interactive="NO" - INSTANCE="openldap${SVCNAME#slapd}" ''' @@ -258,7 +254,6 @@ RC_INTERACTIVE="NO" #------------------------------------------------------------------------------- # If you have multiple slapd instances per #376699, this will provide a default config RC_INTERACTIVE="NO" - INSTANCE="openldap${SVCNAME#slapd}" ''' @@ -277,6 +272,7 @@ INSTANCE="openldap${SVCNAME#slapd}" # If you have multiple slapd instances per #376699, this will provide a default config rc_interactive="NO" INSTANCE="openldap${SVCNAME#slapd}" + # Comment1 # Comment2 OPTS_CONF="-f /etc/${INSTANCE}/slapd.conf" @@ -294,7 +290,6 @@ OPTS_CONF="-f /etc/${INSTANCE}/slapd.conf" #------------------------------------------------------------------------------- # If you have multiple slapd instances per #376699, this will provide a default config rc_interactive="NO" - INSTANCE="openldap${SVCNAME#slapd}" ''' @@ -316,15 +311,10 @@ new_param=5 NEW_PARAM2=6''' result = '''PARAM=1 - param2=2 - big_param=3 - LOW_PARAM=4 - new_param=5 - NEW_PARAM2=6 ''' diff --git a/tests/templates/format/test_postfix.py b/tests/templates/format/test_postfix.py index e519977..a5ad018 100644 --- a/tests/templates/format/test_postfix.py +++ b/tests/templates/format/test_postfix.py @@ -164,15 +164,10 @@ queue_directory = /var/spool/postfix #Путь для всех выполняемых программ почтового сервера. command_directory = /usr/sbin - mynetworks = 10.0.0.0/8, 80.246.243.18, 94.159.1.246, 80.246.245.82, 93.100.239.44 - relay_recipient_maps = hash:/etc/postfix/valid_recipient_maps - chmod = 0644 - chown = root:root - mail_owner = postfix ''' @@ -207,7 +202,6 @@ tls_random_source = dev:/dev/urandom #------------------------------------------------------------------------------- # Comment1 parameter_name = /home/divanov/Home - smtp_tls_note_starttls_offer = yes ''' @@ -245,7 +239,6 @@ tls_random_source = dev:/dev/urandom #------------------------------------------------------------------------------- # Comment1 parameter_name = /home/divanov/Home - smtp_tls_note_starttls_offer = yes ''' @@ -284,7 +277,6 @@ tls_random_source = dev:/dev/urandom #------------------------------------------------------------------------------- # Comment1 parameter_name = /home/divanov/Home - smtp_tls_note_starttls_offer = yes ''' diff --git a/tests/templates/format/test_procmail.py b/tests/templates/format/test_procmail.py index a97203e..ac4b876 100644 --- a/tests/templates/format/test_procmail.py +++ b/tests/templates/format/test_procmail.py @@ -151,11 +151,8 @@ port=8200 # specify the user account name or uid to run as user=lol - vm.dirty_ratio=4 - media_dir=PV,/var/calculate/server-data/samba/share - net.ipv4.icmp_echo_ignore_broadcasts=1 ''' @@ -183,7 +180,6 @@ other_parameter = yes # /path/to/template #------------------------------------------------------------------------------- net.ipv4.ip_forward=0 - other_parameter=yes ''' @@ -214,7 +210,6 @@ other_parameter = yes # /path/to/template #------------------------------------------------------------------------------- net.ipv4.ip_forward=0 - other_parameter=yes ''' @@ -246,7 +241,6 @@ other_parameter = yes # /path/to/template #------------------------------------------------------------------------------- net.ipv4.ip_forward=0 - other_parameter=yes ''' diff --git a/tests/templates/format/test_raw.py b/tests/templates/format/test_raw.py index cb5b0c5..949cce3 100644 --- a/tests/templates/format/test_raw.py +++ b/tests/templates/format/test_raw.py @@ -242,3 +242,29 @@ exit 0 template_1 = RawFormat(template_text_1, 'path/to/template_1') original_1.join_template(template_1) assert original_1.document_text == output + + def test_make_shebang_great_again(self): + parameters = ParametersContainer({'comment': '#'}) + original_text = '' + + template_text_1 = '''#!/bin/bash +echo "message" +exit 0''' + + output = '''#!/bin/bash +#------------------------------------------------------------------------------- +# Modified by Calculate Utilities 4.0 +# Processed template files: +# path/to/template_1 +#------------------------------------------------------------------------------- +echo "message" +exit 0 +''' + + original_1 = RawFormat(original_text, 'path/to/template_1', + add_header=True, + already_changed=False, + parameters=parameters) + template_1 = RawFormat(template_text_1, 'path/to/template_1') + original_1.join_template(template_1) + assert original_1.document_text == output diff --git a/tests/templates/format/test_samba.py b/tests/templates/format/test_samba.py index 8b23b0f..51d88b6 100644 --- a/tests/templates/format/test_samba.py +++ b/tests/templates/format/test_samba.py @@ -257,9 +257,11 @@ class TestParsingMethods: # comment 2 parameter name = /home/divanov/Home other parameter = yes + [section name 3] unspoken parameter = 1 unbelievable parameter = Mystical + [section name 1] parameter name = /homeless/poorness one more parameter = oh no @@ -304,11 +306,13 @@ class TestParsingMethods: # Comment2 # Comment3 other parameter = yes + [section name3] #Comment another parameter = 1 # Comment unspoken parameter = 1 + [section name1] parameter name = /homeless/poorness one more parameter = oh no @@ -358,11 +362,13 @@ class TestParsingMethods: # Comment2 # Comment3 other parameter = yes + [section name3] #Comment another parameter = 1 # Comment unspoken parameter = 1 + [section name1] parameter name = /homeless/poorness one more parameter = oh no @@ -413,11 +419,13 @@ class TestParsingMethods: # Comment2 # Comment3 other parameter = yes + [section name3] #Comment another parameter = 1 # Comment unspoken parameter = 1 + [section name1] parameter name = /homeless/poorness one more parameter = oh no diff --git a/tests/templates/format/testfiles/openrc/result b/tests/templates/format/testfiles/openrc/result index 768d5e0..534b943 100644 --- a/tests/templates/format/testfiles/openrc/result +++ b/tests/templates/format/testfiles/openrc/result @@ -28,9 +28,6 @@ rc_cgroup_cleanup="NO" # If this is yes, we will send sighup to the processes in the cgroup # immediately after stopsig and sigcont. rc_send_sighup="YES" - clock="Any Clock Type" - clock_systohc="YES" - rc_timeout_stopsec="90" diff --git a/tests/templates/format/testfiles/postfix/result b/tests/templates/format/testfiles/postfix/result index 34396ea..bbd15d1 100644 --- a/tests/templates/format/testfiles/postfix/result +++ b/tests/templates/format/testfiles/postfix/result @@ -24,17 +24,10 @@ mynetworks = 10.0.0.0/8, 80.246.243.18, 95.213.228.194, 94.159.1.246, 109.167.15 #по умолчанию: посылать в Internet напрямую #relayhost = [mail.$mydomain] relay_domains = lists.calculate-linux.org, and something above - relay_recipient_maps = hash:/etc/postfix/valid_recipient_maps - chmod = 0644 - chown = root:root - mail_owner = postfix - recipient_maps = ldap:/etc/postfix/ldap-recipient.cf, ldap:/etc/postfix/ldap-recipient-gr.cf - local_recipient_maps = ldap:/etc/postfix/ldap-recipient.cf, ldap:/etc/postfix/ldap-recipient-gr.cf, ldap:/etc/postfix/ldap-recipient-repl.cf - unknown_local_recipient_reject_code = 550 diff --git a/tests/templates/format/testfiles/procmail/result b/tests/templates/format/testfiles/procmail/result index d58ef03..7b4a95e 100644 --- a/tests/templates/format/testfiles/procmail/result +++ b/tests/templates/format/testfiles/procmail/result @@ -7,9 +7,7 @@ user=jmaggard # You should compile nfsd into the kernel or add it # to modules.autoload for this to work properly vm.dirty_background_ratio=1 - vm.dirty_ratio=3 - kernel.unprivileged_userns_clone=0 # this should be a list of file names to check for when searching for album art @@ -22,9 +20,6 @@ inotify=no # set this to yes to enable support for streaming .jpg and .mp3 files to a TiVo supporting HMO enable_tivo=no - friendly_name=mike-desktop - media_dir=PV,/var/calculate/server-data/samba/share - net.ipv4.icmp_echo_ignore_broadcasts=1 diff --git a/tests/templates/format/testfiles/samba/result b/tests/templates/format/testfiles/samba/result index 1a410d0..cd60e8f 100644 --- a/tests/templates/format/testfiles/samba/result +++ b/tests/templates/format/testfiles/samba/result @@ -13,6 +13,7 @@ force security mode = 0644 create mask = 0644 enable privileges = yes + #============================== Share Definitions ============================= [homes] path = /home/user/somewhere @@ -20,11 +21,13 @@ valid users = %U read only = No browseable = No + # Un-comment the following and create the netlogon directory for Domain Logons [netlogon] path = /var/lib/samba/netlogon guest ok = yes share modes = no + [tmp] comment = Public Stuff path = /home/samba @@ -32,6 +35,7 @@ writable = no printable = no write list = @staff + [unix] path = /home/path browseable = No